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 if (symbol_conf.nanosecs)
215 hists__new_col_len(hists, HISTC_TIME, 16);
217 hists__new_col_len(hists, HISTC_TIME, 12);
218 hists__new_col_len(hists, HISTC_CODE_PAGE_SIZE, 6);
221 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
222 hists__new_col_len(hists, HISTC_SRCLINE, len);
226 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
229 hists__new_col_len(hists, HISTC_TRANSACTION,
230 hist_entry__transaction_len());
233 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
236 const char *cgrp_name = "unknown";
237 struct cgroup *cgrp = cgroup__find(h->ms.maps->machine->env,
240 cgrp_name = cgrp->name;
242 hists__new_col_len(hists, HISTC_CGROUP, strlen(cgrp_name));
246 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
248 struct rb_node *next = rb_first_cached(&hists->entries);
249 struct hist_entry *n;
252 hists__reset_col_len(hists);
254 while (next && row++ < max_rows) {
255 n = rb_entry(next, struct hist_entry, rb_node);
257 hists__calc_col_len(hists, n);
258 next = rb_next(&n->rb_node);
262 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
263 unsigned int cpumode, u64 period)
266 case PERF_RECORD_MISC_KERNEL:
267 he_stat->period_sys += period;
269 case PERF_RECORD_MISC_USER:
270 he_stat->period_us += period;
272 case PERF_RECORD_MISC_GUEST_KERNEL:
273 he_stat->period_guest_sys += period;
275 case PERF_RECORD_MISC_GUEST_USER:
276 he_stat->period_guest_us += period;
283 static long hist_time(unsigned long htime)
285 unsigned long time_quantum = symbol_conf.time_quantum;
287 return (htime / time_quantum) * time_quantum;
291 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
292 u64 weight, u64 ins_lat)
295 he_stat->period += period;
296 he_stat->weight += weight;
297 he_stat->nr_events += 1;
298 he_stat->ins_lat += ins_lat;
301 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
303 dest->period += src->period;
304 dest->period_sys += src->period_sys;
305 dest->period_us += src->period_us;
306 dest->period_guest_sys += src->period_guest_sys;
307 dest->period_guest_us += src->period_guest_us;
308 dest->nr_events += src->nr_events;
309 dest->weight += src->weight;
310 dest->ins_lat += src->ins_lat;
313 static void he_stat__decay(struct he_stat *he_stat)
315 he_stat->period = (he_stat->period * 7) / 8;
316 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
317 /* XXX need decay for weight too? */
320 static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
322 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
324 u64 prev_period = he->stat.period;
327 if (prev_period == 0)
330 he_stat__decay(&he->stat);
331 if (symbol_conf.cumulate_callchain)
332 he_stat__decay(he->stat_acc);
333 decay_callchain(he->callchain);
335 diff = prev_period - he->stat.period;
338 hists->stats.total_period -= diff;
340 hists->stats.total_non_filtered_period -= diff;
344 struct hist_entry *child;
345 struct rb_node *node = rb_first_cached(&he->hroot_out);
347 child = rb_entry(node, struct hist_entry, rb_node);
348 node = rb_next(node);
350 if (hists__decay_entry(hists, child))
351 hists__delete_entry(hists, child);
355 return he->stat.period == 0;
358 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
360 struct rb_root_cached *root_in;
361 struct rb_root_cached *root_out;
364 root_in = &he->parent_he->hroot_in;
365 root_out = &he->parent_he->hroot_out;
367 if (hists__has(hists, need_collapse))
368 root_in = &hists->entries_collapsed;
370 root_in = hists->entries_in;
371 root_out = &hists->entries;
374 rb_erase_cached(&he->rb_node_in, root_in);
375 rb_erase_cached(&he->rb_node, root_out);
379 --hists->nr_non_filtered_entries;
381 hist_entry__delete(he);
384 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
386 struct rb_node *next = rb_first_cached(&hists->entries);
387 struct hist_entry *n;
390 n = rb_entry(next, struct hist_entry, rb_node);
391 next = rb_next(&n->rb_node);
392 if (((zap_user && n->level == '.') ||
393 (zap_kernel && n->level != '.') ||
394 hists__decay_entry(hists, n))) {
395 hists__delete_entry(hists, n);
400 void hists__delete_entries(struct hists *hists)
402 struct rb_node *next = rb_first_cached(&hists->entries);
403 struct hist_entry *n;
406 n = rb_entry(next, struct hist_entry, rb_node);
407 next = rb_next(&n->rb_node);
409 hists__delete_entry(hists, n);
413 struct hist_entry *hists__get_entry(struct hists *hists, int idx)
415 struct rb_node *next = rb_first_cached(&hists->entries);
416 struct hist_entry *n;
420 n = rb_entry(next, struct hist_entry, rb_node);
424 next = rb_next(&n->rb_node);
432 * histogram, sorted on item, collects periods
435 static int hist_entry__init(struct hist_entry *he,
436 struct hist_entry *template,
438 size_t callchain_size)
441 he->callchain_size = callchain_size;
443 if (symbol_conf.cumulate_callchain) {
444 he->stat_acc = malloc(sizeof(he->stat));
445 if (he->stat_acc == NULL)
447 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
449 memset(&he->stat, 0, sizeof(he->stat));
452 map__get(he->ms.map);
454 if (he->branch_info) {
456 * This branch info is (a part of) allocated from
457 * sample__resolve_bstack() and will be freed after
458 * adding new entries. So we need to save a copy.
460 he->branch_info = malloc(sizeof(*he->branch_info));
461 if (he->branch_info == NULL)
464 memcpy(he->branch_info, template->branch_info,
465 sizeof(*he->branch_info));
467 map__get(he->branch_info->from.ms.map);
468 map__get(he->branch_info->to.ms.map);
472 map__get(he->mem_info->iaddr.ms.map);
473 map__get(he->mem_info->daddr.ms.map);
476 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
477 callchain_init(he->callchain);
480 he->raw_data = memdup(he->raw_data, he->raw_size);
481 if (he->raw_data == NULL)
486 he->srcline = strdup(he->srcline);
487 if (he->srcline == NULL)
491 if (symbol_conf.res_sample) {
492 he->res_samples = calloc(sizeof(struct res_sample),
493 symbol_conf.res_sample);
494 if (!he->res_samples)
498 INIT_LIST_HEAD(&he->pairs.node);
499 thread__get(he->thread);
500 he->hroot_in = RB_ROOT_CACHED;
501 he->hroot_out = RB_ROOT_CACHED;
503 if (!symbol_conf.report_hierarchy)
512 zfree(&he->raw_data);
515 if (he->branch_info) {
516 map__put(he->branch_info->from.ms.map);
517 map__put(he->branch_info->to.ms.map);
518 zfree(&he->branch_info);
521 map__put(he->mem_info->iaddr.ms.map);
522 map__put(he->mem_info->daddr.ms.map);
525 map__zput(he->ms.map);
526 zfree(&he->stat_acc);
530 static void *hist_entry__zalloc(size_t size)
532 return zalloc(size + sizeof(struct hist_entry));
535 static void hist_entry__free(void *ptr)
540 static struct hist_entry_ops default_ops = {
541 .new = hist_entry__zalloc,
542 .free = hist_entry__free,
545 static struct hist_entry *hist_entry__new(struct hist_entry *template,
548 struct hist_entry_ops *ops = template->ops;
549 size_t callchain_size = 0;
550 struct hist_entry *he;
554 ops = template->ops = &default_ops;
556 if (symbol_conf.use_callchain)
557 callchain_size = sizeof(struct callchain_root);
559 he = ops->new(callchain_size);
561 err = hist_entry__init(he, template, sample_self, callchain_size);
571 static u8 symbol__parent_filter(const struct symbol *parent)
573 if (symbol_conf.exclude_other && parent == NULL)
574 return 1 << HIST_FILTER__PARENT;
578 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
580 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
583 he->hists->callchain_period += period;
585 he->hists->callchain_non_filtered_period += period;
588 static struct hist_entry *hists__findnew_entry(struct hists *hists,
589 struct hist_entry *entry,
590 struct addr_location *al,
594 struct rb_node *parent = NULL;
595 struct hist_entry *he;
597 u64 period = entry->stat.period;
598 u64 weight = entry->stat.weight;
599 u64 ins_lat = entry->stat.ins_lat;
600 bool leftmost = true;
602 p = &hists->entries_in->rb_root.rb_node;
606 he = rb_entry(parent, struct hist_entry, rb_node_in);
609 * Make sure that it receives arguments in a same order as
610 * hist_entry__collapse() so that we can use an appropriate
611 * function when searching an entry regardless which sort
614 cmp = hist_entry__cmp(he, entry);
618 he_stat__add_period(&he->stat, period, weight, ins_lat);
619 hist_entry__add_callchain_period(he, period);
621 if (symbol_conf.cumulate_callchain)
622 he_stat__add_period(he->stat_acc, period, weight, ins_lat);
625 * This mem info was allocated from sample__resolve_mem
626 * and will not be used anymore.
628 mem_info__zput(entry->mem_info);
630 block_info__zput(entry->block_info);
632 /* If the map of an existing hist_entry has
633 * become out-of-date due to an exec() or
634 * similar, update it. Otherwise we will
635 * mis-adjust symbol addresses when computing
636 * the history counter to increment.
638 if (he->ms.map != entry->ms.map) {
639 map__put(he->ms.map);
640 he->ms.map = map__get(entry->ms.map);
653 he = hist_entry__new(entry, sample_self);
658 hist_entry__add_callchain_period(he, period);
661 rb_link_node(&he->rb_node_in, parent, p);
662 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
665 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
666 if (symbol_conf.cumulate_callchain)
667 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
671 static unsigned random_max(unsigned high)
673 unsigned thresh = -high % high;
675 unsigned r = random();
681 static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
683 struct res_sample *r;
686 if (he->num_res < symbol_conf.res_sample) {
689 j = random_max(symbol_conf.res_sample);
691 r = &he->res_samples[j];
692 r->time = sample->time;
693 r->cpu = sample->cpu;
694 r->tid = sample->tid;
697 static struct hist_entry*
698 __hists__add_entry(struct hists *hists,
699 struct addr_location *al,
700 struct symbol *sym_parent,
701 struct branch_info *bi,
703 struct block_info *block_info,
704 struct perf_sample *sample,
706 struct hist_entry_ops *ops)
708 struct namespaces *ns = thread__namespaces(al->thread);
709 struct hist_entry entry = {
710 .thread = al->thread,
711 .comm = thread__comm(al->thread),
713 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
714 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
716 .cgroup = sample->cgroup,
722 .srcline = (char *) al->srcline,
723 .socket = al->socket,
725 .cpumode = al->cpumode,
728 .code_page_size = sample->code_page_size,
731 .period = sample->period,
732 .weight = sample->weight,
733 .ins_lat = sample->ins_lat,
735 .parent = sym_parent,
736 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
740 .block_info = block_info,
741 .transaction = sample->transaction,
742 .raw_data = sample->raw_data,
743 .raw_size = sample->raw_size,
745 .time = hist_time(sample->time),
746 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
748 if (!hists->has_callchains && he && he->callchain_size != 0)
749 hists->has_callchains = true;
750 if (he && symbol_conf.res_sample)
751 hists__res_sample(he, sample);
755 struct hist_entry *hists__add_entry(struct hists *hists,
756 struct addr_location *al,
757 struct symbol *sym_parent,
758 struct branch_info *bi,
760 struct perf_sample *sample,
763 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
764 sample, sample_self, NULL);
767 struct hist_entry *hists__add_entry_ops(struct hists *hists,
768 struct hist_entry_ops *ops,
769 struct addr_location *al,
770 struct symbol *sym_parent,
771 struct branch_info *bi,
773 struct perf_sample *sample,
776 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
777 sample, sample_self, ops);
780 struct hist_entry *hists__add_entry_block(struct hists *hists,
781 struct addr_location *al,
782 struct block_info *block_info)
784 struct hist_entry entry = {
785 .block_info = block_info,
792 }, *he = hists__findnew_entry(hists, &entry, al, false);
798 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
799 struct addr_location *al __maybe_unused)
805 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
806 struct addr_location *al __maybe_unused)
812 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
814 struct perf_sample *sample = iter->sample;
817 mi = sample__resolve_mem(sample, al);
826 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
829 struct mem_info *mi = iter->priv;
830 struct hists *hists = evsel__hists(iter->evsel);
831 struct perf_sample *sample = iter->sample;
832 struct hist_entry *he;
837 cost = sample->weight;
842 * must pass period=weight in order to get the correct
843 * sorting from hists__collapse_resort() which is solely
844 * based on periods. We want sorting be done on nr_events * weight
845 * and this is indirectly achieved by passing period=weight here
846 * and the he_stat__add_period() function.
848 sample->period = cost;
850 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
860 iter_finish_mem_entry(struct hist_entry_iter *iter,
861 struct addr_location *al __maybe_unused)
863 struct evsel *evsel = iter->evsel;
864 struct hists *hists = evsel__hists(evsel);
865 struct hist_entry *he = iter->he;
871 hists__inc_nr_samples(hists, he->filtered);
873 err = hist_entry__append_callchain(he, iter->sample);
877 * We don't need to free iter->priv (mem_info) here since the mem info
878 * was either already freed in hists__findnew_entry() or passed to a
879 * new hist entry by hist_entry__new().
888 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
890 struct branch_info *bi;
891 struct perf_sample *sample = iter->sample;
893 bi = sample__resolve_bstack(sample, al);
898 iter->total = sample->branch_stack->nr;
905 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
906 struct addr_location *al __maybe_unused)
912 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
914 struct branch_info *bi = iter->priv;
920 if (iter->curr >= iter->total)
923 al->maps = bi[i].to.ms.maps;
924 al->map = bi[i].to.ms.map;
925 al->sym = bi[i].to.ms.sym;
926 al->addr = bi[i].to.addr;
931 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
933 struct branch_info *bi;
934 struct evsel *evsel = iter->evsel;
935 struct hists *hists = evsel__hists(evsel);
936 struct perf_sample *sample = iter->sample;
937 struct hist_entry *he = NULL;
943 if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
947 * The report shows the percentage of total branches captured
948 * and not events sampled. Thus we use a pseudo period of 1.
951 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
953 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
958 hists__inc_nr_samples(hists, he->filtered);
967 iter_finish_branch_entry(struct hist_entry_iter *iter,
968 struct addr_location *al __maybe_unused)
973 return iter->curr >= iter->total ? 0 : -1;
977 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
978 struct addr_location *al __maybe_unused)
984 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
986 struct evsel *evsel = iter->evsel;
987 struct perf_sample *sample = iter->sample;
988 struct hist_entry *he;
990 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1000 iter_finish_normal_entry(struct hist_entry_iter *iter,
1001 struct addr_location *al __maybe_unused)
1003 struct hist_entry *he = iter->he;
1004 struct evsel *evsel = iter->evsel;
1005 struct perf_sample *sample = iter->sample;
1012 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
1014 return hist_entry__append_callchain(he, sample);
1018 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
1019 struct addr_location *al __maybe_unused)
1021 struct hist_entry **he_cache;
1023 callchain_cursor_commit(&callchain_cursor);
1026 * This is for detecting cycles or recursions so that they're
1027 * cumulated only one time to prevent entries more than 100%
1030 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
1031 if (he_cache == NULL)
1034 iter->priv = he_cache;
1041 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1042 struct addr_location *al)
1044 struct evsel *evsel = iter->evsel;
1045 struct hists *hists = evsel__hists(evsel);
1046 struct perf_sample *sample = iter->sample;
1047 struct hist_entry **he_cache = iter->priv;
1048 struct hist_entry *he;
1051 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1057 he_cache[iter->curr++] = he;
1059 hist_entry__append_callchain(he, sample);
1062 * We need to re-initialize the cursor since callchain_append()
1063 * advanced the cursor to the end.
1065 callchain_cursor_commit(&callchain_cursor);
1067 hists__inc_nr_samples(hists, he->filtered);
1073 iter_next_cumulative_entry(struct hist_entry_iter *iter,
1074 struct addr_location *al)
1076 struct callchain_cursor_node *node;
1078 node = callchain_cursor_current(&callchain_cursor);
1082 return fill_callchain_info(al, node, iter->hide_unresolved);
1086 hist_entry__fast__sym_diff(struct hist_entry *left,
1087 struct hist_entry *right)
1089 struct symbol *sym_l = left->ms.sym;
1090 struct symbol *sym_r = right->ms.sym;
1092 if (!sym_l && !sym_r)
1093 return left->ip != right->ip;
1095 return !!_sort__sym_cmp(sym_l, sym_r);
1100 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1101 struct addr_location *al)
1103 struct evsel *evsel = iter->evsel;
1104 struct perf_sample *sample = iter->sample;
1105 struct hist_entry **he_cache = iter->priv;
1106 struct hist_entry *he;
1107 struct hist_entry he_tmp = {
1108 .hists = evsel__hists(evsel),
1110 .thread = al->thread,
1111 .comm = thread__comm(al->thread),
1118 .srcline = (char *) al->srcline,
1119 .parent = iter->parent,
1120 .raw_data = sample->raw_data,
1121 .raw_size = sample->raw_size,
1124 struct callchain_cursor cursor;
1125 bool fast = hists__has(he_tmp.hists, sym);
1127 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1129 callchain_cursor_advance(&callchain_cursor);
1132 * Check if there's duplicate entries in the callchain.
1133 * It's possible that it has cycles or recursive calls.
1135 for (i = 0; i < iter->curr; i++) {
1137 * For most cases, there are no duplicate entries in callchain.
1138 * The symbols are usually different. Do a quick check for
1141 if (fast && hist_entry__fast__sym_diff(he_cache[i], &he_tmp))
1144 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1145 /* to avoid calling callback function */
1151 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1157 he_cache[iter->curr++] = he;
1159 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
1160 callchain_append(he->callchain, &cursor, sample->period);
1165 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1166 struct addr_location *al __maybe_unused)
1174 const struct hist_iter_ops hist_iter_mem = {
1175 .prepare_entry = iter_prepare_mem_entry,
1176 .add_single_entry = iter_add_single_mem_entry,
1177 .next_entry = iter_next_nop_entry,
1178 .add_next_entry = iter_add_next_nop_entry,
1179 .finish_entry = iter_finish_mem_entry,
1182 const struct hist_iter_ops hist_iter_branch = {
1183 .prepare_entry = iter_prepare_branch_entry,
1184 .add_single_entry = iter_add_single_branch_entry,
1185 .next_entry = iter_next_branch_entry,
1186 .add_next_entry = iter_add_next_branch_entry,
1187 .finish_entry = iter_finish_branch_entry,
1190 const struct hist_iter_ops hist_iter_normal = {
1191 .prepare_entry = iter_prepare_normal_entry,
1192 .add_single_entry = iter_add_single_normal_entry,
1193 .next_entry = iter_next_nop_entry,
1194 .add_next_entry = iter_add_next_nop_entry,
1195 .finish_entry = iter_finish_normal_entry,
1198 const struct hist_iter_ops hist_iter_cumulative = {
1199 .prepare_entry = iter_prepare_cumulative_entry,
1200 .add_single_entry = iter_add_single_cumulative_entry,
1201 .next_entry = iter_next_cumulative_entry,
1202 .add_next_entry = iter_add_next_cumulative_entry,
1203 .finish_entry = iter_finish_cumulative_entry,
1206 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1207 int max_stack_depth, void *arg)
1210 struct map *alm = NULL;
1213 alm = map__get(al->map);
1215 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1216 iter->evsel, al, max_stack_depth);
1222 err = iter->ops->prepare_entry(iter, al);
1226 err = iter->ops->add_single_entry(iter, al);
1230 if (iter->he && iter->add_entry_cb) {
1231 err = iter->add_entry_cb(iter, al, true, arg);
1236 while (iter->ops->next_entry(iter, al)) {
1237 err = iter->ops->add_next_entry(iter, al);
1241 if (iter->he && iter->add_entry_cb) {
1242 err = iter->add_entry_cb(iter, al, false, arg);
1249 err2 = iter->ops->finish_entry(iter, al);
1259 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1261 struct hists *hists = left->hists;
1262 struct perf_hpp_fmt *fmt;
1265 hists__for_each_sort_list(hists, fmt) {
1266 if (perf_hpp__is_dynamic_entry(fmt) &&
1267 !perf_hpp__defined_dynamic_entry(fmt, hists))
1270 cmp = fmt->cmp(fmt, left, right);
1279 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1281 struct hists *hists = left->hists;
1282 struct perf_hpp_fmt *fmt;
1285 hists__for_each_sort_list(hists, fmt) {
1286 if (perf_hpp__is_dynamic_entry(fmt) &&
1287 !perf_hpp__defined_dynamic_entry(fmt, hists))
1290 cmp = fmt->collapse(fmt, left, right);
1298 void hist_entry__delete(struct hist_entry *he)
1300 struct hist_entry_ops *ops = he->ops;
1302 thread__zput(he->thread);
1303 map__zput(he->ms.map);
1305 if (he->branch_info) {
1306 map__zput(he->branch_info->from.ms.map);
1307 map__zput(he->branch_info->to.ms.map);
1308 free_srcline(he->branch_info->srcline_from);
1309 free_srcline(he->branch_info->srcline_to);
1310 zfree(&he->branch_info);
1314 map__zput(he->mem_info->iaddr.ms.map);
1315 map__zput(he->mem_info->daddr.ms.map);
1316 mem_info__zput(he->mem_info);
1320 block_info__zput(he->block_info);
1322 zfree(&he->res_samples);
1323 zfree(&he->stat_acc);
1324 free_srcline(he->srcline);
1325 if (he->srcfile && he->srcfile[0])
1326 zfree(&he->srcfile);
1327 free_callchain(he->callchain);
1328 zfree(&he->trace_output);
1329 zfree(&he->raw_data);
1334 * If this is not the last column, then we need to pad it according to the
1335 * pre-calculated max length for this column, otherwise don't bother adding
1336 * spaces because that would break viewing this with, for instance, 'less',
1337 * that would show tons of trailing spaces when a long C++ demangled method
1340 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1341 struct perf_hpp_fmt *fmt, int printed)
1343 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1344 const int width = fmt->width(fmt, hpp, he->hists);
1345 if (printed < width) {
1346 advance_hpp(hpp, printed);
1347 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1355 * collapse the histogram
1358 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1359 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1360 enum hist_filter type);
1362 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1364 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1366 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1369 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1370 enum hist_filter type,
1373 struct perf_hpp_fmt *fmt;
1374 bool type_match = false;
1375 struct hist_entry *parent = he->parent_he;
1378 case HIST_FILTER__THREAD:
1379 if (symbol_conf.comm_list == NULL &&
1380 symbol_conf.pid_list == NULL &&
1381 symbol_conf.tid_list == NULL)
1384 case HIST_FILTER__DSO:
1385 if (symbol_conf.dso_list == NULL)
1388 case HIST_FILTER__SYMBOL:
1389 if (symbol_conf.sym_list == NULL)
1392 case HIST_FILTER__PARENT:
1393 case HIST_FILTER__GUEST:
1394 case HIST_FILTER__HOST:
1395 case HIST_FILTER__SOCKET:
1396 case HIST_FILTER__C2C:
1401 /* if it's filtered by own fmt, it has to have filter bits */
1402 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1411 * If the filter is for current level entry, propagate
1412 * filter marker to parents. The marker bit was
1413 * already set by default so it only needs to clear
1414 * non-filtered entries.
1416 if (!(he->filtered & (1 << type))) {
1418 parent->filtered &= ~(1 << type);
1419 parent = parent->parent_he;
1424 * If current entry doesn't have matching formats, set
1425 * filter marker for upper level entries. it will be
1426 * cleared if its lower level entries is not filtered.
1428 * For lower-level entries, it inherits parent's
1429 * filter bit so that lower level entries of a
1430 * non-filtered entry won't set the filter marker.
1433 he->filtered |= (1 << type);
1435 he->filtered |= (parent->filtered & (1 << type));
1439 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1441 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1442 check_thread_entry);
1444 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1445 perf_hpp__is_dso_entry);
1447 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1448 perf_hpp__is_sym_entry);
1450 hists__apply_filters(he->hists, he);
1453 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1454 struct rb_root_cached *root,
1455 struct hist_entry *he,
1456 struct hist_entry *parent_he,
1457 struct perf_hpp_list *hpp_list)
1459 struct rb_node **p = &root->rb_root.rb_node;
1460 struct rb_node *parent = NULL;
1461 struct hist_entry *iter, *new;
1462 struct perf_hpp_fmt *fmt;
1464 bool leftmost = true;
1466 while (*p != NULL) {
1468 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1471 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1472 cmp = fmt->collapse(fmt, iter, he);
1478 he_stat__add_stat(&iter->stat, &he->stat);
1483 p = &parent->rb_left;
1485 p = &parent->rb_right;
1490 new = hist_entry__new(he, true);
1494 hists->nr_entries++;
1496 /* save related format list for output */
1497 new->hpp_list = hpp_list;
1498 new->parent_he = parent_he;
1500 hist_entry__apply_hierarchy_filters(new);
1502 /* some fields are now passed to 'new' */
1503 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1504 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1505 he->trace_output = NULL;
1507 new->trace_output = NULL;
1509 if (perf_hpp__is_srcline_entry(fmt))
1512 new->srcline = NULL;
1514 if (perf_hpp__is_srcfile_entry(fmt))
1517 new->srcfile = NULL;
1520 rb_link_node(&new->rb_node_in, parent, p);
1521 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
1525 static int hists__hierarchy_insert_entry(struct hists *hists,
1526 struct rb_root_cached *root,
1527 struct hist_entry *he)
1529 struct perf_hpp_list_node *node;
1530 struct hist_entry *new_he = NULL;
1531 struct hist_entry *parent = NULL;
1535 list_for_each_entry(node, &hists->hpp_formats, list) {
1536 /* skip period (overhead) and elided columns */
1537 if (node->level == 0 || node->skip)
1540 /* insert copy of 'he' for each fmt into the hierarchy */
1541 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1542 if (new_he == NULL) {
1547 root = &new_he->hroot_in;
1548 new_he->depth = depth++;
1553 new_he->leaf = true;
1555 if (hist_entry__has_callchains(new_he) &&
1556 symbol_conf.use_callchain) {
1557 callchain_cursor_reset(&callchain_cursor);
1558 if (callchain_merge(&callchain_cursor,
1565 /* 'he' is no longer used */
1566 hist_entry__delete(he);
1568 /* return 0 (or -1) since it already applied filters */
1572 static int hists__collapse_insert_entry(struct hists *hists,
1573 struct rb_root_cached *root,
1574 struct hist_entry *he)
1576 struct rb_node **p = &root->rb_root.rb_node;
1577 struct rb_node *parent = NULL;
1578 struct hist_entry *iter;
1580 bool leftmost = true;
1582 if (symbol_conf.report_hierarchy)
1583 return hists__hierarchy_insert_entry(hists, root, he);
1585 while (*p != NULL) {
1587 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1589 cmp = hist_entry__collapse(iter, he);
1594 he_stat__add_stat(&iter->stat, &he->stat);
1595 if (symbol_conf.cumulate_callchain)
1596 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1598 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
1599 callchain_cursor_reset(&callchain_cursor);
1600 if (callchain_merge(&callchain_cursor,
1605 hist_entry__delete(he);
1612 p = &(*p)->rb_right;
1616 hists->nr_entries++;
1618 rb_link_node(&he->rb_node_in, parent, p);
1619 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
1623 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
1625 struct rb_root_cached *root;
1627 pthread_mutex_lock(&hists->lock);
1629 root = hists->entries_in;
1630 if (++hists->entries_in > &hists->entries_in_array[1])
1631 hists->entries_in = &hists->entries_in_array[0];
1633 pthread_mutex_unlock(&hists->lock);
1638 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1640 hists__filter_entry_by_dso(hists, he);
1641 hists__filter_entry_by_thread(hists, he);
1642 hists__filter_entry_by_symbol(hists, he);
1643 hists__filter_entry_by_socket(hists, he);
1646 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1648 struct rb_root_cached *root;
1649 struct rb_node *next;
1650 struct hist_entry *n;
1653 if (!hists__has(hists, need_collapse))
1656 hists->nr_entries = 0;
1658 root = hists__get_rotate_entries_in(hists);
1660 next = rb_first_cached(root);
1665 n = rb_entry(next, struct hist_entry, rb_node_in);
1666 next = rb_next(&n->rb_node_in);
1668 rb_erase_cached(&n->rb_node_in, root);
1669 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1675 * If it wasn't combined with one of the entries already
1676 * collapsed, we need to apply the filters that may have
1677 * been set by, say, the hist_browser.
1679 hists__apply_filters(hists, n);
1682 ui_progress__update(prog, 1);
1687 static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1689 struct hists *hists = a->hists;
1690 struct perf_hpp_fmt *fmt;
1693 hists__for_each_sort_list(hists, fmt) {
1694 if (perf_hpp__should_skip(fmt, a->hists))
1697 cmp = fmt->sort(fmt, a, b);
1705 static void hists__reset_filter_stats(struct hists *hists)
1707 hists->nr_non_filtered_entries = 0;
1708 hists->stats.total_non_filtered_period = 0;
1711 void hists__reset_stats(struct hists *hists)
1713 hists->nr_entries = 0;
1714 hists->stats.total_period = 0;
1716 hists__reset_filter_stats(hists);
1719 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1721 hists->nr_non_filtered_entries++;
1722 hists->stats.total_non_filtered_period += h->stat.period;
1725 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1728 hists__inc_filter_stats(hists, h);
1730 hists->nr_entries++;
1731 hists->stats.total_period += h->stat.period;
1734 static void hierarchy_recalc_total_periods(struct hists *hists)
1736 struct rb_node *node;
1737 struct hist_entry *he;
1739 node = rb_first_cached(&hists->entries);
1741 hists->stats.total_period = 0;
1742 hists->stats.total_non_filtered_period = 0;
1745 * recalculate total period using top-level entries only
1746 * since lower level entries only see non-filtered entries
1747 * but upper level entries have sum of both entries.
1750 he = rb_entry(node, struct hist_entry, rb_node);
1751 node = rb_next(node);
1753 hists->stats.total_period += he->stat.period;
1755 hists->stats.total_non_filtered_period += he->stat.period;
1759 static void hierarchy_insert_output_entry(struct rb_root_cached *root,
1760 struct hist_entry *he)
1762 struct rb_node **p = &root->rb_root.rb_node;
1763 struct rb_node *parent = NULL;
1764 struct hist_entry *iter;
1765 struct perf_hpp_fmt *fmt;
1766 bool leftmost = true;
1768 while (*p != NULL) {
1770 iter = rb_entry(parent, struct hist_entry, rb_node);
1772 if (hist_entry__sort(he, iter) > 0)
1773 p = &parent->rb_left;
1775 p = &parent->rb_right;
1780 rb_link_node(&he->rb_node, parent, p);
1781 rb_insert_color_cached(&he->rb_node, root, leftmost);
1783 /* update column width of dynamic entry */
1784 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1785 if (perf_hpp__is_dynamic_entry(fmt))
1786 fmt->sort(fmt, he, NULL);
1790 static void hists__hierarchy_output_resort(struct hists *hists,
1791 struct ui_progress *prog,
1792 struct rb_root_cached *root_in,
1793 struct rb_root_cached *root_out,
1794 u64 min_callchain_hits,
1797 struct rb_node *node;
1798 struct hist_entry *he;
1800 *root_out = RB_ROOT_CACHED;
1801 node = rb_first_cached(root_in);
1804 he = rb_entry(node, struct hist_entry, rb_node_in);
1805 node = rb_next(node);
1807 hierarchy_insert_output_entry(root_out, he);
1810 ui_progress__update(prog, 1);
1812 hists->nr_entries++;
1813 if (!he->filtered) {
1814 hists->nr_non_filtered_entries++;
1815 hists__calc_col_len(hists, he);
1819 hists__hierarchy_output_resort(hists, prog,
1830 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1831 u64 total = he->stat.period;
1833 if (symbol_conf.cumulate_callchain)
1834 total = he->stat_acc->period;
1836 min_callchain_hits = total * (callchain_param.min_percent / 100);
1839 callchain_param.sort(&he->sorted_chain, he->callchain,
1840 min_callchain_hits, &callchain_param);
1844 static void __hists__insert_output_entry(struct rb_root_cached *entries,
1845 struct hist_entry *he,
1846 u64 min_callchain_hits,
1849 struct rb_node **p = &entries->rb_root.rb_node;
1850 struct rb_node *parent = NULL;
1851 struct hist_entry *iter;
1852 struct perf_hpp_fmt *fmt;
1853 bool leftmost = true;
1855 if (use_callchain) {
1856 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1857 u64 total = he->stat.period;
1859 if (symbol_conf.cumulate_callchain)
1860 total = he->stat_acc->period;
1862 min_callchain_hits = total * (callchain_param.min_percent / 100);
1864 callchain_param.sort(&he->sorted_chain, he->callchain,
1865 min_callchain_hits, &callchain_param);
1868 while (*p != NULL) {
1870 iter = rb_entry(parent, struct hist_entry, rb_node);
1872 if (hist_entry__sort(he, iter) > 0)
1875 p = &(*p)->rb_right;
1880 rb_link_node(&he->rb_node, parent, p);
1881 rb_insert_color_cached(&he->rb_node, entries, leftmost);
1883 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1884 if (perf_hpp__is_dynamic_entry(fmt) &&
1885 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1886 fmt->sort(fmt, he, NULL); /* update column width */
1890 static void output_resort(struct hists *hists, struct ui_progress *prog,
1891 bool use_callchain, hists__resort_cb_t cb,
1894 struct rb_root_cached *root;
1895 struct rb_node *next;
1896 struct hist_entry *n;
1897 u64 callchain_total;
1898 u64 min_callchain_hits;
1900 callchain_total = hists->callchain_period;
1901 if (symbol_conf.filter_relative)
1902 callchain_total = hists->callchain_non_filtered_period;
1904 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1906 hists__reset_stats(hists);
1907 hists__reset_col_len(hists);
1909 if (symbol_conf.report_hierarchy) {
1910 hists__hierarchy_output_resort(hists, prog,
1911 &hists->entries_collapsed,
1915 hierarchy_recalc_total_periods(hists);
1919 if (hists__has(hists, need_collapse))
1920 root = &hists->entries_collapsed;
1922 root = hists->entries_in;
1924 next = rb_first_cached(root);
1925 hists->entries = RB_ROOT_CACHED;
1928 n = rb_entry(next, struct hist_entry, rb_node_in);
1929 next = rb_next(&n->rb_node_in);
1931 if (cb && cb(n, cb_arg))
1934 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1935 hists__inc_stats(hists, n);
1938 hists__calc_col_len(hists, n);
1941 ui_progress__update(prog, 1);
1945 void evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
1946 hists__resort_cb_t cb, void *cb_arg)
1950 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1951 use_callchain = evsel__has_callchain(evsel);
1953 use_callchain = symbol_conf.use_callchain;
1955 use_callchain |= symbol_conf.show_branchflag_count;
1957 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1960 void evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
1962 return evsel__output_resort_cb(evsel, prog, NULL, NULL);
1965 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1967 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
1970 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1971 hists__resort_cb_t cb)
1973 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
1976 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1978 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1981 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1987 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1989 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1991 while (can_goto_child(he, HMD_NORMAL)) {
1992 node = rb_last(&he->hroot_out.rb_root);
1993 he = rb_entry(node, struct hist_entry, rb_node);
1998 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
2000 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2002 if (can_goto_child(he, hmd))
2003 node = rb_first_cached(&he->hroot_out);
2005 node = rb_next(node);
2007 while (node == NULL) {
2012 node = rb_next(&he->rb_node);
2017 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
2019 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2021 node = rb_prev(node);
2023 return rb_hierarchy_last(node);
2029 return &he->rb_node;
2032 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
2034 struct rb_node *node;
2035 struct hist_entry *child;
2041 node = rb_first_cached(&he->hroot_out);
2042 child = rb_entry(node, struct hist_entry, rb_node);
2044 while (node && child->filtered) {
2045 node = rb_next(node);
2046 child = rb_entry(node, struct hist_entry, rb_node);
2050 percent = hist_entry__get_percent_limit(child);
2054 return node && percent >= limit;
2057 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
2058 enum hist_filter filter)
2060 h->filtered &= ~(1 << filter);
2062 if (symbol_conf.report_hierarchy) {
2063 struct hist_entry *parent = h->parent_he;
2066 he_stat__add_stat(&parent->stat, &h->stat);
2068 parent->filtered &= ~(1 << filter);
2070 if (parent->filtered)
2073 /* force fold unfiltered entry for simplicity */
2074 parent->unfolded = false;
2075 parent->has_no_entry = false;
2076 parent->row_offset = 0;
2077 parent->nr_rows = 0;
2079 parent = parent->parent_he;
2086 /* force fold unfiltered entry for simplicity */
2087 h->unfolded = false;
2088 h->has_no_entry = false;
2092 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
2094 hists__inc_filter_stats(hists, h);
2095 hists__calc_col_len(hists, h);
2099 static bool hists__filter_entry_by_dso(struct hists *hists,
2100 struct hist_entry *he)
2102 if (hists->dso_filter != NULL &&
2103 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2104 he->filtered |= (1 << HIST_FILTER__DSO);
2111 static bool hists__filter_entry_by_thread(struct hists *hists,
2112 struct hist_entry *he)
2114 if (hists->thread_filter != NULL &&
2115 he->thread != hists->thread_filter) {
2116 he->filtered |= (1 << HIST_FILTER__THREAD);
2123 static bool hists__filter_entry_by_symbol(struct hists *hists,
2124 struct hist_entry *he)
2126 if (hists->symbol_filter_str != NULL &&
2127 (!he->ms.sym || strstr(he->ms.sym->name,
2128 hists->symbol_filter_str) == NULL)) {
2129 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2136 static bool hists__filter_entry_by_socket(struct hists *hists,
2137 struct hist_entry *he)
2139 if ((hists->socket_filter > -1) &&
2140 (he->socket != hists->socket_filter)) {
2141 he->filtered |= (1 << HIST_FILTER__SOCKET);
2148 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2150 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
2154 hists->stats.nr_non_filtered_samples = 0;
2156 hists__reset_filter_stats(hists);
2157 hists__reset_col_len(hists);
2159 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
2160 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2162 if (filter(hists, h))
2165 hists__remove_entry_filter(hists, h, type);
2169 static void resort_filtered_entry(struct rb_root_cached *root,
2170 struct hist_entry *he)
2172 struct rb_node **p = &root->rb_root.rb_node;
2173 struct rb_node *parent = NULL;
2174 struct hist_entry *iter;
2175 struct rb_root_cached new_root = RB_ROOT_CACHED;
2177 bool leftmost = true;
2179 while (*p != NULL) {
2181 iter = rb_entry(parent, struct hist_entry, rb_node);
2183 if (hist_entry__sort(he, iter) > 0)
2186 p = &(*p)->rb_right;
2191 rb_link_node(&he->rb_node, parent, p);
2192 rb_insert_color_cached(&he->rb_node, root, leftmost);
2194 if (he->leaf || he->filtered)
2197 nd = rb_first_cached(&he->hroot_out);
2199 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2202 rb_erase_cached(&h->rb_node, &he->hroot_out);
2204 resort_filtered_entry(&new_root, h);
2207 he->hroot_out = new_root;
2210 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2213 struct rb_root_cached new_root = RB_ROOT_CACHED;
2215 hists->stats.nr_non_filtered_samples = 0;
2217 hists__reset_filter_stats(hists);
2218 hists__reset_col_len(hists);
2220 nd = rb_first_cached(&hists->entries);
2222 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2225 ret = hist_entry__filter(h, type, arg);
2228 * case 1. non-matching type
2229 * zero out the period, set filter marker and move to child
2232 memset(&h->stat, 0, sizeof(h->stat));
2233 h->filtered |= (1 << type);
2235 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2238 * case 2. matched type (filter out)
2239 * set filter marker and move to next
2241 else if (ret == 1) {
2242 h->filtered |= (1 << type);
2244 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2247 * case 3. ok (not filtered)
2248 * add period to hists and parents, erase the filter marker
2249 * and move to next sibling
2252 hists__remove_entry_filter(hists, h, type);
2254 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2258 hierarchy_recalc_total_periods(hists);
2261 * resort output after applying a new filter since filter in a lower
2262 * hierarchy can change periods in a upper hierarchy.
2264 nd = rb_first_cached(&hists->entries);
2266 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2269 rb_erase_cached(&h->rb_node, &hists->entries);
2271 resort_filtered_entry(&new_root, h);
2274 hists->entries = new_root;
2277 void hists__filter_by_thread(struct hists *hists)
2279 if (symbol_conf.report_hierarchy)
2280 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2281 hists->thread_filter);
2283 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2284 hists__filter_entry_by_thread);
2287 void hists__filter_by_dso(struct hists *hists)
2289 if (symbol_conf.report_hierarchy)
2290 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2293 hists__filter_by_type(hists, HIST_FILTER__DSO,
2294 hists__filter_entry_by_dso);
2297 void hists__filter_by_symbol(struct hists *hists)
2299 if (symbol_conf.report_hierarchy)
2300 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2301 hists->symbol_filter_str);
2303 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2304 hists__filter_entry_by_symbol);
2307 void hists__filter_by_socket(struct hists *hists)
2309 if (symbol_conf.report_hierarchy)
2310 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2311 &hists->socket_filter);
2313 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2314 hists__filter_entry_by_socket);
2317 void events_stats__inc(struct events_stats *stats, u32 type)
2319 ++stats->nr_events[0];
2320 ++stats->nr_events[type];
2323 void hists__inc_nr_events(struct hists *hists, u32 type)
2325 events_stats__inc(&hists->stats, type);
2328 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2330 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2332 hists->stats.nr_non_filtered_samples++;
2335 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2336 struct hist_entry *pair)
2338 struct rb_root_cached *root;
2340 struct rb_node *parent = NULL;
2341 struct hist_entry *he;
2343 bool leftmost = true;
2345 if (hists__has(hists, need_collapse))
2346 root = &hists->entries_collapsed;
2348 root = hists->entries_in;
2350 p = &root->rb_root.rb_node;
2352 while (*p != NULL) {
2354 he = rb_entry(parent, struct hist_entry, rb_node_in);
2356 cmp = hist_entry__collapse(he, pair);
2364 p = &(*p)->rb_right;
2369 he = hist_entry__new(pair, true);
2371 memset(&he->stat, 0, sizeof(he->stat));
2373 if (symbol_conf.cumulate_callchain)
2374 memset(he->stat_acc, 0, sizeof(he->stat));
2375 rb_link_node(&he->rb_node_in, parent, p);
2376 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2377 hists__inc_stats(hists, he);
2384 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2385 struct rb_root_cached *root,
2386 struct hist_entry *pair)
2389 struct rb_node *parent = NULL;
2390 struct hist_entry *he;
2391 struct perf_hpp_fmt *fmt;
2392 bool leftmost = true;
2394 p = &root->rb_root.rb_node;
2395 while (*p != NULL) {
2399 he = rb_entry(parent, struct hist_entry, rb_node_in);
2401 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2402 cmp = fmt->collapse(fmt, he, pair);
2410 p = &parent->rb_left;
2412 p = &parent->rb_right;
2417 he = hist_entry__new(pair, true);
2419 rb_link_node(&he->rb_node_in, parent, p);
2420 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2424 memset(&he->stat, 0, sizeof(he->stat));
2425 hists__inc_stats(hists, he);
2431 static struct hist_entry *hists__find_entry(struct hists *hists,
2432 struct hist_entry *he)
2436 if (hists__has(hists, need_collapse))
2437 n = hists->entries_collapsed.rb_root.rb_node;
2439 n = hists->entries_in->rb_root.rb_node;
2442 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2443 int64_t cmp = hist_entry__collapse(iter, he);
2456 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
2457 struct hist_entry *he)
2459 struct rb_node *n = root->rb_root.rb_node;
2462 struct hist_entry *iter;
2463 struct perf_hpp_fmt *fmt;
2466 iter = rb_entry(n, struct hist_entry, rb_node_in);
2467 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2468 cmp = fmt->collapse(fmt, iter, he);
2484 static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2485 struct rb_root_cached *other_root)
2488 struct hist_entry *pos, *pair;
2490 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
2491 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2492 pair = hists__find_hierarchy_entry(other_root, pos);
2495 hist_entry__add_pair(pair, pos);
2496 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2502 * Look for pairs to link to the leader buckets (hist_entries):
2504 void hists__match(struct hists *leader, struct hists *other)
2506 struct rb_root_cached *root;
2508 struct hist_entry *pos, *pair;
2510 if (symbol_conf.report_hierarchy) {
2511 /* hierarchy report always collapses entries */
2512 return hists__match_hierarchy(&leader->entries_collapsed,
2513 &other->entries_collapsed);
2516 if (hists__has(leader, need_collapse))
2517 root = &leader->entries_collapsed;
2519 root = leader->entries_in;
2521 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2522 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2523 pair = hists__find_entry(other, pos);
2526 hist_entry__add_pair(pair, pos);
2530 static int hists__link_hierarchy(struct hists *leader_hists,
2531 struct hist_entry *parent,
2532 struct rb_root_cached *leader_root,
2533 struct rb_root_cached *other_root)
2536 struct hist_entry *pos, *leader;
2538 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
2539 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2541 if (hist_entry__has_pairs(pos)) {
2544 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2545 if (leader->hists == leader_hists) {
2553 leader = add_dummy_hierarchy_entry(leader_hists,
2558 /* do not point parent in the pos */
2559 leader->parent_he = parent;
2561 hist_entry__add_pair(pos, leader);
2565 if (hists__link_hierarchy(leader_hists, leader,
2567 &pos->hroot_in) < 0)
2575 * Look for entries in the other hists that are not present in the leader, if
2576 * we find them, just add a dummy entry on the leader hists, with period=0,
2577 * nr_events=0, to serve as the list header.
2579 int hists__link(struct hists *leader, struct hists *other)
2581 struct rb_root_cached *root;
2583 struct hist_entry *pos, *pair;
2585 if (symbol_conf.report_hierarchy) {
2586 /* hierarchy report always collapses entries */
2587 return hists__link_hierarchy(leader, NULL,
2588 &leader->entries_collapsed,
2589 &other->entries_collapsed);
2592 if (hists__has(other, need_collapse))
2593 root = &other->entries_collapsed;
2595 root = other->entries_in;
2597 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2598 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2600 if (!hist_entry__has_pairs(pos)) {
2601 pair = hists__add_dummy_entry(leader, pos);
2604 hist_entry__add_pair(pos, pair);
2611 int hists__unlink(struct hists *hists)
2613 struct rb_root_cached *root;
2615 struct hist_entry *pos;
2617 if (hists__has(hists, need_collapse))
2618 root = &hists->entries_collapsed;
2620 root = hists->entries_in;
2622 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2623 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2624 list_del_init(&pos->pairs.node);
2630 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2631 struct perf_sample *sample, bool nonany_branch_mode,
2634 struct branch_info *bi;
2635 struct branch_entry *entries = perf_sample__branch_entries(sample);
2637 /* If we have branch cycles always annotate them. */
2638 if (bs && bs->nr && entries[0].flags.cycles) {
2641 bi = sample__resolve_bstack(sample, al);
2643 struct addr_map_symbol *prev = NULL;
2646 * Ignore errors, still want to process the
2649 * For non standard branch modes always
2650 * force no IPC (prev == NULL)
2652 * Note that perf stores branches reversed from
2655 for (i = bs->nr - 1; i >= 0; i--) {
2656 addr_map_symbol__account_cycles(&bi[i].from,
2657 nonany_branch_mode ? NULL : prev,
2658 bi[i].flags.cycles);
2662 *total_cycles += bi[i].flags.cycles;
2669 size_t evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp)
2674 evlist__for_each_entry(evlist, pos) {
2675 ret += fprintf(fp, "%s stats:\n", evsel__name(pos));
2676 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2683 u64 hists__total_period(struct hists *hists)
2685 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2686 hists->stats.total_period;
2689 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2693 const struct dso *dso = hists->dso_filter;
2694 struct thread *thread = hists->thread_filter;
2695 int socket_id = hists->socket_filter;
2696 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2697 u64 nr_events = hists->stats.total_period;
2698 struct evsel *evsel = hists_to_evsel(hists);
2699 const char *ev_name = evsel__name(evsel);
2700 char buf[512], sample_freq_str[64] = "";
2701 size_t buflen = sizeof(buf);
2702 char ref[30] = " show reference callgraph, ";
2703 bool enable_ref = false;
2705 if (symbol_conf.filter_relative) {
2706 nr_samples = hists->stats.nr_non_filtered_samples;
2707 nr_events = hists->stats.total_non_filtered_period;
2710 if (evsel__is_group_event(evsel)) {
2713 evsel__group_desc(evsel, buf, buflen);
2716 for_each_group_member(pos, evsel) {
2717 struct hists *pos_hists = evsel__hists(pos);
2719 if (symbol_conf.filter_relative) {
2720 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2721 nr_events += pos_hists->stats.total_non_filtered_period;
2723 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2724 nr_events += pos_hists->stats.total_period;
2729 if (symbol_conf.show_ref_callgraph &&
2730 strstr(ev_name, "call-graph=no"))
2734 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
2736 nr_samples = convert_unit(nr_samples, &unit);
2737 printed = scnprintf(bf, size,
2738 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
2739 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
2740 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2743 if (hists->uid_filter_str)
2744 printed += snprintf(bf + printed, size - printed,
2745 ", UID: %s", hists->uid_filter_str);
2747 if (hists__has(hists, thread)) {
2748 printed += scnprintf(bf + printed, size - printed,
2750 (thread->comm_set ? thread__comm_str(thread) : ""),
2753 printed += scnprintf(bf + printed, size - printed,
2755 (thread->comm_set ? thread__comm_str(thread) : ""));
2759 printed += scnprintf(bf + printed, size - printed,
2760 ", DSO: %s", dso->short_name);
2762 printed += scnprintf(bf + printed, size - printed,
2763 ", Processor Socket: %d", socket_id);
2768 int parse_filter_percentage(const struct option *opt __maybe_unused,
2769 const char *arg, int unset __maybe_unused)
2771 if (!strcmp(arg, "relative"))
2772 symbol_conf.filter_relative = true;
2773 else if (!strcmp(arg, "absolute"))
2774 symbol_conf.filter_relative = false;
2776 pr_debug("Invalid percentage: %s\n", arg);
2783 int perf_hist_config(const char *var, const char *value)
2785 if (!strcmp(var, "hist.percentage"))
2786 return parse_filter_percentage(NULL, value, 0);
2791 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2793 memset(hists, 0, sizeof(*hists));
2794 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
2795 hists->entries_in = &hists->entries_in_array[0];
2796 hists->entries_collapsed = RB_ROOT_CACHED;
2797 hists->entries = RB_ROOT_CACHED;
2798 pthread_mutex_init(&hists->lock, NULL);
2799 hists->socket_filter = -1;
2800 hists->hpp_list = hpp_list;
2801 INIT_LIST_HEAD(&hists->hpp_formats);
2805 static void hists__delete_remaining_entries(struct rb_root_cached *root)
2807 struct rb_node *node;
2808 struct hist_entry *he;
2810 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2811 node = rb_first_cached(root);
2812 rb_erase_cached(node, root);
2814 he = rb_entry(node, struct hist_entry, rb_node_in);
2815 hist_entry__delete(he);
2819 static void hists__delete_all_entries(struct hists *hists)
2821 hists__delete_entries(hists);
2822 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2823 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2824 hists__delete_remaining_entries(&hists->entries_collapsed);
2827 static void hists_evsel__exit(struct evsel *evsel)
2829 struct hists *hists = evsel__hists(evsel);
2830 struct perf_hpp_fmt *fmt, *pos;
2831 struct perf_hpp_list_node *node, *tmp;
2833 hists__delete_all_entries(hists);
2835 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2836 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2837 list_del_init(&fmt->list);
2840 list_del_init(&node->list);
2845 static int hists_evsel__init(struct evsel *evsel)
2847 struct hists *hists = evsel__hists(evsel);
2849 __hists__init(hists, &perf_hpp_list);
2854 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2855 * stored in the rbtree...
2858 int hists__init(void)
2860 int err = evsel__object_config(sizeof(struct hists_evsel),
2861 hists_evsel__init, hists_evsel__exit);
2863 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2868 void perf_hpp_list__init(struct perf_hpp_list *list)
2870 INIT_LIST_HEAD(&list->fields);
2871 INIT_LIST_HEAD(&list->sorts);