6 #include "namespaces.h"
12 #include "ui/progress.h"
16 static bool hists__filter_entry_by_dso(struct hists *hists,
17 struct hist_entry *he);
18 static bool hists__filter_entry_by_thread(struct hists *hists,
19 struct hist_entry *he);
20 static bool hists__filter_entry_by_symbol(struct hists *hists,
21 struct hist_entry *he);
22 static bool hists__filter_entry_by_socket(struct hists *hists,
23 struct hist_entry *he);
25 u16 hists__col_len(struct hists *hists, enum hist_column col)
27 return hists->col_len[col];
30 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
32 hists->col_len[col] = len;
35 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
37 if (len > hists__col_len(hists, col)) {
38 hists__set_col_len(hists, col, len);
44 void hists__reset_col_len(struct hists *hists)
48 for (col = 0; col < HISTC_NR_COLS; ++col)
49 hists__set_col_len(hists, col, 0);
52 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
54 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
56 if (hists__col_len(hists, dso) < unresolved_col_width &&
57 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
58 !symbol_conf.dso_list)
59 hists__set_col_len(hists, dso, unresolved_col_width);
62 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
64 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
69 * +4 accounts for '[x] ' priv level info
70 * +2 accounts for 0x prefix on raw addresses
71 * +3 accounts for ' y ' symtab origin info
74 symlen = h->ms.sym->namelen + 4;
76 symlen += BITS_PER_LONG / 4 + 2 + 3;
77 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
79 symlen = unresolved_col_width + 4 + 2;
80 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
81 hists__set_unres_dso_col_len(hists, HISTC_DSO);
84 len = thread__comm_len(h->thread);
85 if (hists__new_col_len(hists, HISTC_COMM, len))
86 hists__set_col_len(hists, HISTC_THREAD, len + 8);
89 len = dso__name_len(h->ms.map->dso);
90 hists__new_col_len(hists, HISTC_DSO, len);
94 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
97 if (h->branch_info->from.sym) {
98 symlen = (int)h->branch_info->from.sym->namelen + 4;
100 symlen += BITS_PER_LONG / 4 + 2 + 3;
101 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
103 symlen = dso__name_len(h->branch_info->from.map->dso);
104 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
106 symlen = unresolved_col_width + 4 + 2;
107 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
108 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
111 if (h->branch_info->to.sym) {
112 symlen = (int)h->branch_info->to.sym->namelen + 4;
114 symlen += BITS_PER_LONG / 4 + 2 + 3;
115 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
117 symlen = dso__name_len(h->branch_info->to.map->dso);
118 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
120 symlen = unresolved_col_width + 4 + 2;
121 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
122 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
125 if (h->branch_info->srcline_from)
126 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
127 strlen(h->branch_info->srcline_from));
128 if (h->branch_info->srcline_to)
129 hists__new_col_len(hists, HISTC_SRCLINE_TO,
130 strlen(h->branch_info->srcline_to));
134 if (h->mem_info->daddr.sym) {
135 symlen = (int)h->mem_info->daddr.sym->namelen + 4
136 + unresolved_col_width + 2;
137 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
139 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
145 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
149 if (h->mem_info->iaddr.sym) {
150 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
151 + unresolved_col_width + 2;
152 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
155 symlen = unresolved_col_width + 4 + 2;
156 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
160 if (h->mem_info->daddr.map) {
161 symlen = dso__name_len(h->mem_info->daddr.map->dso);
162 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
165 symlen = unresolved_col_width + 4 + 2;
166 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
169 symlen = unresolved_col_width + 4 + 2;
170 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
171 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
172 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
175 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
176 hists__new_col_len(hists, HISTC_CPU, 3);
177 hists__new_col_len(hists, HISTC_SOCKET, 6);
178 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
179 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
180 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
181 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
182 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
183 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
186 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
187 hists__new_col_len(hists, HISTC_SRCLINE, len);
191 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
194 hists__new_col_len(hists, HISTC_TRANSACTION,
195 hist_entry__transaction_len());
198 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
201 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
203 struct rb_node *next = rb_first(&hists->entries);
204 struct hist_entry *n;
207 hists__reset_col_len(hists);
209 while (next && row++ < max_rows) {
210 n = rb_entry(next, struct hist_entry, rb_node);
212 hists__calc_col_len(hists, n);
213 next = rb_next(&n->rb_node);
217 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
218 unsigned int cpumode, u64 period)
221 case PERF_RECORD_MISC_KERNEL:
222 he_stat->period_sys += period;
224 case PERF_RECORD_MISC_USER:
225 he_stat->period_us += period;
227 case PERF_RECORD_MISC_GUEST_KERNEL:
228 he_stat->period_guest_sys += period;
230 case PERF_RECORD_MISC_GUEST_USER:
231 he_stat->period_guest_us += period;
238 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
242 he_stat->period += period;
243 he_stat->weight += weight;
244 he_stat->nr_events += 1;
247 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
249 dest->period += src->period;
250 dest->period_sys += src->period_sys;
251 dest->period_us += src->period_us;
252 dest->period_guest_sys += src->period_guest_sys;
253 dest->period_guest_us += src->period_guest_us;
254 dest->nr_events += src->nr_events;
255 dest->weight += src->weight;
258 static void he_stat__decay(struct he_stat *he_stat)
260 he_stat->period = (he_stat->period * 7) / 8;
261 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
262 /* XXX need decay for weight too? */
265 static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
267 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
269 u64 prev_period = he->stat.period;
272 if (prev_period == 0)
275 he_stat__decay(&he->stat);
276 if (symbol_conf.cumulate_callchain)
277 he_stat__decay(he->stat_acc);
278 decay_callchain(he->callchain);
280 diff = prev_period - he->stat.period;
283 hists->stats.total_period -= diff;
285 hists->stats.total_non_filtered_period -= diff;
289 struct hist_entry *child;
290 struct rb_node *node = rb_first(&he->hroot_out);
292 child = rb_entry(node, struct hist_entry, rb_node);
293 node = rb_next(node);
295 if (hists__decay_entry(hists, child))
296 hists__delete_entry(hists, child);
300 return he->stat.period == 0;
303 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
305 struct rb_root *root_in;
306 struct rb_root *root_out;
309 root_in = &he->parent_he->hroot_in;
310 root_out = &he->parent_he->hroot_out;
312 if (hists__has(hists, need_collapse))
313 root_in = &hists->entries_collapsed;
315 root_in = hists->entries_in;
316 root_out = &hists->entries;
319 rb_erase(&he->rb_node_in, root_in);
320 rb_erase(&he->rb_node, root_out);
324 --hists->nr_non_filtered_entries;
326 hist_entry__delete(he);
329 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
331 struct rb_node *next = rb_first(&hists->entries);
332 struct hist_entry *n;
335 n = rb_entry(next, struct hist_entry, rb_node);
336 next = rb_next(&n->rb_node);
337 if (((zap_user && n->level == '.') ||
338 (zap_kernel && n->level != '.') ||
339 hists__decay_entry(hists, n))) {
340 hists__delete_entry(hists, n);
345 void hists__delete_entries(struct hists *hists)
347 struct rb_node *next = rb_first(&hists->entries);
348 struct hist_entry *n;
351 n = rb_entry(next, struct hist_entry, rb_node);
352 next = rb_next(&n->rb_node);
354 hists__delete_entry(hists, n);
359 * histogram, sorted on item, collects periods
362 static int hist_entry__init(struct hist_entry *he,
363 struct hist_entry *template,
368 if (symbol_conf.cumulate_callchain) {
369 he->stat_acc = malloc(sizeof(he->stat));
370 if (he->stat_acc == NULL)
372 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
374 memset(&he->stat, 0, sizeof(he->stat));
377 map__get(he->ms.map);
379 if (he->branch_info) {
381 * This branch info is (a part of) allocated from
382 * sample__resolve_bstack() and will be freed after
383 * adding new entries. So we need to save a copy.
385 he->branch_info = malloc(sizeof(*he->branch_info));
386 if (he->branch_info == NULL) {
387 map__zput(he->ms.map);
392 memcpy(he->branch_info, template->branch_info,
393 sizeof(*he->branch_info));
395 map__get(he->branch_info->from.map);
396 map__get(he->branch_info->to.map);
400 map__get(he->mem_info->iaddr.map);
401 map__get(he->mem_info->daddr.map);
404 if (symbol_conf.use_callchain)
405 callchain_init(he->callchain);
408 he->raw_data = memdup(he->raw_data, he->raw_size);
410 if (he->raw_data == NULL) {
411 map__put(he->ms.map);
412 if (he->branch_info) {
413 map__put(he->branch_info->from.map);
414 map__put(he->branch_info->to.map);
415 free(he->branch_info);
418 map__put(he->mem_info->iaddr.map);
419 map__put(he->mem_info->daddr.map);
425 INIT_LIST_HEAD(&he->pairs.node);
426 thread__get(he->thread);
427 he->hroot_in = RB_ROOT;
428 he->hroot_out = RB_ROOT;
430 if (!symbol_conf.report_hierarchy)
436 static void *hist_entry__zalloc(size_t size)
438 return zalloc(size + sizeof(struct hist_entry));
441 static void hist_entry__free(void *ptr)
446 static struct hist_entry_ops default_ops = {
447 .new = hist_entry__zalloc,
448 .free = hist_entry__free,
451 static struct hist_entry *hist_entry__new(struct hist_entry *template,
454 struct hist_entry_ops *ops = template->ops;
455 size_t callchain_size = 0;
456 struct hist_entry *he;
460 ops = template->ops = &default_ops;
462 if (symbol_conf.use_callchain)
463 callchain_size = sizeof(struct callchain_root);
465 he = ops->new(callchain_size);
467 err = hist_entry__init(he, template, sample_self);
477 static u8 symbol__parent_filter(const struct symbol *parent)
479 if (symbol_conf.exclude_other && parent == NULL)
480 return 1 << HIST_FILTER__PARENT;
484 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
486 if (!symbol_conf.use_callchain)
489 he->hists->callchain_period += period;
491 he->hists->callchain_non_filtered_period += period;
494 static struct hist_entry *hists__findnew_entry(struct hists *hists,
495 struct hist_entry *entry,
496 struct addr_location *al,
500 struct rb_node *parent = NULL;
501 struct hist_entry *he;
503 u64 period = entry->stat.period;
504 u64 weight = entry->stat.weight;
506 p = &hists->entries_in->rb_node;
510 he = rb_entry(parent, struct hist_entry, rb_node_in);
513 * Make sure that it receives arguments in a same order as
514 * hist_entry__collapse() so that we can use an appropriate
515 * function when searching an entry regardless which sort
518 cmp = hist_entry__cmp(he, entry);
522 he_stat__add_period(&he->stat, period, weight);
523 hist_entry__add_callchain_period(he, period);
525 if (symbol_conf.cumulate_callchain)
526 he_stat__add_period(he->stat_acc, period, weight);
529 * This mem info was allocated from sample__resolve_mem
530 * and will not be used anymore.
532 zfree(&entry->mem_info);
534 /* If the map of an existing hist_entry has
535 * become out-of-date due to an exec() or
536 * similar, update it. Otherwise we will
537 * mis-adjust symbol addresses when computing
538 * the history counter to increment.
540 if (he->ms.map != entry->ms.map) {
541 map__put(he->ms.map);
542 he->ms.map = map__get(entry->ms.map);
553 he = hist_entry__new(entry, sample_self);
558 hist_entry__add_callchain_period(he, period);
561 rb_link_node(&he->rb_node_in, parent, p);
562 rb_insert_color(&he->rb_node_in, hists->entries_in);
565 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
566 if (symbol_conf.cumulate_callchain)
567 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
571 static struct hist_entry*
572 __hists__add_entry(struct hists *hists,
573 struct addr_location *al,
574 struct symbol *sym_parent,
575 struct branch_info *bi,
577 struct perf_sample *sample,
579 struct hist_entry_ops *ops)
581 struct namespaces *ns = thread__namespaces(al->thread);
582 struct hist_entry entry = {
583 .thread = al->thread,
584 .comm = thread__comm(al->thread),
586 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
587 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
593 .socket = al->socket,
595 .cpumode = al->cpumode,
600 .period = sample->period,
601 .weight = sample->weight,
603 .parent = sym_parent,
604 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
608 .transaction = sample->transaction,
609 .raw_data = sample->raw_data,
610 .raw_size = sample->raw_size,
614 return hists__findnew_entry(hists, &entry, al, sample_self);
617 struct hist_entry *hists__add_entry(struct hists *hists,
618 struct addr_location *al,
619 struct symbol *sym_parent,
620 struct branch_info *bi,
622 struct perf_sample *sample,
625 return __hists__add_entry(hists, al, sym_parent, bi, mi,
626 sample, sample_self, NULL);
629 struct hist_entry *hists__add_entry_ops(struct hists *hists,
630 struct hist_entry_ops *ops,
631 struct addr_location *al,
632 struct symbol *sym_parent,
633 struct branch_info *bi,
635 struct perf_sample *sample,
638 return __hists__add_entry(hists, al, sym_parent, bi, mi,
639 sample, sample_self, ops);
643 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
644 struct addr_location *al __maybe_unused)
650 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
651 struct addr_location *al __maybe_unused)
657 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
659 struct perf_sample *sample = iter->sample;
662 mi = sample__resolve_mem(sample, al);
671 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
674 struct mem_info *mi = iter->priv;
675 struct hists *hists = evsel__hists(iter->evsel);
676 struct perf_sample *sample = iter->sample;
677 struct hist_entry *he;
682 cost = sample->weight;
687 * must pass period=weight in order to get the correct
688 * sorting from hists__collapse_resort() which is solely
689 * based on periods. We want sorting be done on nr_events * weight
690 * and this is indirectly achieved by passing period=weight here
691 * and the he_stat__add_period() function.
693 sample->period = cost;
695 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
705 iter_finish_mem_entry(struct hist_entry_iter *iter,
706 struct addr_location *al __maybe_unused)
708 struct perf_evsel *evsel = iter->evsel;
709 struct hists *hists = evsel__hists(evsel);
710 struct hist_entry *he = iter->he;
716 hists__inc_nr_samples(hists, he->filtered);
718 err = hist_entry__append_callchain(he, iter->sample);
722 * We don't need to free iter->priv (mem_info) here since the mem info
723 * was either already freed in hists__findnew_entry() or passed to a
724 * new hist entry by hist_entry__new().
733 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
735 struct branch_info *bi;
736 struct perf_sample *sample = iter->sample;
738 bi = sample__resolve_bstack(sample, al);
743 iter->total = sample->branch_stack->nr;
750 iter_add_single_branch_entry(struct hist_entry_iter *iter,
751 struct addr_location *al __maybe_unused)
753 /* to avoid calling callback function */
760 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
762 struct branch_info *bi = iter->priv;
768 if (iter->curr >= iter->total)
771 al->map = bi[i].to.map;
772 al->sym = bi[i].to.sym;
773 al->addr = bi[i].to.addr;
778 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
780 struct branch_info *bi;
781 struct perf_evsel *evsel = iter->evsel;
782 struct hists *hists = evsel__hists(evsel);
783 struct perf_sample *sample = iter->sample;
784 struct hist_entry *he = NULL;
790 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
794 * The report shows the percentage of total branches captured
795 * and not events sampled. Thus we use a pseudo period of 1.
798 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
800 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
805 hists__inc_nr_samples(hists, he->filtered);
814 iter_finish_branch_entry(struct hist_entry_iter *iter,
815 struct addr_location *al __maybe_unused)
820 return iter->curr >= iter->total ? 0 : -1;
824 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
825 struct addr_location *al __maybe_unused)
831 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
833 struct perf_evsel *evsel = iter->evsel;
834 struct perf_sample *sample = iter->sample;
835 struct hist_entry *he;
837 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
847 iter_finish_normal_entry(struct hist_entry_iter *iter,
848 struct addr_location *al __maybe_unused)
850 struct hist_entry *he = iter->he;
851 struct perf_evsel *evsel = iter->evsel;
852 struct perf_sample *sample = iter->sample;
859 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
861 return hist_entry__append_callchain(he, sample);
865 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
866 struct addr_location *al __maybe_unused)
868 struct hist_entry **he_cache;
870 callchain_cursor_commit(&callchain_cursor);
873 * This is for detecting cycles or recursions so that they're
874 * cumulated only one time to prevent entries more than 100%
877 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
878 if (he_cache == NULL)
881 iter->priv = he_cache;
888 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
889 struct addr_location *al)
891 struct perf_evsel *evsel = iter->evsel;
892 struct hists *hists = evsel__hists(evsel);
893 struct perf_sample *sample = iter->sample;
894 struct hist_entry **he_cache = iter->priv;
895 struct hist_entry *he;
898 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
904 he_cache[iter->curr++] = he;
906 hist_entry__append_callchain(he, sample);
909 * We need to re-initialize the cursor since callchain_append()
910 * advanced the cursor to the end.
912 callchain_cursor_commit(&callchain_cursor);
914 hists__inc_nr_samples(hists, he->filtered);
920 iter_next_cumulative_entry(struct hist_entry_iter *iter,
921 struct addr_location *al)
923 struct callchain_cursor_node *node;
925 node = callchain_cursor_current(&callchain_cursor);
929 return fill_callchain_info(al, node, iter->hide_unresolved);
933 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
934 struct addr_location *al)
936 struct perf_evsel *evsel = iter->evsel;
937 struct perf_sample *sample = iter->sample;
938 struct hist_entry **he_cache = iter->priv;
939 struct hist_entry *he;
940 struct hist_entry he_tmp = {
941 .hists = evsel__hists(evsel),
943 .thread = al->thread,
944 .comm = thread__comm(al->thread),
950 .parent = iter->parent,
951 .raw_data = sample->raw_data,
952 .raw_size = sample->raw_size,
955 struct callchain_cursor cursor;
957 callchain_cursor_snapshot(&cursor, &callchain_cursor);
959 callchain_cursor_advance(&callchain_cursor);
962 * Check if there's duplicate entries in the callchain.
963 * It's possible that it has cycles or recursive calls.
965 for (i = 0; i < iter->curr; i++) {
966 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
967 /* to avoid calling callback function */
973 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
979 he_cache[iter->curr++] = he;
981 if (symbol_conf.use_callchain)
982 callchain_append(he->callchain, &cursor, sample->period);
987 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
988 struct addr_location *al __maybe_unused)
996 const struct hist_iter_ops hist_iter_mem = {
997 .prepare_entry = iter_prepare_mem_entry,
998 .add_single_entry = iter_add_single_mem_entry,
999 .next_entry = iter_next_nop_entry,
1000 .add_next_entry = iter_add_next_nop_entry,
1001 .finish_entry = iter_finish_mem_entry,
1004 const struct hist_iter_ops hist_iter_branch = {
1005 .prepare_entry = iter_prepare_branch_entry,
1006 .add_single_entry = iter_add_single_branch_entry,
1007 .next_entry = iter_next_branch_entry,
1008 .add_next_entry = iter_add_next_branch_entry,
1009 .finish_entry = iter_finish_branch_entry,
1012 const struct hist_iter_ops hist_iter_normal = {
1013 .prepare_entry = iter_prepare_normal_entry,
1014 .add_single_entry = iter_add_single_normal_entry,
1015 .next_entry = iter_next_nop_entry,
1016 .add_next_entry = iter_add_next_nop_entry,
1017 .finish_entry = iter_finish_normal_entry,
1020 const struct hist_iter_ops hist_iter_cumulative = {
1021 .prepare_entry = iter_prepare_cumulative_entry,
1022 .add_single_entry = iter_add_single_cumulative_entry,
1023 .next_entry = iter_next_cumulative_entry,
1024 .add_next_entry = iter_add_next_cumulative_entry,
1025 .finish_entry = iter_finish_cumulative_entry,
1028 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1029 int max_stack_depth, void *arg)
1032 struct map *alm = NULL;
1035 alm = map__get(al->map);
1037 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1038 iter->evsel, al, max_stack_depth);
1042 iter->max_stack = max_stack_depth;
1044 err = iter->ops->prepare_entry(iter, al);
1048 err = iter->ops->add_single_entry(iter, al);
1052 if (iter->he && iter->add_entry_cb) {
1053 err = iter->add_entry_cb(iter, al, true, arg);
1058 while (iter->ops->next_entry(iter, al)) {
1059 err = iter->ops->add_next_entry(iter, al);
1063 if (iter->he && iter->add_entry_cb) {
1064 err = iter->add_entry_cb(iter, al, false, arg);
1071 err2 = iter->ops->finish_entry(iter, al);
1081 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1083 struct hists *hists = left->hists;
1084 struct perf_hpp_fmt *fmt;
1087 hists__for_each_sort_list(hists, fmt) {
1088 if (perf_hpp__is_dynamic_entry(fmt) &&
1089 !perf_hpp__defined_dynamic_entry(fmt, hists))
1092 cmp = fmt->cmp(fmt, left, right);
1101 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1103 struct hists *hists = left->hists;
1104 struct perf_hpp_fmt *fmt;
1107 hists__for_each_sort_list(hists, fmt) {
1108 if (perf_hpp__is_dynamic_entry(fmt) &&
1109 !perf_hpp__defined_dynamic_entry(fmt, hists))
1112 cmp = fmt->collapse(fmt, left, right);
1120 void hist_entry__delete(struct hist_entry *he)
1122 struct hist_entry_ops *ops = he->ops;
1124 thread__zput(he->thread);
1125 map__zput(he->ms.map);
1127 if (he->branch_info) {
1128 map__zput(he->branch_info->from.map);
1129 map__zput(he->branch_info->to.map);
1130 free_srcline(he->branch_info->srcline_from);
1131 free_srcline(he->branch_info->srcline_to);
1132 zfree(&he->branch_info);
1136 map__zput(he->mem_info->iaddr.map);
1137 map__zput(he->mem_info->daddr.map);
1138 zfree(&he->mem_info);
1141 if (he->inline_node) {
1142 inline_node__delete(he->inline_node);
1143 he->inline_node = NULL;
1146 zfree(&he->stat_acc);
1147 free_srcline(he->srcline);
1148 if (he->srcfile && he->srcfile[0])
1150 free_callchain(he->callchain);
1151 free(he->trace_output);
1157 * If this is not the last column, then we need to pad it according to the
1158 * pre-calculated max lenght for this column, otherwise don't bother adding
1159 * spaces because that would break viewing this with, for instance, 'less',
1160 * that would show tons of trailing spaces when a long C++ demangled method
1163 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1164 struct perf_hpp_fmt *fmt, int printed)
1166 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1167 const int width = fmt->width(fmt, hpp, he->hists);
1168 if (printed < width) {
1169 advance_hpp(hpp, printed);
1170 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1178 * collapse the histogram
1181 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1182 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1183 enum hist_filter type);
1185 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1187 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1189 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1192 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1193 enum hist_filter type,
1196 struct perf_hpp_fmt *fmt;
1197 bool type_match = false;
1198 struct hist_entry *parent = he->parent_he;
1201 case HIST_FILTER__THREAD:
1202 if (symbol_conf.comm_list == NULL &&
1203 symbol_conf.pid_list == NULL &&
1204 symbol_conf.tid_list == NULL)
1207 case HIST_FILTER__DSO:
1208 if (symbol_conf.dso_list == NULL)
1211 case HIST_FILTER__SYMBOL:
1212 if (symbol_conf.sym_list == NULL)
1215 case HIST_FILTER__PARENT:
1216 case HIST_FILTER__GUEST:
1217 case HIST_FILTER__HOST:
1218 case HIST_FILTER__SOCKET:
1219 case HIST_FILTER__C2C:
1224 /* if it's filtered by own fmt, it has to have filter bits */
1225 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1234 * If the filter is for current level entry, propagate
1235 * filter marker to parents. The marker bit was
1236 * already set by default so it only needs to clear
1237 * non-filtered entries.
1239 if (!(he->filtered & (1 << type))) {
1241 parent->filtered &= ~(1 << type);
1242 parent = parent->parent_he;
1247 * If current entry doesn't have matching formats, set
1248 * filter marker for upper level entries. it will be
1249 * cleared if its lower level entries is not filtered.
1251 * For lower-level entries, it inherits parent's
1252 * filter bit so that lower level entries of a
1253 * non-filtered entry won't set the filter marker.
1256 he->filtered |= (1 << type);
1258 he->filtered |= (parent->filtered & (1 << type));
1262 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1264 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1265 check_thread_entry);
1267 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1268 perf_hpp__is_dso_entry);
1270 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1271 perf_hpp__is_sym_entry);
1273 hists__apply_filters(he->hists, he);
1276 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1277 struct rb_root *root,
1278 struct hist_entry *he,
1279 struct hist_entry *parent_he,
1280 struct perf_hpp_list *hpp_list)
1282 struct rb_node **p = &root->rb_node;
1283 struct rb_node *parent = NULL;
1284 struct hist_entry *iter, *new;
1285 struct perf_hpp_fmt *fmt;
1288 while (*p != NULL) {
1290 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1293 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1294 cmp = fmt->collapse(fmt, iter, he);
1300 he_stat__add_stat(&iter->stat, &he->stat);
1305 p = &parent->rb_left;
1307 p = &parent->rb_right;
1310 new = hist_entry__new(he, true);
1314 hists->nr_entries++;
1316 /* save related format list for output */
1317 new->hpp_list = hpp_list;
1318 new->parent_he = parent_he;
1320 hist_entry__apply_hierarchy_filters(new);
1322 /* some fields are now passed to 'new' */
1323 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1324 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1325 he->trace_output = NULL;
1327 new->trace_output = NULL;
1329 if (perf_hpp__is_srcline_entry(fmt))
1332 new->srcline = NULL;
1334 if (perf_hpp__is_srcfile_entry(fmt))
1337 new->srcfile = NULL;
1340 rb_link_node(&new->rb_node_in, parent, p);
1341 rb_insert_color(&new->rb_node_in, root);
1345 static int hists__hierarchy_insert_entry(struct hists *hists,
1346 struct rb_root *root,
1347 struct hist_entry *he)
1349 struct perf_hpp_list_node *node;
1350 struct hist_entry *new_he = NULL;
1351 struct hist_entry *parent = NULL;
1355 list_for_each_entry(node, &hists->hpp_formats, list) {
1356 /* skip period (overhead) and elided columns */
1357 if (node->level == 0 || node->skip)
1360 /* insert copy of 'he' for each fmt into the hierarchy */
1361 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1362 if (new_he == NULL) {
1367 root = &new_he->hroot_in;
1368 new_he->depth = depth++;
1373 new_he->leaf = true;
1375 if (symbol_conf.use_callchain) {
1376 callchain_cursor_reset(&callchain_cursor);
1377 if (callchain_merge(&callchain_cursor,
1384 /* 'he' is no longer used */
1385 hist_entry__delete(he);
1387 /* return 0 (or -1) since it already applied filters */
1391 static int hists__collapse_insert_entry(struct hists *hists,
1392 struct rb_root *root,
1393 struct hist_entry *he)
1395 struct rb_node **p = &root->rb_node;
1396 struct rb_node *parent = NULL;
1397 struct hist_entry *iter;
1400 if (symbol_conf.report_hierarchy)
1401 return hists__hierarchy_insert_entry(hists, root, he);
1403 while (*p != NULL) {
1405 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1407 cmp = hist_entry__collapse(iter, he);
1412 he_stat__add_stat(&iter->stat, &he->stat);
1413 if (symbol_conf.cumulate_callchain)
1414 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1416 if (symbol_conf.use_callchain) {
1417 callchain_cursor_reset(&callchain_cursor);
1418 if (callchain_merge(&callchain_cursor,
1423 hist_entry__delete(he);
1430 p = &(*p)->rb_right;
1432 hists->nr_entries++;
1434 rb_link_node(&he->rb_node_in, parent, p);
1435 rb_insert_color(&he->rb_node_in, root);
1439 struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
1441 struct rb_root *root;
1443 pthread_mutex_lock(&hists->lock);
1445 root = hists->entries_in;
1446 if (++hists->entries_in > &hists->entries_in_array[1])
1447 hists->entries_in = &hists->entries_in_array[0];
1449 pthread_mutex_unlock(&hists->lock);
1454 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1456 hists__filter_entry_by_dso(hists, he);
1457 hists__filter_entry_by_thread(hists, he);
1458 hists__filter_entry_by_symbol(hists, he);
1459 hists__filter_entry_by_socket(hists, he);
1462 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1464 struct rb_root *root;
1465 struct rb_node *next;
1466 struct hist_entry *n;
1469 if (!hists__has(hists, need_collapse))
1472 hists->nr_entries = 0;
1474 root = hists__get_rotate_entries_in(hists);
1476 next = rb_first(root);
1481 n = rb_entry(next, struct hist_entry, rb_node_in);
1482 next = rb_next(&n->rb_node_in);
1484 rb_erase(&n->rb_node_in, root);
1485 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1491 * If it wasn't combined with one of the entries already
1492 * collapsed, we need to apply the filters that may have
1493 * been set by, say, the hist_browser.
1495 hists__apply_filters(hists, n);
1498 ui_progress__update(prog, 1);
1503 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1505 struct hists *hists = a->hists;
1506 struct perf_hpp_fmt *fmt;
1509 hists__for_each_sort_list(hists, fmt) {
1510 if (perf_hpp__should_skip(fmt, a->hists))
1513 cmp = fmt->sort(fmt, a, b);
1521 static void hists__reset_filter_stats(struct hists *hists)
1523 hists->nr_non_filtered_entries = 0;
1524 hists->stats.total_non_filtered_period = 0;
1527 void hists__reset_stats(struct hists *hists)
1529 hists->nr_entries = 0;
1530 hists->stats.total_period = 0;
1532 hists__reset_filter_stats(hists);
1535 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1537 hists->nr_non_filtered_entries++;
1538 hists->stats.total_non_filtered_period += h->stat.period;
1541 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1544 hists__inc_filter_stats(hists, h);
1546 hists->nr_entries++;
1547 hists->stats.total_period += h->stat.period;
1550 static void hierarchy_recalc_total_periods(struct hists *hists)
1552 struct rb_node *node;
1553 struct hist_entry *he;
1555 node = rb_first(&hists->entries);
1557 hists->stats.total_period = 0;
1558 hists->stats.total_non_filtered_period = 0;
1561 * recalculate total period using top-level entries only
1562 * since lower level entries only see non-filtered entries
1563 * but upper level entries have sum of both entries.
1566 he = rb_entry(node, struct hist_entry, rb_node);
1567 node = rb_next(node);
1569 hists->stats.total_period += he->stat.period;
1571 hists->stats.total_non_filtered_period += he->stat.period;
1575 static void hierarchy_insert_output_entry(struct rb_root *root,
1576 struct hist_entry *he)
1578 struct rb_node **p = &root->rb_node;
1579 struct rb_node *parent = NULL;
1580 struct hist_entry *iter;
1581 struct perf_hpp_fmt *fmt;
1583 while (*p != NULL) {
1585 iter = rb_entry(parent, struct hist_entry, rb_node);
1587 if (hist_entry__sort(he, iter) > 0)
1588 p = &parent->rb_left;
1590 p = &parent->rb_right;
1593 rb_link_node(&he->rb_node, parent, p);
1594 rb_insert_color(&he->rb_node, root);
1596 /* update column width of dynamic entry */
1597 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1598 if (perf_hpp__is_dynamic_entry(fmt))
1599 fmt->sort(fmt, he, NULL);
1603 static void hists__hierarchy_output_resort(struct hists *hists,
1604 struct ui_progress *prog,
1605 struct rb_root *root_in,
1606 struct rb_root *root_out,
1607 u64 min_callchain_hits,
1610 struct rb_node *node;
1611 struct hist_entry *he;
1613 *root_out = RB_ROOT;
1614 node = rb_first(root_in);
1617 he = rb_entry(node, struct hist_entry, rb_node_in);
1618 node = rb_next(node);
1620 hierarchy_insert_output_entry(root_out, he);
1623 ui_progress__update(prog, 1);
1625 hists->nr_entries++;
1626 if (!he->filtered) {
1627 hists->nr_non_filtered_entries++;
1628 hists__calc_col_len(hists, he);
1632 hists__hierarchy_output_resort(hists, prog,
1643 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1644 u64 total = he->stat.period;
1646 if (symbol_conf.cumulate_callchain)
1647 total = he->stat_acc->period;
1649 min_callchain_hits = total * (callchain_param.min_percent / 100);
1652 callchain_param.sort(&he->sorted_chain, he->callchain,
1653 min_callchain_hits, &callchain_param);
1657 static void __hists__insert_output_entry(struct rb_root *entries,
1658 struct hist_entry *he,
1659 u64 min_callchain_hits,
1662 struct rb_node **p = &entries->rb_node;
1663 struct rb_node *parent = NULL;
1664 struct hist_entry *iter;
1665 struct perf_hpp_fmt *fmt;
1667 if (use_callchain) {
1668 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1669 u64 total = he->stat.period;
1671 if (symbol_conf.cumulate_callchain)
1672 total = he->stat_acc->period;
1674 min_callchain_hits = total * (callchain_param.min_percent / 100);
1676 callchain_param.sort(&he->sorted_chain, he->callchain,
1677 min_callchain_hits, &callchain_param);
1680 while (*p != NULL) {
1682 iter = rb_entry(parent, struct hist_entry, rb_node);
1684 if (hist_entry__sort(he, iter) > 0)
1687 p = &(*p)->rb_right;
1690 rb_link_node(&he->rb_node, parent, p);
1691 rb_insert_color(&he->rb_node, entries);
1693 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1694 if (perf_hpp__is_dynamic_entry(fmt) &&
1695 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1696 fmt->sort(fmt, he, NULL); /* update column width */
1700 static void output_resort(struct hists *hists, struct ui_progress *prog,
1701 bool use_callchain, hists__resort_cb_t cb)
1703 struct rb_root *root;
1704 struct rb_node *next;
1705 struct hist_entry *n;
1706 u64 callchain_total;
1707 u64 min_callchain_hits;
1709 callchain_total = hists->callchain_period;
1710 if (symbol_conf.filter_relative)
1711 callchain_total = hists->callchain_non_filtered_period;
1713 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1715 hists__reset_stats(hists);
1716 hists__reset_col_len(hists);
1718 if (symbol_conf.report_hierarchy) {
1719 hists__hierarchy_output_resort(hists, prog,
1720 &hists->entries_collapsed,
1724 hierarchy_recalc_total_periods(hists);
1728 if (hists__has(hists, need_collapse))
1729 root = &hists->entries_collapsed;
1731 root = hists->entries_in;
1733 next = rb_first(root);
1734 hists->entries = RB_ROOT;
1737 n = rb_entry(next, struct hist_entry, rb_node_in);
1738 next = rb_next(&n->rb_node_in);
1743 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1744 hists__inc_stats(hists, n);
1747 hists__calc_col_len(hists, n);
1750 ui_progress__update(prog, 1);
1754 void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
1758 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1759 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1761 use_callchain = symbol_conf.use_callchain;
1763 output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
1766 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1768 output_resort(hists, prog, symbol_conf.use_callchain, NULL);
1771 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1772 hists__resort_cb_t cb)
1774 output_resort(hists, prog, symbol_conf.use_callchain, cb);
1777 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1779 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1782 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1788 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1790 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1792 while (can_goto_child(he, HMD_NORMAL)) {
1793 node = rb_last(&he->hroot_out);
1794 he = rb_entry(node, struct hist_entry, rb_node);
1799 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1801 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1803 if (can_goto_child(he, hmd))
1804 node = rb_first(&he->hroot_out);
1806 node = rb_next(node);
1808 while (node == NULL) {
1813 node = rb_next(&he->rb_node);
1818 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1820 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1822 node = rb_prev(node);
1824 return rb_hierarchy_last(node);
1830 return &he->rb_node;
1833 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1835 struct rb_node *node;
1836 struct hist_entry *child;
1842 node = rb_first(&he->hroot_out);
1843 child = rb_entry(node, struct hist_entry, rb_node);
1845 while (node && child->filtered) {
1846 node = rb_next(node);
1847 child = rb_entry(node, struct hist_entry, rb_node);
1851 percent = hist_entry__get_percent_limit(child);
1855 return node && percent >= limit;
1858 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1859 enum hist_filter filter)
1861 h->filtered &= ~(1 << filter);
1863 if (symbol_conf.report_hierarchy) {
1864 struct hist_entry *parent = h->parent_he;
1867 he_stat__add_stat(&parent->stat, &h->stat);
1869 parent->filtered &= ~(1 << filter);
1871 if (parent->filtered)
1874 /* force fold unfiltered entry for simplicity */
1875 parent->unfolded = false;
1876 parent->has_no_entry = false;
1877 parent->row_offset = 0;
1878 parent->nr_rows = 0;
1880 parent = parent->parent_he;
1887 /* force fold unfiltered entry for simplicity */
1888 h->unfolded = false;
1889 h->has_no_entry = false;
1893 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1895 hists__inc_filter_stats(hists, h);
1896 hists__calc_col_len(hists, h);
1900 static bool hists__filter_entry_by_dso(struct hists *hists,
1901 struct hist_entry *he)
1903 if (hists->dso_filter != NULL &&
1904 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1905 he->filtered |= (1 << HIST_FILTER__DSO);
1912 static bool hists__filter_entry_by_thread(struct hists *hists,
1913 struct hist_entry *he)
1915 if (hists->thread_filter != NULL &&
1916 he->thread != hists->thread_filter) {
1917 he->filtered |= (1 << HIST_FILTER__THREAD);
1924 static bool hists__filter_entry_by_symbol(struct hists *hists,
1925 struct hist_entry *he)
1927 if (hists->symbol_filter_str != NULL &&
1928 (!he->ms.sym || strstr(he->ms.sym->name,
1929 hists->symbol_filter_str) == NULL)) {
1930 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1937 static bool hists__filter_entry_by_socket(struct hists *hists,
1938 struct hist_entry *he)
1940 if ((hists->socket_filter > -1) &&
1941 (he->socket != hists->socket_filter)) {
1942 he->filtered |= (1 << HIST_FILTER__SOCKET);
1949 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1951 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
1955 hists->stats.nr_non_filtered_samples = 0;
1957 hists__reset_filter_stats(hists);
1958 hists__reset_col_len(hists);
1960 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1961 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1963 if (filter(hists, h))
1966 hists__remove_entry_filter(hists, h, type);
1970 static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1972 struct rb_node **p = &root->rb_node;
1973 struct rb_node *parent = NULL;
1974 struct hist_entry *iter;
1975 struct rb_root new_root = RB_ROOT;
1978 while (*p != NULL) {
1980 iter = rb_entry(parent, struct hist_entry, rb_node);
1982 if (hist_entry__sort(he, iter) > 0)
1985 p = &(*p)->rb_right;
1988 rb_link_node(&he->rb_node, parent, p);
1989 rb_insert_color(&he->rb_node, root);
1991 if (he->leaf || he->filtered)
1994 nd = rb_first(&he->hroot_out);
1996 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1999 rb_erase(&h->rb_node, &he->hroot_out);
2001 resort_filtered_entry(&new_root, h);
2004 he->hroot_out = new_root;
2007 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2010 struct rb_root new_root = RB_ROOT;
2012 hists->stats.nr_non_filtered_samples = 0;
2014 hists__reset_filter_stats(hists);
2015 hists__reset_col_len(hists);
2017 nd = rb_first(&hists->entries);
2019 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2022 ret = hist_entry__filter(h, type, arg);
2025 * case 1. non-matching type
2026 * zero out the period, set filter marker and move to child
2029 memset(&h->stat, 0, sizeof(h->stat));
2030 h->filtered |= (1 << type);
2032 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2035 * case 2. matched type (filter out)
2036 * set filter marker and move to next
2038 else if (ret == 1) {
2039 h->filtered |= (1 << type);
2041 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2044 * case 3. ok (not filtered)
2045 * add period to hists and parents, erase the filter marker
2046 * and move to next sibling
2049 hists__remove_entry_filter(hists, h, type);
2051 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2055 hierarchy_recalc_total_periods(hists);
2058 * resort output after applying a new filter since filter in a lower
2059 * hierarchy can change periods in a upper hierarchy.
2061 nd = rb_first(&hists->entries);
2063 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2066 rb_erase(&h->rb_node, &hists->entries);
2068 resort_filtered_entry(&new_root, h);
2071 hists->entries = new_root;
2074 void hists__filter_by_thread(struct hists *hists)
2076 if (symbol_conf.report_hierarchy)
2077 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2078 hists->thread_filter);
2080 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2081 hists__filter_entry_by_thread);
2084 void hists__filter_by_dso(struct hists *hists)
2086 if (symbol_conf.report_hierarchy)
2087 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2090 hists__filter_by_type(hists, HIST_FILTER__DSO,
2091 hists__filter_entry_by_dso);
2094 void hists__filter_by_symbol(struct hists *hists)
2096 if (symbol_conf.report_hierarchy)
2097 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2098 hists->symbol_filter_str);
2100 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2101 hists__filter_entry_by_symbol);
2104 void hists__filter_by_socket(struct hists *hists)
2106 if (symbol_conf.report_hierarchy)
2107 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2108 &hists->socket_filter);
2110 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2111 hists__filter_entry_by_socket);
2114 void events_stats__inc(struct events_stats *stats, u32 type)
2116 ++stats->nr_events[0];
2117 ++stats->nr_events[type];
2120 void hists__inc_nr_events(struct hists *hists, u32 type)
2122 events_stats__inc(&hists->stats, type);
2125 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2127 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2129 hists->stats.nr_non_filtered_samples++;
2132 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2133 struct hist_entry *pair)
2135 struct rb_root *root;
2137 struct rb_node *parent = NULL;
2138 struct hist_entry *he;
2141 if (hists__has(hists, need_collapse))
2142 root = &hists->entries_collapsed;
2144 root = hists->entries_in;
2148 while (*p != NULL) {
2150 he = rb_entry(parent, struct hist_entry, rb_node_in);
2152 cmp = hist_entry__collapse(he, pair);
2160 p = &(*p)->rb_right;
2163 he = hist_entry__new(pair, true);
2165 memset(&he->stat, 0, sizeof(he->stat));
2167 if (symbol_conf.cumulate_callchain)
2168 memset(he->stat_acc, 0, sizeof(he->stat));
2169 rb_link_node(&he->rb_node_in, parent, p);
2170 rb_insert_color(&he->rb_node_in, root);
2171 hists__inc_stats(hists, he);
2178 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2179 struct rb_root *root,
2180 struct hist_entry *pair)
2183 struct rb_node *parent = NULL;
2184 struct hist_entry *he;
2185 struct perf_hpp_fmt *fmt;
2188 while (*p != NULL) {
2192 he = rb_entry(parent, struct hist_entry, rb_node_in);
2194 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2195 cmp = fmt->collapse(fmt, he, pair);
2203 p = &parent->rb_left;
2205 p = &parent->rb_right;
2208 he = hist_entry__new(pair, true);
2210 rb_link_node(&he->rb_node_in, parent, p);
2211 rb_insert_color(&he->rb_node_in, root);
2215 memset(&he->stat, 0, sizeof(he->stat));
2216 hists__inc_stats(hists, he);
2222 static struct hist_entry *hists__find_entry(struct hists *hists,
2223 struct hist_entry *he)
2227 if (hists__has(hists, need_collapse))
2228 n = hists->entries_collapsed.rb_node;
2230 n = hists->entries_in->rb_node;
2233 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2234 int64_t cmp = hist_entry__collapse(iter, he);
2247 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
2248 struct hist_entry *he)
2250 struct rb_node *n = root->rb_node;
2253 struct hist_entry *iter;
2254 struct perf_hpp_fmt *fmt;
2257 iter = rb_entry(n, struct hist_entry, rb_node_in);
2258 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2259 cmp = fmt->collapse(fmt, iter, he);
2275 static void hists__match_hierarchy(struct rb_root *leader_root,
2276 struct rb_root *other_root)
2279 struct hist_entry *pos, *pair;
2281 for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
2282 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2283 pair = hists__find_hierarchy_entry(other_root, pos);
2286 hist_entry__add_pair(pair, pos);
2287 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2293 * Look for pairs to link to the leader buckets (hist_entries):
2295 void hists__match(struct hists *leader, struct hists *other)
2297 struct rb_root *root;
2299 struct hist_entry *pos, *pair;
2301 if (symbol_conf.report_hierarchy) {
2302 /* hierarchy report always collapses entries */
2303 return hists__match_hierarchy(&leader->entries_collapsed,
2304 &other->entries_collapsed);
2307 if (hists__has(leader, need_collapse))
2308 root = &leader->entries_collapsed;
2310 root = leader->entries_in;
2312 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2313 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2314 pair = hists__find_entry(other, pos);
2317 hist_entry__add_pair(pair, pos);
2321 static int hists__link_hierarchy(struct hists *leader_hists,
2322 struct hist_entry *parent,
2323 struct rb_root *leader_root,
2324 struct rb_root *other_root)
2327 struct hist_entry *pos, *leader;
2329 for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
2330 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2332 if (hist_entry__has_pairs(pos)) {
2335 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2336 if (leader->hists == leader_hists) {
2344 leader = add_dummy_hierarchy_entry(leader_hists,
2349 /* do not point parent in the pos */
2350 leader->parent_he = parent;
2352 hist_entry__add_pair(pos, leader);
2356 if (hists__link_hierarchy(leader_hists, leader,
2358 &pos->hroot_in) < 0)
2366 * Look for entries in the other hists that are not present in the leader, if
2367 * we find them, just add a dummy entry on the leader hists, with period=0,
2368 * nr_events=0, to serve as the list header.
2370 int hists__link(struct hists *leader, struct hists *other)
2372 struct rb_root *root;
2374 struct hist_entry *pos, *pair;
2376 if (symbol_conf.report_hierarchy) {
2377 /* hierarchy report always collapses entries */
2378 return hists__link_hierarchy(leader, NULL,
2379 &leader->entries_collapsed,
2380 &other->entries_collapsed);
2383 if (hists__has(other, need_collapse))
2384 root = &other->entries_collapsed;
2386 root = other->entries_in;
2388 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2389 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2391 if (!hist_entry__has_pairs(pos)) {
2392 pair = hists__add_dummy_entry(leader, pos);
2395 hist_entry__add_pair(pos, pair);
2402 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2403 struct perf_sample *sample, bool nonany_branch_mode)
2405 struct branch_info *bi;
2407 /* If we have branch cycles always annotate them. */
2408 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2411 bi = sample__resolve_bstack(sample, al);
2413 struct addr_map_symbol *prev = NULL;
2416 * Ignore errors, still want to process the
2419 * For non standard branch modes always
2420 * force no IPC (prev == NULL)
2422 * Note that perf stores branches reversed from
2425 for (i = bs->nr - 1; i >= 0; i--) {
2426 addr_map_symbol__account_cycles(&bi[i].from,
2427 nonany_branch_mode ? NULL : prev,
2428 bi[i].flags.cycles);
2436 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2438 struct perf_evsel *pos;
2441 evlist__for_each_entry(evlist, pos) {
2442 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2443 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2450 u64 hists__total_period(struct hists *hists)
2452 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2453 hists->stats.total_period;
2456 int parse_filter_percentage(const struct option *opt __maybe_unused,
2457 const char *arg, int unset __maybe_unused)
2459 if (!strcmp(arg, "relative"))
2460 symbol_conf.filter_relative = true;
2461 else if (!strcmp(arg, "absolute"))
2462 symbol_conf.filter_relative = false;
2464 pr_debug("Invalid percentage: %s\n", arg);
2471 int perf_hist_config(const char *var, const char *value)
2473 if (!strcmp(var, "hist.percentage"))
2474 return parse_filter_percentage(NULL, value, 0);
2479 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2481 memset(hists, 0, sizeof(*hists));
2482 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2483 hists->entries_in = &hists->entries_in_array[0];
2484 hists->entries_collapsed = RB_ROOT;
2485 hists->entries = RB_ROOT;
2486 pthread_mutex_init(&hists->lock, NULL);
2487 hists->socket_filter = -1;
2488 hists->hpp_list = hpp_list;
2489 INIT_LIST_HEAD(&hists->hpp_formats);
2493 static void hists__delete_remaining_entries(struct rb_root *root)
2495 struct rb_node *node;
2496 struct hist_entry *he;
2498 while (!RB_EMPTY_ROOT(root)) {
2499 node = rb_first(root);
2500 rb_erase(node, root);
2502 he = rb_entry(node, struct hist_entry, rb_node_in);
2503 hist_entry__delete(he);
2507 static void hists__delete_all_entries(struct hists *hists)
2509 hists__delete_entries(hists);
2510 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2511 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2512 hists__delete_remaining_entries(&hists->entries_collapsed);
2515 static void hists_evsel__exit(struct perf_evsel *evsel)
2517 struct hists *hists = evsel__hists(evsel);
2518 struct perf_hpp_fmt *fmt, *pos;
2519 struct perf_hpp_list_node *node, *tmp;
2521 hists__delete_all_entries(hists);
2523 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2524 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2525 list_del(&fmt->list);
2528 list_del(&node->list);
2533 static int hists_evsel__init(struct perf_evsel *evsel)
2535 struct hists *hists = evsel__hists(evsel);
2537 __hists__init(hists, &perf_hpp_list);
2542 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2543 * stored in the rbtree...
2546 int hists__init(void)
2548 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
2552 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2557 void perf_hpp_list__init(struct perf_hpp_list *list)
2559 INIT_LIST_HEAD(&list->fields);
2560 INIT_LIST_HEAD(&list->sorts);