1 // SPDX-License-Identifier: GPL-2.0
8 #include "namespaces.h"
17 #include "ui/progress.h"
21 #include <sys/param.h>
22 #include <linux/time64.h>
24 static bool hists__filter_entry_by_dso(struct hists *hists,
25 struct hist_entry *he);
26 static bool hists__filter_entry_by_thread(struct hists *hists,
27 struct hist_entry *he);
28 static bool hists__filter_entry_by_symbol(struct hists *hists,
29 struct hist_entry *he);
30 static bool hists__filter_entry_by_socket(struct hists *hists,
31 struct hist_entry *he);
33 u16 hists__col_len(struct hists *hists, enum hist_column col)
35 return hists->col_len[col];
38 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
40 hists->col_len[col] = len;
43 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
45 if (len > hists__col_len(hists, col)) {
46 hists__set_col_len(hists, col, len);
52 void hists__reset_col_len(struct hists *hists)
56 for (col = 0; col < HISTC_NR_COLS; ++col)
57 hists__set_col_len(hists, col, 0);
60 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
62 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
64 if (hists__col_len(hists, dso) < unresolved_col_width &&
65 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
66 !symbol_conf.dso_list)
67 hists__set_col_len(hists, dso, unresolved_col_width);
70 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
72 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
77 * +4 accounts for '[x] ' priv level info
78 * +2 accounts for 0x prefix on raw addresses
79 * +3 accounts for ' y ' symtab origin info
82 symlen = h->ms.sym->namelen + 4;
84 symlen += BITS_PER_LONG / 4 + 2 + 3;
85 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
87 symlen = unresolved_col_width + 4 + 2;
88 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
89 hists__set_unres_dso_col_len(hists, HISTC_DSO);
92 len = thread__comm_len(h->thread);
93 if (hists__new_col_len(hists, HISTC_COMM, len))
94 hists__set_col_len(hists, HISTC_THREAD, len + 8);
97 len = dso__name_len(h->ms.map->dso);
98 hists__new_col_len(hists, HISTC_DSO, len);
102 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
104 if (h->branch_info) {
105 if (h->branch_info->from.sym) {
106 symlen = (int)h->branch_info->from.sym->namelen + 4;
108 symlen += BITS_PER_LONG / 4 + 2 + 3;
109 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
111 symlen = dso__name_len(h->branch_info->from.map->dso);
112 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
114 symlen = unresolved_col_width + 4 + 2;
115 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
116 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
119 if (h->branch_info->to.sym) {
120 symlen = (int)h->branch_info->to.sym->namelen + 4;
122 symlen += BITS_PER_LONG / 4 + 2 + 3;
123 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
125 symlen = dso__name_len(h->branch_info->to.map->dso);
126 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
128 symlen = unresolved_col_width + 4 + 2;
129 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
130 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
133 if (h->branch_info->srcline_from)
134 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
135 strlen(h->branch_info->srcline_from));
136 if (h->branch_info->srcline_to)
137 hists__new_col_len(hists, HISTC_SRCLINE_TO,
138 strlen(h->branch_info->srcline_to));
142 if (h->mem_info->daddr.sym) {
143 symlen = (int)h->mem_info->daddr.sym->namelen + 4
144 + unresolved_col_width + 2;
145 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
147 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
150 symlen = unresolved_col_width + 4 + 2;
151 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
153 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
157 if (h->mem_info->iaddr.sym) {
158 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
159 + unresolved_col_width + 2;
160 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
163 symlen = unresolved_col_width + 4 + 2;
164 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
168 if (h->mem_info->daddr.map) {
169 symlen = dso__name_len(h->mem_info->daddr.map->dso);
170 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
173 symlen = unresolved_col_width + 4 + 2;
174 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
177 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
178 unresolved_col_width + 4 + 2);
181 symlen = unresolved_col_width + 4 + 2;
182 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
183 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
184 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
187 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
188 hists__new_col_len(hists, HISTC_CPU, 3);
189 hists__new_col_len(hists, HISTC_SOCKET, 6);
190 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
191 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
192 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
193 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
194 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
195 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
196 hists__new_col_len(hists, HISTC_TIME, 12);
199 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
200 hists__new_col_len(hists, HISTC_SRCLINE, len);
204 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
207 hists__new_col_len(hists, HISTC_TRANSACTION,
208 hist_entry__transaction_len());
211 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
214 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
216 struct rb_node *next = rb_first_cached(&hists->entries);
217 struct hist_entry *n;
220 hists__reset_col_len(hists);
222 while (next && row++ < max_rows) {
223 n = rb_entry(next, struct hist_entry, rb_node);
225 hists__calc_col_len(hists, n);
226 next = rb_next(&n->rb_node);
230 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
231 unsigned int cpumode, u64 period)
234 case PERF_RECORD_MISC_KERNEL:
235 he_stat->period_sys += period;
237 case PERF_RECORD_MISC_USER:
238 he_stat->period_us += period;
240 case PERF_RECORD_MISC_GUEST_KERNEL:
241 he_stat->period_guest_sys += period;
243 case PERF_RECORD_MISC_GUEST_USER:
244 he_stat->period_guest_us += period;
251 static long hist_time(unsigned long htime)
253 unsigned long time_quantum = symbol_conf.time_quantum;
255 return (htime / time_quantum) * time_quantum;
259 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
263 he_stat->period += period;
264 he_stat->weight += weight;
265 he_stat->nr_events += 1;
268 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
270 dest->period += src->period;
271 dest->period_sys += src->period_sys;
272 dest->period_us += src->period_us;
273 dest->period_guest_sys += src->period_guest_sys;
274 dest->period_guest_us += src->period_guest_us;
275 dest->nr_events += src->nr_events;
276 dest->weight += src->weight;
279 static void he_stat__decay(struct he_stat *he_stat)
281 he_stat->period = (he_stat->period * 7) / 8;
282 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
283 /* XXX need decay for weight too? */
286 static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
288 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
290 u64 prev_period = he->stat.period;
293 if (prev_period == 0)
296 he_stat__decay(&he->stat);
297 if (symbol_conf.cumulate_callchain)
298 he_stat__decay(he->stat_acc);
299 decay_callchain(he->callchain);
301 diff = prev_period - he->stat.period;
304 hists->stats.total_period -= diff;
306 hists->stats.total_non_filtered_period -= diff;
310 struct hist_entry *child;
311 struct rb_node *node = rb_first_cached(&he->hroot_out);
313 child = rb_entry(node, struct hist_entry, rb_node);
314 node = rb_next(node);
316 if (hists__decay_entry(hists, child))
317 hists__delete_entry(hists, child);
321 return he->stat.period == 0;
324 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
326 struct rb_root_cached *root_in;
327 struct rb_root_cached *root_out;
330 root_in = &he->parent_he->hroot_in;
331 root_out = &he->parent_he->hroot_out;
333 if (hists__has(hists, need_collapse))
334 root_in = &hists->entries_collapsed;
336 root_in = hists->entries_in;
337 root_out = &hists->entries;
340 rb_erase_cached(&he->rb_node_in, root_in);
341 rb_erase_cached(&he->rb_node, root_out);
345 --hists->nr_non_filtered_entries;
347 hist_entry__delete(he);
350 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
352 struct rb_node *next = rb_first_cached(&hists->entries);
353 struct hist_entry *n;
356 n = rb_entry(next, struct hist_entry, rb_node);
357 next = rb_next(&n->rb_node);
358 if (((zap_user && n->level == '.') ||
359 (zap_kernel && n->level != '.') ||
360 hists__decay_entry(hists, n))) {
361 hists__delete_entry(hists, n);
366 void hists__delete_entries(struct hists *hists)
368 struct rb_node *next = rb_first_cached(&hists->entries);
369 struct hist_entry *n;
372 n = rb_entry(next, struct hist_entry, rb_node);
373 next = rb_next(&n->rb_node);
375 hists__delete_entry(hists, n);
379 struct hist_entry *hists__get_entry(struct hists *hists, int idx)
381 struct rb_node *next = rb_first_cached(&hists->entries);
382 struct hist_entry *n;
386 n = rb_entry(next, struct hist_entry, rb_node);
390 next = rb_next(&n->rb_node);
398 * histogram, sorted on item, collects periods
401 static int hist_entry__init(struct hist_entry *he,
402 struct hist_entry *template,
404 size_t callchain_size)
407 he->callchain_size = callchain_size;
409 if (symbol_conf.cumulate_callchain) {
410 he->stat_acc = malloc(sizeof(he->stat));
411 if (he->stat_acc == NULL)
413 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
415 memset(&he->stat, 0, sizeof(he->stat));
418 map__get(he->ms.map);
420 if (he->branch_info) {
422 * This branch info is (a part of) allocated from
423 * sample__resolve_bstack() and will be freed after
424 * adding new entries. So we need to save a copy.
426 he->branch_info = malloc(sizeof(*he->branch_info));
427 if (he->branch_info == NULL)
430 memcpy(he->branch_info, template->branch_info,
431 sizeof(*he->branch_info));
433 map__get(he->branch_info->from.map);
434 map__get(he->branch_info->to.map);
438 map__get(he->mem_info->iaddr.map);
439 map__get(he->mem_info->daddr.map);
442 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
443 callchain_init(he->callchain);
446 he->raw_data = memdup(he->raw_data, he->raw_size);
447 if (he->raw_data == NULL)
452 he->srcline = strdup(he->srcline);
453 if (he->srcline == NULL)
457 if (symbol_conf.res_sample) {
458 he->res_samples = calloc(sizeof(struct res_sample),
459 symbol_conf.res_sample);
460 if (!he->res_samples)
464 INIT_LIST_HEAD(&he->pairs.node);
465 thread__get(he->thread);
466 he->hroot_in = RB_ROOT_CACHED;
467 he->hroot_out = RB_ROOT_CACHED;
469 if (!symbol_conf.report_hierarchy)
481 if (he->branch_info) {
482 map__put(he->branch_info->from.map);
483 map__put(he->branch_info->to.map);
484 free(he->branch_info);
487 map__put(he->mem_info->iaddr.map);
488 map__put(he->mem_info->daddr.map);
491 map__zput(he->ms.map);
496 static void *hist_entry__zalloc(size_t size)
498 return zalloc(size + sizeof(struct hist_entry));
501 static void hist_entry__free(void *ptr)
506 static struct hist_entry_ops default_ops = {
507 .new = hist_entry__zalloc,
508 .free = hist_entry__free,
511 static struct hist_entry *hist_entry__new(struct hist_entry *template,
514 struct hist_entry_ops *ops = template->ops;
515 size_t callchain_size = 0;
516 struct hist_entry *he;
520 ops = template->ops = &default_ops;
522 if (symbol_conf.use_callchain)
523 callchain_size = sizeof(struct callchain_root);
525 he = ops->new(callchain_size);
527 err = hist_entry__init(he, template, sample_self, callchain_size);
537 static u8 symbol__parent_filter(const struct symbol *parent)
539 if (symbol_conf.exclude_other && parent == NULL)
540 return 1 << HIST_FILTER__PARENT;
544 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
546 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
549 he->hists->callchain_period += period;
551 he->hists->callchain_non_filtered_period += period;
554 static struct hist_entry *hists__findnew_entry(struct hists *hists,
555 struct hist_entry *entry,
556 struct addr_location *al,
560 struct rb_node *parent = NULL;
561 struct hist_entry *he;
563 u64 period = entry->stat.period;
564 u64 weight = entry->stat.weight;
565 bool leftmost = true;
567 p = &hists->entries_in->rb_root.rb_node;
571 he = rb_entry(parent, struct hist_entry, rb_node_in);
574 * Make sure that it receives arguments in a same order as
575 * hist_entry__collapse() so that we can use an appropriate
576 * function when searching an entry regardless which sort
579 cmp = hist_entry__cmp(he, entry);
583 he_stat__add_period(&he->stat, period, weight);
584 hist_entry__add_callchain_period(he, period);
586 if (symbol_conf.cumulate_callchain)
587 he_stat__add_period(he->stat_acc, period, weight);
590 * This mem info was allocated from sample__resolve_mem
591 * and will not be used anymore.
593 mem_info__zput(entry->mem_info);
595 block_info__zput(entry->block_info);
597 /* If the map of an existing hist_entry has
598 * become out-of-date due to an exec() or
599 * similar, update it. Otherwise we will
600 * mis-adjust symbol addresses when computing
601 * the history counter to increment.
603 if (he->ms.map != entry->ms.map) {
604 map__put(he->ms.map);
605 he->ms.map = map__get(entry->ms.map);
618 he = hist_entry__new(entry, sample_self);
623 hist_entry__add_callchain_period(he, period);
626 rb_link_node(&he->rb_node_in, parent, p);
627 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
630 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
631 if (symbol_conf.cumulate_callchain)
632 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
636 static unsigned random_max(unsigned high)
638 unsigned thresh = -high % high;
640 unsigned r = random();
646 static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
648 struct res_sample *r;
651 if (he->num_res < symbol_conf.res_sample) {
654 j = random_max(symbol_conf.res_sample);
656 r = &he->res_samples[j];
657 r->time = sample->time;
658 r->cpu = sample->cpu;
659 r->tid = sample->tid;
662 static struct hist_entry*
663 __hists__add_entry(struct hists *hists,
664 struct addr_location *al,
665 struct symbol *sym_parent,
666 struct branch_info *bi,
668 struct block_info *block_info,
669 struct perf_sample *sample,
671 struct hist_entry_ops *ops)
673 struct namespaces *ns = thread__namespaces(al->thread);
674 struct hist_entry entry = {
675 .thread = al->thread,
676 .comm = thread__comm(al->thread),
678 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
679 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
685 .srcline = (char *) al->srcline,
686 .socket = al->socket,
688 .cpumode = al->cpumode,
693 .period = sample->period,
694 .weight = sample->weight,
696 .parent = sym_parent,
697 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
701 .block_info = block_info,
702 .transaction = sample->transaction,
703 .raw_data = sample->raw_data,
704 .raw_size = sample->raw_size,
706 .time = hist_time(sample->time),
707 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
709 if (!hists->has_callchains && he && he->callchain_size != 0)
710 hists->has_callchains = true;
711 if (he && symbol_conf.res_sample)
712 hists__res_sample(he, sample);
716 struct hist_entry *hists__add_entry(struct hists *hists,
717 struct addr_location *al,
718 struct symbol *sym_parent,
719 struct branch_info *bi,
721 struct perf_sample *sample,
724 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
725 sample, sample_self, NULL);
728 struct hist_entry *hists__add_entry_ops(struct hists *hists,
729 struct hist_entry_ops *ops,
730 struct addr_location *al,
731 struct symbol *sym_parent,
732 struct branch_info *bi,
734 struct perf_sample *sample,
737 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
738 sample, sample_self, ops);
741 struct hist_entry *hists__add_entry_block(struct hists *hists,
742 struct addr_location *al,
743 struct block_info *block_info)
745 struct hist_entry entry = {
746 .block_info = block_info,
748 }, *he = hists__findnew_entry(hists, &entry, al, false);
754 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
755 struct addr_location *al __maybe_unused)
761 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
762 struct addr_location *al __maybe_unused)
768 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
770 struct perf_sample *sample = iter->sample;
773 mi = sample__resolve_mem(sample, al);
782 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
785 struct mem_info *mi = iter->priv;
786 struct hists *hists = evsel__hists(iter->evsel);
787 struct perf_sample *sample = iter->sample;
788 struct hist_entry *he;
793 cost = sample->weight;
798 * must pass period=weight in order to get the correct
799 * sorting from hists__collapse_resort() which is solely
800 * based on periods. We want sorting be done on nr_events * weight
801 * and this is indirectly achieved by passing period=weight here
802 * and the he_stat__add_period() function.
804 sample->period = cost;
806 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
816 iter_finish_mem_entry(struct hist_entry_iter *iter,
817 struct addr_location *al __maybe_unused)
819 struct perf_evsel *evsel = iter->evsel;
820 struct hists *hists = evsel__hists(evsel);
821 struct hist_entry *he = iter->he;
827 hists__inc_nr_samples(hists, he->filtered);
829 err = hist_entry__append_callchain(he, iter->sample);
833 * We don't need to free iter->priv (mem_info) here since the mem info
834 * was either already freed in hists__findnew_entry() or passed to a
835 * new hist entry by hist_entry__new().
844 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
846 struct branch_info *bi;
847 struct perf_sample *sample = iter->sample;
849 bi = sample__resolve_bstack(sample, al);
854 iter->total = sample->branch_stack->nr;
861 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
862 struct addr_location *al __maybe_unused)
868 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
870 struct branch_info *bi = iter->priv;
876 if (iter->curr >= iter->total)
879 al->map = bi[i].to.map;
880 al->sym = bi[i].to.sym;
881 al->addr = bi[i].to.addr;
886 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
888 struct branch_info *bi;
889 struct perf_evsel *evsel = iter->evsel;
890 struct hists *hists = evsel__hists(evsel);
891 struct perf_sample *sample = iter->sample;
892 struct hist_entry *he = NULL;
898 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
902 * The report shows the percentage of total branches captured
903 * and not events sampled. Thus we use a pseudo period of 1.
906 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
908 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
913 hists__inc_nr_samples(hists, he->filtered);
922 iter_finish_branch_entry(struct hist_entry_iter *iter,
923 struct addr_location *al __maybe_unused)
928 return iter->curr >= iter->total ? 0 : -1;
932 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
933 struct addr_location *al __maybe_unused)
939 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
941 struct perf_evsel *evsel = iter->evsel;
942 struct perf_sample *sample = iter->sample;
943 struct hist_entry *he;
945 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
955 iter_finish_normal_entry(struct hist_entry_iter *iter,
956 struct addr_location *al __maybe_unused)
958 struct hist_entry *he = iter->he;
959 struct perf_evsel *evsel = iter->evsel;
960 struct perf_sample *sample = iter->sample;
967 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
969 return hist_entry__append_callchain(he, sample);
973 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
974 struct addr_location *al __maybe_unused)
976 struct hist_entry **he_cache;
978 callchain_cursor_commit(&callchain_cursor);
981 * This is for detecting cycles or recursions so that they're
982 * cumulated only one time to prevent entries more than 100%
985 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
986 if (he_cache == NULL)
989 iter->priv = he_cache;
996 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
997 struct addr_location *al)
999 struct perf_evsel *evsel = iter->evsel;
1000 struct hists *hists = evsel__hists(evsel);
1001 struct perf_sample *sample = iter->sample;
1002 struct hist_entry **he_cache = iter->priv;
1003 struct hist_entry *he;
1006 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1012 he_cache[iter->curr++] = he;
1014 hist_entry__append_callchain(he, sample);
1017 * We need to re-initialize the cursor since callchain_append()
1018 * advanced the cursor to the end.
1020 callchain_cursor_commit(&callchain_cursor);
1022 hists__inc_nr_samples(hists, he->filtered);
1028 iter_next_cumulative_entry(struct hist_entry_iter *iter,
1029 struct addr_location *al)
1031 struct callchain_cursor_node *node;
1033 node = callchain_cursor_current(&callchain_cursor);
1037 return fill_callchain_info(al, node, iter->hide_unresolved);
1041 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1042 struct addr_location *al)
1044 struct perf_evsel *evsel = iter->evsel;
1045 struct perf_sample *sample = iter->sample;
1046 struct hist_entry **he_cache = iter->priv;
1047 struct hist_entry *he;
1048 struct hist_entry he_tmp = {
1049 .hists = evsel__hists(evsel),
1051 .thread = al->thread,
1052 .comm = thread__comm(al->thread),
1058 .srcline = (char *) al->srcline,
1059 .parent = iter->parent,
1060 .raw_data = sample->raw_data,
1061 .raw_size = sample->raw_size,
1064 struct callchain_cursor cursor;
1066 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1068 callchain_cursor_advance(&callchain_cursor);
1071 * Check if there's duplicate entries in the callchain.
1072 * It's possible that it has cycles or recursive calls.
1074 for (i = 0; i < iter->curr; i++) {
1075 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1076 /* to avoid calling callback function */
1082 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1088 he_cache[iter->curr++] = he;
1090 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
1091 callchain_append(he->callchain, &cursor, sample->period);
1096 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1097 struct addr_location *al __maybe_unused)
1105 const struct hist_iter_ops hist_iter_mem = {
1106 .prepare_entry = iter_prepare_mem_entry,
1107 .add_single_entry = iter_add_single_mem_entry,
1108 .next_entry = iter_next_nop_entry,
1109 .add_next_entry = iter_add_next_nop_entry,
1110 .finish_entry = iter_finish_mem_entry,
1113 const struct hist_iter_ops hist_iter_branch = {
1114 .prepare_entry = iter_prepare_branch_entry,
1115 .add_single_entry = iter_add_single_branch_entry,
1116 .next_entry = iter_next_branch_entry,
1117 .add_next_entry = iter_add_next_branch_entry,
1118 .finish_entry = iter_finish_branch_entry,
1121 const struct hist_iter_ops hist_iter_normal = {
1122 .prepare_entry = iter_prepare_normal_entry,
1123 .add_single_entry = iter_add_single_normal_entry,
1124 .next_entry = iter_next_nop_entry,
1125 .add_next_entry = iter_add_next_nop_entry,
1126 .finish_entry = iter_finish_normal_entry,
1129 const struct hist_iter_ops hist_iter_cumulative = {
1130 .prepare_entry = iter_prepare_cumulative_entry,
1131 .add_single_entry = iter_add_single_cumulative_entry,
1132 .next_entry = iter_next_cumulative_entry,
1133 .add_next_entry = iter_add_next_cumulative_entry,
1134 .finish_entry = iter_finish_cumulative_entry,
1137 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1138 int max_stack_depth, void *arg)
1141 struct map *alm = NULL;
1144 alm = map__get(al->map);
1146 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1147 iter->evsel, al, max_stack_depth);
1153 err = iter->ops->prepare_entry(iter, al);
1157 err = iter->ops->add_single_entry(iter, al);
1161 if (iter->he && iter->add_entry_cb) {
1162 err = iter->add_entry_cb(iter, al, true, arg);
1167 while (iter->ops->next_entry(iter, al)) {
1168 err = iter->ops->add_next_entry(iter, al);
1172 if (iter->he && iter->add_entry_cb) {
1173 err = iter->add_entry_cb(iter, al, false, arg);
1180 err2 = iter->ops->finish_entry(iter, al);
1190 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1192 struct hists *hists = left->hists;
1193 struct perf_hpp_fmt *fmt;
1196 hists__for_each_sort_list(hists, fmt) {
1197 if (perf_hpp__is_dynamic_entry(fmt) &&
1198 !perf_hpp__defined_dynamic_entry(fmt, hists))
1201 cmp = fmt->cmp(fmt, left, right);
1210 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1212 struct hists *hists = left->hists;
1213 struct perf_hpp_fmt *fmt;
1216 hists__for_each_sort_list(hists, fmt) {
1217 if (perf_hpp__is_dynamic_entry(fmt) &&
1218 !perf_hpp__defined_dynamic_entry(fmt, hists))
1221 cmp = fmt->collapse(fmt, left, right);
1229 void hist_entry__delete(struct hist_entry *he)
1231 struct hist_entry_ops *ops = he->ops;
1233 thread__zput(he->thread);
1234 map__zput(he->ms.map);
1236 if (he->branch_info) {
1237 map__zput(he->branch_info->from.map);
1238 map__zput(he->branch_info->to.map);
1239 free_srcline(he->branch_info->srcline_from);
1240 free_srcline(he->branch_info->srcline_to);
1241 zfree(&he->branch_info);
1245 map__zput(he->mem_info->iaddr.map);
1246 map__zput(he->mem_info->daddr.map);
1247 mem_info__zput(he->mem_info);
1251 block_info__zput(he->block_info);
1253 zfree(&he->res_samples);
1254 zfree(&he->stat_acc);
1255 free_srcline(he->srcline);
1256 if (he->srcfile && he->srcfile[0])
1258 free_callchain(he->callchain);
1259 free(he->trace_output);
1265 * If this is not the last column, then we need to pad it according to the
1266 * pre-calculated max length for this column, otherwise don't bother adding
1267 * spaces because that would break viewing this with, for instance, 'less',
1268 * that would show tons of trailing spaces when a long C++ demangled method
1271 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1272 struct perf_hpp_fmt *fmt, int printed)
1274 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1275 const int width = fmt->width(fmt, hpp, he->hists);
1276 if (printed < width) {
1277 advance_hpp(hpp, printed);
1278 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1286 * collapse the histogram
1289 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1290 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1291 enum hist_filter type);
1293 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1295 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1297 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1300 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1301 enum hist_filter type,
1304 struct perf_hpp_fmt *fmt;
1305 bool type_match = false;
1306 struct hist_entry *parent = he->parent_he;
1309 case HIST_FILTER__THREAD:
1310 if (symbol_conf.comm_list == NULL &&
1311 symbol_conf.pid_list == NULL &&
1312 symbol_conf.tid_list == NULL)
1315 case HIST_FILTER__DSO:
1316 if (symbol_conf.dso_list == NULL)
1319 case HIST_FILTER__SYMBOL:
1320 if (symbol_conf.sym_list == NULL)
1323 case HIST_FILTER__PARENT:
1324 case HIST_FILTER__GUEST:
1325 case HIST_FILTER__HOST:
1326 case HIST_FILTER__SOCKET:
1327 case HIST_FILTER__C2C:
1332 /* if it's filtered by own fmt, it has to have filter bits */
1333 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1342 * If the filter is for current level entry, propagate
1343 * filter marker to parents. The marker bit was
1344 * already set by default so it only needs to clear
1345 * non-filtered entries.
1347 if (!(he->filtered & (1 << type))) {
1349 parent->filtered &= ~(1 << type);
1350 parent = parent->parent_he;
1355 * If current entry doesn't have matching formats, set
1356 * filter marker for upper level entries. it will be
1357 * cleared if its lower level entries is not filtered.
1359 * For lower-level entries, it inherits parent's
1360 * filter bit so that lower level entries of a
1361 * non-filtered entry won't set the filter marker.
1364 he->filtered |= (1 << type);
1366 he->filtered |= (parent->filtered & (1 << type));
1370 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1372 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1373 check_thread_entry);
1375 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1376 perf_hpp__is_dso_entry);
1378 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1379 perf_hpp__is_sym_entry);
1381 hists__apply_filters(he->hists, he);
1384 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1385 struct rb_root_cached *root,
1386 struct hist_entry *he,
1387 struct hist_entry *parent_he,
1388 struct perf_hpp_list *hpp_list)
1390 struct rb_node **p = &root->rb_root.rb_node;
1391 struct rb_node *parent = NULL;
1392 struct hist_entry *iter, *new;
1393 struct perf_hpp_fmt *fmt;
1395 bool leftmost = true;
1397 while (*p != NULL) {
1399 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1402 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1403 cmp = fmt->collapse(fmt, iter, he);
1409 he_stat__add_stat(&iter->stat, &he->stat);
1414 p = &parent->rb_left;
1416 p = &parent->rb_right;
1421 new = hist_entry__new(he, true);
1425 hists->nr_entries++;
1427 /* save related format list for output */
1428 new->hpp_list = hpp_list;
1429 new->parent_he = parent_he;
1431 hist_entry__apply_hierarchy_filters(new);
1433 /* some fields are now passed to 'new' */
1434 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1435 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1436 he->trace_output = NULL;
1438 new->trace_output = NULL;
1440 if (perf_hpp__is_srcline_entry(fmt))
1443 new->srcline = NULL;
1445 if (perf_hpp__is_srcfile_entry(fmt))
1448 new->srcfile = NULL;
1451 rb_link_node(&new->rb_node_in, parent, p);
1452 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
1456 static int hists__hierarchy_insert_entry(struct hists *hists,
1457 struct rb_root_cached *root,
1458 struct hist_entry *he)
1460 struct perf_hpp_list_node *node;
1461 struct hist_entry *new_he = NULL;
1462 struct hist_entry *parent = NULL;
1466 list_for_each_entry(node, &hists->hpp_formats, list) {
1467 /* skip period (overhead) and elided columns */
1468 if (node->level == 0 || node->skip)
1471 /* insert copy of 'he' for each fmt into the hierarchy */
1472 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1473 if (new_he == NULL) {
1478 root = &new_he->hroot_in;
1479 new_he->depth = depth++;
1484 new_he->leaf = true;
1486 if (hist_entry__has_callchains(new_he) &&
1487 symbol_conf.use_callchain) {
1488 callchain_cursor_reset(&callchain_cursor);
1489 if (callchain_merge(&callchain_cursor,
1496 /* 'he' is no longer used */
1497 hist_entry__delete(he);
1499 /* return 0 (or -1) since it already applied filters */
1503 static int hists__collapse_insert_entry(struct hists *hists,
1504 struct rb_root_cached *root,
1505 struct hist_entry *he)
1507 struct rb_node **p = &root->rb_root.rb_node;
1508 struct rb_node *parent = NULL;
1509 struct hist_entry *iter;
1511 bool leftmost = true;
1513 if (symbol_conf.report_hierarchy)
1514 return hists__hierarchy_insert_entry(hists, root, he);
1516 while (*p != NULL) {
1518 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1520 cmp = hist_entry__collapse(iter, he);
1525 he_stat__add_stat(&iter->stat, &he->stat);
1526 if (symbol_conf.cumulate_callchain)
1527 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1529 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
1530 callchain_cursor_reset(&callchain_cursor);
1531 if (callchain_merge(&callchain_cursor,
1536 hist_entry__delete(he);
1543 p = &(*p)->rb_right;
1547 hists->nr_entries++;
1549 rb_link_node(&he->rb_node_in, parent, p);
1550 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
1554 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
1556 struct rb_root_cached *root;
1558 pthread_mutex_lock(&hists->lock);
1560 root = hists->entries_in;
1561 if (++hists->entries_in > &hists->entries_in_array[1])
1562 hists->entries_in = &hists->entries_in_array[0];
1564 pthread_mutex_unlock(&hists->lock);
1569 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1571 hists__filter_entry_by_dso(hists, he);
1572 hists__filter_entry_by_thread(hists, he);
1573 hists__filter_entry_by_symbol(hists, he);
1574 hists__filter_entry_by_socket(hists, he);
1577 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1579 struct rb_root_cached *root;
1580 struct rb_node *next;
1581 struct hist_entry *n;
1584 if (!hists__has(hists, need_collapse))
1587 hists->nr_entries = 0;
1589 root = hists__get_rotate_entries_in(hists);
1591 next = rb_first_cached(root);
1596 n = rb_entry(next, struct hist_entry, rb_node_in);
1597 next = rb_next(&n->rb_node_in);
1599 rb_erase_cached(&n->rb_node_in, root);
1600 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1606 * If it wasn't combined with one of the entries already
1607 * collapsed, we need to apply the filters that may have
1608 * been set by, say, the hist_browser.
1610 hists__apply_filters(hists, n);
1613 ui_progress__update(prog, 1);
1618 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1620 struct hists *hists = a->hists;
1621 struct perf_hpp_fmt *fmt;
1624 hists__for_each_sort_list(hists, fmt) {
1625 if (perf_hpp__should_skip(fmt, a->hists))
1628 cmp = fmt->sort(fmt, a, b);
1636 static void hists__reset_filter_stats(struct hists *hists)
1638 hists->nr_non_filtered_entries = 0;
1639 hists->stats.total_non_filtered_period = 0;
1642 void hists__reset_stats(struct hists *hists)
1644 hists->nr_entries = 0;
1645 hists->stats.total_period = 0;
1647 hists__reset_filter_stats(hists);
1650 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1652 hists->nr_non_filtered_entries++;
1653 hists->stats.total_non_filtered_period += h->stat.period;
1656 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1659 hists__inc_filter_stats(hists, h);
1661 hists->nr_entries++;
1662 hists->stats.total_period += h->stat.period;
1665 static void hierarchy_recalc_total_periods(struct hists *hists)
1667 struct rb_node *node;
1668 struct hist_entry *he;
1670 node = rb_first_cached(&hists->entries);
1672 hists->stats.total_period = 0;
1673 hists->stats.total_non_filtered_period = 0;
1676 * recalculate total period using top-level entries only
1677 * since lower level entries only see non-filtered entries
1678 * but upper level entries have sum of both entries.
1681 he = rb_entry(node, struct hist_entry, rb_node);
1682 node = rb_next(node);
1684 hists->stats.total_period += he->stat.period;
1686 hists->stats.total_non_filtered_period += he->stat.period;
1690 static void hierarchy_insert_output_entry(struct rb_root_cached *root,
1691 struct hist_entry *he)
1693 struct rb_node **p = &root->rb_root.rb_node;
1694 struct rb_node *parent = NULL;
1695 struct hist_entry *iter;
1696 struct perf_hpp_fmt *fmt;
1697 bool leftmost = true;
1699 while (*p != NULL) {
1701 iter = rb_entry(parent, struct hist_entry, rb_node);
1703 if (hist_entry__sort(he, iter) > 0)
1704 p = &parent->rb_left;
1706 p = &parent->rb_right;
1711 rb_link_node(&he->rb_node, parent, p);
1712 rb_insert_color_cached(&he->rb_node, root, leftmost);
1714 /* update column width of dynamic entry */
1715 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1716 if (perf_hpp__is_dynamic_entry(fmt))
1717 fmt->sort(fmt, he, NULL);
1721 static void hists__hierarchy_output_resort(struct hists *hists,
1722 struct ui_progress *prog,
1723 struct rb_root_cached *root_in,
1724 struct rb_root_cached *root_out,
1725 u64 min_callchain_hits,
1728 struct rb_node *node;
1729 struct hist_entry *he;
1731 *root_out = RB_ROOT_CACHED;
1732 node = rb_first_cached(root_in);
1735 he = rb_entry(node, struct hist_entry, rb_node_in);
1736 node = rb_next(node);
1738 hierarchy_insert_output_entry(root_out, he);
1741 ui_progress__update(prog, 1);
1743 hists->nr_entries++;
1744 if (!he->filtered) {
1745 hists->nr_non_filtered_entries++;
1746 hists__calc_col_len(hists, he);
1750 hists__hierarchy_output_resort(hists, prog,
1761 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1762 u64 total = he->stat.period;
1764 if (symbol_conf.cumulate_callchain)
1765 total = he->stat_acc->period;
1767 min_callchain_hits = total * (callchain_param.min_percent / 100);
1770 callchain_param.sort(&he->sorted_chain, he->callchain,
1771 min_callchain_hits, &callchain_param);
1775 static void __hists__insert_output_entry(struct rb_root_cached *entries,
1776 struct hist_entry *he,
1777 u64 min_callchain_hits,
1780 struct rb_node **p = &entries->rb_root.rb_node;
1781 struct rb_node *parent = NULL;
1782 struct hist_entry *iter;
1783 struct perf_hpp_fmt *fmt;
1784 bool leftmost = true;
1786 if (use_callchain) {
1787 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1788 u64 total = he->stat.period;
1790 if (symbol_conf.cumulate_callchain)
1791 total = he->stat_acc->period;
1793 min_callchain_hits = total * (callchain_param.min_percent / 100);
1795 callchain_param.sort(&he->sorted_chain, he->callchain,
1796 min_callchain_hits, &callchain_param);
1799 while (*p != NULL) {
1801 iter = rb_entry(parent, struct hist_entry, rb_node);
1803 if (hist_entry__sort(he, iter) > 0)
1806 p = &(*p)->rb_right;
1811 rb_link_node(&he->rb_node, parent, p);
1812 rb_insert_color_cached(&he->rb_node, entries, leftmost);
1814 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1815 if (perf_hpp__is_dynamic_entry(fmt) &&
1816 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1817 fmt->sort(fmt, he, NULL); /* update column width */
1821 static void output_resort(struct hists *hists, struct ui_progress *prog,
1822 bool use_callchain, hists__resort_cb_t cb,
1825 struct rb_root_cached *root;
1826 struct rb_node *next;
1827 struct hist_entry *n;
1828 u64 callchain_total;
1829 u64 min_callchain_hits;
1831 callchain_total = hists->callchain_period;
1832 if (symbol_conf.filter_relative)
1833 callchain_total = hists->callchain_non_filtered_period;
1835 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1837 hists__reset_stats(hists);
1838 hists__reset_col_len(hists);
1840 if (symbol_conf.report_hierarchy) {
1841 hists__hierarchy_output_resort(hists, prog,
1842 &hists->entries_collapsed,
1846 hierarchy_recalc_total_periods(hists);
1850 if (hists__has(hists, need_collapse))
1851 root = &hists->entries_collapsed;
1853 root = hists->entries_in;
1855 next = rb_first_cached(root);
1856 hists->entries = RB_ROOT_CACHED;
1859 n = rb_entry(next, struct hist_entry, rb_node_in);
1860 next = rb_next(&n->rb_node_in);
1862 if (cb && cb(n, cb_arg))
1865 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1866 hists__inc_stats(hists, n);
1869 hists__calc_col_len(hists, n);
1872 ui_progress__update(prog, 1);
1876 void perf_evsel__output_resort_cb(struct perf_evsel *evsel, struct ui_progress *prog,
1877 hists__resort_cb_t cb, void *cb_arg)
1881 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1882 use_callchain = evsel__has_callchain(evsel);
1884 use_callchain = symbol_conf.use_callchain;
1886 use_callchain |= symbol_conf.show_branchflag_count;
1888 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1891 void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
1893 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL);
1896 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1898 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
1901 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1902 hists__resort_cb_t cb)
1904 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
1907 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1909 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1912 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1918 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1920 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1922 while (can_goto_child(he, HMD_NORMAL)) {
1923 node = rb_last(&he->hroot_out.rb_root);
1924 he = rb_entry(node, struct hist_entry, rb_node);
1929 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1931 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1933 if (can_goto_child(he, hmd))
1934 node = rb_first_cached(&he->hroot_out);
1936 node = rb_next(node);
1938 while (node == NULL) {
1943 node = rb_next(&he->rb_node);
1948 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1950 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1952 node = rb_prev(node);
1954 return rb_hierarchy_last(node);
1960 return &he->rb_node;
1963 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1965 struct rb_node *node;
1966 struct hist_entry *child;
1972 node = rb_first_cached(&he->hroot_out);
1973 child = rb_entry(node, struct hist_entry, rb_node);
1975 while (node && child->filtered) {
1976 node = rb_next(node);
1977 child = rb_entry(node, struct hist_entry, rb_node);
1981 percent = hist_entry__get_percent_limit(child);
1985 return node && percent >= limit;
1988 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1989 enum hist_filter filter)
1991 h->filtered &= ~(1 << filter);
1993 if (symbol_conf.report_hierarchy) {
1994 struct hist_entry *parent = h->parent_he;
1997 he_stat__add_stat(&parent->stat, &h->stat);
1999 parent->filtered &= ~(1 << filter);
2001 if (parent->filtered)
2004 /* force fold unfiltered entry for simplicity */
2005 parent->unfolded = false;
2006 parent->has_no_entry = false;
2007 parent->row_offset = 0;
2008 parent->nr_rows = 0;
2010 parent = parent->parent_he;
2017 /* force fold unfiltered entry for simplicity */
2018 h->unfolded = false;
2019 h->has_no_entry = false;
2023 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
2025 hists__inc_filter_stats(hists, h);
2026 hists__calc_col_len(hists, h);
2030 static bool hists__filter_entry_by_dso(struct hists *hists,
2031 struct hist_entry *he)
2033 if (hists->dso_filter != NULL &&
2034 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2035 he->filtered |= (1 << HIST_FILTER__DSO);
2042 static bool hists__filter_entry_by_thread(struct hists *hists,
2043 struct hist_entry *he)
2045 if (hists->thread_filter != NULL &&
2046 he->thread != hists->thread_filter) {
2047 he->filtered |= (1 << HIST_FILTER__THREAD);
2054 static bool hists__filter_entry_by_symbol(struct hists *hists,
2055 struct hist_entry *he)
2057 if (hists->symbol_filter_str != NULL &&
2058 (!he->ms.sym || strstr(he->ms.sym->name,
2059 hists->symbol_filter_str) == NULL)) {
2060 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2067 static bool hists__filter_entry_by_socket(struct hists *hists,
2068 struct hist_entry *he)
2070 if ((hists->socket_filter > -1) &&
2071 (he->socket != hists->socket_filter)) {
2072 he->filtered |= (1 << HIST_FILTER__SOCKET);
2079 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2081 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
2085 hists->stats.nr_non_filtered_samples = 0;
2087 hists__reset_filter_stats(hists);
2088 hists__reset_col_len(hists);
2090 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
2091 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2093 if (filter(hists, h))
2096 hists__remove_entry_filter(hists, h, type);
2100 static void resort_filtered_entry(struct rb_root_cached *root,
2101 struct hist_entry *he)
2103 struct rb_node **p = &root->rb_root.rb_node;
2104 struct rb_node *parent = NULL;
2105 struct hist_entry *iter;
2106 struct rb_root_cached new_root = RB_ROOT_CACHED;
2108 bool leftmost = true;
2110 while (*p != NULL) {
2112 iter = rb_entry(parent, struct hist_entry, rb_node);
2114 if (hist_entry__sort(he, iter) > 0)
2117 p = &(*p)->rb_right;
2122 rb_link_node(&he->rb_node, parent, p);
2123 rb_insert_color_cached(&he->rb_node, root, leftmost);
2125 if (he->leaf || he->filtered)
2128 nd = rb_first_cached(&he->hroot_out);
2130 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2133 rb_erase_cached(&h->rb_node, &he->hroot_out);
2135 resort_filtered_entry(&new_root, h);
2138 he->hroot_out = new_root;
2141 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2144 struct rb_root_cached new_root = RB_ROOT_CACHED;
2146 hists->stats.nr_non_filtered_samples = 0;
2148 hists__reset_filter_stats(hists);
2149 hists__reset_col_len(hists);
2151 nd = rb_first_cached(&hists->entries);
2153 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2156 ret = hist_entry__filter(h, type, arg);
2159 * case 1. non-matching type
2160 * zero out the period, set filter marker and move to child
2163 memset(&h->stat, 0, sizeof(h->stat));
2164 h->filtered |= (1 << type);
2166 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2169 * case 2. matched type (filter out)
2170 * set filter marker and move to next
2172 else if (ret == 1) {
2173 h->filtered |= (1 << type);
2175 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2178 * case 3. ok (not filtered)
2179 * add period to hists and parents, erase the filter marker
2180 * and move to next sibling
2183 hists__remove_entry_filter(hists, h, type);
2185 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2189 hierarchy_recalc_total_periods(hists);
2192 * resort output after applying a new filter since filter in a lower
2193 * hierarchy can change periods in a upper hierarchy.
2195 nd = rb_first_cached(&hists->entries);
2197 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2200 rb_erase_cached(&h->rb_node, &hists->entries);
2202 resort_filtered_entry(&new_root, h);
2205 hists->entries = new_root;
2208 void hists__filter_by_thread(struct hists *hists)
2210 if (symbol_conf.report_hierarchy)
2211 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2212 hists->thread_filter);
2214 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2215 hists__filter_entry_by_thread);
2218 void hists__filter_by_dso(struct hists *hists)
2220 if (symbol_conf.report_hierarchy)
2221 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2224 hists__filter_by_type(hists, HIST_FILTER__DSO,
2225 hists__filter_entry_by_dso);
2228 void hists__filter_by_symbol(struct hists *hists)
2230 if (symbol_conf.report_hierarchy)
2231 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2232 hists->symbol_filter_str);
2234 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2235 hists__filter_entry_by_symbol);
2238 void hists__filter_by_socket(struct hists *hists)
2240 if (symbol_conf.report_hierarchy)
2241 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2242 &hists->socket_filter);
2244 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2245 hists__filter_entry_by_socket);
2248 void events_stats__inc(struct events_stats *stats, u32 type)
2250 ++stats->nr_events[0];
2251 ++stats->nr_events[type];
2254 void hists__inc_nr_events(struct hists *hists, u32 type)
2256 events_stats__inc(&hists->stats, type);
2259 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2261 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2263 hists->stats.nr_non_filtered_samples++;
2266 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2267 struct hist_entry *pair)
2269 struct rb_root_cached *root;
2271 struct rb_node *parent = NULL;
2272 struct hist_entry *he;
2274 bool leftmost = true;
2276 if (hists__has(hists, need_collapse))
2277 root = &hists->entries_collapsed;
2279 root = hists->entries_in;
2281 p = &root->rb_root.rb_node;
2283 while (*p != NULL) {
2285 he = rb_entry(parent, struct hist_entry, rb_node_in);
2287 cmp = hist_entry__collapse(he, pair);
2295 p = &(*p)->rb_right;
2300 he = hist_entry__new(pair, true);
2302 memset(&he->stat, 0, sizeof(he->stat));
2304 if (symbol_conf.cumulate_callchain)
2305 memset(he->stat_acc, 0, sizeof(he->stat));
2306 rb_link_node(&he->rb_node_in, parent, p);
2307 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2308 hists__inc_stats(hists, he);
2315 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2316 struct rb_root_cached *root,
2317 struct hist_entry *pair)
2320 struct rb_node *parent = NULL;
2321 struct hist_entry *he;
2322 struct perf_hpp_fmt *fmt;
2323 bool leftmost = true;
2325 p = &root->rb_root.rb_node;
2326 while (*p != NULL) {
2330 he = rb_entry(parent, struct hist_entry, rb_node_in);
2332 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2333 cmp = fmt->collapse(fmt, he, pair);
2341 p = &parent->rb_left;
2343 p = &parent->rb_right;
2348 he = hist_entry__new(pair, true);
2350 rb_link_node(&he->rb_node_in, parent, p);
2351 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2355 memset(&he->stat, 0, sizeof(he->stat));
2356 hists__inc_stats(hists, he);
2362 static struct hist_entry *hists__find_entry(struct hists *hists,
2363 struct hist_entry *he)
2367 if (hists__has(hists, need_collapse))
2368 n = hists->entries_collapsed.rb_root.rb_node;
2370 n = hists->entries_in->rb_root.rb_node;
2373 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2374 int64_t cmp = hist_entry__collapse(iter, he);
2387 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
2388 struct hist_entry *he)
2390 struct rb_node *n = root->rb_root.rb_node;
2393 struct hist_entry *iter;
2394 struct perf_hpp_fmt *fmt;
2397 iter = rb_entry(n, struct hist_entry, rb_node_in);
2398 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2399 cmp = fmt->collapse(fmt, iter, he);
2415 static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2416 struct rb_root_cached *other_root)
2419 struct hist_entry *pos, *pair;
2421 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
2422 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2423 pair = hists__find_hierarchy_entry(other_root, pos);
2426 hist_entry__add_pair(pair, pos);
2427 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2433 * Look for pairs to link to the leader buckets (hist_entries):
2435 void hists__match(struct hists *leader, struct hists *other)
2437 struct rb_root_cached *root;
2439 struct hist_entry *pos, *pair;
2441 if (symbol_conf.report_hierarchy) {
2442 /* hierarchy report always collapses entries */
2443 return hists__match_hierarchy(&leader->entries_collapsed,
2444 &other->entries_collapsed);
2447 if (hists__has(leader, need_collapse))
2448 root = &leader->entries_collapsed;
2450 root = leader->entries_in;
2452 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2453 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2454 pair = hists__find_entry(other, pos);
2457 hist_entry__add_pair(pair, pos);
2461 static int hists__link_hierarchy(struct hists *leader_hists,
2462 struct hist_entry *parent,
2463 struct rb_root_cached *leader_root,
2464 struct rb_root_cached *other_root)
2467 struct hist_entry *pos, *leader;
2469 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
2470 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2472 if (hist_entry__has_pairs(pos)) {
2475 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2476 if (leader->hists == leader_hists) {
2484 leader = add_dummy_hierarchy_entry(leader_hists,
2489 /* do not point parent in the pos */
2490 leader->parent_he = parent;
2492 hist_entry__add_pair(pos, leader);
2496 if (hists__link_hierarchy(leader_hists, leader,
2498 &pos->hroot_in) < 0)
2506 * Look for entries in the other hists that are not present in the leader, if
2507 * we find them, just add a dummy entry on the leader hists, with period=0,
2508 * nr_events=0, to serve as the list header.
2510 int hists__link(struct hists *leader, struct hists *other)
2512 struct rb_root_cached *root;
2514 struct hist_entry *pos, *pair;
2516 if (symbol_conf.report_hierarchy) {
2517 /* hierarchy report always collapses entries */
2518 return hists__link_hierarchy(leader, NULL,
2519 &leader->entries_collapsed,
2520 &other->entries_collapsed);
2523 if (hists__has(other, need_collapse))
2524 root = &other->entries_collapsed;
2526 root = other->entries_in;
2528 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2529 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2531 if (!hist_entry__has_pairs(pos)) {
2532 pair = hists__add_dummy_entry(leader, pos);
2535 hist_entry__add_pair(pos, pair);
2542 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2543 struct perf_sample *sample, bool nonany_branch_mode)
2545 struct branch_info *bi;
2547 /* If we have branch cycles always annotate them. */
2548 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2551 bi = sample__resolve_bstack(sample, al);
2553 struct addr_map_symbol *prev = NULL;
2556 * Ignore errors, still want to process the
2559 * For non standard branch modes always
2560 * force no IPC (prev == NULL)
2562 * Note that perf stores branches reversed from
2565 for (i = bs->nr - 1; i >= 0; i--) {
2566 addr_map_symbol__account_cycles(&bi[i].from,
2567 nonany_branch_mode ? NULL : prev,
2568 bi[i].flags.cycles);
2576 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2578 struct perf_evsel *pos;
2581 evlist__for_each_entry(evlist, pos) {
2582 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2583 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2590 u64 hists__total_period(struct hists *hists)
2592 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2593 hists->stats.total_period;
2596 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2600 const struct dso *dso = hists->dso_filter;
2601 struct thread *thread = hists->thread_filter;
2602 int socket_id = hists->socket_filter;
2603 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2604 u64 nr_events = hists->stats.total_period;
2605 struct perf_evsel *evsel = hists_to_evsel(hists);
2606 const char *ev_name = perf_evsel__name(evsel);
2607 char buf[512], sample_freq_str[64] = "";
2608 size_t buflen = sizeof(buf);
2609 char ref[30] = " show reference callgraph, ";
2610 bool enable_ref = false;
2612 if (symbol_conf.filter_relative) {
2613 nr_samples = hists->stats.nr_non_filtered_samples;
2614 nr_events = hists->stats.total_non_filtered_period;
2617 if (perf_evsel__is_group_event(evsel)) {
2618 struct perf_evsel *pos;
2620 perf_evsel__group_desc(evsel, buf, buflen);
2623 for_each_group_member(pos, evsel) {
2624 struct hists *pos_hists = evsel__hists(pos);
2626 if (symbol_conf.filter_relative) {
2627 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2628 nr_events += pos_hists->stats.total_non_filtered_period;
2630 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2631 nr_events += pos_hists->stats.total_period;
2636 if (symbol_conf.show_ref_callgraph &&
2637 strstr(ev_name, "call-graph=no"))
2641 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->attr.sample_freq);
2643 nr_samples = convert_unit(nr_samples, &unit);
2644 printed = scnprintf(bf, size,
2645 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
2646 nr_samples, unit, evsel->nr_members > 1 ? "s" : "",
2647 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2650 if (hists->uid_filter_str)
2651 printed += snprintf(bf + printed, size - printed,
2652 ", UID: %s", hists->uid_filter_str);
2654 if (hists__has(hists, thread)) {
2655 printed += scnprintf(bf + printed, size - printed,
2657 (thread->comm_set ? thread__comm_str(thread) : ""),
2660 printed += scnprintf(bf + printed, size - printed,
2662 (thread->comm_set ? thread__comm_str(thread) : ""));
2666 printed += scnprintf(bf + printed, size - printed,
2667 ", DSO: %s", dso->short_name);
2669 printed += scnprintf(bf + printed, size - printed,
2670 ", Processor Socket: %d", socket_id);
2675 int parse_filter_percentage(const struct option *opt __maybe_unused,
2676 const char *arg, int unset __maybe_unused)
2678 if (!strcmp(arg, "relative"))
2679 symbol_conf.filter_relative = true;
2680 else if (!strcmp(arg, "absolute"))
2681 symbol_conf.filter_relative = false;
2683 pr_debug("Invalid percentage: %s\n", arg);
2690 int perf_hist_config(const char *var, const char *value)
2692 if (!strcmp(var, "hist.percentage"))
2693 return parse_filter_percentage(NULL, value, 0);
2698 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2700 memset(hists, 0, sizeof(*hists));
2701 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
2702 hists->entries_in = &hists->entries_in_array[0];
2703 hists->entries_collapsed = RB_ROOT_CACHED;
2704 hists->entries = RB_ROOT_CACHED;
2705 pthread_mutex_init(&hists->lock, NULL);
2706 hists->socket_filter = -1;
2707 hists->hpp_list = hpp_list;
2708 INIT_LIST_HEAD(&hists->hpp_formats);
2712 static void hists__delete_remaining_entries(struct rb_root_cached *root)
2714 struct rb_node *node;
2715 struct hist_entry *he;
2717 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2718 node = rb_first_cached(root);
2719 rb_erase_cached(node, root);
2721 he = rb_entry(node, struct hist_entry, rb_node_in);
2722 hist_entry__delete(he);
2726 static void hists__delete_all_entries(struct hists *hists)
2728 hists__delete_entries(hists);
2729 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2730 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2731 hists__delete_remaining_entries(&hists->entries_collapsed);
2734 static void hists_evsel__exit(struct perf_evsel *evsel)
2736 struct hists *hists = evsel__hists(evsel);
2737 struct perf_hpp_fmt *fmt, *pos;
2738 struct perf_hpp_list_node *node, *tmp;
2740 hists__delete_all_entries(hists);
2742 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2743 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2744 list_del(&fmt->list);
2747 list_del(&node->list);
2752 static int hists_evsel__init(struct perf_evsel *evsel)
2754 struct hists *hists = evsel__hists(evsel);
2756 __hists__init(hists, &perf_hpp_list);
2761 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2762 * stored in the rbtree...
2765 int hists__init(void)
2767 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
2771 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2776 void perf_hpp_list__init(struct perf_hpp_list *list)
2778 INIT_LIST_HEAD(&list->fields);
2779 INIT_LIST_HEAD(&list->sorts);