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);
276 --hists->nr_non_filtered_entries;
278 hist_entry__delete(he);
281 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
283 struct rb_node *next = rb_first(&hists->entries);
284 struct hist_entry *n;
287 n = rb_entry(next, struct hist_entry, rb_node);
288 next = rb_next(&n->rb_node);
289 if (((zap_user && n->level == '.') ||
290 (zap_kernel && n->level != '.') ||
291 hists__decay_entry(hists, n))) {
292 hists__delete_entry(hists, n);
297 void hists__delete_entries(struct hists *hists)
299 struct rb_node *next = rb_first(&hists->entries);
300 struct hist_entry *n;
303 n = rb_entry(next, struct hist_entry, rb_node);
304 next = rb_next(&n->rb_node);
306 hists__delete_entry(hists, n);
311 * histogram, sorted on item, collects periods
314 static struct hist_entry *hist_entry__new(struct hist_entry *template,
317 size_t callchain_size = 0;
318 struct hist_entry *he;
320 if (symbol_conf.use_callchain)
321 callchain_size = sizeof(struct callchain_root);
323 he = zalloc(sizeof(*he) + callchain_size);
328 if (symbol_conf.cumulate_callchain) {
329 he->stat_acc = malloc(sizeof(he->stat));
330 if (he->stat_acc == NULL) {
334 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
336 memset(&he->stat, 0, sizeof(he->stat));
339 map__get(he->ms.map);
341 if (he->branch_info) {
343 * This branch info is (a part of) allocated from
344 * sample__resolve_bstack() and will be freed after
345 * adding new entries. So we need to save a copy.
347 he->branch_info = malloc(sizeof(*he->branch_info));
348 if (he->branch_info == NULL) {
349 map__zput(he->ms.map);
355 memcpy(he->branch_info, template->branch_info,
356 sizeof(*he->branch_info));
358 map__get(he->branch_info->from.map);
359 map__get(he->branch_info->to.map);
363 map__get(he->mem_info->iaddr.map);
364 map__get(he->mem_info->daddr.map);
367 if (symbol_conf.use_callchain)
368 callchain_init(he->callchain);
370 INIT_LIST_HEAD(&he->pairs.node);
371 thread__get(he->thread);
377 static u8 symbol__parent_filter(const struct symbol *parent)
379 if (symbol_conf.exclude_other && parent == NULL)
380 return 1 << HIST_FILTER__PARENT;
384 static struct hist_entry *hists__findnew_entry(struct hists *hists,
385 struct hist_entry *entry,
386 struct addr_location *al,
390 struct rb_node *parent = NULL;
391 struct hist_entry *he;
393 u64 period = entry->stat.period;
394 u64 weight = entry->stat.weight;
396 p = &hists->entries_in->rb_node;
400 he = rb_entry(parent, struct hist_entry, rb_node_in);
403 * Make sure that it receives arguments in a same order as
404 * hist_entry__collapse() so that we can use an appropriate
405 * function when searching an entry regardless which sort
408 cmp = hist_entry__cmp(he, entry);
412 he_stat__add_period(&he->stat, period, weight);
413 if (symbol_conf.cumulate_callchain)
414 he_stat__add_period(he->stat_acc, period, weight);
417 * This mem info was allocated from sample__resolve_mem
418 * and will not be used anymore.
420 zfree(&entry->mem_info);
422 /* If the map of an existing hist_entry has
423 * become out-of-date due to an exec() or
424 * similar, update it. Otherwise we will
425 * mis-adjust symbol addresses when computing
426 * the history counter to increment.
428 if (he->ms.map != entry->ms.map) {
429 map__put(he->ms.map);
430 he->ms.map = map__get(entry->ms.map);
441 he = hist_entry__new(entry, sample_self);
447 rb_link_node(&he->rb_node_in, parent, p);
448 rb_insert_color(&he->rb_node_in, hists->entries_in);
451 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
452 if (symbol_conf.cumulate_callchain)
453 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
457 struct hist_entry *__hists__add_entry(struct hists *hists,
458 struct addr_location *al,
459 struct symbol *sym_parent,
460 struct branch_info *bi,
462 u64 period, u64 weight, u64 transaction,
465 struct hist_entry entry = {
466 .thread = al->thread,
467 .comm = thread__comm(al->thread),
472 .socket = al->socket,
474 .cpumode = al->cpumode,
482 .parent = sym_parent,
483 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
487 .transaction = transaction,
490 return hists__findnew_entry(hists, &entry, al, sample_self);
494 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
495 struct addr_location *al __maybe_unused)
501 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
502 struct addr_location *al __maybe_unused)
508 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
510 struct perf_sample *sample = iter->sample;
513 mi = sample__resolve_mem(sample, al);
522 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
525 struct mem_info *mi = iter->priv;
526 struct hists *hists = evsel__hists(iter->evsel);
527 struct hist_entry *he;
532 cost = iter->sample->weight;
537 * must pass period=weight in order to get the correct
538 * sorting from hists__collapse_resort() which is solely
539 * based on periods. We want sorting be done on nr_events * weight
540 * and this is indirectly achieved by passing period=weight here
541 * and the he_stat__add_period() function.
543 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
544 cost, cost, 0, true);
553 iter_finish_mem_entry(struct hist_entry_iter *iter,
554 struct addr_location *al __maybe_unused)
556 struct perf_evsel *evsel = iter->evsel;
557 struct hists *hists = evsel__hists(evsel);
558 struct hist_entry *he = iter->he;
564 hists__inc_nr_samples(hists, he->filtered);
566 err = hist_entry__append_callchain(he, iter->sample);
570 * We don't need to free iter->priv (mem_info) here since the mem info
571 * was either already freed in hists__findnew_entry() or passed to a
572 * new hist entry by hist_entry__new().
581 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
583 struct branch_info *bi;
584 struct perf_sample *sample = iter->sample;
586 bi = sample__resolve_bstack(sample, al);
591 iter->total = sample->branch_stack->nr;
598 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
599 struct addr_location *al __maybe_unused)
601 /* to avoid calling callback function */
608 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
610 struct branch_info *bi = iter->priv;
616 if (iter->curr >= iter->total)
619 al->map = bi[i].to.map;
620 al->sym = bi[i].to.sym;
621 al->addr = bi[i].to.addr;
626 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
628 struct branch_info *bi;
629 struct perf_evsel *evsel = iter->evsel;
630 struct hists *hists = evsel__hists(evsel);
631 struct hist_entry *he = NULL;
637 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
641 * The report shows the percentage of total branches captured
642 * and not events sampled. Thus we use a pseudo period of 1.
644 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
645 1, bi->flags.cycles ? bi->flags.cycles : 1,
650 hists__inc_nr_samples(hists, he->filtered);
659 iter_finish_branch_entry(struct hist_entry_iter *iter,
660 struct addr_location *al __maybe_unused)
665 return iter->curr >= iter->total ? 0 : -1;
669 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
670 struct addr_location *al __maybe_unused)
676 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
678 struct perf_evsel *evsel = iter->evsel;
679 struct perf_sample *sample = iter->sample;
680 struct hist_entry *he;
682 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
683 sample->period, sample->weight,
684 sample->transaction, true);
693 iter_finish_normal_entry(struct hist_entry_iter *iter,
694 struct addr_location *al __maybe_unused)
696 struct hist_entry *he = iter->he;
697 struct perf_evsel *evsel = iter->evsel;
698 struct perf_sample *sample = iter->sample;
705 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
707 return hist_entry__append_callchain(he, sample);
711 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
712 struct addr_location *al __maybe_unused)
714 struct hist_entry **he_cache;
716 callchain_cursor_commit(&callchain_cursor);
719 * This is for detecting cycles or recursions so that they're
720 * cumulated only one time to prevent entries more than 100%
723 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
724 if (he_cache == NULL)
727 iter->priv = he_cache;
734 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
735 struct addr_location *al)
737 struct perf_evsel *evsel = iter->evsel;
738 struct hists *hists = evsel__hists(evsel);
739 struct perf_sample *sample = iter->sample;
740 struct hist_entry **he_cache = iter->priv;
741 struct hist_entry *he;
744 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
745 sample->period, sample->weight,
746 sample->transaction, true);
751 he_cache[iter->curr++] = he;
753 hist_entry__append_callchain(he, sample);
756 * We need to re-initialize the cursor since callchain_append()
757 * advanced the cursor to the end.
759 callchain_cursor_commit(&callchain_cursor);
761 hists__inc_nr_samples(hists, he->filtered);
767 iter_next_cumulative_entry(struct hist_entry_iter *iter,
768 struct addr_location *al)
770 struct callchain_cursor_node *node;
772 node = callchain_cursor_current(&callchain_cursor);
776 return fill_callchain_info(al, node, iter->hide_unresolved);
780 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
781 struct addr_location *al)
783 struct perf_evsel *evsel = iter->evsel;
784 struct perf_sample *sample = iter->sample;
785 struct hist_entry **he_cache = iter->priv;
786 struct hist_entry *he;
787 struct hist_entry he_tmp = {
788 .hists = evsel__hists(evsel),
790 .thread = al->thread,
791 .comm = thread__comm(al->thread),
797 .parent = iter->parent,
800 struct callchain_cursor cursor;
802 callchain_cursor_snapshot(&cursor, &callchain_cursor);
804 callchain_cursor_advance(&callchain_cursor);
807 * Check if there's duplicate entries in the callchain.
808 * It's possible that it has cycles or recursive calls.
810 for (i = 0; i < iter->curr; i++) {
811 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
812 /* to avoid calling callback function */
818 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
819 sample->period, sample->weight,
820 sample->transaction, false);
825 he_cache[iter->curr++] = he;
827 if (symbol_conf.use_callchain)
828 callchain_append(he->callchain, &cursor, sample->period);
833 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
834 struct addr_location *al __maybe_unused)
842 const struct hist_iter_ops hist_iter_mem = {
843 .prepare_entry = iter_prepare_mem_entry,
844 .add_single_entry = iter_add_single_mem_entry,
845 .next_entry = iter_next_nop_entry,
846 .add_next_entry = iter_add_next_nop_entry,
847 .finish_entry = iter_finish_mem_entry,
850 const struct hist_iter_ops hist_iter_branch = {
851 .prepare_entry = iter_prepare_branch_entry,
852 .add_single_entry = iter_add_single_branch_entry,
853 .next_entry = iter_next_branch_entry,
854 .add_next_entry = iter_add_next_branch_entry,
855 .finish_entry = iter_finish_branch_entry,
858 const struct hist_iter_ops hist_iter_normal = {
859 .prepare_entry = iter_prepare_normal_entry,
860 .add_single_entry = iter_add_single_normal_entry,
861 .next_entry = iter_next_nop_entry,
862 .add_next_entry = iter_add_next_nop_entry,
863 .finish_entry = iter_finish_normal_entry,
866 const struct hist_iter_ops hist_iter_cumulative = {
867 .prepare_entry = iter_prepare_cumulative_entry,
868 .add_single_entry = iter_add_single_cumulative_entry,
869 .next_entry = iter_next_cumulative_entry,
870 .add_next_entry = iter_add_next_cumulative_entry,
871 .finish_entry = iter_finish_cumulative_entry,
874 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
875 int max_stack_depth, void *arg)
879 err = sample__resolve_callchain(iter->sample, &iter->parent,
880 iter->evsel, al, max_stack_depth);
884 iter->max_stack = max_stack_depth;
886 err = iter->ops->prepare_entry(iter, al);
890 err = iter->ops->add_single_entry(iter, al);
894 if (iter->he && iter->add_entry_cb) {
895 err = iter->add_entry_cb(iter, al, true, arg);
900 while (iter->ops->next_entry(iter, al)) {
901 err = iter->ops->add_next_entry(iter, al);
905 if (iter->he && iter->add_entry_cb) {
906 err = iter->add_entry_cb(iter, al, false, arg);
913 err2 = iter->ops->finish_entry(iter, al);
921 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
923 struct perf_hpp_fmt *fmt;
926 perf_hpp__for_each_sort_list(fmt) {
927 cmp = fmt->cmp(fmt, left, right);
936 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
938 struct perf_hpp_fmt *fmt;
941 perf_hpp__for_each_sort_list(fmt) {
942 cmp = fmt->collapse(fmt, left, right);
950 void hist_entry__delete(struct hist_entry *he)
952 thread__zput(he->thread);
953 map__zput(he->ms.map);
955 if (he->branch_info) {
956 map__zput(he->branch_info->from.map);
957 map__zput(he->branch_info->to.map);
958 zfree(&he->branch_info);
962 map__zput(he->mem_info->iaddr.map);
963 map__zput(he->mem_info->daddr.map);
964 zfree(&he->mem_info);
967 zfree(&he->stat_acc);
968 free_srcline(he->srcline);
969 if (he->srcfile && he->srcfile[0])
971 free_callchain(he->callchain);
976 * collapse the histogram
979 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
980 struct rb_root *root,
981 struct hist_entry *he)
983 struct rb_node **p = &root->rb_node;
984 struct rb_node *parent = NULL;
985 struct hist_entry *iter;
990 iter = rb_entry(parent, struct hist_entry, rb_node_in);
992 cmp = hist_entry__collapse(iter, he);
995 he_stat__add_stat(&iter->stat, &he->stat);
996 if (symbol_conf.cumulate_callchain)
997 he_stat__add_stat(iter->stat_acc, he->stat_acc);
999 if (symbol_conf.use_callchain) {
1000 callchain_cursor_reset(&callchain_cursor);
1001 callchain_merge(&callchain_cursor,
1005 hist_entry__delete(he);
1012 p = &(*p)->rb_right;
1014 hists->nr_entries++;
1016 rb_link_node(&he->rb_node_in, parent, p);
1017 rb_insert_color(&he->rb_node_in, root);
1021 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
1023 struct rb_root *root;
1025 pthread_mutex_lock(&hists->lock);
1027 root = hists->entries_in;
1028 if (++hists->entries_in > &hists->entries_in_array[1])
1029 hists->entries_in = &hists->entries_in_array[0];
1031 pthread_mutex_unlock(&hists->lock);
1036 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1038 hists__filter_entry_by_dso(hists, he);
1039 hists__filter_entry_by_thread(hists, he);
1040 hists__filter_entry_by_symbol(hists, he);
1041 hists__filter_entry_by_socket(hists, he);
1044 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1046 struct rb_root *root;
1047 struct rb_node *next;
1048 struct hist_entry *n;
1050 if (!sort__need_collapse)
1053 hists->nr_entries = 0;
1055 root = hists__get_rotate_entries_in(hists);
1057 next = rb_first(root);
1062 n = rb_entry(next, struct hist_entry, rb_node_in);
1063 next = rb_next(&n->rb_node_in);
1065 rb_erase(&n->rb_node_in, root);
1066 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1068 * If it wasn't combined with one of the entries already
1069 * collapsed, we need to apply the filters that may have
1070 * been set by, say, the hist_browser.
1072 hists__apply_filters(hists, n);
1075 ui_progress__update(prog, 1);
1079 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1081 struct perf_hpp_fmt *fmt;
1084 perf_hpp__for_each_sort_list(fmt) {
1085 if (perf_hpp__should_skip(fmt))
1088 cmp = fmt->sort(fmt, a, b);
1096 static void hists__reset_filter_stats(struct hists *hists)
1098 hists->nr_non_filtered_entries = 0;
1099 hists->stats.total_non_filtered_period = 0;
1102 void hists__reset_stats(struct hists *hists)
1104 hists->nr_entries = 0;
1105 hists->stats.total_period = 0;
1107 hists__reset_filter_stats(hists);
1110 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1112 hists->nr_non_filtered_entries++;
1113 hists->stats.total_non_filtered_period += h->stat.period;
1116 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1119 hists__inc_filter_stats(hists, h);
1121 hists->nr_entries++;
1122 hists->stats.total_period += h->stat.period;
1125 static void __hists__insert_output_entry(struct rb_root *entries,
1126 struct hist_entry *he,
1127 u64 min_callchain_hits,
1130 struct rb_node **p = &entries->rb_node;
1131 struct rb_node *parent = NULL;
1132 struct hist_entry *iter;
1135 callchain_param.sort(&he->sorted_chain, he->callchain,
1136 min_callchain_hits, &callchain_param);
1138 while (*p != NULL) {
1140 iter = rb_entry(parent, struct hist_entry, rb_node);
1142 if (hist_entry__sort(he, iter) > 0)
1145 p = &(*p)->rb_right;
1148 rb_link_node(&he->rb_node, parent, p);
1149 rb_insert_color(&he->rb_node, entries);
1152 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1154 struct rb_root *root;
1155 struct rb_node *next;
1156 struct hist_entry *n;
1157 u64 min_callchain_hits;
1158 struct perf_evsel *evsel = hists_to_evsel(hists);
1161 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1162 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1164 use_callchain = symbol_conf.use_callchain;
1166 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
1168 if (sort__need_collapse)
1169 root = &hists->entries_collapsed;
1171 root = hists->entries_in;
1173 next = rb_first(root);
1174 hists->entries = RB_ROOT;
1176 hists__reset_stats(hists);
1177 hists__reset_col_len(hists);
1180 n = rb_entry(next, struct hist_entry, rb_node_in);
1181 next = rb_next(&n->rb_node_in);
1183 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1184 hists__inc_stats(hists, n);
1187 hists__calc_col_len(hists, n);
1190 ui_progress__update(prog, 1);
1194 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1195 enum hist_filter filter)
1197 h->filtered &= ~(1 << filter);
1201 /* force fold unfiltered entry for simplicity */
1202 h->unfolded = false;
1206 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1208 hists__inc_filter_stats(hists, h);
1209 hists__calc_col_len(hists, h);
1213 static bool hists__filter_entry_by_dso(struct hists *hists,
1214 struct hist_entry *he)
1216 if (hists->dso_filter != NULL &&
1217 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1218 he->filtered |= (1 << HIST_FILTER__DSO);
1225 void hists__filter_by_dso(struct hists *hists)
1229 hists->stats.nr_non_filtered_samples = 0;
1231 hists__reset_filter_stats(hists);
1232 hists__reset_col_len(hists);
1234 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1235 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1237 if (symbol_conf.exclude_other && !h->parent)
1240 if (hists__filter_entry_by_dso(hists, h))
1243 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1247 static bool hists__filter_entry_by_thread(struct hists *hists,
1248 struct hist_entry *he)
1250 if (hists->thread_filter != NULL &&
1251 he->thread != hists->thread_filter) {
1252 he->filtered |= (1 << HIST_FILTER__THREAD);
1259 void hists__filter_by_thread(struct hists *hists)
1263 hists->stats.nr_non_filtered_samples = 0;
1265 hists__reset_filter_stats(hists);
1266 hists__reset_col_len(hists);
1268 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1269 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1271 if (hists__filter_entry_by_thread(hists, h))
1274 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1278 static bool hists__filter_entry_by_symbol(struct hists *hists,
1279 struct hist_entry *he)
1281 if (hists->symbol_filter_str != NULL &&
1282 (!he->ms.sym || strstr(he->ms.sym->name,
1283 hists->symbol_filter_str) == NULL)) {
1284 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1291 void hists__filter_by_symbol(struct hists *hists)
1295 hists->stats.nr_non_filtered_samples = 0;
1297 hists__reset_filter_stats(hists);
1298 hists__reset_col_len(hists);
1300 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1301 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1303 if (hists__filter_entry_by_symbol(hists, h))
1306 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1310 static bool hists__filter_entry_by_socket(struct hists *hists,
1311 struct hist_entry *he)
1313 if ((hists->socket_filter > -1) &&
1314 (he->socket != hists->socket_filter)) {
1315 he->filtered |= (1 << HIST_FILTER__SOCKET);
1322 void hists__filter_by_socket(struct hists *hists)
1326 hists->stats.nr_non_filtered_samples = 0;
1328 hists__reset_filter_stats(hists);
1329 hists__reset_col_len(hists);
1331 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1332 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1334 if (hists__filter_entry_by_socket(hists, h))
1337 hists__remove_entry_filter(hists, h, HIST_FILTER__SOCKET);
1341 void events_stats__inc(struct events_stats *stats, u32 type)
1343 ++stats->nr_events[0];
1344 ++stats->nr_events[type];
1347 void hists__inc_nr_events(struct hists *hists, u32 type)
1349 events_stats__inc(&hists->stats, type);
1352 void hists__inc_nr_samples(struct hists *hists, bool filtered)
1354 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1356 hists->stats.nr_non_filtered_samples++;
1359 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1360 struct hist_entry *pair)
1362 struct rb_root *root;
1364 struct rb_node *parent = NULL;
1365 struct hist_entry *he;
1368 if (sort__need_collapse)
1369 root = &hists->entries_collapsed;
1371 root = hists->entries_in;
1375 while (*p != NULL) {
1377 he = rb_entry(parent, struct hist_entry, rb_node_in);
1379 cmp = hist_entry__collapse(he, pair);
1387 p = &(*p)->rb_right;
1390 he = hist_entry__new(pair, true);
1392 memset(&he->stat, 0, sizeof(he->stat));
1394 rb_link_node(&he->rb_node_in, parent, p);
1395 rb_insert_color(&he->rb_node_in, root);
1396 hists__inc_stats(hists, he);
1403 static struct hist_entry *hists__find_entry(struct hists *hists,
1404 struct hist_entry *he)
1408 if (sort__need_collapse)
1409 n = hists->entries_collapsed.rb_node;
1411 n = hists->entries_in->rb_node;
1414 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1415 int64_t cmp = hist_entry__collapse(iter, he);
1429 * Look for pairs to link to the leader buckets (hist_entries):
1431 void hists__match(struct hists *leader, struct hists *other)
1433 struct rb_root *root;
1435 struct hist_entry *pos, *pair;
1437 if (sort__need_collapse)
1438 root = &leader->entries_collapsed;
1440 root = leader->entries_in;
1442 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1443 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1444 pair = hists__find_entry(other, pos);
1447 hist_entry__add_pair(pair, pos);
1452 * Look for entries in the other hists that are not present in the leader, if
1453 * we find them, just add a dummy entry on the leader hists, with period=0,
1454 * nr_events=0, to serve as the list header.
1456 int hists__link(struct hists *leader, struct hists *other)
1458 struct rb_root *root;
1460 struct hist_entry *pos, *pair;
1462 if (sort__need_collapse)
1463 root = &other->entries_collapsed;
1465 root = other->entries_in;
1467 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1468 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1470 if (!hist_entry__has_pairs(pos)) {
1471 pair = hists__add_dummy_entry(leader, pos);
1474 hist_entry__add_pair(pos, pair);
1481 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1482 struct perf_sample *sample, bool nonany_branch_mode)
1484 struct branch_info *bi;
1486 /* If we have branch cycles always annotate them. */
1487 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1490 bi = sample__resolve_bstack(sample, al);
1492 struct addr_map_symbol *prev = NULL;
1495 * Ignore errors, still want to process the
1498 * For non standard branch modes always
1499 * force no IPC (prev == NULL)
1501 * Note that perf stores branches reversed from
1504 for (i = bs->nr - 1; i >= 0; i--) {
1505 addr_map_symbol__account_cycles(&bi[i].from,
1506 nonany_branch_mode ? NULL : prev,
1507 bi[i].flags.cycles);
1515 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1517 struct perf_evsel *pos;
1520 evlist__for_each(evlist, pos) {
1521 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1522 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1529 u64 hists__total_period(struct hists *hists)
1531 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1532 hists->stats.total_period;
1535 int parse_filter_percentage(const struct option *opt __maybe_unused,
1536 const char *arg, int unset __maybe_unused)
1538 if (!strcmp(arg, "relative"))
1539 symbol_conf.filter_relative = true;
1540 else if (!strcmp(arg, "absolute"))
1541 symbol_conf.filter_relative = false;
1548 int perf_hist_config(const char *var, const char *value)
1550 if (!strcmp(var, "hist.percentage"))
1551 return parse_filter_percentage(NULL, value, 0);
1556 static int hists_evsel__init(struct perf_evsel *evsel)
1558 struct hists *hists = evsel__hists(evsel);
1560 memset(hists, 0, sizeof(*hists));
1561 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1562 hists->entries_in = &hists->entries_in_array[0];
1563 hists->entries_collapsed = RB_ROOT;
1564 hists->entries = RB_ROOT;
1565 pthread_mutex_init(&hists->lock, NULL);
1566 hists->socket_filter = -1;
1571 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1572 * stored in the rbtree...
1575 int hists__init(void)
1577 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1578 hists_evsel__init, NULL);
1580 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);