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);
23 struct callchain_param callchain_param = {
24 .mode = CHAIN_GRAPH_REL,
26 .order = ORDER_CALLEE,
30 u16 hists__col_len(struct hists *hists, enum hist_column col)
32 return hists->col_len[col];
35 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
37 hists->col_len[col] = len;
40 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
42 if (len > hists__col_len(hists, col)) {
43 hists__set_col_len(hists, col, len);
49 void hists__reset_col_len(struct hists *hists)
53 for (col = 0; col < HISTC_NR_COLS; ++col)
54 hists__set_col_len(hists, col, 0);
57 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
59 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
61 if (hists__col_len(hists, dso) < unresolved_col_width &&
62 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
63 !symbol_conf.dso_list)
64 hists__set_col_len(hists, dso, unresolved_col_width);
67 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
69 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
74 * +4 accounts for '[x] ' priv level info
75 * +2 accounts for 0x prefix on raw addresses
76 * +3 accounts for ' y ' symtab origin info
79 symlen = h->ms.sym->namelen + 4;
81 symlen += BITS_PER_LONG / 4 + 2 + 3;
82 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
84 symlen = unresolved_col_width + 4 + 2;
85 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
86 hists__set_unres_dso_col_len(hists, HISTC_DSO);
89 len = thread__comm_len(h->thread);
90 if (hists__new_col_len(hists, HISTC_COMM, len))
91 hists__set_col_len(hists, HISTC_THREAD, len + 6);
94 len = dso__name_len(h->ms.map->dso);
95 hists__new_col_len(hists, HISTC_DSO, len);
99 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
101 if (h->branch_info) {
102 if (h->branch_info->from.sym) {
103 symlen = (int)h->branch_info->from.sym->namelen + 4;
105 symlen += BITS_PER_LONG / 4 + 2 + 3;
106 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
108 symlen = dso__name_len(h->branch_info->from.map->dso);
109 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
111 symlen = unresolved_col_width + 4 + 2;
112 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
113 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
116 if (h->branch_info->to.sym) {
117 symlen = (int)h->branch_info->to.sym->namelen + 4;
119 symlen += BITS_PER_LONG / 4 + 2 + 3;
120 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
122 symlen = dso__name_len(h->branch_info->to.map->dso);
123 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
125 symlen = unresolved_col_width + 4 + 2;
126 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
127 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
132 if (h->mem_info->daddr.sym) {
133 symlen = (int)h->mem_info->daddr.sym->namelen + 4
134 + unresolved_col_width + 2;
135 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
138 symlen = unresolved_col_width + 4 + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
142 if (h->mem_info->daddr.map) {
143 symlen = dso__name_len(h->mem_info->daddr.map->dso);
144 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
147 symlen = unresolved_col_width + 4 + 2;
148 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
151 symlen = unresolved_col_width + 4 + 2;
152 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
153 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
156 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
157 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
158 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
159 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
160 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
161 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
164 hists__new_col_len(hists, HISTC_TRANSACTION,
165 hist_entry__transaction_len());
168 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
170 struct rb_node *next = rb_first(&hists->entries);
171 struct hist_entry *n;
174 hists__reset_col_len(hists);
176 while (next && row++ < max_rows) {
177 n = rb_entry(next, struct hist_entry, rb_node);
179 hists__calc_col_len(hists, n);
180 next = rb_next(&n->rb_node);
184 static void hist_entry__add_cpumode_period(struct hist_entry *he,
185 unsigned int cpumode, u64 period)
188 case PERF_RECORD_MISC_KERNEL:
189 he->stat.period_sys += period;
191 case PERF_RECORD_MISC_USER:
192 he->stat.period_us += period;
194 case PERF_RECORD_MISC_GUEST_KERNEL:
195 he->stat.period_guest_sys += period;
197 case PERF_RECORD_MISC_GUEST_USER:
198 he->stat.period_guest_us += period;
205 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
209 he_stat->period += period;
210 he_stat->weight += weight;
211 he_stat->nr_events += 1;
214 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
216 dest->period += src->period;
217 dest->period_sys += src->period_sys;
218 dest->period_us += src->period_us;
219 dest->period_guest_sys += src->period_guest_sys;
220 dest->period_guest_us += src->period_guest_us;
221 dest->nr_events += src->nr_events;
222 dest->weight += src->weight;
225 static void hist_entry__decay(struct hist_entry *he)
227 he->stat.period = (he->stat.period * 7) / 8;
228 he->stat.nr_events = (he->stat.nr_events * 7) / 8;
229 /* XXX need decay for weight too? */
232 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
234 u64 prev_period = he->stat.period;
236 if (prev_period == 0)
239 hist_entry__decay(he);
242 hists->stats.total_period -= prev_period - he->stat.period;
244 return he->stat.period == 0;
247 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
249 struct rb_node *next = rb_first(&hists->entries);
250 struct hist_entry *n;
253 n = rb_entry(next, struct hist_entry, rb_node);
254 next = rb_next(&n->rb_node);
256 * We may be annotating this, for instance, so keep it here in
257 * case some it gets new samples, we'll eventually free it when
258 * the user stops browsing and it agains gets fully decayed.
260 if (((zap_user && n->level == '.') ||
261 (zap_kernel && n->level != '.') ||
262 hists__decay_entry(hists, n)) &&
264 rb_erase(&n->rb_node, &hists->entries);
266 if (sort__need_collapse)
267 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
276 * histogram, sorted on item, collects periods
279 static struct hist_entry *hist_entry__new(struct hist_entry *template)
281 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
282 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
288 he->ms.map->referenced = true;
290 if (he->branch_info) {
292 * This branch info is (a part of) allocated from
293 * machine__resolve_bstack() and will be freed after
294 * adding new entries. So we need to save a copy.
296 he->branch_info = malloc(sizeof(*he->branch_info));
297 if (he->branch_info == NULL) {
302 memcpy(he->branch_info, template->branch_info,
303 sizeof(*he->branch_info));
305 if (he->branch_info->from.map)
306 he->branch_info->from.map->referenced = true;
307 if (he->branch_info->to.map)
308 he->branch_info->to.map->referenced = true;
312 if (he->mem_info->iaddr.map)
313 he->mem_info->iaddr.map->referenced = true;
314 if (he->mem_info->daddr.map)
315 he->mem_info->daddr.map->referenced = true;
318 if (symbol_conf.use_callchain)
319 callchain_init(he->callchain);
321 INIT_LIST_HEAD(&he->pairs.node);
327 void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
330 hists__calc_col_len(hists, h);
332 hists->stats.total_period += h->stat.period;
336 static u8 symbol__parent_filter(const struct symbol *parent)
338 if (symbol_conf.exclude_other && parent == NULL)
339 return 1 << HIST_FILTER__PARENT;
343 static struct hist_entry *add_hist_entry(struct hists *hists,
344 struct hist_entry *entry,
345 struct addr_location *al)
348 struct rb_node *parent = NULL;
349 struct hist_entry *he;
351 u64 period = entry->stat.period;
352 u64 weight = entry->stat.weight;
354 p = &hists->entries_in->rb_node;
358 he = rb_entry(parent, struct hist_entry, rb_node_in);
361 * Make sure that it receives arguments in a same order as
362 * hist_entry__collapse() so that we can use an appropriate
363 * function when searching an entry regardless which sort
366 cmp = hist_entry__cmp(he, entry);
369 he_stat__add_period(&he->stat, period, weight);
372 * This mem info was allocated from machine__resolve_mem
373 * and will not be used anymore.
375 zfree(&entry->mem_info);
377 /* If the map of an existing hist_entry has
378 * become out-of-date due to an exec() or
379 * similar, update it. Otherwise we will
380 * mis-adjust symbol addresses when computing
381 * the history counter to increment.
383 if (he->ms.map != entry->ms.map) {
384 he->ms.map = entry->ms.map;
386 he->ms.map->referenced = true;
397 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_entry(struct hists *hists,
410 struct addr_location *al,
411 struct symbol *sym_parent,
412 struct branch_info *bi,
414 u64 period, u64 weight, u64 transaction)
416 struct hist_entry entry = {
417 .thread = al->thread,
418 .comm = thread__comm(al->thread),
431 .parent = sym_parent,
432 .filtered = symbol__parent_filter(sym_parent),
436 .transaction = transaction,
439 return add_hist_entry(hists, &entry, al);
443 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
445 struct sort_entry *se;
448 list_for_each_entry(se, &hist_entry__sort_list, list) {
449 cmp = se->se_cmp(left, right);
458 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
460 struct sort_entry *se;
463 list_for_each_entry(se, &hist_entry__sort_list, list) {
464 int64_t (*f)(struct hist_entry *, struct hist_entry *);
466 f = se->se_collapse ?: se->se_cmp;
468 cmp = f(left, right);
476 void hist_entry__free(struct hist_entry *he)
478 zfree(&he->branch_info);
479 zfree(&he->mem_info);
480 free_srcline(he->srcline);
485 * collapse the histogram
488 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
489 struct rb_root *root,
490 struct hist_entry *he)
492 struct rb_node **p = &root->rb_node;
493 struct rb_node *parent = NULL;
494 struct hist_entry *iter;
499 iter = rb_entry(parent, struct hist_entry, rb_node_in);
501 cmp = hist_entry__collapse(iter, he);
504 he_stat__add_stat(&iter->stat, &he->stat);
506 if (symbol_conf.use_callchain) {
507 callchain_cursor_reset(&callchain_cursor);
508 callchain_merge(&callchain_cursor,
512 hist_entry__free(he);
522 rb_link_node(&he->rb_node_in, parent, p);
523 rb_insert_color(&he->rb_node_in, root);
527 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
529 struct rb_root *root;
531 pthread_mutex_lock(&hists->lock);
533 root = hists->entries_in;
534 if (++hists->entries_in > &hists->entries_in_array[1])
535 hists->entries_in = &hists->entries_in_array[0];
537 pthread_mutex_unlock(&hists->lock);
542 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
544 hists__filter_entry_by_dso(hists, he);
545 hists__filter_entry_by_thread(hists, he);
546 hists__filter_entry_by_symbol(hists, he);
549 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
551 struct rb_root *root;
552 struct rb_node *next;
553 struct hist_entry *n;
555 if (!sort__need_collapse)
558 root = hists__get_rotate_entries_in(hists);
559 next = rb_first(root);
564 n = rb_entry(next, struct hist_entry, rb_node_in);
565 next = rb_next(&n->rb_node_in);
567 rb_erase(&n->rb_node_in, root);
568 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
570 * If it wasn't combined with one of the entries already
571 * collapsed, we need to apply the filters that may have
572 * been set by, say, the hist_browser.
574 hists__apply_filters(hists, n);
577 ui_progress__update(prog, 1);
582 * reverse the map, sort on period.
585 static int period_cmp(u64 period_a, u64 period_b)
587 if (period_a > period_b)
589 if (period_a < period_b)
594 static int hist_entry__sort_on_period(struct hist_entry *a,
595 struct hist_entry *b)
599 struct perf_evsel *evsel;
600 struct hist_entry *pair;
601 u64 *periods_a, *periods_b;
603 ret = period_cmp(a->stat.period, b->stat.period);
604 if (ret || !symbol_conf.event_group)
607 evsel = hists_to_evsel(a->hists);
608 nr_members = evsel->nr_members;
612 periods_a = zalloc(sizeof(periods_a) * nr_members);
613 periods_b = zalloc(sizeof(periods_b) * nr_members);
615 if (!periods_a || !periods_b)
618 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
619 evsel = hists_to_evsel(pair->hists);
620 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
623 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
624 evsel = hists_to_evsel(pair->hists);
625 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
628 for (i = 1; i < nr_members; i++) {
629 ret = period_cmp(periods_a[i], periods_b[i]);
641 static void __hists__insert_output_entry(struct rb_root *entries,
642 struct hist_entry *he,
643 u64 min_callchain_hits)
645 struct rb_node **p = &entries->rb_node;
646 struct rb_node *parent = NULL;
647 struct hist_entry *iter;
649 if (symbol_conf.use_callchain)
650 callchain_param.sort(&he->sorted_chain, he->callchain,
651 min_callchain_hits, &callchain_param);
655 iter = rb_entry(parent, struct hist_entry, rb_node);
657 if (hist_entry__sort_on_period(he, iter) > 0)
663 rb_link_node(&he->rb_node, parent, p);
664 rb_insert_color(&he->rb_node, entries);
667 void hists__output_resort(struct hists *hists)
669 struct rb_root *root;
670 struct rb_node *next;
671 struct hist_entry *n;
672 u64 min_callchain_hits;
674 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
676 if (sort__need_collapse)
677 root = &hists->entries_collapsed;
679 root = hists->entries_in;
681 next = rb_first(root);
682 hists->entries = RB_ROOT;
684 hists->nr_entries = 0;
685 hists->stats.total_period = 0;
686 hists__reset_col_len(hists);
689 n = rb_entry(next, struct hist_entry, rb_node_in);
690 next = rb_next(&n->rb_node_in);
692 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
693 hists__inc_nr_entries(hists, n);
697 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
698 enum hist_filter filter)
700 h->filtered &= ~(1 << filter);
706 hists->nr_entries += h->nr_rows;
708 hists->stats.total_period += h->stat.period;
709 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
711 hists__calc_col_len(hists, h);
715 static bool hists__filter_entry_by_dso(struct hists *hists,
716 struct hist_entry *he)
718 if (hists->dso_filter != NULL &&
719 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
720 he->filtered |= (1 << HIST_FILTER__DSO);
727 void hists__filter_by_dso(struct hists *hists)
731 hists->nr_entries = hists->stats.total_period = 0;
732 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
733 hists__reset_col_len(hists);
735 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
736 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
738 if (symbol_conf.exclude_other && !h->parent)
741 if (hists__filter_entry_by_dso(hists, h))
744 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
748 static bool hists__filter_entry_by_thread(struct hists *hists,
749 struct hist_entry *he)
751 if (hists->thread_filter != NULL &&
752 he->thread != hists->thread_filter) {
753 he->filtered |= (1 << HIST_FILTER__THREAD);
760 void hists__filter_by_thread(struct hists *hists)
764 hists->nr_entries = hists->stats.total_period = 0;
765 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 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_entries = hists->stats.total_period = 0;
796 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
797 hists__reset_col_len(hists);
799 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
800 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
802 if (hists__filter_entry_by_symbol(hists, h))
805 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
809 void events_stats__inc(struct events_stats *stats, u32 type)
811 ++stats->nr_events[0];
812 ++stats->nr_events[type];
815 void hists__inc_nr_events(struct hists *hists, u32 type)
817 events_stats__inc(&hists->stats, type);
820 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
821 struct hist_entry *pair)
823 struct rb_root *root;
825 struct rb_node *parent = NULL;
826 struct hist_entry *he;
829 if (sort__need_collapse)
830 root = &hists->entries_collapsed;
832 root = hists->entries_in;
838 he = rb_entry(parent, struct hist_entry, rb_node_in);
840 cmp = hist_entry__collapse(he, pair);
851 he = hist_entry__new(pair);
853 memset(&he->stat, 0, sizeof(he->stat));
855 rb_link_node(&he->rb_node_in, parent, p);
856 rb_insert_color(&he->rb_node_in, root);
857 hists__inc_nr_entries(hists, he);
864 static struct hist_entry *hists__find_entry(struct hists *hists,
865 struct hist_entry *he)
869 if (sort__need_collapse)
870 n = hists->entries_collapsed.rb_node;
872 n = hists->entries_in->rb_node;
875 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
876 int64_t cmp = hist_entry__collapse(iter, he);
890 * Look for pairs to link to the leader buckets (hist_entries):
892 void hists__match(struct hists *leader, struct hists *other)
894 struct rb_root *root;
896 struct hist_entry *pos, *pair;
898 if (sort__need_collapse)
899 root = &leader->entries_collapsed;
901 root = leader->entries_in;
903 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
904 pos = rb_entry(nd, struct hist_entry, rb_node_in);
905 pair = hists__find_entry(other, pos);
908 hist_entry__add_pair(pair, pos);
913 * Look for entries in the other hists that are not present in the leader, if
914 * we find them, just add a dummy entry on the leader hists, with period=0,
915 * nr_events=0, to serve as the list header.
917 int hists__link(struct hists *leader, struct hists *other)
919 struct rb_root *root;
921 struct hist_entry *pos, *pair;
923 if (sort__need_collapse)
924 root = &other->entries_collapsed;
926 root = other->entries_in;
928 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
929 pos = rb_entry(nd, struct hist_entry, rb_node_in);
931 if (!hist_entry__has_pairs(pos)) {
932 pair = hists__add_dummy_entry(leader, pos);
935 hist_entry__add_pair(pos, pair);