10 static bool hists__filter_entry_by_dso(struct hists *hists,
11 struct hist_entry *he);
12 static bool hists__filter_entry_by_thread(struct hists *hists,
13 struct hist_entry *he);
14 static bool hists__filter_entry_by_symbol(struct hists *hists,
15 struct hist_entry *he);
24 struct callchain_param callchain_param = {
25 .mode = CHAIN_GRAPH_REL,
27 .order = ORDER_CALLEE,
31 u16 hists__col_len(struct hists *hists, enum hist_column col)
33 return hists->col_len[col];
36 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
38 hists->col_len[col] = len;
41 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
43 if (len > hists__col_len(hists, col)) {
44 hists__set_col_len(hists, col, len);
50 void hists__reset_col_len(struct hists *hists)
54 for (col = 0; col < HISTC_NR_COLS; ++col)
55 hists__set_col_len(hists, col, 0);
58 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
60 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
62 if (hists__col_len(hists, dso) < unresolved_col_width &&
63 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
64 !symbol_conf.dso_list)
65 hists__set_col_len(hists, dso, unresolved_col_width);
68 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
70 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
75 * +4 accounts for '[x] ' priv level info
76 * +2 accounts for 0x prefix on raw addresses
77 * +3 accounts for ' y ' symtab origin info
80 symlen = h->ms.sym->namelen + 4;
82 symlen += BITS_PER_LONG / 4 + 2 + 3;
83 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
85 symlen = unresolved_col_width + 4 + 2;
86 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
87 hists__set_unres_dso_col_len(hists, HISTC_DSO);
90 len = thread__comm_len(h->thread);
91 if (hists__new_col_len(hists, HISTC_COMM, len))
92 hists__set_col_len(hists, HISTC_THREAD, len + 6);
95 len = dso__name_len(h->ms.map->dso);
96 hists__new_col_len(hists, HISTC_DSO, len);
100 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
102 if (h->branch_info) {
103 if (h->branch_info->from.sym) {
104 symlen = (int)h->branch_info->from.sym->namelen + 4;
106 symlen += BITS_PER_LONG / 4 + 2 + 3;
107 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
109 symlen = dso__name_len(h->branch_info->from.map->dso);
110 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
112 symlen = unresolved_col_width + 4 + 2;
113 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
114 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
117 if (h->branch_info->to.sym) {
118 symlen = (int)h->branch_info->to.sym->namelen + 4;
120 symlen += BITS_PER_LONG / 4 + 2 + 3;
121 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
123 symlen = dso__name_len(h->branch_info->to.map->dso);
124 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
126 symlen = unresolved_col_width + 4 + 2;
127 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
128 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
133 if (h->mem_info->daddr.sym) {
134 symlen = (int)h->mem_info->daddr.sym->namelen + 4
135 + unresolved_col_width + 2;
136 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
139 symlen = unresolved_col_width + 4 + 2;
140 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
143 if (h->mem_info->daddr.map) {
144 symlen = dso__name_len(h->mem_info->daddr.map->dso);
145 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
148 symlen = unresolved_col_width + 4 + 2;
149 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
152 symlen = unresolved_col_width + 4 + 2;
153 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
154 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
157 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
158 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
159 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
160 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
161 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
162 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
165 hists__new_col_len(hists, HISTC_TRANSACTION,
166 hist_entry__transaction_len());
169 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
171 struct rb_node *next = rb_first(&hists->entries);
172 struct hist_entry *n;
175 hists__reset_col_len(hists);
177 while (next && row++ < max_rows) {
178 n = rb_entry(next, struct hist_entry, rb_node);
180 hists__calc_col_len(hists, n);
181 next = rb_next(&n->rb_node);
185 static void hist_entry__add_cpumode_period(struct hist_entry *he,
186 unsigned int cpumode, u64 period)
189 case PERF_RECORD_MISC_KERNEL:
190 he->stat.period_sys += period;
192 case PERF_RECORD_MISC_USER:
193 he->stat.period_us += period;
195 case PERF_RECORD_MISC_GUEST_KERNEL:
196 he->stat.period_guest_sys += period;
198 case PERF_RECORD_MISC_GUEST_USER:
199 he->stat.period_guest_us += period;
206 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
210 he_stat->period += period;
211 he_stat->weight += weight;
212 he_stat->nr_events += 1;
215 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
217 dest->period += src->period;
218 dest->period_sys += src->period_sys;
219 dest->period_us += src->period_us;
220 dest->period_guest_sys += src->period_guest_sys;
221 dest->period_guest_us += src->period_guest_us;
222 dest->nr_events += src->nr_events;
223 dest->weight += src->weight;
226 static void hist_entry__decay(struct hist_entry *he)
228 he->stat.period = (he->stat.period * 7) / 8;
229 he->stat.nr_events = (he->stat.nr_events * 7) / 8;
230 /* XXX need decay for weight too? */
233 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
235 u64 prev_period = he->stat.period;
237 if (prev_period == 0)
240 hist_entry__decay(he);
243 hists->stats.total_period -= prev_period - he->stat.period;
245 return he->stat.period == 0;
248 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
250 struct rb_node *next = rb_first(&hists->entries);
251 struct hist_entry *n;
254 n = rb_entry(next, struct hist_entry, rb_node);
255 next = rb_next(&n->rb_node);
257 * We may be annotating this, for instance, so keep it here in
258 * case some it gets new samples, we'll eventually free it when
259 * the user stops browsing and it agains gets fully decayed.
261 if (((zap_user && n->level == '.') ||
262 (zap_kernel && n->level != '.') ||
263 hists__decay_entry(hists, n)) &&
265 rb_erase(&n->rb_node, &hists->entries);
267 if (sort__need_collapse)
268 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
277 * histogram, sorted on item, collects periods
280 static struct hist_entry *hist_entry__new(struct hist_entry *template)
282 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
283 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
289 he->ms.map->referenced = true;
291 if (he->branch_info) {
293 * This branch info is (a part of) allocated from
294 * machine__resolve_bstack() and will be freed after
295 * adding new entries. So we need to save a copy.
297 he->branch_info = malloc(sizeof(*he->branch_info));
298 if (he->branch_info == NULL) {
303 memcpy(he->branch_info, template->branch_info,
304 sizeof(*he->branch_info));
306 if (he->branch_info->from.map)
307 he->branch_info->from.map->referenced = true;
308 if (he->branch_info->to.map)
309 he->branch_info->to.map->referenced = true;
313 if (he->mem_info->iaddr.map)
314 he->mem_info->iaddr.map->referenced = true;
315 if (he->mem_info->daddr.map)
316 he->mem_info->daddr.map->referenced = true;
319 if (symbol_conf.use_callchain)
320 callchain_init(he->callchain);
322 INIT_LIST_HEAD(&he->pairs.node);
328 void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
331 hists__calc_col_len(hists, h);
333 hists->stats.total_period += h->stat.period;
337 static u8 symbol__parent_filter(const struct symbol *parent)
339 if (symbol_conf.exclude_other && parent == NULL)
340 return 1 << HIST_FILTER__PARENT;
344 static struct hist_entry *add_hist_entry(struct hists *hists,
345 struct hist_entry *entry,
346 struct addr_location *al,
351 struct rb_node *parent = NULL;
352 struct hist_entry *he;
355 p = &hists->entries_in->rb_node;
359 he = rb_entry(parent, struct hist_entry, rb_node_in);
362 * Make sure that it receives arguments in a same order as
363 * hist_entry__collapse() so that we can use an appropriate
364 * function when searching an entry regardless which sort
367 cmp = hist_entry__cmp(he, entry);
370 he_stat__add_period(&he->stat, period, weight);
373 * This mem info was allocated from machine__resolve_mem
374 * and will not be used anymore.
376 free(entry->mem_info);
378 /* If the map of an existing hist_entry has
379 * become out-of-date due to an exec() or
380 * similar, update it. Otherwise we will
381 * mis-adjust symbol addresses when computing
382 * the history counter to increment.
384 if (he->ms.map != entry->ms.map) {
385 he->ms.map = entry->ms.map;
387 he->ms.map->referenced = true;
398 he = hist_entry__new(entry);
402 rb_link_node(&he->rb_node_in, parent, p);
403 rb_insert_color(&he->rb_node_in, hists->entries_in);
405 hist_entry__add_cpumode_period(he, al->cpumode, period);
409 struct hist_entry *__hists__add_mem_entry(struct hists *self,
410 struct addr_location *al,
411 struct symbol *sym_parent,
416 struct hist_entry entry = {
417 .thread = al->thread,
430 .parent = sym_parent,
431 .filtered = symbol__parent_filter(sym_parent),
436 return add_hist_entry(self, &entry, al, period, weight);
439 struct hist_entry *__hists__add_branch_entry(struct hists *self,
440 struct addr_location *al,
441 struct symbol *sym_parent,
442 struct branch_info *bi,
446 struct hist_entry entry = {
447 .thread = al->thread,
460 .parent = sym_parent,
461 .filtered = symbol__parent_filter(sym_parent),
467 return add_hist_entry(self, &entry, al, period, weight);
470 struct hist_entry *__hists__add_entry(struct hists *self,
471 struct addr_location *al,
472 struct symbol *sym_parent, u64 period,
473 u64 weight, u64 transaction)
475 struct hist_entry entry = {
476 .thread = al->thread,
489 .parent = sym_parent,
490 .filtered = symbol__parent_filter(sym_parent),
494 .transaction = transaction,
497 return add_hist_entry(self, &entry, al, period, weight);
501 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
503 struct sort_entry *se;
506 list_for_each_entry(se, &hist_entry__sort_list, list) {
507 cmp = se->se_cmp(left, right);
516 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
518 struct sort_entry *se;
521 list_for_each_entry(se, &hist_entry__sort_list, list) {
522 int64_t (*f)(struct hist_entry *, struct hist_entry *);
524 f = se->se_collapse ?: se->se_cmp;
526 cmp = f(left, right);
534 void hist_entry__free(struct hist_entry *he)
536 free(he->branch_info);
538 free_srcline(he->srcline);
543 * collapse the histogram
546 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
547 struct rb_root *root,
548 struct hist_entry *he)
550 struct rb_node **p = &root->rb_node;
551 struct rb_node *parent = NULL;
552 struct hist_entry *iter;
557 iter = rb_entry(parent, struct hist_entry, rb_node_in);
559 cmp = hist_entry__collapse(iter, he);
562 he_stat__add_stat(&iter->stat, &he->stat);
564 if (symbol_conf.use_callchain) {
565 callchain_cursor_reset(&callchain_cursor);
566 callchain_merge(&callchain_cursor,
570 hist_entry__free(he);
580 rb_link_node(&he->rb_node_in, parent, p);
581 rb_insert_color(&he->rb_node_in, root);
585 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
587 struct rb_root *root;
589 pthread_mutex_lock(&hists->lock);
591 root = hists->entries_in;
592 if (++hists->entries_in > &hists->entries_in_array[1])
593 hists->entries_in = &hists->entries_in_array[0];
595 pthread_mutex_unlock(&hists->lock);
600 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
602 hists__filter_entry_by_dso(hists, he);
603 hists__filter_entry_by_thread(hists, he);
604 hists__filter_entry_by_symbol(hists, he);
607 void hists__collapse_resort(struct hists *hists)
609 struct rb_root *root;
610 struct rb_node *next;
611 struct hist_entry *n;
613 if (!sort__need_collapse)
616 root = hists__get_rotate_entries_in(hists);
617 next = rb_first(root);
622 n = rb_entry(next, struct hist_entry, rb_node_in);
623 next = rb_next(&n->rb_node_in);
625 rb_erase(&n->rb_node_in, root);
626 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
628 * If it wasn't combined with one of the entries already
629 * collapsed, we need to apply the filters that may have
630 * been set by, say, the hist_browser.
632 hists__apply_filters(hists, n);
638 * reverse the map, sort on period.
641 static int period_cmp(u64 period_a, u64 period_b)
643 if (period_a > period_b)
645 if (period_a < period_b)
650 static int hist_entry__sort_on_period(struct hist_entry *a,
651 struct hist_entry *b)
655 struct perf_evsel *evsel;
656 struct hist_entry *pair;
657 u64 *periods_a, *periods_b;
659 ret = period_cmp(a->stat.period, b->stat.period);
660 if (ret || !symbol_conf.event_group)
663 evsel = hists_to_evsel(a->hists);
664 nr_members = evsel->nr_members;
668 periods_a = zalloc(sizeof(periods_a) * nr_members);
669 periods_b = zalloc(sizeof(periods_b) * nr_members);
671 if (!periods_a || !periods_b)
674 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
675 evsel = hists_to_evsel(pair->hists);
676 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
679 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
680 evsel = hists_to_evsel(pair->hists);
681 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
684 for (i = 1; i < nr_members; i++) {
685 ret = period_cmp(periods_a[i], periods_b[i]);
697 static void __hists__insert_output_entry(struct rb_root *entries,
698 struct hist_entry *he,
699 u64 min_callchain_hits)
701 struct rb_node **p = &entries->rb_node;
702 struct rb_node *parent = NULL;
703 struct hist_entry *iter;
705 if (symbol_conf.use_callchain)
706 callchain_param.sort(&he->sorted_chain, he->callchain,
707 min_callchain_hits, &callchain_param);
711 iter = rb_entry(parent, struct hist_entry, rb_node);
713 if (hist_entry__sort_on_period(he, iter) > 0)
719 rb_link_node(&he->rb_node, parent, p);
720 rb_insert_color(&he->rb_node, entries);
723 void hists__output_resort(struct hists *hists)
725 struct rb_root *root;
726 struct rb_node *next;
727 struct hist_entry *n;
728 u64 min_callchain_hits;
730 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
732 if (sort__need_collapse)
733 root = &hists->entries_collapsed;
735 root = hists->entries_in;
737 next = rb_first(root);
738 hists->entries = RB_ROOT;
740 hists->nr_entries = 0;
741 hists->stats.total_period = 0;
742 hists__reset_col_len(hists);
745 n = rb_entry(next, struct hist_entry, rb_node_in);
746 next = rb_next(&n->rb_node_in);
748 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
749 hists__inc_nr_entries(hists, n);
753 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
754 enum hist_filter filter)
756 h->filtered &= ~(1 << filter);
762 hists->nr_entries += h->nr_rows;
764 hists->stats.total_period += h->stat.period;
765 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
767 hists__calc_col_len(hists, h);
771 static bool hists__filter_entry_by_dso(struct hists *hists,
772 struct hist_entry *he)
774 if (hists->dso_filter != NULL &&
775 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
776 he->filtered |= (1 << HIST_FILTER__DSO);
783 void hists__filter_by_dso(struct hists *hists)
787 hists->nr_entries = hists->stats.total_period = 0;
788 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
789 hists__reset_col_len(hists);
791 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
792 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
794 if (symbol_conf.exclude_other && !h->parent)
797 if (hists__filter_entry_by_dso(hists, h))
800 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
804 static bool hists__filter_entry_by_thread(struct hists *hists,
805 struct hist_entry *he)
807 if (hists->thread_filter != NULL &&
808 he->thread != hists->thread_filter) {
809 he->filtered |= (1 << HIST_FILTER__THREAD);
816 void hists__filter_by_thread(struct hists *hists)
820 hists->nr_entries = hists->stats.total_period = 0;
821 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
822 hists__reset_col_len(hists);
824 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
825 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
827 if (hists__filter_entry_by_thread(hists, h))
830 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
834 static bool hists__filter_entry_by_symbol(struct hists *hists,
835 struct hist_entry *he)
837 if (hists->symbol_filter_str != NULL &&
838 (!he->ms.sym || strstr(he->ms.sym->name,
839 hists->symbol_filter_str) == NULL)) {
840 he->filtered |= (1 << HIST_FILTER__SYMBOL);
847 void hists__filter_by_symbol(struct hists *hists)
851 hists->nr_entries = hists->stats.total_period = 0;
852 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
853 hists__reset_col_len(hists);
855 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
856 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
858 if (hists__filter_entry_by_symbol(hists, h))
861 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
865 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
867 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
870 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
872 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
875 void events_stats__inc(struct events_stats *stats, u32 type)
877 ++stats->nr_events[0];
878 ++stats->nr_events[type];
881 void hists__inc_nr_events(struct hists *hists, u32 type)
883 events_stats__inc(&hists->stats, type);
886 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
887 struct hist_entry *pair)
889 struct rb_root *root;
891 struct rb_node *parent = NULL;
892 struct hist_entry *he;
895 if (sort__need_collapse)
896 root = &hists->entries_collapsed;
898 root = hists->entries_in;
904 he = rb_entry(parent, struct hist_entry, rb_node_in);
906 cmp = hist_entry__collapse(he, pair);
917 he = hist_entry__new(pair);
919 memset(&he->stat, 0, sizeof(he->stat));
921 rb_link_node(&he->rb_node_in, parent, p);
922 rb_insert_color(&he->rb_node_in, root);
923 hists__inc_nr_entries(hists, he);
930 static struct hist_entry *hists__find_entry(struct hists *hists,
931 struct hist_entry *he)
935 if (sort__need_collapse)
936 n = hists->entries_collapsed.rb_node;
938 n = hists->entries_in->rb_node;
941 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
942 int64_t cmp = hist_entry__collapse(iter, he);
956 * Look for pairs to link to the leader buckets (hist_entries):
958 void hists__match(struct hists *leader, struct hists *other)
960 struct rb_root *root;
962 struct hist_entry *pos, *pair;
964 if (sort__need_collapse)
965 root = &leader->entries_collapsed;
967 root = leader->entries_in;
969 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
970 pos = rb_entry(nd, struct hist_entry, rb_node_in);
971 pair = hists__find_entry(other, pos);
974 hist_entry__add_pair(pair, pos);
979 * Look for entries in the other hists that are not present in the leader, if
980 * we find them, just add a dummy entry on the leader hists, with period=0,
981 * nr_events=0, to serve as the list header.
983 int hists__link(struct hists *leader, struct hists *other)
985 struct rb_root *root;
987 struct hist_entry *pos, *pair;
989 if (sort__need_collapse)
990 root = &other->entries_collapsed;
992 root = other->entries_in;
994 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
995 pos = rb_entry(nd, struct hist_entry, rb_node_in);
997 if (!hist_entry__has_pairs(pos)) {
998 pair = hists__add_dummy_entry(leader, pos);
1001 hist_entry__add_pair(pos, pair);