9 static bool hists__filter_entry_by_dso(struct hists *hists,
10 struct hist_entry *he);
11 static bool hists__filter_entry_by_thread(struct hists *hists,
12 struct hist_entry *he);
13 static bool hists__filter_entry_by_symbol(struct hists *hists,
14 struct hist_entry *he);
16 struct callchain_param callchain_param = {
17 .mode = CHAIN_GRAPH_REL,
19 .order = ORDER_CALLEE,
23 u16 hists__col_len(struct hists *hists, enum hist_column col)
25 return hists->col_len[col];
28 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
30 hists->col_len[col] = len;
33 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
35 if (len > hists__col_len(hists, col)) {
36 hists__set_col_len(hists, col, len);
42 void hists__reset_col_len(struct hists *hists)
46 for (col = 0; col < HISTC_NR_COLS; ++col)
47 hists__set_col_len(hists, col, 0);
50 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
52 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
54 if (hists__col_len(hists, dso) < unresolved_col_width &&
55 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
56 !symbol_conf.dso_list)
57 hists__set_col_len(hists, dso, unresolved_col_width);
60 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
62 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
67 * +4 accounts for '[x] ' priv level info
68 * +2 accounts for 0x prefix on raw addresses
69 * +3 accounts for ' y ' symtab origin info
72 symlen = h->ms.sym->namelen + 4;
74 symlen += BITS_PER_LONG / 4 + 2 + 3;
75 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
77 symlen = unresolved_col_width + 4 + 2;
78 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
79 hists__set_unres_dso_col_len(hists, HISTC_DSO);
82 len = thread__comm_len(h->thread);
83 if (hists__new_col_len(hists, HISTC_COMM, len))
84 hists__set_col_len(hists, HISTC_THREAD, len + 6);
87 len = dso__name_len(h->ms.map->dso);
88 hists__new_col_len(hists, HISTC_DSO, len);
92 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
95 if (h->branch_info->from.sym) {
96 symlen = (int)h->branch_info->from.sym->namelen + 4;
98 symlen += BITS_PER_LONG / 4 + 2 + 3;
99 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
101 symlen = dso__name_len(h->branch_info->from.map->dso);
102 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
104 symlen = unresolved_col_width + 4 + 2;
105 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
106 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
109 if (h->branch_info->to.sym) {
110 symlen = (int)h->branch_info->to.sym->namelen + 4;
112 symlen += BITS_PER_LONG / 4 + 2 + 3;
113 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
115 symlen = dso__name_len(h->branch_info->to.map->dso);
116 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
118 symlen = unresolved_col_width + 4 + 2;
119 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
120 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
125 if (h->mem_info->daddr.sym) {
126 symlen = (int)h->mem_info->daddr.sym->namelen + 4
127 + unresolved_col_width + 2;
128 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
131 symlen = unresolved_col_width + 4 + 2;
132 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
135 if (h->mem_info->daddr.map) {
136 symlen = dso__name_len(h->mem_info->daddr.map->dso);
137 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
140 symlen = unresolved_col_width + 4 + 2;
141 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
144 symlen = unresolved_col_width + 4 + 2;
145 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
146 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
149 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
150 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
151 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
152 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
153 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
154 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
157 hists__new_col_len(hists, HISTC_TRANSACTION,
158 hist_entry__transaction_len());
161 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
163 struct rb_node *next = rb_first(&hists->entries);
164 struct hist_entry *n;
167 hists__reset_col_len(hists);
169 while (next && row++ < max_rows) {
170 n = rb_entry(next, struct hist_entry, rb_node);
172 hists__calc_col_len(hists, n);
173 next = rb_next(&n->rb_node);
177 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
178 unsigned int cpumode, u64 period)
181 case PERF_RECORD_MISC_KERNEL:
182 he_stat->period_sys += period;
184 case PERF_RECORD_MISC_USER:
185 he_stat->period_us += period;
187 case PERF_RECORD_MISC_GUEST_KERNEL:
188 he_stat->period_guest_sys += period;
190 case PERF_RECORD_MISC_GUEST_USER:
191 he_stat->period_guest_us += period;
198 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
202 he_stat->period += period;
203 he_stat->weight += weight;
204 he_stat->nr_events += 1;
207 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
209 dest->period += src->period;
210 dest->period_sys += src->period_sys;
211 dest->period_us += src->period_us;
212 dest->period_guest_sys += src->period_guest_sys;
213 dest->period_guest_us += src->period_guest_us;
214 dest->nr_events += src->nr_events;
215 dest->weight += src->weight;
218 static void he_stat__decay(struct he_stat *he_stat)
220 he_stat->period = (he_stat->period * 7) / 8;
221 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
222 /* XXX need decay for weight too? */
225 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
227 u64 prev_period = he->stat.period;
229 if (prev_period == 0)
232 he_stat__decay(&he->stat);
235 hists->stats.total_period -= prev_period - he->stat.period;
237 return he->stat.period == 0;
240 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
242 struct rb_node *next = rb_first(&hists->entries);
243 struct hist_entry *n;
246 n = rb_entry(next, struct hist_entry, rb_node);
247 next = rb_next(&n->rb_node);
249 * We may be annotating this, for instance, so keep it here in
250 * case some it gets new samples, we'll eventually free it when
251 * the user stops browsing and it agains gets fully decayed.
253 if (((zap_user && n->level == '.') ||
254 (zap_kernel && n->level != '.') ||
255 hists__decay_entry(hists, n)) &&
257 rb_erase(&n->rb_node, &hists->entries);
259 if (sort__need_collapse)
260 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
269 * histogram, sorted on item, collects periods
272 static struct hist_entry *hist_entry__new(struct hist_entry *template)
274 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
275 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
281 he->ms.map->referenced = true;
283 if (he->branch_info) {
285 * This branch info is (a part of) allocated from
286 * sample__resolve_bstack() and will be freed after
287 * adding new entries. So we need to save a copy.
289 he->branch_info = malloc(sizeof(*he->branch_info));
290 if (he->branch_info == NULL) {
295 memcpy(he->branch_info, template->branch_info,
296 sizeof(*he->branch_info));
298 if (he->branch_info->from.map)
299 he->branch_info->from.map->referenced = true;
300 if (he->branch_info->to.map)
301 he->branch_info->to.map->referenced = true;
305 if (he->mem_info->iaddr.map)
306 he->mem_info->iaddr.map->referenced = true;
307 if (he->mem_info->daddr.map)
308 he->mem_info->daddr.map->referenced = true;
311 if (symbol_conf.use_callchain)
312 callchain_init(he->callchain);
314 INIT_LIST_HEAD(&he->pairs.node);
320 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
323 hists->nr_non_filtered_entries++;
324 hists->stats.total_non_filtered_period += h->stat.period;
327 hists->stats.total_period += h->stat.period;
330 static u8 symbol__parent_filter(const struct symbol *parent)
332 if (symbol_conf.exclude_other && parent == NULL)
333 return 1 << HIST_FILTER__PARENT;
337 static struct hist_entry *add_hist_entry(struct hists *hists,
338 struct hist_entry *entry,
339 struct addr_location *al)
342 struct rb_node *parent = NULL;
343 struct hist_entry *he;
345 u64 period = entry->stat.period;
346 u64 weight = entry->stat.weight;
348 p = &hists->entries_in->rb_node;
352 he = rb_entry(parent, struct hist_entry, rb_node_in);
355 * Make sure that it receives arguments in a same order as
356 * hist_entry__collapse() so that we can use an appropriate
357 * function when searching an entry regardless which sort
360 cmp = hist_entry__cmp(he, entry);
363 he_stat__add_period(&he->stat, period, weight);
366 * This mem info was allocated from sample__resolve_mem
367 * and will not be used anymore.
369 zfree(&entry->mem_info);
371 /* If the map of an existing hist_entry has
372 * become out-of-date due to an exec() or
373 * similar, update it. Otherwise we will
374 * mis-adjust symbol addresses when computing
375 * the history counter to increment.
377 if (he->ms.map != entry->ms.map) {
378 he->ms.map = entry->ms.map;
380 he->ms.map->referenced = true;
391 he = hist_entry__new(entry);
396 rb_link_node(&he->rb_node_in, parent, p);
397 rb_insert_color(&he->rb_node_in, hists->entries_in);
399 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
403 struct hist_entry *__hists__add_entry(struct hists *hists,
404 struct addr_location *al,
405 struct symbol *sym_parent,
406 struct branch_info *bi,
408 u64 period, u64 weight, u64 transaction)
410 struct hist_entry entry = {
411 .thread = al->thread,
412 .comm = thread__comm(al->thread),
425 .parent = sym_parent,
426 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
430 .transaction = transaction,
433 return add_hist_entry(hists, &entry, al);
437 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
439 struct sort_entry *se;
442 list_for_each_entry(se, &hist_entry__sort_list, list) {
443 cmp = se->se_cmp(left, right);
452 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
454 struct sort_entry *se;
457 list_for_each_entry(se, &hist_entry__sort_list, list) {
458 int64_t (*f)(struct hist_entry *, struct hist_entry *);
460 f = se->se_collapse ?: se->se_cmp;
462 cmp = f(left, right);
470 void hist_entry__free(struct hist_entry *he)
472 zfree(&he->branch_info);
473 zfree(&he->mem_info);
474 free_srcline(he->srcline);
479 * collapse the histogram
482 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
483 struct rb_root *root,
484 struct hist_entry *he)
486 struct rb_node **p = &root->rb_node;
487 struct rb_node *parent = NULL;
488 struct hist_entry *iter;
493 iter = rb_entry(parent, struct hist_entry, rb_node_in);
495 cmp = hist_entry__collapse(iter, he);
498 he_stat__add_stat(&iter->stat, &he->stat);
500 if (symbol_conf.use_callchain) {
501 callchain_cursor_reset(&callchain_cursor);
502 callchain_merge(&callchain_cursor,
506 hist_entry__free(he);
516 rb_link_node(&he->rb_node_in, parent, p);
517 rb_insert_color(&he->rb_node_in, root);
521 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
523 struct rb_root *root;
525 pthread_mutex_lock(&hists->lock);
527 root = hists->entries_in;
528 if (++hists->entries_in > &hists->entries_in_array[1])
529 hists->entries_in = &hists->entries_in_array[0];
531 pthread_mutex_unlock(&hists->lock);
536 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
538 hists__filter_entry_by_dso(hists, he);
539 hists__filter_entry_by_thread(hists, he);
540 hists__filter_entry_by_symbol(hists, he);
543 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
545 struct rb_root *root;
546 struct rb_node *next;
547 struct hist_entry *n;
549 if (!sort__need_collapse)
552 root = hists__get_rotate_entries_in(hists);
553 next = rb_first(root);
558 n = rb_entry(next, struct hist_entry, rb_node_in);
559 next = rb_next(&n->rb_node_in);
561 rb_erase(&n->rb_node_in, root);
562 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
564 * If it wasn't combined with one of the entries already
565 * collapsed, we need to apply the filters that may have
566 * been set by, say, the hist_browser.
568 hists__apply_filters(hists, n);
571 ui_progress__update(prog, 1);
576 * reverse the map, sort on period.
579 static int period_cmp(u64 period_a, u64 period_b)
581 if (period_a > period_b)
583 if (period_a < period_b)
588 static int hist_entry__sort_on_period(struct hist_entry *a,
589 struct hist_entry *b)
593 struct perf_evsel *evsel;
594 struct hist_entry *pair;
595 u64 *periods_a, *periods_b;
597 ret = period_cmp(a->stat.period, b->stat.period);
598 if (ret || !symbol_conf.event_group)
601 evsel = hists_to_evsel(a->hists);
602 nr_members = evsel->nr_members;
606 periods_a = zalloc(sizeof(periods_a) * nr_members);
607 periods_b = zalloc(sizeof(periods_b) * nr_members);
609 if (!periods_a || !periods_b)
612 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
613 evsel = hists_to_evsel(pair->hists);
614 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
617 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
618 evsel = hists_to_evsel(pair->hists);
619 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
622 for (i = 1; i < nr_members; i++) {
623 ret = period_cmp(periods_a[i], periods_b[i]);
635 static void __hists__insert_output_entry(struct rb_root *entries,
636 struct hist_entry *he,
637 u64 min_callchain_hits)
639 struct rb_node **p = &entries->rb_node;
640 struct rb_node *parent = NULL;
641 struct hist_entry *iter;
643 if (symbol_conf.use_callchain)
644 callchain_param.sort(&he->sorted_chain, he->callchain,
645 min_callchain_hits, &callchain_param);
649 iter = rb_entry(parent, struct hist_entry, rb_node);
651 if (hist_entry__sort_on_period(he, iter) > 0)
657 rb_link_node(&he->rb_node, parent, p);
658 rb_insert_color(&he->rb_node, entries);
661 void hists__output_resort(struct hists *hists)
663 struct rb_root *root;
664 struct rb_node *next;
665 struct hist_entry *n;
666 u64 min_callchain_hits;
668 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
670 if (sort__need_collapse)
671 root = &hists->entries_collapsed;
673 root = hists->entries_in;
675 next = rb_first(root);
676 hists->entries = RB_ROOT;
678 hists->nr_non_filtered_entries = 0;
679 hists->stats.total_period = 0;
680 hists->stats.total_non_filtered_period = 0;
681 hists__reset_col_len(hists);
684 n = rb_entry(next, struct hist_entry, rb_node_in);
685 next = rb_next(&n->rb_node_in);
687 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
688 hists__inc_stats(hists, n);
691 hists__calc_col_len(hists, n);
695 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
696 enum hist_filter filter)
698 h->filtered &= ~(1 << filter);
702 ++hists->nr_non_filtered_entries;
704 hists->nr_non_filtered_entries += h->nr_rows;
706 hists->stats.total_non_filtered_period += h->stat.period;
707 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
709 hists__calc_col_len(hists, h);
713 static bool hists__filter_entry_by_dso(struct hists *hists,
714 struct hist_entry *he)
716 if (hists->dso_filter != NULL &&
717 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
718 he->filtered |= (1 << HIST_FILTER__DSO);
725 void hists__filter_by_dso(struct hists *hists)
729 hists->nr_non_filtered_entries = 0;
730 hists->stats.total_non_filtered_period = 0;
731 hists->stats.nr_non_filtered_samples = 0;
732 hists__reset_col_len(hists);
734 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
735 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
737 if (symbol_conf.exclude_other && !h->parent)
740 if (hists__filter_entry_by_dso(hists, h))
743 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
747 static bool hists__filter_entry_by_thread(struct hists *hists,
748 struct hist_entry *he)
750 if (hists->thread_filter != NULL &&
751 he->thread != hists->thread_filter) {
752 he->filtered |= (1 << HIST_FILTER__THREAD);
759 void hists__filter_by_thread(struct hists *hists)
763 hists->nr_non_filtered_entries = 0;
764 hists->stats.total_non_filtered_period = 0;
765 hists->stats.nr_non_filtered_samples = 0;
766 hists__reset_col_len(hists);
768 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
769 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
771 if (hists__filter_entry_by_thread(hists, h))
774 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
778 static bool hists__filter_entry_by_symbol(struct hists *hists,
779 struct hist_entry *he)
781 if (hists->symbol_filter_str != NULL &&
782 (!he->ms.sym || strstr(he->ms.sym->name,
783 hists->symbol_filter_str) == NULL)) {
784 he->filtered |= (1 << HIST_FILTER__SYMBOL);
791 void hists__filter_by_symbol(struct hists *hists)
795 hists->nr_non_filtered_entries = 0;
796 hists->stats.total_non_filtered_period = 0;
797 hists->stats.nr_non_filtered_samples = 0;
798 hists__reset_col_len(hists);
800 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
801 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
803 if (hists__filter_entry_by_symbol(hists, h))
806 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
810 void events_stats__inc(struct events_stats *stats, u32 type)
812 ++stats->nr_events[0];
813 ++stats->nr_events[type];
816 void hists__inc_nr_events(struct hists *hists, u32 type)
818 events_stats__inc(&hists->stats, type);
821 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
822 struct hist_entry *pair)
824 struct rb_root *root;
826 struct rb_node *parent = NULL;
827 struct hist_entry *he;
830 if (sort__need_collapse)
831 root = &hists->entries_collapsed;
833 root = hists->entries_in;
839 he = rb_entry(parent, struct hist_entry, rb_node_in);
841 cmp = hist_entry__collapse(he, pair);
852 he = hist_entry__new(pair);
854 memset(&he->stat, 0, sizeof(he->stat));
856 rb_link_node(&he->rb_node_in, parent, p);
857 rb_insert_color(&he->rb_node_in, root);
858 hists__inc_stats(hists, he);
865 static struct hist_entry *hists__find_entry(struct hists *hists,
866 struct hist_entry *he)
870 if (sort__need_collapse)
871 n = hists->entries_collapsed.rb_node;
873 n = hists->entries_in->rb_node;
876 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
877 int64_t cmp = hist_entry__collapse(iter, he);
891 * Look for pairs to link to the leader buckets (hist_entries):
893 void hists__match(struct hists *leader, struct hists *other)
895 struct rb_root *root;
897 struct hist_entry *pos, *pair;
899 if (sort__need_collapse)
900 root = &leader->entries_collapsed;
902 root = leader->entries_in;
904 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
905 pos = rb_entry(nd, struct hist_entry, rb_node_in);
906 pair = hists__find_entry(other, pos);
909 hist_entry__add_pair(pair, pos);
914 * Look for entries in the other hists that are not present in the leader, if
915 * we find them, just add a dummy entry on the leader hists, with period=0,
916 * nr_events=0, to serve as the list header.
918 int hists__link(struct hists *leader, struct hists *other)
920 struct rb_root *root;
922 struct hist_entry *pos, *pair;
924 if (sort__need_collapse)
925 root = &other->entries_collapsed;
927 root = other->entries_in;
929 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
930 pos = rb_entry(nd, struct hist_entry, rb_node_in);
932 if (!hist_entry__has_pairs(pos)) {
933 pair = hists__add_dummy_entry(leader, pos);
936 hist_entry__add_pair(pos, pair);
943 u64 hists__total_period(struct hists *hists)
945 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
946 hists->stats.total_period;
949 int parse_filter_percentage(const struct option *opt __maybe_unused,
950 const char *arg, int unset __maybe_unused)
952 if (!strcmp(arg, "relative"))
953 symbol_conf.filter_relative = true;
954 else if (!strcmp(arg, "absolute"))
955 symbol_conf.filter_relative = false;
962 int perf_hist_config(const char *var, const char *value)
964 if (!strcmp(var, "hist.percentage"))
965 return parse_filter_percentage(NULL, value, 0);