9 #include "ui/progress.h"
12 static bool hists__filter_entry_by_dso(struct hists *hists,
13 struct hist_entry *he);
14 static bool hists__filter_entry_by_thread(struct hists *hists,
15 struct hist_entry *he);
16 static bool hists__filter_entry_by_symbol(struct hists *hists,
17 struct hist_entry *he);
19 u16 hists__col_len(struct hists *hists, enum hist_column col)
21 return hists->col_len[col];
24 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
26 hists->col_len[col] = len;
29 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
31 if (len > hists__col_len(hists, col)) {
32 hists__set_col_len(hists, col, len);
38 void hists__reset_col_len(struct hists *hists)
42 for (col = 0; col < HISTC_NR_COLS; ++col)
43 hists__set_col_len(hists, col, 0);
46 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
48 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
50 if (hists__col_len(hists, dso) < unresolved_col_width &&
51 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
52 !symbol_conf.dso_list)
53 hists__set_col_len(hists, dso, unresolved_col_width);
56 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
58 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
63 * +4 accounts for '[x] ' priv level info
64 * +2 accounts for 0x prefix on raw addresses
65 * +3 accounts for ' y ' symtab origin info
68 symlen = h->ms.sym->namelen + 4;
70 symlen += BITS_PER_LONG / 4 + 2 + 3;
71 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
73 symlen = unresolved_col_width + 4 + 2;
74 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
75 hists__set_unres_dso_col_len(hists, HISTC_DSO);
78 len = thread__comm_len(h->thread);
79 if (hists__new_col_len(hists, HISTC_COMM, len))
80 hists__set_col_len(hists, HISTC_THREAD, len + 6);
83 len = dso__name_len(h->ms.map->dso);
84 hists__new_col_len(hists, HISTC_DSO, len);
88 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
91 if (h->branch_info->from.sym) {
92 symlen = (int)h->branch_info->from.sym->namelen + 4;
94 symlen += BITS_PER_LONG / 4 + 2 + 3;
95 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
97 symlen = dso__name_len(h->branch_info->from.map->dso);
98 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
100 symlen = unresolved_col_width + 4 + 2;
101 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
102 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
105 if (h->branch_info->to.sym) {
106 symlen = (int)h->branch_info->to.sym->namelen + 4;
108 symlen += BITS_PER_LONG / 4 + 2 + 3;
109 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
111 symlen = dso__name_len(h->branch_info->to.map->dso);
112 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
114 symlen = unresolved_col_width + 4 + 2;
115 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
116 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
121 if (h->mem_info->daddr.sym) {
122 symlen = (int)h->mem_info->daddr.sym->namelen + 4
123 + unresolved_col_width + 2;
124 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
126 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
129 symlen = unresolved_col_width + 4 + 2;
130 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
133 if (h->mem_info->daddr.map) {
134 symlen = dso__name_len(h->mem_info->daddr.map->dso);
135 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
138 symlen = unresolved_col_width + 4 + 2;
139 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
144 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
147 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
148 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
149 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
150 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
151 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
152 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
155 hists__new_col_len(hists, HISTC_TRANSACTION,
156 hist_entry__transaction_len());
159 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
161 struct rb_node *next = rb_first(&hists->entries);
162 struct hist_entry *n;
165 hists__reset_col_len(hists);
167 while (next && row++ < max_rows) {
168 n = rb_entry(next, struct hist_entry, rb_node);
170 hists__calc_col_len(hists, n);
171 next = rb_next(&n->rb_node);
175 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
176 unsigned int cpumode, u64 period)
179 case PERF_RECORD_MISC_KERNEL:
180 he_stat->period_sys += period;
182 case PERF_RECORD_MISC_USER:
183 he_stat->period_us += period;
185 case PERF_RECORD_MISC_GUEST_KERNEL:
186 he_stat->period_guest_sys += period;
188 case PERF_RECORD_MISC_GUEST_USER:
189 he_stat->period_guest_us += period;
196 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
200 he_stat->period += period;
201 he_stat->weight += weight;
202 he_stat->nr_events += 1;
205 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
207 dest->period += src->period;
208 dest->period_sys += src->period_sys;
209 dest->period_us += src->period_us;
210 dest->period_guest_sys += src->period_guest_sys;
211 dest->period_guest_us += src->period_guest_us;
212 dest->nr_events += src->nr_events;
213 dest->weight += src->weight;
216 static void he_stat__decay(struct he_stat *he_stat)
218 he_stat->period = (he_stat->period * 7) / 8;
219 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
220 /* XXX need decay for weight too? */
223 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
225 u64 prev_period = he->stat.period;
228 if (prev_period == 0)
231 he_stat__decay(&he->stat);
232 if (symbol_conf.cumulate_callchain)
233 he_stat__decay(he->stat_acc);
235 diff = prev_period - he->stat.period;
237 hists->stats.total_period -= diff;
239 hists->stats.total_non_filtered_period -= diff;
241 return he->stat.period == 0;
244 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
246 rb_erase(&he->rb_node, &hists->entries);
248 if (sort__need_collapse)
249 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
253 --hists->nr_non_filtered_entries;
255 hist_entry__delete(he);
258 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
260 struct rb_node *next = rb_first(&hists->entries);
261 struct hist_entry *n;
264 n = rb_entry(next, struct hist_entry, rb_node);
265 next = rb_next(&n->rb_node);
266 if (((zap_user && n->level == '.') ||
267 (zap_kernel && n->level != '.') ||
268 hists__decay_entry(hists, n))) {
269 hists__delete_entry(hists, n);
274 void hists__delete_entries(struct hists *hists)
276 struct rb_node *next = rb_first(&hists->entries);
277 struct hist_entry *n;
280 n = rb_entry(next, struct hist_entry, rb_node);
281 next = rb_next(&n->rb_node);
283 hists__delete_entry(hists, n);
288 * histogram, sorted on item, collects periods
291 static struct hist_entry *hist_entry__new(struct hist_entry *template,
294 size_t callchain_size = 0;
295 struct hist_entry *he;
297 if (symbol_conf.use_callchain)
298 callchain_size = sizeof(struct callchain_root);
300 he = zalloc(sizeof(*he) + callchain_size);
305 if (symbol_conf.cumulate_callchain) {
306 he->stat_acc = malloc(sizeof(he->stat));
307 if (he->stat_acc == NULL) {
311 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
313 memset(&he->stat, 0, sizeof(he->stat));
316 map__get(he->ms.map);
318 if (he->branch_info) {
320 * This branch info is (a part of) allocated from
321 * sample__resolve_bstack() and will be freed after
322 * adding new entries. So we need to save a copy.
324 he->branch_info = malloc(sizeof(*he->branch_info));
325 if (he->branch_info == NULL) {
326 map__zput(he->ms.map);
332 memcpy(he->branch_info, template->branch_info,
333 sizeof(*he->branch_info));
335 map__get(he->branch_info->from.map);
336 map__get(he->branch_info->to.map);
340 map__get(he->mem_info->iaddr.map);
341 map__get(he->mem_info->daddr.map);
344 if (symbol_conf.use_callchain)
345 callchain_init(he->callchain);
347 INIT_LIST_HEAD(&he->pairs.node);
348 thread__get(he->thread);
354 static u8 symbol__parent_filter(const struct symbol *parent)
356 if (symbol_conf.exclude_other && parent == NULL)
357 return 1 << HIST_FILTER__PARENT;
361 static struct hist_entry *hists__findnew_entry(struct hists *hists,
362 struct hist_entry *entry,
363 struct addr_location *al,
367 struct rb_node *parent = NULL;
368 struct hist_entry *he;
370 u64 period = entry->stat.period;
371 u64 weight = entry->stat.weight;
373 p = &hists->entries_in->rb_node;
377 he = rb_entry(parent, struct hist_entry, rb_node_in);
380 * Make sure that it receives arguments in a same order as
381 * hist_entry__collapse() so that we can use an appropriate
382 * function when searching an entry regardless which sort
385 cmp = hist_entry__cmp(he, entry);
389 he_stat__add_period(&he->stat, period, weight);
390 if (symbol_conf.cumulate_callchain)
391 he_stat__add_period(he->stat_acc, period, weight);
394 * This mem info was allocated from sample__resolve_mem
395 * and will not be used anymore.
397 zfree(&entry->mem_info);
399 /* If the map of an existing hist_entry has
400 * become out-of-date due to an exec() or
401 * similar, update it. Otherwise we will
402 * mis-adjust symbol addresses when computing
403 * the history counter to increment.
405 if (he->ms.map != entry->ms.map) {
406 map__put(he->ms.map);
407 he->ms.map = map__get(entry->ms.map);
418 he = hist_entry__new(entry, sample_self);
424 rb_link_node(&he->rb_node_in, parent, p);
425 rb_insert_color(&he->rb_node_in, hists->entries_in);
428 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
429 if (symbol_conf.cumulate_callchain)
430 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
434 struct hist_entry *__hists__add_entry(struct hists *hists,
435 struct addr_location *al,
436 struct symbol *sym_parent,
437 struct branch_info *bi,
439 u64 period, u64 weight, u64 transaction,
442 struct hist_entry entry = {
443 .thread = al->thread,
444 .comm = thread__comm(al->thread),
450 .cpumode = al->cpumode,
458 .parent = sym_parent,
459 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
463 .transaction = transaction,
466 return hists__findnew_entry(hists, &entry, al, sample_self);
470 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
471 struct addr_location *al __maybe_unused)
477 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
478 struct addr_location *al __maybe_unused)
484 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
486 struct perf_sample *sample = iter->sample;
489 mi = sample__resolve_mem(sample, al);
498 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
501 struct mem_info *mi = iter->priv;
502 struct hists *hists = evsel__hists(iter->evsel);
503 struct hist_entry *he;
508 cost = iter->sample->weight;
513 * must pass period=weight in order to get the correct
514 * sorting from hists__collapse_resort() which is solely
515 * based on periods. We want sorting be done on nr_events * weight
516 * and this is indirectly achieved by passing period=weight here
517 * and the he_stat__add_period() function.
519 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
520 cost, cost, 0, true);
529 iter_finish_mem_entry(struct hist_entry_iter *iter,
530 struct addr_location *al __maybe_unused)
532 struct perf_evsel *evsel = iter->evsel;
533 struct hists *hists = evsel__hists(evsel);
534 struct hist_entry *he = iter->he;
540 hists__inc_nr_samples(hists, he->filtered);
542 err = hist_entry__append_callchain(he, iter->sample);
546 * We don't need to free iter->priv (mem_info) here since the mem info
547 * was either already freed in hists__findnew_entry() or passed to a
548 * new hist entry by hist_entry__new().
557 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
559 struct branch_info *bi;
560 struct perf_sample *sample = iter->sample;
562 bi = sample__resolve_bstack(sample, al);
567 iter->total = sample->branch_stack->nr;
574 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
575 struct addr_location *al __maybe_unused)
577 /* to avoid calling callback function */
584 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
586 struct branch_info *bi = iter->priv;
592 if (iter->curr >= iter->total)
595 al->map = bi[i].to.map;
596 al->sym = bi[i].to.sym;
597 al->addr = bi[i].to.addr;
602 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
604 struct branch_info *bi;
605 struct perf_evsel *evsel = iter->evsel;
606 struct hists *hists = evsel__hists(evsel);
607 struct hist_entry *he = NULL;
613 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
617 * The report shows the percentage of total branches captured
618 * and not events sampled. Thus we use a pseudo period of 1.
620 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
625 hists__inc_nr_samples(hists, he->filtered);
634 iter_finish_branch_entry(struct hist_entry_iter *iter,
635 struct addr_location *al __maybe_unused)
640 return iter->curr >= iter->total ? 0 : -1;
644 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
645 struct addr_location *al __maybe_unused)
651 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
653 struct perf_evsel *evsel = iter->evsel;
654 struct perf_sample *sample = iter->sample;
655 struct hist_entry *he;
657 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
658 sample->period, sample->weight,
659 sample->transaction, true);
668 iter_finish_normal_entry(struct hist_entry_iter *iter,
669 struct addr_location *al __maybe_unused)
671 struct hist_entry *he = iter->he;
672 struct perf_evsel *evsel = iter->evsel;
673 struct perf_sample *sample = iter->sample;
680 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
682 return hist_entry__append_callchain(he, sample);
686 iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
687 struct addr_location *al __maybe_unused)
689 struct hist_entry **he_cache;
691 callchain_cursor_commit(&callchain_cursor);
694 * This is for detecting cycles or recursions so that they're
695 * cumulated only one time to prevent entries more than 100%
698 he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
699 if (he_cache == NULL)
702 iter->priv = he_cache;
709 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
710 struct addr_location *al)
712 struct perf_evsel *evsel = iter->evsel;
713 struct hists *hists = evsel__hists(evsel);
714 struct perf_sample *sample = iter->sample;
715 struct hist_entry **he_cache = iter->priv;
716 struct hist_entry *he;
719 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
720 sample->period, sample->weight,
721 sample->transaction, true);
726 he_cache[iter->curr++] = he;
728 hist_entry__append_callchain(he, sample);
731 * We need to re-initialize the cursor since callchain_append()
732 * advanced the cursor to the end.
734 callchain_cursor_commit(&callchain_cursor);
736 hists__inc_nr_samples(hists, he->filtered);
742 iter_next_cumulative_entry(struct hist_entry_iter *iter,
743 struct addr_location *al)
745 struct callchain_cursor_node *node;
747 node = callchain_cursor_current(&callchain_cursor);
751 return fill_callchain_info(al, node, iter->hide_unresolved);
755 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
756 struct addr_location *al)
758 struct perf_evsel *evsel = iter->evsel;
759 struct perf_sample *sample = iter->sample;
760 struct hist_entry **he_cache = iter->priv;
761 struct hist_entry *he;
762 struct hist_entry he_tmp = {
764 .thread = al->thread,
765 .comm = thread__comm(al->thread),
771 .parent = iter->parent,
774 struct callchain_cursor cursor;
776 callchain_cursor_snapshot(&cursor, &callchain_cursor);
778 callchain_cursor_advance(&callchain_cursor);
781 * Check if there's duplicate entries in the callchain.
782 * It's possible that it has cycles or recursive calls.
784 for (i = 0; i < iter->curr; i++) {
785 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
786 /* to avoid calling callback function */
792 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
793 sample->period, sample->weight,
794 sample->transaction, false);
799 he_cache[iter->curr++] = he;
801 if (symbol_conf.use_callchain)
802 callchain_append(he->callchain, &cursor, sample->period);
807 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
808 struct addr_location *al __maybe_unused)
816 const struct hist_iter_ops hist_iter_mem = {
817 .prepare_entry = iter_prepare_mem_entry,
818 .add_single_entry = iter_add_single_mem_entry,
819 .next_entry = iter_next_nop_entry,
820 .add_next_entry = iter_add_next_nop_entry,
821 .finish_entry = iter_finish_mem_entry,
824 const struct hist_iter_ops hist_iter_branch = {
825 .prepare_entry = iter_prepare_branch_entry,
826 .add_single_entry = iter_add_single_branch_entry,
827 .next_entry = iter_next_branch_entry,
828 .add_next_entry = iter_add_next_branch_entry,
829 .finish_entry = iter_finish_branch_entry,
832 const struct hist_iter_ops hist_iter_normal = {
833 .prepare_entry = iter_prepare_normal_entry,
834 .add_single_entry = iter_add_single_normal_entry,
835 .next_entry = iter_next_nop_entry,
836 .add_next_entry = iter_add_next_nop_entry,
837 .finish_entry = iter_finish_normal_entry,
840 const struct hist_iter_ops hist_iter_cumulative = {
841 .prepare_entry = iter_prepare_cumulative_entry,
842 .add_single_entry = iter_add_single_cumulative_entry,
843 .next_entry = iter_next_cumulative_entry,
844 .add_next_entry = iter_add_next_cumulative_entry,
845 .finish_entry = iter_finish_cumulative_entry,
848 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
849 int max_stack_depth, void *arg)
853 err = sample__resolve_callchain(iter->sample, &iter->parent,
854 iter->evsel, al, max_stack_depth);
858 err = iter->ops->prepare_entry(iter, al);
862 err = iter->ops->add_single_entry(iter, al);
866 if (iter->he && iter->add_entry_cb) {
867 err = iter->add_entry_cb(iter, al, true, arg);
872 while (iter->ops->next_entry(iter, al)) {
873 err = iter->ops->add_next_entry(iter, al);
877 if (iter->he && iter->add_entry_cb) {
878 err = iter->add_entry_cb(iter, al, false, arg);
885 err2 = iter->ops->finish_entry(iter, al);
893 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
895 struct perf_hpp_fmt *fmt;
898 perf_hpp__for_each_sort_list(fmt) {
899 if (perf_hpp__should_skip(fmt))
902 cmp = fmt->cmp(fmt, left, right);
911 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
913 struct perf_hpp_fmt *fmt;
916 perf_hpp__for_each_sort_list(fmt) {
917 if (perf_hpp__should_skip(fmt))
920 cmp = fmt->collapse(fmt, left, right);
928 void hist_entry__delete(struct hist_entry *he)
930 thread__zput(he->thread);
931 map__zput(he->ms.map);
933 if (he->branch_info) {
934 map__zput(he->branch_info->from.map);
935 map__zput(he->branch_info->to.map);
936 zfree(&he->branch_info);
940 map__zput(he->mem_info->iaddr.map);
941 map__zput(he->mem_info->daddr.map);
942 zfree(&he->mem_info);
945 zfree(&he->stat_acc);
946 free_srcline(he->srcline);
947 free_callchain(he->callchain);
952 * collapse the histogram
955 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
956 struct rb_root *root,
957 struct hist_entry *he)
959 struct rb_node **p = &root->rb_node;
960 struct rb_node *parent = NULL;
961 struct hist_entry *iter;
966 iter = rb_entry(parent, struct hist_entry, rb_node_in);
968 cmp = hist_entry__collapse(iter, he);
971 he_stat__add_stat(&iter->stat, &he->stat);
972 if (symbol_conf.cumulate_callchain)
973 he_stat__add_stat(iter->stat_acc, he->stat_acc);
975 if (symbol_conf.use_callchain) {
976 callchain_cursor_reset(&callchain_cursor);
977 callchain_merge(&callchain_cursor,
981 hist_entry__delete(he);
992 rb_link_node(&he->rb_node_in, parent, p);
993 rb_insert_color(&he->rb_node_in, root);
997 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
999 struct rb_root *root;
1001 pthread_mutex_lock(&hists->lock);
1003 root = hists->entries_in;
1004 if (++hists->entries_in > &hists->entries_in_array[1])
1005 hists->entries_in = &hists->entries_in_array[0];
1007 pthread_mutex_unlock(&hists->lock);
1012 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1014 hists__filter_entry_by_dso(hists, he);
1015 hists__filter_entry_by_thread(hists, he);
1016 hists__filter_entry_by_symbol(hists, he);
1019 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1021 struct rb_root *root;
1022 struct rb_node *next;
1023 struct hist_entry *n;
1025 if (!sort__need_collapse)
1028 hists->nr_entries = 0;
1030 root = hists__get_rotate_entries_in(hists);
1032 next = rb_first(root);
1037 n = rb_entry(next, struct hist_entry, rb_node_in);
1038 next = rb_next(&n->rb_node_in);
1040 rb_erase(&n->rb_node_in, root);
1041 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1043 * If it wasn't combined with one of the entries already
1044 * collapsed, we need to apply the filters that may have
1045 * been set by, say, the hist_browser.
1047 hists__apply_filters(hists, n);
1050 ui_progress__update(prog, 1);
1054 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1056 struct perf_hpp_fmt *fmt;
1059 perf_hpp__for_each_sort_list(fmt) {
1060 if (perf_hpp__should_skip(fmt))
1063 cmp = fmt->sort(fmt, a, b);
1071 static void hists__reset_filter_stats(struct hists *hists)
1073 hists->nr_non_filtered_entries = 0;
1074 hists->stats.total_non_filtered_period = 0;
1077 void hists__reset_stats(struct hists *hists)
1079 hists->nr_entries = 0;
1080 hists->stats.total_period = 0;
1082 hists__reset_filter_stats(hists);
1085 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1087 hists->nr_non_filtered_entries++;
1088 hists->stats.total_non_filtered_period += h->stat.period;
1091 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1094 hists__inc_filter_stats(hists, h);
1096 hists->nr_entries++;
1097 hists->stats.total_period += h->stat.period;
1100 static void __hists__insert_output_entry(struct rb_root *entries,
1101 struct hist_entry *he,
1102 u64 min_callchain_hits)
1104 struct rb_node **p = &entries->rb_node;
1105 struct rb_node *parent = NULL;
1106 struct hist_entry *iter;
1108 if (symbol_conf.use_callchain)
1109 callchain_param.sort(&he->sorted_chain, he->callchain,
1110 min_callchain_hits, &callchain_param);
1112 while (*p != NULL) {
1114 iter = rb_entry(parent, struct hist_entry, rb_node);
1116 if (hist_entry__sort(he, iter) > 0)
1119 p = &(*p)->rb_right;
1122 rb_link_node(&he->rb_node, parent, p);
1123 rb_insert_color(&he->rb_node, entries);
1126 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1128 struct rb_root *root;
1129 struct rb_node *next;
1130 struct hist_entry *n;
1131 u64 min_callchain_hits;
1133 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
1135 if (sort__need_collapse)
1136 root = &hists->entries_collapsed;
1138 root = hists->entries_in;
1140 next = rb_first(root);
1141 hists->entries = RB_ROOT;
1143 hists__reset_stats(hists);
1144 hists__reset_col_len(hists);
1147 n = rb_entry(next, struct hist_entry, rb_node_in);
1148 next = rb_next(&n->rb_node_in);
1150 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
1151 hists__inc_stats(hists, n);
1154 hists__calc_col_len(hists, n);
1157 ui_progress__update(prog, 1);
1161 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1162 enum hist_filter filter)
1164 h->filtered &= ~(1 << filter);
1168 /* force fold unfiltered entry for simplicity */
1169 h->unfolded = false;
1173 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1175 hists__inc_filter_stats(hists, h);
1176 hists__calc_col_len(hists, h);
1180 static bool hists__filter_entry_by_dso(struct hists *hists,
1181 struct hist_entry *he)
1183 if (hists->dso_filter != NULL &&
1184 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1185 he->filtered |= (1 << HIST_FILTER__DSO);
1192 void hists__filter_by_dso(struct hists *hists)
1196 hists->stats.nr_non_filtered_samples = 0;
1198 hists__reset_filter_stats(hists);
1199 hists__reset_col_len(hists);
1201 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1202 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1204 if (symbol_conf.exclude_other && !h->parent)
1207 if (hists__filter_entry_by_dso(hists, h))
1210 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1214 static bool hists__filter_entry_by_thread(struct hists *hists,
1215 struct hist_entry *he)
1217 if (hists->thread_filter != NULL &&
1218 he->thread != hists->thread_filter) {
1219 he->filtered |= (1 << HIST_FILTER__THREAD);
1226 void hists__filter_by_thread(struct hists *hists)
1230 hists->stats.nr_non_filtered_samples = 0;
1232 hists__reset_filter_stats(hists);
1233 hists__reset_col_len(hists);
1235 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1236 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1238 if (hists__filter_entry_by_thread(hists, h))
1241 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1245 static bool hists__filter_entry_by_symbol(struct hists *hists,
1246 struct hist_entry *he)
1248 if (hists->symbol_filter_str != NULL &&
1249 (!he->ms.sym || strstr(he->ms.sym->name,
1250 hists->symbol_filter_str) == NULL)) {
1251 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1258 void hists__filter_by_symbol(struct hists *hists)
1262 hists->stats.nr_non_filtered_samples = 0;
1264 hists__reset_filter_stats(hists);
1265 hists__reset_col_len(hists);
1267 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1268 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1270 if (hists__filter_entry_by_symbol(hists, h))
1273 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1277 void events_stats__inc(struct events_stats *stats, u32 type)
1279 ++stats->nr_events[0];
1280 ++stats->nr_events[type];
1283 void hists__inc_nr_events(struct hists *hists, u32 type)
1285 events_stats__inc(&hists->stats, type);
1288 void hists__inc_nr_samples(struct hists *hists, bool filtered)
1290 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1292 hists->stats.nr_non_filtered_samples++;
1295 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1296 struct hist_entry *pair)
1298 struct rb_root *root;
1300 struct rb_node *parent = NULL;
1301 struct hist_entry *he;
1304 if (sort__need_collapse)
1305 root = &hists->entries_collapsed;
1307 root = hists->entries_in;
1311 while (*p != NULL) {
1313 he = rb_entry(parent, struct hist_entry, rb_node_in);
1315 cmp = hist_entry__collapse(he, pair);
1323 p = &(*p)->rb_right;
1326 he = hist_entry__new(pair, true);
1328 memset(&he->stat, 0, sizeof(he->stat));
1330 rb_link_node(&he->rb_node_in, parent, p);
1331 rb_insert_color(&he->rb_node_in, root);
1332 hists__inc_stats(hists, he);
1339 static struct hist_entry *hists__find_entry(struct hists *hists,
1340 struct hist_entry *he)
1344 if (sort__need_collapse)
1345 n = hists->entries_collapsed.rb_node;
1347 n = hists->entries_in->rb_node;
1350 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1351 int64_t cmp = hist_entry__collapse(iter, he);
1365 * Look for pairs to link to the leader buckets (hist_entries):
1367 void hists__match(struct hists *leader, struct hists *other)
1369 struct rb_root *root;
1371 struct hist_entry *pos, *pair;
1373 if (sort__need_collapse)
1374 root = &leader->entries_collapsed;
1376 root = leader->entries_in;
1378 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1379 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1380 pair = hists__find_entry(other, pos);
1383 hist_entry__add_pair(pair, pos);
1388 * Look for entries in the other hists that are not present in the leader, if
1389 * we find them, just add a dummy entry on the leader hists, with period=0,
1390 * nr_events=0, to serve as the list header.
1392 int hists__link(struct hists *leader, struct hists *other)
1394 struct rb_root *root;
1396 struct hist_entry *pos, *pair;
1398 if (sort__need_collapse)
1399 root = &other->entries_collapsed;
1401 root = other->entries_in;
1403 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1404 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1406 if (!hist_entry__has_pairs(pos)) {
1407 pair = hists__add_dummy_entry(leader, pos);
1410 hist_entry__add_pair(pos, pair);
1418 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1420 struct perf_evsel *pos;
1423 evlist__for_each(evlist, pos) {
1424 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1425 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1432 u64 hists__total_period(struct hists *hists)
1434 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1435 hists->stats.total_period;
1438 int parse_filter_percentage(const struct option *opt __maybe_unused,
1439 const char *arg, int unset __maybe_unused)
1441 if (!strcmp(arg, "relative"))
1442 symbol_conf.filter_relative = true;
1443 else if (!strcmp(arg, "absolute"))
1444 symbol_conf.filter_relative = false;
1451 int perf_hist_config(const char *var, const char *value)
1453 if (!strcmp(var, "hist.percentage"))
1454 return parse_filter_percentage(NULL, value, 0);
1459 static int hists_evsel__init(struct perf_evsel *evsel)
1461 struct hists *hists = evsel__hists(evsel);
1463 memset(hists, 0, sizeof(*hists));
1464 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1465 hists->entries_in = &hists->entries_in_array[0];
1466 hists->entries_collapsed = RB_ROOT;
1467 hists->entries = RB_ROOT;
1468 pthread_mutex_init(&hists->lock, NULL);
1473 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1474 * stored in the rbtree...
1477 int hists__init(void)
1479 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1480 hists_evsel__init, NULL);
1482 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);