10 static bool hists__filter_entry_by_dso(struct hists *hists,
11 struct hist_entry *he);
12 static bool hists__filter_entry_by_thread(struct hists *hists,
13 struct hist_entry *he);
14 static bool hists__filter_entry_by_symbol(struct hists *hists,
15 struct hist_entry *he);
17 struct callchain_param callchain_param = {
18 .mode = CHAIN_GRAPH_REL,
20 .order = ORDER_CALLEE,
24 u16 hists__col_len(struct hists *hists, enum hist_column col)
26 return hists->col_len[col];
29 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
31 hists->col_len[col] = len;
34 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
36 if (len > hists__col_len(hists, col)) {
37 hists__set_col_len(hists, col, len);
43 void hists__reset_col_len(struct hists *hists)
47 for (col = 0; col < HISTC_NR_COLS; ++col)
48 hists__set_col_len(hists, col, 0);
51 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
53 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
55 if (hists__col_len(hists, dso) < unresolved_col_width &&
56 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
57 !symbol_conf.dso_list)
58 hists__set_col_len(hists, dso, unresolved_col_width);
61 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
63 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
68 * +4 accounts for '[x] ' priv level info
69 * +2 accounts for 0x prefix on raw addresses
70 * +3 accounts for ' y ' symtab origin info
73 symlen = h->ms.sym->namelen + 4;
75 symlen += BITS_PER_LONG / 4 + 2 + 3;
76 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
78 symlen = unresolved_col_width + 4 + 2;
79 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
80 hists__set_unres_dso_col_len(hists, HISTC_DSO);
83 len = thread__comm_len(h->thread);
84 if (hists__new_col_len(hists, HISTC_COMM, len))
85 hists__set_col_len(hists, HISTC_THREAD, len + 6);
88 len = dso__name_len(h->ms.map->dso);
89 hists__new_col_len(hists, HISTC_DSO, len);
93 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
96 if (h->branch_info->from.sym) {
97 symlen = (int)h->branch_info->from.sym->namelen + 4;
99 symlen += BITS_PER_LONG / 4 + 2 + 3;
100 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
102 symlen = dso__name_len(h->branch_info->from.map->dso);
103 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
105 symlen = unresolved_col_width + 4 + 2;
106 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
107 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
110 if (h->branch_info->to.sym) {
111 symlen = (int)h->branch_info->to.sym->namelen + 4;
113 symlen += BITS_PER_LONG / 4 + 2 + 3;
114 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
116 symlen = dso__name_len(h->branch_info->to.map->dso);
117 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
119 symlen = unresolved_col_width + 4 + 2;
120 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
121 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
126 if (h->mem_info->daddr.sym) {
127 symlen = (int)h->mem_info->daddr.sym->namelen + 4
128 + unresolved_col_width + 2;
129 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
132 symlen = unresolved_col_width + 4 + 2;
133 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
136 if (h->mem_info->daddr.map) {
137 symlen = dso__name_len(h->mem_info->daddr.map->dso);
138 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
141 symlen = unresolved_col_width + 4 + 2;
142 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
145 symlen = unresolved_col_width + 4 + 2;
146 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
147 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
150 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
151 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
152 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
153 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
154 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
155 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
158 hists__new_col_len(hists, HISTC_TRANSACTION,
159 hist_entry__transaction_len());
162 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
164 struct rb_node *next = rb_first(&hists->entries);
165 struct hist_entry *n;
168 hists__reset_col_len(hists);
170 while (next && row++ < max_rows) {
171 n = rb_entry(next, struct hist_entry, rb_node);
173 hists__calc_col_len(hists, n);
174 next = rb_next(&n->rb_node);
178 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
179 unsigned int cpumode, u64 period)
182 case PERF_RECORD_MISC_KERNEL:
183 he_stat->period_sys += period;
185 case PERF_RECORD_MISC_USER:
186 he_stat->period_us += period;
188 case PERF_RECORD_MISC_GUEST_KERNEL:
189 he_stat->period_guest_sys += period;
191 case PERF_RECORD_MISC_GUEST_USER:
192 he_stat->period_guest_us += period;
199 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
203 he_stat->period += period;
204 he_stat->weight += weight;
205 he_stat->nr_events += 1;
208 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
210 dest->period += src->period;
211 dest->period_sys += src->period_sys;
212 dest->period_us += src->period_us;
213 dest->period_guest_sys += src->period_guest_sys;
214 dest->period_guest_us += src->period_guest_us;
215 dest->nr_events += src->nr_events;
216 dest->weight += src->weight;
219 static void he_stat__decay(struct he_stat *he_stat)
221 he_stat->period = (he_stat->period * 7) / 8;
222 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
223 /* XXX need decay for weight too? */
226 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
228 u64 prev_period = he->stat.period;
231 if (prev_period == 0)
234 he_stat__decay(&he->stat);
235 if (symbol_conf.cumulate_callchain)
236 he_stat__decay(he->stat_acc);
238 diff = prev_period - he->stat.period;
240 hists->stats.total_period -= diff;
242 hists->stats.total_non_filtered_period -= diff;
244 return he->stat.period == 0;
247 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
249 struct rb_node *next = rb_first(&hists->entries);
250 struct hist_entry *n;
253 n = rb_entry(next, struct hist_entry, rb_node);
254 next = rb_next(&n->rb_node);
256 * We may be annotating this, for instance, so keep it here in
257 * case some it gets new samples, we'll eventually free it when
258 * the user stops browsing and it agains gets fully decayed.
260 if (((zap_user && n->level == '.') ||
261 (zap_kernel && n->level != '.') ||
262 hists__decay_entry(hists, n)) &&
264 rb_erase(&n->rb_node, &hists->entries);
266 if (sort__need_collapse)
267 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
271 --hists->nr_non_filtered_entries;
279 * histogram, sorted on item, collects periods
282 static struct hist_entry *hist_entry__new(struct hist_entry *template,
285 size_t callchain_size = 0;
286 struct hist_entry *he;
288 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain)
289 callchain_size = sizeof(struct callchain_root);
291 he = zalloc(sizeof(*he) + callchain_size);
296 if (symbol_conf.cumulate_callchain) {
297 he->stat_acc = malloc(sizeof(he->stat));
298 if (he->stat_acc == NULL) {
302 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
304 memset(&he->stat, 0, sizeof(he->stat));
308 he->ms.map->referenced = true;
310 if (he->branch_info) {
312 * This branch info is (a part of) allocated from
313 * sample__resolve_bstack() and will be freed after
314 * adding new entries. So we need to save a copy.
316 he->branch_info = malloc(sizeof(*he->branch_info));
317 if (he->branch_info == NULL) {
323 memcpy(he->branch_info, template->branch_info,
324 sizeof(*he->branch_info));
326 if (he->branch_info->from.map)
327 he->branch_info->from.map->referenced = true;
328 if (he->branch_info->to.map)
329 he->branch_info->to.map->referenced = true;
333 if (he->mem_info->iaddr.map)
334 he->mem_info->iaddr.map->referenced = true;
335 if (he->mem_info->daddr.map)
336 he->mem_info->daddr.map->referenced = true;
339 if (symbol_conf.use_callchain)
340 callchain_init(he->callchain);
342 INIT_LIST_HEAD(&he->pairs.node);
348 static u8 symbol__parent_filter(const struct symbol *parent)
350 if (symbol_conf.exclude_other && parent == NULL)
351 return 1 << HIST_FILTER__PARENT;
355 static struct hist_entry *add_hist_entry(struct hists *hists,
356 struct hist_entry *entry,
357 struct addr_location *al,
361 struct rb_node *parent = NULL;
362 struct hist_entry *he;
364 u64 period = entry->stat.period;
365 u64 weight = entry->stat.weight;
367 p = &hists->entries_in->rb_node;
371 he = rb_entry(parent, struct hist_entry, rb_node_in);
374 * Make sure that it receives arguments in a same order as
375 * hist_entry__collapse() so that we can use an appropriate
376 * function when searching an entry regardless which sort
379 cmp = hist_entry__cmp(he, entry);
383 he_stat__add_period(&he->stat, period, weight);
384 if (symbol_conf.cumulate_callchain)
385 he_stat__add_period(he->stat_acc, period, weight);
388 * This mem info was allocated from sample__resolve_mem
389 * and will not be used anymore.
391 zfree(&entry->mem_info);
393 /* If the map of an existing hist_entry has
394 * become out-of-date due to an exec() or
395 * similar, update it. Otherwise we will
396 * mis-adjust symbol addresses when computing
397 * the history counter to increment.
399 if (he->ms.map != entry->ms.map) {
400 he->ms.map = entry->ms.map;
402 he->ms.map->referenced = true;
413 he = hist_entry__new(entry, sample_self);
417 rb_link_node(&he->rb_node_in, parent, p);
418 rb_insert_color(&he->rb_node_in, hists->entries_in);
421 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
422 if (symbol_conf.cumulate_callchain)
423 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
427 struct hist_entry *__hists__add_entry(struct hists *hists,
428 struct addr_location *al,
429 struct symbol *sym_parent,
430 struct branch_info *bi,
432 u64 period, u64 weight, u64 transaction,
435 struct hist_entry entry = {
436 .thread = al->thread,
437 .comm = thread__comm(al->thread),
450 .parent = sym_parent,
451 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
455 .transaction = transaction,
458 return add_hist_entry(hists, &entry, al, sample_self);
462 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
463 struct addr_location *al __maybe_unused)
469 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
470 struct addr_location *al __maybe_unused)
476 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
478 struct perf_sample *sample = iter->sample;
481 mi = sample__resolve_mem(sample, al);
490 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
493 struct mem_info *mi = iter->priv;
494 struct hist_entry *he;
499 cost = iter->sample->weight;
504 * must pass period=weight in order to get the correct
505 * sorting from hists__collapse_resort() which is solely
506 * based on periods. We want sorting be done on nr_events * weight
507 * and this is indirectly achieved by passing period=weight here
508 * and the he_stat__add_period() function.
510 he = __hists__add_entry(&iter->evsel->hists, al, iter->parent, NULL, mi,
511 cost, cost, 0, true);
520 iter_finish_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
522 struct perf_evsel *evsel = iter->evsel;
523 struct hist_entry *he = iter->he;
530 if (ui__has_annotation()) {
531 err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
536 err = addr_map_symbol__inc_samples(&mx->daddr, evsel->idx);
541 hists__inc_nr_samples(&evsel->hists, he->filtered);
543 err = hist_entry__append_callchain(he, iter->sample);
547 * We don't need to free iter->priv (mem_info) here since
548 * the mem info was either already freed in add_hist_entry() or
549 * passed to a new hist entry by hist_entry__new().
558 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
560 struct branch_info *bi;
561 struct perf_sample *sample = iter->sample;
563 bi = sample__resolve_bstack(sample, al);
568 iter->total = sample->branch_stack->nr;
575 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
576 struct addr_location *al __maybe_unused)
582 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
584 struct branch_info *bi = iter->priv;
590 if (iter->curr >= iter->total)
593 al->map = bi[i].to.map;
594 al->sym = bi[i].to.sym;
595 al->addr = bi[i].to.addr;
600 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
602 struct branch_info *bi, *bx;
603 struct perf_evsel *evsel = iter->evsel;
604 struct hist_entry *he = NULL;
610 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
614 * The report shows the percentage of total branches captured
615 * and not events sampled. Thus we use a pseudo period of 1.
617 he = __hists__add_entry(&evsel->hists, al, iter->parent, &bi[i], NULL,
622 if (ui__has_annotation()) {
623 bx = he->branch_info;
624 err = addr_map_symbol__inc_samples(&bx->from, evsel->idx);
628 err = addr_map_symbol__inc_samples(&bx->to, evsel->idx);
633 hists__inc_nr_samples(&evsel->hists, he->filtered);
642 iter_finish_branch_entry(struct hist_entry_iter *iter,
643 struct addr_location *al __maybe_unused)
648 return iter->curr >= iter->total ? 0 : -1;
652 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
653 struct addr_location *al __maybe_unused)
659 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
661 struct perf_evsel *evsel = iter->evsel;
662 struct perf_sample *sample = iter->sample;
663 struct hist_entry *he;
665 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
666 sample->period, sample->weight,
667 sample->transaction, true);
676 iter_finish_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
679 struct hist_entry *he = iter->he;
680 struct perf_evsel *evsel = iter->evsel;
681 struct perf_sample *sample = iter->sample;
688 if (ui__has_annotation()) {
689 err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
694 hists__inc_nr_samples(&evsel->hists, he->filtered);
696 return hist_entry__append_callchain(he, sample);
700 iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
701 struct addr_location *al __maybe_unused)
703 callchain_cursor_commit(&callchain_cursor);
708 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
709 struct addr_location *al)
711 struct perf_evsel *evsel = iter->evsel;
712 struct perf_sample *sample = iter->sample;
713 struct hist_entry *he;
716 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
717 sample->period, sample->weight,
718 sample->transaction, true);
725 * The iter->he will be over-written after ->add_next_entry()
726 * called so inc stats for the original entry now.
728 if (ui__has_annotation())
729 err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
731 hists__inc_nr_samples(&evsel->hists, he->filtered);
737 iter_next_cumulative_entry(struct hist_entry_iter *iter,
738 struct addr_location *al)
740 struct callchain_cursor_node *node;
742 node = callchain_cursor_current(&callchain_cursor);
749 al->addr = node->map->map_ip(node->map, node->ip);
753 if (iter->hide_unresolved && al->sym == NULL)
756 callchain_cursor_advance(&callchain_cursor);
761 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
762 struct addr_location *al)
764 struct perf_evsel *evsel = iter->evsel;
765 struct perf_sample *sample = iter->sample;
766 struct hist_entry *he;
768 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
769 sample->period, sample->weight,
770 sample->transaction, false);
780 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
781 struct addr_location *al __maybe_unused)
787 const struct hist_iter_ops hist_iter_mem = {
788 .prepare_entry = iter_prepare_mem_entry,
789 .add_single_entry = iter_add_single_mem_entry,
790 .next_entry = iter_next_nop_entry,
791 .add_next_entry = iter_add_next_nop_entry,
792 .finish_entry = iter_finish_mem_entry,
795 const struct hist_iter_ops hist_iter_branch = {
796 .prepare_entry = iter_prepare_branch_entry,
797 .add_single_entry = iter_add_single_branch_entry,
798 .next_entry = iter_next_branch_entry,
799 .add_next_entry = iter_add_next_branch_entry,
800 .finish_entry = iter_finish_branch_entry,
803 const struct hist_iter_ops hist_iter_normal = {
804 .prepare_entry = iter_prepare_normal_entry,
805 .add_single_entry = iter_add_single_normal_entry,
806 .next_entry = iter_next_nop_entry,
807 .add_next_entry = iter_add_next_nop_entry,
808 .finish_entry = iter_finish_normal_entry,
811 const struct hist_iter_ops hist_iter_cumulative = {
812 .prepare_entry = iter_prepare_cumulative_entry,
813 .add_single_entry = iter_add_single_cumulative_entry,
814 .next_entry = iter_next_cumulative_entry,
815 .add_next_entry = iter_add_next_cumulative_entry,
816 .finish_entry = iter_finish_cumulative_entry,
819 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
820 struct perf_evsel *evsel, struct perf_sample *sample,
825 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
831 iter->sample = sample;
833 err = iter->ops->prepare_entry(iter, al);
837 err = iter->ops->add_single_entry(iter, al);
841 while (iter->ops->next_entry(iter, al)) {
842 err = iter->ops->add_next_entry(iter, al);
848 err2 = iter->ops->finish_entry(iter, al);
856 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
858 struct perf_hpp_fmt *fmt;
861 perf_hpp__for_each_sort_list(fmt) {
862 if (perf_hpp__should_skip(fmt))
865 cmp = fmt->cmp(left, right);
874 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
876 struct perf_hpp_fmt *fmt;
879 perf_hpp__for_each_sort_list(fmt) {
880 if (perf_hpp__should_skip(fmt))
883 cmp = fmt->collapse(left, right);
891 void hist_entry__free(struct hist_entry *he)
893 zfree(&he->branch_info);
894 zfree(&he->mem_info);
895 zfree(&he->stat_acc);
896 free_srcline(he->srcline);
901 * collapse the histogram
904 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
905 struct rb_root *root,
906 struct hist_entry *he)
908 struct rb_node **p = &root->rb_node;
909 struct rb_node *parent = NULL;
910 struct hist_entry *iter;
915 iter = rb_entry(parent, struct hist_entry, rb_node_in);
917 cmp = hist_entry__collapse(iter, he);
920 he_stat__add_stat(&iter->stat, &he->stat);
921 if (symbol_conf.cumulate_callchain)
922 he_stat__add_stat(iter->stat_acc, he->stat_acc);
924 if (symbol_conf.use_callchain) {
925 callchain_cursor_reset(&callchain_cursor);
926 callchain_merge(&callchain_cursor,
930 hist_entry__free(he);
940 rb_link_node(&he->rb_node_in, parent, p);
941 rb_insert_color(&he->rb_node_in, root);
945 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
947 struct rb_root *root;
949 pthread_mutex_lock(&hists->lock);
951 root = hists->entries_in;
952 if (++hists->entries_in > &hists->entries_in_array[1])
953 hists->entries_in = &hists->entries_in_array[0];
955 pthread_mutex_unlock(&hists->lock);
960 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
962 hists__filter_entry_by_dso(hists, he);
963 hists__filter_entry_by_thread(hists, he);
964 hists__filter_entry_by_symbol(hists, he);
967 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
969 struct rb_root *root;
970 struct rb_node *next;
971 struct hist_entry *n;
973 if (!sort__need_collapse)
976 root = hists__get_rotate_entries_in(hists);
977 next = rb_first(root);
982 n = rb_entry(next, struct hist_entry, rb_node_in);
983 next = rb_next(&n->rb_node_in);
985 rb_erase(&n->rb_node_in, root);
986 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
988 * If it wasn't combined with one of the entries already
989 * collapsed, we need to apply the filters that may have
990 * been set by, say, the hist_browser.
992 hists__apply_filters(hists, n);
995 ui_progress__update(prog, 1);
999 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1001 struct perf_hpp_fmt *fmt;
1004 perf_hpp__for_each_sort_list(fmt) {
1005 if (perf_hpp__should_skip(fmt))
1008 cmp = fmt->sort(a, b);
1016 static void hists__reset_filter_stats(struct hists *hists)
1018 hists->nr_non_filtered_entries = 0;
1019 hists->stats.total_non_filtered_period = 0;
1022 void hists__reset_stats(struct hists *hists)
1024 hists->nr_entries = 0;
1025 hists->stats.total_period = 0;
1027 hists__reset_filter_stats(hists);
1030 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1032 hists->nr_non_filtered_entries++;
1033 hists->stats.total_non_filtered_period += h->stat.period;
1036 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1039 hists__inc_filter_stats(hists, h);
1041 hists->nr_entries++;
1042 hists->stats.total_period += h->stat.period;
1045 static void __hists__insert_output_entry(struct rb_root *entries,
1046 struct hist_entry *he,
1047 u64 min_callchain_hits)
1049 struct rb_node **p = &entries->rb_node;
1050 struct rb_node *parent = NULL;
1051 struct hist_entry *iter;
1053 if (symbol_conf.use_callchain)
1054 callchain_param.sort(&he->sorted_chain, he->callchain,
1055 min_callchain_hits, &callchain_param);
1057 while (*p != NULL) {
1059 iter = rb_entry(parent, struct hist_entry, rb_node);
1061 if (hist_entry__sort(he, iter) > 0)
1064 p = &(*p)->rb_right;
1067 rb_link_node(&he->rb_node, parent, p);
1068 rb_insert_color(&he->rb_node, entries);
1071 void hists__output_resort(struct hists *hists)
1073 struct rb_root *root;
1074 struct rb_node *next;
1075 struct hist_entry *n;
1076 u64 min_callchain_hits;
1078 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
1080 if (sort__need_collapse)
1081 root = &hists->entries_collapsed;
1083 root = hists->entries_in;
1085 next = rb_first(root);
1086 hists->entries = RB_ROOT;
1088 hists__reset_stats(hists);
1089 hists__reset_col_len(hists);
1092 n = rb_entry(next, struct hist_entry, rb_node_in);
1093 next = rb_next(&n->rb_node_in);
1095 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
1096 hists__inc_stats(hists, n);
1099 hists__calc_col_len(hists, n);
1103 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1104 enum hist_filter filter)
1106 h->filtered &= ~(1 << filter);
1110 /* force fold unfiltered entry for simplicity */
1111 h->ms.unfolded = false;
1114 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1116 hists__inc_filter_stats(hists, h);
1117 hists__calc_col_len(hists, h);
1121 static bool hists__filter_entry_by_dso(struct hists *hists,
1122 struct hist_entry *he)
1124 if (hists->dso_filter != NULL &&
1125 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1126 he->filtered |= (1 << HIST_FILTER__DSO);
1133 void hists__filter_by_dso(struct hists *hists)
1137 hists->stats.nr_non_filtered_samples = 0;
1139 hists__reset_filter_stats(hists);
1140 hists__reset_col_len(hists);
1142 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1143 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1145 if (symbol_conf.exclude_other && !h->parent)
1148 if (hists__filter_entry_by_dso(hists, h))
1151 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1155 static bool hists__filter_entry_by_thread(struct hists *hists,
1156 struct hist_entry *he)
1158 if (hists->thread_filter != NULL &&
1159 he->thread != hists->thread_filter) {
1160 he->filtered |= (1 << HIST_FILTER__THREAD);
1167 void hists__filter_by_thread(struct hists *hists)
1171 hists->stats.nr_non_filtered_samples = 0;
1173 hists__reset_filter_stats(hists);
1174 hists__reset_col_len(hists);
1176 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1177 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1179 if (hists__filter_entry_by_thread(hists, h))
1182 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1186 static bool hists__filter_entry_by_symbol(struct hists *hists,
1187 struct hist_entry *he)
1189 if (hists->symbol_filter_str != NULL &&
1190 (!he->ms.sym || strstr(he->ms.sym->name,
1191 hists->symbol_filter_str) == NULL)) {
1192 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1199 void hists__filter_by_symbol(struct hists *hists)
1203 hists->stats.nr_non_filtered_samples = 0;
1205 hists__reset_filter_stats(hists);
1206 hists__reset_col_len(hists);
1208 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1209 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1211 if (hists__filter_entry_by_symbol(hists, h))
1214 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1218 void events_stats__inc(struct events_stats *stats, u32 type)
1220 ++stats->nr_events[0];
1221 ++stats->nr_events[type];
1224 void hists__inc_nr_events(struct hists *hists, u32 type)
1226 events_stats__inc(&hists->stats, type);
1229 void hists__inc_nr_samples(struct hists *hists, bool filtered)
1231 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1233 hists->stats.nr_non_filtered_samples++;
1236 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1237 struct hist_entry *pair)
1239 struct rb_root *root;
1241 struct rb_node *parent = NULL;
1242 struct hist_entry *he;
1245 if (sort__need_collapse)
1246 root = &hists->entries_collapsed;
1248 root = hists->entries_in;
1252 while (*p != NULL) {
1254 he = rb_entry(parent, struct hist_entry, rb_node_in);
1256 cmp = hist_entry__collapse(he, pair);
1264 p = &(*p)->rb_right;
1267 he = hist_entry__new(pair, true);
1269 memset(&he->stat, 0, sizeof(he->stat));
1271 rb_link_node(&he->rb_node_in, parent, p);
1272 rb_insert_color(&he->rb_node_in, root);
1273 hists__inc_stats(hists, he);
1280 static struct hist_entry *hists__find_entry(struct hists *hists,
1281 struct hist_entry *he)
1285 if (sort__need_collapse)
1286 n = hists->entries_collapsed.rb_node;
1288 n = hists->entries_in->rb_node;
1291 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1292 int64_t cmp = hist_entry__collapse(iter, he);
1306 * Look for pairs to link to the leader buckets (hist_entries):
1308 void hists__match(struct hists *leader, struct hists *other)
1310 struct rb_root *root;
1312 struct hist_entry *pos, *pair;
1314 if (sort__need_collapse)
1315 root = &leader->entries_collapsed;
1317 root = leader->entries_in;
1319 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1320 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1321 pair = hists__find_entry(other, pos);
1324 hist_entry__add_pair(pair, pos);
1329 * Look for entries in the other hists that are not present in the leader, if
1330 * we find them, just add a dummy entry on the leader hists, with period=0,
1331 * nr_events=0, to serve as the list header.
1333 int hists__link(struct hists *leader, struct hists *other)
1335 struct rb_root *root;
1337 struct hist_entry *pos, *pair;
1339 if (sort__need_collapse)
1340 root = &other->entries_collapsed;
1342 root = other->entries_in;
1344 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1345 pos = rb_entry(nd, struct hist_entry, rb_node_in);
1347 if (!hist_entry__has_pairs(pos)) {
1348 pair = hists__add_dummy_entry(leader, pos);
1351 hist_entry__add_pair(pos, pair);
1358 u64 hists__total_period(struct hists *hists)
1360 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1361 hists->stats.total_period;
1364 int parse_filter_percentage(const struct option *opt __maybe_unused,
1365 const char *arg, int unset __maybe_unused)
1367 if (!strcmp(arg, "relative"))
1368 symbol_conf.filter_relative = true;
1369 else if (!strcmp(arg, "absolute"))
1370 symbol_conf.filter_relative = false;
1377 int perf_hist_config(const char *var, const char *value)
1379 if (!strcmp(var, "hist.percentage"))
1380 return parse_filter_percentage(NULL, value, 0);