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);
18 static bool hists__filter_entry_by_socket(struct hists *hists,
19 struct hist_entry *he);
21 u16 hists__col_len(struct hists *hists, enum hist_column col)
23 return hists->col_len[col];
26 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
28 hists->col_len[col] = len;
31 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
33 if (len > hists__col_len(hists, col)) {
34 hists__set_col_len(hists, col, len);
40 void hists__reset_col_len(struct hists *hists)
44 for (col = 0; col < HISTC_NR_COLS; ++col)
45 hists__set_col_len(hists, col, 0);
48 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
50 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
52 if (hists__col_len(hists, dso) < unresolved_col_width &&
53 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
54 !symbol_conf.dso_list)
55 hists__set_col_len(hists, dso, unresolved_col_width);
58 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
60 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
65 * +4 accounts for '[x] ' priv level info
66 * +2 accounts for 0x prefix on raw addresses
67 * +3 accounts for ' y ' symtab origin info
70 symlen = h->ms.sym->namelen + 4;
72 symlen += BITS_PER_LONG / 4 + 2 + 3;
73 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
75 symlen = unresolved_col_width + 4 + 2;
76 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
77 hists__set_unres_dso_col_len(hists, HISTC_DSO);
80 len = thread__comm_len(h->thread);
81 if (hists__new_col_len(hists, HISTC_COMM, len))
82 hists__set_col_len(hists, HISTC_THREAD, len + 6);
85 len = dso__name_len(h->ms.map->dso);
86 hists__new_col_len(hists, HISTC_DSO, len);
90 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
93 if (h->branch_info->from.sym) {
94 symlen = (int)h->branch_info->from.sym->namelen + 4;
96 symlen += BITS_PER_LONG / 4 + 2 + 3;
97 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
99 symlen = dso__name_len(h->branch_info->from.map->dso);
100 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
102 symlen = unresolved_col_width + 4 + 2;
103 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
107 if (h->branch_info->to.sym) {
108 symlen = (int)h->branch_info->to.sym->namelen + 4;
110 symlen += BITS_PER_LONG / 4 + 2 + 3;
111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
113 symlen = dso__name_len(h->branch_info->to.map->dso);
114 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
116 symlen = unresolved_col_width + 4 + 2;
117 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
118 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
123 if (h->mem_info->daddr.sym) {
124 symlen = (int)h->mem_info->daddr.sym->namelen + 4
125 + unresolved_col_width + 2;
126 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
128 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
131 symlen = unresolved_col_width + 4 + 2;
132 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
136 if (h->mem_info->iaddr.sym) {
137 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
138 + unresolved_col_width + 2;
139 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
147 if (h->mem_info->daddr.map) {
148 symlen = dso__name_len(h->mem_info->daddr.map->dso);
149 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
152 symlen = unresolved_col_width + 4 + 2;
153 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
156 symlen = unresolved_col_width + 4 + 2;
157 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
158 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
159 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
162 hists__new_col_len(hists, HISTC_CPU, 3);
163 hists__new_col_len(hists, HISTC_SOCKET, 6);
164 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
165 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
166 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
167 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
168 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
169 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
172 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
175 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
178 hists__new_col_len(hists, HISTC_TRANSACTION,
179 hist_entry__transaction_len());
182 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
184 struct rb_node *next = rb_first(&hists->entries);
185 struct hist_entry *n;
188 hists__reset_col_len(hists);
190 while (next && row++ < max_rows) {
191 n = rb_entry(next, struct hist_entry, rb_node);
193 hists__calc_col_len(hists, n);
194 next = rb_next(&n->rb_node);
198 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
199 unsigned int cpumode, u64 period)
202 case PERF_RECORD_MISC_KERNEL:
203 he_stat->period_sys += period;
205 case PERF_RECORD_MISC_USER:
206 he_stat->period_us += period;
208 case PERF_RECORD_MISC_GUEST_KERNEL:
209 he_stat->period_guest_sys += period;
211 case PERF_RECORD_MISC_GUEST_USER:
212 he_stat->period_guest_us += period;
219 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
223 he_stat->period += period;
224 he_stat->weight += weight;
225 he_stat->nr_events += 1;
228 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
230 dest->period += src->period;
231 dest->period_sys += src->period_sys;
232 dest->period_us += src->period_us;
233 dest->period_guest_sys += src->period_guest_sys;
234 dest->period_guest_us += src->period_guest_us;
235 dest->nr_events += src->nr_events;
236 dest->weight += src->weight;
239 static void he_stat__decay(struct he_stat *he_stat)
241 he_stat->period = (he_stat->period * 7) / 8;
242 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
243 /* XXX need decay for weight too? */
246 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
248 u64 prev_period = he->stat.period;
251 if (prev_period == 0)
254 he_stat__decay(&he->stat);
255 if (symbol_conf.cumulate_callchain)
256 he_stat__decay(he->stat_acc);
258 diff = prev_period - he->stat.period;
260 hists->stats.total_period -= diff;
262 hists->stats.total_non_filtered_period -= diff;
264 return he->stat.period == 0;
267 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
269 rb_erase(&he->rb_node, &hists->entries);
271 if (sort__need_collapse)
272 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
274 rb_erase(&he->rb_node_in, hists->entries_in);
278 --hists->nr_non_filtered_entries;
280 hist_entry__delete(he);
283 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
285 struct rb_node *next = rb_first(&hists->entries);
286 struct hist_entry *n;
289 n = rb_entry(next, struct hist_entry, rb_node);
290 next = rb_next(&n->rb_node);
291 if (((zap_user && n->level == '.') ||
292 (zap_kernel && n->level != '.') ||
293 hists__decay_entry(hists, n))) {
294 hists__delete_entry(hists, n);
299 void hists__delete_entries(struct hists *hists)
301 struct rb_node *next = rb_first(&hists->entries);
302 struct hist_entry *n;
305 n = rb_entry(next, struct hist_entry, rb_node);
306 next = rb_next(&n->rb_node);
308 hists__delete_entry(hists, n);
313 * histogram, sorted on item, collects periods
316 static struct hist_entry *hist_entry__new(struct hist_entry *template,
319 size_t callchain_size = 0;
320 struct hist_entry *he;
322 if (symbol_conf.use_callchain)
323 callchain_size = sizeof(struct callchain_root);
325 he = zalloc(sizeof(*he) + callchain_size);
330 if (symbol_conf.cumulate_callchain) {
331 he->stat_acc = malloc(sizeof(he->stat));
332 if (he->stat_acc == NULL) {
336 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
338 memset(&he->stat, 0, sizeof(he->stat));
341 map__get(he->ms.map);
343 if (he->branch_info) {
345 * This branch info is (a part of) allocated from
346 * sample__resolve_bstack() and will be freed after
347 * adding new entries. So we need to save a copy.
349 he->branch_info = malloc(sizeof(*he->branch_info));
350 if (he->branch_info == NULL) {
351 map__zput(he->ms.map);
357 memcpy(he->branch_info, template->branch_info,
358 sizeof(*he->branch_info));
360 map__get(he->branch_info->from.map);
361 map__get(he->branch_info->to.map);
365 map__get(he->mem_info->iaddr.map);
366 map__get(he->mem_info->daddr.map);
369 if (symbol_conf.use_callchain)
370 callchain_init(he->callchain);
372 INIT_LIST_HEAD(&he->pairs.node);
373 thread__get(he->thread);
379 static u8 symbol__parent_filter(const struct symbol *parent)
381 if (symbol_conf.exclude_other && parent == NULL)
382 return 1 << HIST_FILTER__PARENT;
386 static struct hist_entry *hists__findnew_entry(struct hists *hists,
387 struct hist_entry *entry,
388 struct addr_location *al,
392 struct rb_node *parent = NULL;
393 struct hist_entry *he;
395 u64 period = entry->stat.period;
396 u64 weight = entry->stat.weight;
398 p = &hists->entries_in->rb_node;
402 he = rb_entry(parent, struct hist_entry, rb_node_in);
405 * Make sure that it receives arguments in a same order as
406 * hist_entry__collapse() so that we can use an appropriate
407 * function when searching an entry regardless which sort
410 cmp = hist_entry__cmp(he, entry);
414 he_stat__add_period(&he->stat, period, weight);
415 if (symbol_conf.cumulate_callchain)
416 he_stat__add_period(he->stat_acc, period, weight);
419 * This mem info was allocated from sample__resolve_mem
420 * and will not be used anymore.
422 zfree(&entry->mem_info);
424 /* If the map of an existing hist_entry has
425 * become out-of-date due to an exec() or
426 * similar, update it. Otherwise we will
427 * mis-adjust symbol addresses when computing
428 * the history counter to increment.
430 if (he->ms.map != entry->ms.map) {
431 map__put(he->ms.map);
432 he->ms.map = map__get(entry->ms.map);
443 he = hist_entry__new(entry, sample_self);
449 rb_link_node(&he->rb_node_in, parent, p);
450 rb_insert_color(&he->rb_node_in, hists->entries_in);
453 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
454 if (symbol_conf.cumulate_callchain)
455 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
459 struct hist_entry *__hists__add_entry(struct hists *hists,
460 struct addr_location *al,
461 struct symbol *sym_parent,
462 struct branch_info *bi,
464 u64 period, u64 weight, u64 transaction,
467 struct hist_entry entry = {
468 .thread = al->thread,
469 .comm = thread__comm(al->thread),
474 .socket = al->socket,
476 .cpumode = al->cpumode,
484 .parent = sym_parent,
485 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
489 .transaction = transaction,
492 return hists__findnew_entry(hists, &entry, al, sample_self);
496 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
497 struct addr_location *al __maybe_unused)
503 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
504 struct addr_location *al __maybe_unused)
510 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
512 struct perf_sample *sample = iter->sample;
515 mi = sample__resolve_mem(sample, al);
524 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
527 struct mem_info *mi = iter->priv;
528 struct hists *hists = evsel__hists(iter->evsel);
529 struct hist_entry *he;
534 cost = iter->sample->weight;
539 * must pass period=weight in order to get the correct
540 * sorting from hists__collapse_resort() which is solely
541 * based on periods. We want sorting be done on nr_events * weight
542 * and this is indirectly achieved by passing period=weight here
543 * and the he_stat__add_period() function.
545 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
546 cost, cost, 0, true);
555 iter_finish_mem_entry(struct hist_entry_iter *iter,
556 struct addr_location *al __maybe_unused)
558 struct perf_evsel *evsel = iter->evsel;
559 struct hists *hists = evsel__hists(evsel);
560 struct hist_entry *he = iter->he;
566 hists__inc_nr_samples(hists, he->filtered);
568 err = hist_entry__append_callchain(he, iter->sample);
572 * We don't need to free iter->priv (mem_info) here since the mem info
573 * was either already freed in hists__findnew_entry() or passed to a
574 * new hist entry by hist_entry__new().
583 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
585 struct branch_info *bi;
586 struct perf_sample *sample = iter->sample;
588 bi = sample__resolve_bstack(sample, al);
593 iter->total = sample->branch_stack->nr;
600 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
601 struct addr_location *al __maybe_unused)
603 /* to avoid calling callback function */
610 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
612 struct branch_info *bi = iter->priv;
618 if (iter->curr >= iter->total)
621 al->map = bi[i].to.map;
622 al->sym = bi[i].to.sym;
623 al->addr = bi[i].to.addr;
628 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
630 struct branch_info *bi;
631 struct perf_evsel *evsel = iter->evsel;
632 struct hists *hists = evsel__hists(evsel);
633 struct hist_entry *he = NULL;
639 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
643 * The report shows the percentage of total branches captured
644 * and not events sampled. Thus we use a pseudo period of 1.
646 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
647 1, bi->flags.cycles ? bi->flags.cycles : 1,
652 hists__inc_nr_samples(hists, he->filtered);
661 iter_finish_branch_entry(struct hist_entry_iter *iter,
662 struct addr_location *al __maybe_unused)
667 return iter->curr >= iter->total ? 0 : -1;
671 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
672 struct addr_location *al __maybe_unused)
678 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
680 struct perf_evsel *evsel = iter->evsel;
681 struct perf_sample *sample = iter->sample;
682 struct hist_entry *he;
684 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
685 sample->period, sample->weight,
686 sample->transaction, true);
695 iter_finish_normal_entry(struct hist_entry_iter *iter,
696 struct addr_location *al __maybe_unused)
698 struct hist_entry *he = iter->he;
699 struct perf_evsel *evsel = iter->evsel;
700 struct perf_sample *sample = iter->sample;
707 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
709 return hist_entry__append_callchain(he, sample);
713 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
714 struct addr_location *al __maybe_unused)
716 struct hist_entry **he_cache;
718 callchain_cursor_commit(&callchain_cursor);
721 * This is for detecting cycles or recursions so that they're
722 * cumulated only one time to prevent entries more than 100%
725 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
726 if (he_cache == NULL)
729 iter->priv = he_cache;
736 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
737 struct addr_location *al)
739 struct perf_evsel *evsel = iter->evsel;
740 struct hists *hists = evsel__hists(evsel);
741 struct perf_sample *sample = iter->sample;
742 struct hist_entry **he_cache = iter->priv;
743 struct hist_entry *he;
746 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
747 sample->period, sample->weight,
748 sample->transaction, true);
753 he_cache[iter->curr++] = he;
755 hist_entry__append_callchain(he, sample);
758 * We need to re-initialize the cursor since callchain_append()
759 * advanced the cursor to the end.
761 callchain_cursor_commit(&callchain_cursor);
763 hists__inc_nr_samples(hists, he->filtered);
769 iter_next_cumulative_entry(struct hist_entry_iter *iter,
770 struct addr_location *al)
772 struct callchain_cursor_node *node;
774 node = callchain_cursor_current(&callchain_cursor);
778 return fill_callchain_info(al, node, iter->hide_unresolved);
782 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
783 struct addr_location *al)
785 struct perf_evsel *evsel = iter->evsel;
786 struct perf_sample *sample = iter->sample;
787 struct hist_entry **he_cache = iter->priv;
788 struct hist_entry *he;
789 struct hist_entry he_tmp = {
790 .hists = evsel__hists(evsel),
792 .thread = al->thread,
793 .comm = thread__comm(al->thread),
799 .parent = iter->parent,
802 struct callchain_cursor cursor;
804 callchain_cursor_snapshot(&cursor, &callchain_cursor);
806 callchain_cursor_advance(&callchain_cursor);
809 * Check if there's duplicate entries in the callchain.
810 * It's possible that it has cycles or recursive calls.
812 for (i = 0; i < iter->curr; i++) {
813 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
814 /* to avoid calling callback function */
820 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
821 sample->period, sample->weight,
822 sample->transaction, false);
827 he_cache[iter->curr++] = he;
829 if (symbol_conf.use_callchain)
830 callchain_append(he->callchain, &cursor, sample->period);
835 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
836 struct addr_location *al __maybe_unused)
844 const struct hist_iter_ops hist_iter_mem = {
845 .prepare_entry = iter_prepare_mem_entry,
846 .add_single_entry = iter_add_single_mem_entry,
847 .next_entry = iter_next_nop_entry,
848 .add_next_entry = iter_add_next_nop_entry,
849 .finish_entry = iter_finish_mem_entry,
852 const struct hist_iter_ops hist_iter_branch = {
853 .prepare_entry = iter_prepare_branch_entry,
854 .add_single_entry = iter_add_single_branch_entry,
855 .next_entry = iter_next_branch_entry,
856 .add_next_entry = iter_add_next_branch_entry,
857 .finish_entry = iter_finish_branch_entry,
860 const struct hist_iter_ops hist_iter_normal = {
861 .prepare_entry = iter_prepare_normal_entry,
862 .add_single_entry = iter_add_single_normal_entry,
863 .next_entry = iter_next_nop_entry,
864 .add_next_entry = iter_add_next_nop_entry,
865 .finish_entry = iter_finish_normal_entry,
868 const struct hist_iter_ops hist_iter_cumulative = {
869 .prepare_entry = iter_prepare_cumulative_entry,
870 .add_single_entry = iter_add_single_cumulative_entry,
871 .next_entry = iter_next_cumulative_entry,
872 .add_next_entry = iter_add_next_cumulative_entry,
873 .finish_entry = iter_finish_cumulative_entry,
876 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
877 int max_stack_depth, void *arg)
881 err = sample__resolve_callchain(iter->sample, &iter->parent,
882 iter->evsel, al, max_stack_depth);
886 iter->max_stack = max_stack_depth;
888 err = iter->ops->prepare_entry(iter, al);
892 err = iter->ops->add_single_entry(iter, al);
896 if (iter->he && iter->add_entry_cb) {
897 err = iter->add_entry_cb(iter, al, true, arg);
902 while (iter->ops->next_entry(iter, al)) {
903 err = iter->ops->add_next_entry(iter, al);
907 if (iter->he && iter->add_entry_cb) {
908 err = iter->add_entry_cb(iter, al, false, arg);
915 err2 = iter->ops->finish_entry(iter, al);
923 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
925 struct perf_hpp_fmt *fmt;
928 perf_hpp__for_each_sort_list(fmt) {
929 cmp = fmt->cmp(fmt, left, right);
938 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
940 struct perf_hpp_fmt *fmt;
943 perf_hpp__for_each_sort_list(fmt) {
944 cmp = fmt->collapse(fmt, left, right);
952 void hist_entry__delete(struct hist_entry *he)
954 thread__zput(he->thread);
955 map__zput(he->ms.map);
957 if (he->branch_info) {
958 map__zput(he->branch_info->from.map);
959 map__zput(he->branch_info->to.map);
960 zfree(&he->branch_info);
964 map__zput(he->mem_info->iaddr.map);
965 map__zput(he->mem_info->daddr.map);
966 zfree(&he->mem_info);
969 zfree(&he->stat_acc);
970 free_srcline(he->srcline);
971 if (he->srcfile && he->srcfile[0])
973 free_callchain(he->callchain);
978 * collapse the histogram
981 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
982 struct rb_root *root,
983 struct hist_entry *he)
985 struct rb_node **p = &root->rb_node;
986 struct rb_node *parent = NULL;
987 struct hist_entry *iter;
992 iter = rb_entry(parent, struct hist_entry, rb_node_in);
994 cmp = hist_entry__collapse(iter, he);
997 he_stat__add_stat(&iter->stat, &he->stat);
998 if (symbol_conf.cumulate_callchain)
999 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1001 if (symbol_conf.use_callchain) {
1002 callchain_cursor_reset(&callchain_cursor);
1003 callchain_merge(&callchain_cursor,
1007 hist_entry__delete(he);
1014 p = &(*p)->rb_right;
1016 hists->nr_entries++;
1018 rb_link_node(&he->rb_node_in, parent, p);
1019 rb_insert_color(&he->rb_node_in, root);
1023 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
1025 struct rb_root *root;
1027 pthread_mutex_lock(&hists->lock);
1029 root = hists->entries_in;
1030 if (++hists->entries_in > &hists->entries_in_array[1])
1031 hists->entries_in = &hists->entries_in_array[0];
1033 pthread_mutex_unlock(&hists->lock);
1038 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1040 hists__filter_entry_by_dso(hists, he);
1041 hists__filter_entry_by_thread(hists, he);
1042 hists__filter_entry_by_symbol(hists, he);
1043 hists__filter_entry_by_socket(hists, he);
1046 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1048 struct rb_root *root;
1049 struct rb_node *next;
1050 struct hist_entry *n;
1052 if (!sort__need_collapse)
1055 hists->nr_entries = 0;
1057 root = hists__get_rotate_entries_in(hists);
1059 next = rb_first(root);
1064 n = rb_entry(next, struct hist_entry, rb_node_in);
1065 next = rb_next(&n->rb_node_in);
1067 rb_erase(&n->rb_node_in, root);
1068 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1070 * If it wasn't combined with one of the entries already
1071 * collapsed, we need to apply the filters that may have
1072 * been set by, say, the hist_browser.
1074 hists__apply_filters(hists, n);
1077 ui_progress__update(prog, 1);
1081 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1083 struct perf_hpp_fmt *fmt;
1086 perf_hpp__for_each_sort_list(fmt) {
1087 if (perf_hpp__should_skip(fmt))
1090 cmp = fmt->sort(fmt, a, b);
1098 static void hists__reset_filter_stats(struct hists *hists)
1100 hists->nr_non_filtered_entries = 0;
1101 hists->stats.total_non_filtered_period = 0;
1104 void hists__reset_stats(struct hists *hists)
1106 hists->nr_entries = 0;
1107 hists->stats.total_period = 0;
1109 hists__reset_filter_stats(hists);
1112 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1114 hists->nr_non_filtered_entries++;
1115 hists->stats.total_non_filtered_period += h->stat.period;
1118 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1121 hists__inc_filter_stats(hists, h);
1123 hists->nr_entries++;
1124 hists->stats.total_period += h->stat.period;
1127 static void __hists__insert_output_entry(struct rb_root *entries,
1128 struct hist_entry *he,
1129 u64 min_callchain_hits,
1132 struct rb_node **p = &entries->rb_node;
1133 struct rb_node *parent = NULL;
1134 struct hist_entry *iter;
1137 callchain_param.sort(&he->sorted_chain, he->callchain,
1138 min_callchain_hits, &callchain_param);
1140 while (*p != NULL) {
1142 iter = rb_entry(parent, struct hist_entry, rb_node);
1144 if (hist_entry__sort(he, iter) > 0)
1147 p = &(*p)->rb_right;
1150 rb_link_node(&he->rb_node, parent, p);
1151 rb_insert_color(&he->rb_node, entries);
1154 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1156 struct rb_root *root;
1157 struct rb_node *next;
1158 struct hist_entry *n;
1159 u64 min_callchain_hits;
1160 struct perf_evsel *evsel = hists_to_evsel(hists);
1163 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1164 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1166 use_callchain = symbol_conf.use_callchain;
1168 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
1170 if (sort__need_collapse)
1171 root = &hists->entries_collapsed;
1173 root = hists->entries_in;
1175 next = rb_first(root);
1176 hists->entries = RB_ROOT;
1178 hists__reset_stats(hists);
1179 hists__reset_col_len(hists);
1182 n = rb_entry(next, struct hist_entry, rb_node_in);
1183 next = rb_next(&n->rb_node_in);
1185 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1186 hists__inc_stats(hists, n);
1189 hists__calc_col_len(hists, n);
1192 ui_progress__update(prog, 1);
1196 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1197 enum hist_filter filter)
1199 h->filtered &= ~(1 << filter);
1203 /* force fold unfiltered entry for simplicity */
1204 h->unfolded = false;
1208 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1210 hists__inc_filter_stats(hists, h);
1211 hists__calc_col_len(hists, h);
1215 static bool hists__filter_entry_by_dso(struct hists *hists,
1216 struct hist_entry *he)
1218 if (hists->dso_filter != NULL &&
1219 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1220 he->filtered |= (1 << HIST_FILTER__DSO);
1227 void hists__filter_by_dso(struct hists *hists)
1231 hists->stats.nr_non_filtered_samples = 0;
1233 hists__reset_filter_stats(hists);
1234 hists__reset_col_len(hists);
1236 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1237 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1239 if (symbol_conf.exclude_other && !h->parent)
1242 if (hists__filter_entry_by_dso(hists, h))
1245 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1249 static bool hists__filter_entry_by_thread(struct hists *hists,
1250 struct hist_entry *he)
1252 if (hists->thread_filter != NULL &&
1253 he->thread != hists->thread_filter) {
1254 he->filtered |= (1 << HIST_FILTER__THREAD);
1261 void hists__filter_by_thread(struct hists *hists)
1265 hists->stats.nr_non_filtered_samples = 0;
1267 hists__reset_filter_stats(hists);
1268 hists__reset_col_len(hists);
1270 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1271 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1273 if (hists__filter_entry_by_thread(hists, h))
1276 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1280 static bool hists__filter_entry_by_symbol(struct hists *hists,
1281 struct hist_entry *he)
1283 if (hists->symbol_filter_str != NULL &&
1284 (!he->ms.sym || strstr(he->ms.sym->name,
1285 hists->symbol_filter_str) == NULL)) {
1286 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1293 void hists__filter_by_symbol(struct hists *hists)
1297 hists->stats.nr_non_filtered_samples = 0;
1299 hists__reset_filter_stats(hists);
1300 hists__reset_col_len(hists);
1302 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1303 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1305 if (hists__filter_entry_by_symbol(hists, h))
1308 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1312 static bool hists__filter_entry_by_socket(struct hists *hists,
1313 struct hist_entry *he)
1315 if ((hists->socket_filter > -1) &&
1316 (he->socket != hists->socket_filter)) {
1317 he->filtered |= (1 << HIST_FILTER__SOCKET);
1324 void hists__filter_by_socket(struct hists *hists)
1328 hists->stats.nr_non_filtered_samples = 0;
1330 hists__reset_filter_stats(hists);
1331 hists__reset_col_len(hists);
1333 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1334 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1336 if (hists__filter_entry_by_socket(hists, h))
1339 hists__remove_entry_filter(hists, h, HIST_FILTER__SOCKET);
1343 void events_stats__inc(struct events_stats *stats, u32 type)
1345 ++stats->nr_events[0];
1346 ++stats->nr_events[type];
1349 void hists__inc_nr_events(struct hists *hists, u32 type)
1351 events_stats__inc(&hists->stats, type);
1354 void hists__inc_nr_samples(struct hists *hists, bool filtered)
1356 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1358 hists->stats.nr_non_filtered_samples++;
1361 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1362 struct hist_entry *pair)
1364 struct rb_root *root;
1366 struct rb_node *parent = NULL;
1367 struct hist_entry *he;
1370 if (sort__need_collapse)
1371 root = &hists->entries_collapsed;
1373 root = hists->entries_in;
1377 while (*p != NULL) {
1379 he = rb_entry(parent, struct hist_entry, rb_node_in);
1381 cmp = hist_entry__collapse(he, pair);
1389 p = &(*p)->rb_right;
1392 he = hist_entry__new(pair, true);
1394 memset(&he->stat, 0, sizeof(he->stat));
1396 rb_link_node(&he->rb_node_in, parent, p);
1397 rb_insert_color(&he->rb_node_in, root);
1398 hists__inc_stats(hists, he);
1405 static struct hist_entry *hists__find_entry(struct hists *hists,
1406 struct hist_entry *he)
1410 if (sort__need_collapse)
1411 n = hists->entries_collapsed.rb_node;
1413 n = hists->entries_in->rb_node;
1416 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1417 int64_t cmp = hist_entry__collapse(iter, he);
1431 * Look for pairs to link to the leader buckets (hist_entries):
1433 void hists__match(struct hists *leader, struct hists *other)
1435 struct rb_root *root;
1437 struct hist_entry *pos, *pair;
1439 if (sort__need_collapse)
1440 root = &leader->entries_collapsed;
1442 root = leader->entries_in;
1444 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1445 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1446 pair = hists__find_entry(other, pos);
1449 hist_entry__add_pair(pair, pos);
1454 * Look for entries in the other hists that are not present in the leader, if
1455 * we find them, just add a dummy entry on the leader hists, with period=0,
1456 * nr_events=0, to serve as the list header.
1458 int hists__link(struct hists *leader, struct hists *other)
1460 struct rb_root *root;
1462 struct hist_entry *pos, *pair;
1464 if (sort__need_collapse)
1465 root = &other->entries_collapsed;
1467 root = other->entries_in;
1469 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1470 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1472 if (!hist_entry__has_pairs(pos)) {
1473 pair = hists__add_dummy_entry(leader, pos);
1476 hist_entry__add_pair(pos, pair);
1483 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1484 struct perf_sample *sample, bool nonany_branch_mode)
1486 struct branch_info *bi;
1488 /* If we have branch cycles always annotate them. */
1489 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1492 bi = sample__resolve_bstack(sample, al);
1494 struct addr_map_symbol *prev = NULL;
1497 * Ignore errors, still want to process the
1500 * For non standard branch modes always
1501 * force no IPC (prev == NULL)
1503 * Note that perf stores branches reversed from
1506 for (i = bs->nr - 1; i >= 0; i--) {
1507 addr_map_symbol__account_cycles(&bi[i].from,
1508 nonany_branch_mode ? NULL : prev,
1509 bi[i].flags.cycles);
1517 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1519 struct perf_evsel *pos;
1522 evlist__for_each(evlist, pos) {
1523 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1524 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1531 u64 hists__total_period(struct hists *hists)
1533 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1534 hists->stats.total_period;
1537 int parse_filter_percentage(const struct option *opt __maybe_unused,
1538 const char *arg, int unset __maybe_unused)
1540 if (!strcmp(arg, "relative"))
1541 symbol_conf.filter_relative = true;
1542 else if (!strcmp(arg, "absolute"))
1543 symbol_conf.filter_relative = false;
1550 int perf_hist_config(const char *var, const char *value)
1552 if (!strcmp(var, "hist.percentage"))
1553 return parse_filter_percentage(NULL, value, 0);
1558 static int hists_evsel__init(struct perf_evsel *evsel)
1560 struct hists *hists = evsel__hists(evsel);
1562 memset(hists, 0, sizeof(*hists));
1563 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1564 hists->entries_in = &hists->entries_in_array[0];
1565 hists->entries_collapsed = RB_ROOT;
1566 hists->entries = RB_ROOT;
1567 pthread_mutex_init(&hists->lock, NULL);
1568 hists->socket_filter = -1;
1572 static void hists__delete_remaining_entries(struct rb_root *root)
1574 struct rb_node *node;
1575 struct hist_entry *he;
1577 while (!RB_EMPTY_ROOT(root)) {
1578 node = rb_first(root);
1579 rb_erase(node, root);
1581 he = rb_entry(node, struct hist_entry, rb_node_in);
1582 hist_entry__delete(he);
1586 static void hists__delete_all_entries(struct hists *hists)
1588 hists__delete_entries(hists);
1589 hists__delete_remaining_entries(&hists->entries_in_array[0]);
1590 hists__delete_remaining_entries(&hists->entries_in_array[1]);
1591 hists__delete_remaining_entries(&hists->entries_collapsed);
1594 static void hists_evsel__exit(struct perf_evsel *evsel)
1596 struct hists *hists = evsel__hists(evsel);
1598 hists__delete_all_entries(hists);
1602 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1603 * stored in the rbtree...
1606 int hists__init(void)
1608 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1612 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);