10 #include "ui/progress.h"
13 static bool hists__filter_entry_by_dso(struct hists *hists,
14 struct hist_entry *he);
15 static bool hists__filter_entry_by_thread(struct hists *hists,
16 struct hist_entry *he);
17 static bool hists__filter_entry_by_symbol(struct hists *hists,
18 struct hist_entry *he);
19 static bool hists__filter_entry_by_socket(struct hists *hists,
20 struct hist_entry *he);
22 u16 hists__col_len(struct hists *hists, enum hist_column col)
24 return hists->col_len[col];
27 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
29 hists->col_len[col] = len;
32 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
34 if (len > hists__col_len(hists, col)) {
35 hists__set_col_len(hists, col, len);
41 void hists__reset_col_len(struct hists *hists)
45 for (col = 0; col < HISTC_NR_COLS; ++col)
46 hists__set_col_len(hists, col, 0);
49 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
51 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
53 if (hists__col_len(hists, dso) < unresolved_col_width &&
54 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
55 !symbol_conf.dso_list)
56 hists__set_col_len(hists, dso, unresolved_col_width);
59 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
61 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
66 * +4 accounts for '[x] ' priv level info
67 * +2 accounts for 0x prefix on raw addresses
68 * +3 accounts for ' y ' symtab origin info
71 symlen = h->ms.sym->namelen + 4;
73 symlen += BITS_PER_LONG / 4 + 2 + 3;
74 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
76 symlen = unresolved_col_width + 4 + 2;
77 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
78 hists__set_unres_dso_col_len(hists, HISTC_DSO);
81 len = thread__comm_len(h->thread);
82 if (hists__new_col_len(hists, HISTC_COMM, len))
83 hists__set_col_len(hists, HISTC_THREAD, len + 8);
86 len = dso__name_len(h->ms.map->dso);
87 hists__new_col_len(hists, HISTC_DSO, len);
91 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
94 if (h->branch_info->from.sym) {
95 symlen = (int)h->branch_info->from.sym->namelen + 4;
97 symlen += BITS_PER_LONG / 4 + 2 + 3;
98 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
100 symlen = dso__name_len(h->branch_info->from.map->dso);
101 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
103 symlen = unresolved_col_width + 4 + 2;
104 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
105 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
108 if (h->branch_info->to.sym) {
109 symlen = (int)h->branch_info->to.sym->namelen + 4;
111 symlen += BITS_PER_LONG / 4 + 2 + 3;
112 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
114 symlen = dso__name_len(h->branch_info->to.map->dso);
115 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
117 symlen = unresolved_col_width + 4 + 2;
118 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
119 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
122 if (h->branch_info->srcline_from)
123 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
124 strlen(h->branch_info->srcline_from));
125 if (h->branch_info->srcline_to)
126 hists__new_col_len(hists, HISTC_SRCLINE_TO,
127 strlen(h->branch_info->srcline_to));
131 if (h->mem_info->daddr.sym) {
132 symlen = (int)h->mem_info->daddr.sym->namelen + 4
133 + unresolved_col_width + 2;
134 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
136 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
139 symlen = unresolved_col_width + 4 + 2;
140 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
142 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
146 if (h->mem_info->iaddr.sym) {
147 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
148 + unresolved_col_width + 2;
149 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
152 symlen = unresolved_col_width + 4 + 2;
153 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
157 if (h->mem_info->daddr.map) {
158 symlen = dso__name_len(h->mem_info->daddr.map->dso);
159 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
162 symlen = unresolved_col_width + 4 + 2;
163 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
166 symlen = unresolved_col_width + 4 + 2;
167 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
168 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
169 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
172 hists__new_col_len(hists, HISTC_CPU, 3);
173 hists__new_col_len(hists, HISTC_SOCKET, 6);
174 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
175 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
176 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
177 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
178 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
179 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
182 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
183 hists__new_col_len(hists, HISTC_SRCLINE, len);
187 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
190 hists__new_col_len(hists, HISTC_TRANSACTION,
191 hist_entry__transaction_len());
194 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
197 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
199 struct rb_node *next = rb_first(&hists->entries);
200 struct hist_entry *n;
203 hists__reset_col_len(hists);
205 while (next && row++ < max_rows) {
206 n = rb_entry(next, struct hist_entry, rb_node);
208 hists__calc_col_len(hists, n);
209 next = rb_next(&n->rb_node);
213 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
214 unsigned int cpumode, u64 period)
217 case PERF_RECORD_MISC_KERNEL:
218 he_stat->period_sys += period;
220 case PERF_RECORD_MISC_USER:
221 he_stat->period_us += period;
223 case PERF_RECORD_MISC_GUEST_KERNEL:
224 he_stat->period_guest_sys += period;
226 case PERF_RECORD_MISC_GUEST_USER:
227 he_stat->period_guest_us += period;
234 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
238 he_stat->period += period;
239 he_stat->weight += weight;
240 he_stat->nr_events += 1;
243 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
245 dest->period += src->period;
246 dest->period_sys += src->period_sys;
247 dest->period_us += src->period_us;
248 dest->period_guest_sys += src->period_guest_sys;
249 dest->period_guest_us += src->period_guest_us;
250 dest->nr_events += src->nr_events;
251 dest->weight += src->weight;
254 static void he_stat__decay(struct he_stat *he_stat)
256 he_stat->period = (he_stat->period * 7) / 8;
257 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
258 /* XXX need decay for weight too? */
261 static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
263 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
265 u64 prev_period = he->stat.period;
268 if (prev_period == 0)
271 he_stat__decay(&he->stat);
272 if (symbol_conf.cumulate_callchain)
273 he_stat__decay(he->stat_acc);
274 decay_callchain(he->callchain);
276 diff = prev_period - he->stat.period;
279 hists->stats.total_period -= diff;
281 hists->stats.total_non_filtered_period -= diff;
285 struct hist_entry *child;
286 struct rb_node *node = rb_first(&he->hroot_out);
288 child = rb_entry(node, struct hist_entry, rb_node);
289 node = rb_next(node);
291 if (hists__decay_entry(hists, child))
292 hists__delete_entry(hists, child);
296 return he->stat.period == 0;
299 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
301 struct rb_root *root_in;
302 struct rb_root *root_out;
305 root_in = &he->parent_he->hroot_in;
306 root_out = &he->parent_he->hroot_out;
308 if (hists__has(hists, need_collapse))
309 root_in = &hists->entries_collapsed;
311 root_in = hists->entries_in;
312 root_out = &hists->entries;
315 rb_erase(&he->rb_node_in, root_in);
316 rb_erase(&he->rb_node, root_out);
320 --hists->nr_non_filtered_entries;
322 hist_entry__delete(he);
325 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
327 struct rb_node *next = rb_first(&hists->entries);
328 struct hist_entry *n;
331 n = rb_entry(next, struct hist_entry, rb_node);
332 next = rb_next(&n->rb_node);
333 if (((zap_user && n->level == '.') ||
334 (zap_kernel && n->level != '.') ||
335 hists__decay_entry(hists, n))) {
336 hists__delete_entry(hists, n);
341 void hists__delete_entries(struct hists *hists)
343 struct rb_node *next = rb_first(&hists->entries);
344 struct hist_entry *n;
347 n = rb_entry(next, struct hist_entry, rb_node);
348 next = rb_next(&n->rb_node);
350 hists__delete_entry(hists, n);
355 * histogram, sorted on item, collects periods
358 static int hist_entry__init(struct hist_entry *he,
359 struct hist_entry *template,
364 if (symbol_conf.cumulate_callchain) {
365 he->stat_acc = malloc(sizeof(he->stat));
366 if (he->stat_acc == NULL)
368 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
370 memset(&he->stat, 0, sizeof(he->stat));
373 map__get(he->ms.map);
375 if (he->branch_info) {
377 * This branch info is (a part of) allocated from
378 * sample__resolve_bstack() and will be freed after
379 * adding new entries. So we need to save a copy.
381 he->branch_info = malloc(sizeof(*he->branch_info));
382 if (he->branch_info == NULL) {
383 map__zput(he->ms.map);
388 memcpy(he->branch_info, template->branch_info,
389 sizeof(*he->branch_info));
391 map__get(he->branch_info->from.map);
392 map__get(he->branch_info->to.map);
396 map__get(he->mem_info->iaddr.map);
397 map__get(he->mem_info->daddr.map);
400 if (symbol_conf.use_callchain)
401 callchain_init(he->callchain);
404 he->raw_data = memdup(he->raw_data, he->raw_size);
406 if (he->raw_data == NULL) {
407 map__put(he->ms.map);
408 if (he->branch_info) {
409 map__put(he->branch_info->from.map);
410 map__put(he->branch_info->to.map);
411 free(he->branch_info);
414 map__put(he->mem_info->iaddr.map);
415 map__put(he->mem_info->daddr.map);
421 INIT_LIST_HEAD(&he->pairs.node);
422 thread__get(he->thread);
423 he->hroot_in = RB_ROOT;
424 he->hroot_out = RB_ROOT;
426 if (!symbol_conf.report_hierarchy)
432 static void *hist_entry__zalloc(size_t size)
434 return zalloc(size + sizeof(struct hist_entry));
437 static void hist_entry__free(void *ptr)
442 static struct hist_entry_ops default_ops = {
443 .new = hist_entry__zalloc,
444 .free = hist_entry__free,
447 static struct hist_entry *hist_entry__new(struct hist_entry *template,
450 struct hist_entry_ops *ops = template->ops;
451 size_t callchain_size = 0;
452 struct hist_entry *he;
456 ops = template->ops = &default_ops;
458 if (symbol_conf.use_callchain)
459 callchain_size = sizeof(struct callchain_root);
461 he = ops->new(callchain_size);
463 err = hist_entry__init(he, template, sample_self);
473 static u8 symbol__parent_filter(const struct symbol *parent)
475 if (symbol_conf.exclude_other && parent == NULL)
476 return 1 << HIST_FILTER__PARENT;
480 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
482 if (!symbol_conf.use_callchain)
485 he->hists->callchain_period += period;
487 he->hists->callchain_non_filtered_period += period;
490 static struct hist_entry *hists__findnew_entry(struct hists *hists,
491 struct hist_entry *entry,
492 struct addr_location *al,
496 struct rb_node *parent = NULL;
497 struct hist_entry *he;
499 u64 period = entry->stat.period;
500 u64 weight = entry->stat.weight;
502 p = &hists->entries_in->rb_node;
506 he = rb_entry(parent, struct hist_entry, rb_node_in);
509 * Make sure that it receives arguments in a same order as
510 * hist_entry__collapse() so that we can use an appropriate
511 * function when searching an entry regardless which sort
514 cmp = hist_entry__cmp(he, entry);
518 he_stat__add_period(&he->stat, period, weight);
519 hist_entry__add_callchain_period(he, period);
521 if (symbol_conf.cumulate_callchain)
522 he_stat__add_period(he->stat_acc, period, weight);
525 * This mem info was allocated from sample__resolve_mem
526 * and will not be used anymore.
528 zfree(&entry->mem_info);
530 /* If the map of an existing hist_entry has
531 * become out-of-date due to an exec() or
532 * similar, update it. Otherwise we will
533 * mis-adjust symbol addresses when computing
534 * the history counter to increment.
536 if (he->ms.map != entry->ms.map) {
537 map__put(he->ms.map);
538 he->ms.map = map__get(entry->ms.map);
549 he = hist_entry__new(entry, sample_self);
554 hist_entry__add_callchain_period(he, period);
557 rb_link_node(&he->rb_node_in, parent, p);
558 rb_insert_color(&he->rb_node_in, hists->entries_in);
561 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
562 if (symbol_conf.cumulate_callchain)
563 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
567 static struct hist_entry*
568 __hists__add_entry(struct hists *hists,
569 struct addr_location *al,
570 struct symbol *sym_parent,
571 struct branch_info *bi,
573 struct perf_sample *sample,
575 struct hist_entry_ops *ops)
577 struct hist_entry entry = {
578 .thread = al->thread,
579 .comm = thread__comm(al->thread),
584 .socket = al->socket,
586 .cpumode = al->cpumode,
591 .period = sample->period,
592 .weight = sample->weight,
594 .parent = sym_parent,
595 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
599 .transaction = sample->transaction,
600 .raw_data = sample->raw_data,
601 .raw_size = sample->raw_size,
605 return hists__findnew_entry(hists, &entry, al, sample_self);
608 struct hist_entry *hists__add_entry(struct hists *hists,
609 struct addr_location *al,
610 struct symbol *sym_parent,
611 struct branch_info *bi,
613 struct perf_sample *sample,
616 return __hists__add_entry(hists, al, sym_parent, bi, mi,
617 sample, sample_self, NULL);
620 struct hist_entry *hists__add_entry_ops(struct hists *hists,
621 struct hist_entry_ops *ops,
622 struct addr_location *al,
623 struct symbol *sym_parent,
624 struct branch_info *bi,
626 struct perf_sample *sample,
629 return __hists__add_entry(hists, al, sym_parent, bi, mi,
630 sample, sample_self, ops);
634 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
635 struct addr_location *al __maybe_unused)
641 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
642 struct addr_location *al __maybe_unused)
648 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
650 struct perf_sample *sample = iter->sample;
653 mi = sample__resolve_mem(sample, al);
662 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
665 struct mem_info *mi = iter->priv;
666 struct hists *hists = evsel__hists(iter->evsel);
667 struct perf_sample *sample = iter->sample;
668 struct hist_entry *he;
673 cost = sample->weight;
678 * must pass period=weight in order to get the correct
679 * sorting from hists__collapse_resort() which is solely
680 * based on periods. We want sorting be done on nr_events * weight
681 * and this is indirectly achieved by passing period=weight here
682 * and the he_stat__add_period() function.
684 sample->period = cost;
686 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
696 iter_finish_mem_entry(struct hist_entry_iter *iter,
697 struct addr_location *al __maybe_unused)
699 struct perf_evsel *evsel = iter->evsel;
700 struct hists *hists = evsel__hists(evsel);
701 struct hist_entry *he = iter->he;
707 hists__inc_nr_samples(hists, he->filtered);
709 err = hist_entry__append_callchain(he, iter->sample);
713 * We don't need to free iter->priv (mem_info) here since the mem info
714 * was either already freed in hists__findnew_entry() or passed to a
715 * new hist entry by hist_entry__new().
724 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
726 struct branch_info *bi;
727 struct perf_sample *sample = iter->sample;
729 bi = sample__resolve_bstack(sample, al);
734 iter->total = sample->branch_stack->nr;
741 iter_add_single_branch_entry(struct hist_entry_iter *iter,
742 struct addr_location *al __maybe_unused)
744 /* to avoid calling callback function */
751 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
753 struct branch_info *bi = iter->priv;
759 if (iter->curr >= iter->total)
762 al->map = bi[i].to.map;
763 al->sym = bi[i].to.sym;
764 al->addr = bi[i].to.addr;
769 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
771 struct branch_info *bi;
772 struct perf_evsel *evsel = iter->evsel;
773 struct hists *hists = evsel__hists(evsel);
774 struct perf_sample *sample = iter->sample;
775 struct hist_entry *he = NULL;
781 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
785 * The report shows the percentage of total branches captured
786 * and not events sampled. Thus we use a pseudo period of 1.
789 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
791 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
796 hists__inc_nr_samples(hists, he->filtered);
805 iter_finish_branch_entry(struct hist_entry_iter *iter,
806 struct addr_location *al __maybe_unused)
811 return iter->curr >= iter->total ? 0 : -1;
815 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
816 struct addr_location *al __maybe_unused)
822 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
824 struct perf_evsel *evsel = iter->evsel;
825 struct perf_sample *sample = iter->sample;
826 struct hist_entry *he;
828 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
838 iter_finish_normal_entry(struct hist_entry_iter *iter,
839 struct addr_location *al __maybe_unused)
841 struct hist_entry *he = iter->he;
842 struct perf_evsel *evsel = iter->evsel;
843 struct perf_sample *sample = iter->sample;
850 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
852 return hist_entry__append_callchain(he, sample);
856 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
857 struct addr_location *al __maybe_unused)
859 struct hist_entry **he_cache;
861 callchain_cursor_commit(&callchain_cursor);
864 * This is for detecting cycles or recursions so that they're
865 * cumulated only one time to prevent entries more than 100%
868 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
869 if (he_cache == NULL)
872 iter->priv = he_cache;
879 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
880 struct addr_location *al)
882 struct perf_evsel *evsel = iter->evsel;
883 struct hists *hists = evsel__hists(evsel);
884 struct perf_sample *sample = iter->sample;
885 struct hist_entry **he_cache = iter->priv;
886 struct hist_entry *he;
889 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
895 he_cache[iter->curr++] = he;
897 hist_entry__append_callchain(he, sample);
900 * We need to re-initialize the cursor since callchain_append()
901 * advanced the cursor to the end.
903 callchain_cursor_commit(&callchain_cursor);
905 hists__inc_nr_samples(hists, he->filtered);
911 iter_next_cumulative_entry(struct hist_entry_iter *iter,
912 struct addr_location *al)
914 struct callchain_cursor_node *node;
916 node = callchain_cursor_current(&callchain_cursor);
920 return fill_callchain_info(al, node, iter->hide_unresolved);
924 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
925 struct addr_location *al)
927 struct perf_evsel *evsel = iter->evsel;
928 struct perf_sample *sample = iter->sample;
929 struct hist_entry **he_cache = iter->priv;
930 struct hist_entry *he;
931 struct hist_entry he_tmp = {
932 .hists = evsel__hists(evsel),
934 .thread = al->thread,
935 .comm = thread__comm(al->thread),
941 .parent = iter->parent,
942 .raw_data = sample->raw_data,
943 .raw_size = sample->raw_size,
946 struct callchain_cursor cursor;
948 callchain_cursor_snapshot(&cursor, &callchain_cursor);
950 callchain_cursor_advance(&callchain_cursor);
953 * Check if there's duplicate entries in the callchain.
954 * It's possible that it has cycles or recursive calls.
956 for (i = 0; i < iter->curr; i++) {
957 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
958 /* to avoid calling callback function */
964 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
970 he_cache[iter->curr++] = he;
972 if (symbol_conf.use_callchain)
973 callchain_append(he->callchain, &cursor, sample->period);
978 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
979 struct addr_location *al __maybe_unused)
987 const struct hist_iter_ops hist_iter_mem = {
988 .prepare_entry = iter_prepare_mem_entry,
989 .add_single_entry = iter_add_single_mem_entry,
990 .next_entry = iter_next_nop_entry,
991 .add_next_entry = iter_add_next_nop_entry,
992 .finish_entry = iter_finish_mem_entry,
995 const struct hist_iter_ops hist_iter_branch = {
996 .prepare_entry = iter_prepare_branch_entry,
997 .add_single_entry = iter_add_single_branch_entry,
998 .next_entry = iter_next_branch_entry,
999 .add_next_entry = iter_add_next_branch_entry,
1000 .finish_entry = iter_finish_branch_entry,
1003 const struct hist_iter_ops hist_iter_normal = {
1004 .prepare_entry = iter_prepare_normal_entry,
1005 .add_single_entry = iter_add_single_normal_entry,
1006 .next_entry = iter_next_nop_entry,
1007 .add_next_entry = iter_add_next_nop_entry,
1008 .finish_entry = iter_finish_normal_entry,
1011 const struct hist_iter_ops hist_iter_cumulative = {
1012 .prepare_entry = iter_prepare_cumulative_entry,
1013 .add_single_entry = iter_add_single_cumulative_entry,
1014 .next_entry = iter_next_cumulative_entry,
1015 .add_next_entry = iter_add_next_cumulative_entry,
1016 .finish_entry = iter_finish_cumulative_entry,
1019 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1020 int max_stack_depth, void *arg)
1023 struct map *alm = NULL;
1026 alm = map__get(al->map);
1028 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1029 iter->evsel, al, max_stack_depth);
1035 err = iter->ops->prepare_entry(iter, al);
1039 err = iter->ops->add_single_entry(iter, al);
1043 if (iter->he && iter->add_entry_cb) {
1044 err = iter->add_entry_cb(iter, al, true, arg);
1049 while (iter->ops->next_entry(iter, al)) {
1050 err = iter->ops->add_next_entry(iter, al);
1054 if (iter->he && iter->add_entry_cb) {
1055 err = iter->add_entry_cb(iter, al, false, arg);
1062 err2 = iter->ops->finish_entry(iter, al);
1072 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1074 struct hists *hists = left->hists;
1075 struct perf_hpp_fmt *fmt;
1078 hists__for_each_sort_list(hists, fmt) {
1079 if (perf_hpp__is_dynamic_entry(fmt) &&
1080 !perf_hpp__defined_dynamic_entry(fmt, hists))
1083 cmp = fmt->cmp(fmt, left, right);
1092 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1094 struct hists *hists = left->hists;
1095 struct perf_hpp_fmt *fmt;
1098 hists__for_each_sort_list(hists, fmt) {
1099 if (perf_hpp__is_dynamic_entry(fmt) &&
1100 !perf_hpp__defined_dynamic_entry(fmt, hists))
1103 cmp = fmt->collapse(fmt, left, right);
1111 void hist_entry__delete(struct hist_entry *he)
1113 struct hist_entry_ops *ops = he->ops;
1115 thread__zput(he->thread);
1116 map__zput(he->ms.map);
1118 if (he->branch_info) {
1119 map__zput(he->branch_info->from.map);
1120 map__zput(he->branch_info->to.map);
1121 free_srcline(he->branch_info->srcline_from);
1122 free_srcline(he->branch_info->srcline_to);
1123 zfree(&he->branch_info);
1127 map__zput(he->mem_info->iaddr.map);
1128 map__zput(he->mem_info->daddr.map);
1129 zfree(&he->mem_info);
1132 zfree(&he->stat_acc);
1133 free_srcline(he->srcline);
1134 if (he->srcfile && he->srcfile[0])
1136 free_callchain(he->callchain);
1137 free(he->trace_output);
1143 * If this is not the last column, then we need to pad it according to the
1144 * pre-calculated max lenght for this column, otherwise don't bother adding
1145 * spaces because that would break viewing this with, for instance, 'less',
1146 * that would show tons of trailing spaces when a long C++ demangled method
1149 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1150 struct perf_hpp_fmt *fmt, int printed)
1152 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1153 const int width = fmt->width(fmt, hpp, he->hists);
1154 if (printed < width) {
1155 advance_hpp(hpp, printed);
1156 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1164 * collapse the histogram
1167 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1168 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1169 enum hist_filter type);
1171 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1173 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1175 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1178 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1179 enum hist_filter type,
1182 struct perf_hpp_fmt *fmt;
1183 bool type_match = false;
1184 struct hist_entry *parent = he->parent_he;
1187 case HIST_FILTER__THREAD:
1188 if (symbol_conf.comm_list == NULL &&
1189 symbol_conf.pid_list == NULL &&
1190 symbol_conf.tid_list == NULL)
1193 case HIST_FILTER__DSO:
1194 if (symbol_conf.dso_list == NULL)
1197 case HIST_FILTER__SYMBOL:
1198 if (symbol_conf.sym_list == NULL)
1201 case HIST_FILTER__PARENT:
1202 case HIST_FILTER__GUEST:
1203 case HIST_FILTER__HOST:
1204 case HIST_FILTER__SOCKET:
1209 /* if it's filtered by own fmt, it has to have filter bits */
1210 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1219 * If the filter is for current level entry, propagate
1220 * filter marker to parents. The marker bit was
1221 * already set by default so it only needs to clear
1222 * non-filtered entries.
1224 if (!(he->filtered & (1 << type))) {
1226 parent->filtered &= ~(1 << type);
1227 parent = parent->parent_he;
1232 * If current entry doesn't have matching formats, set
1233 * filter marker for upper level entries. it will be
1234 * cleared if its lower level entries is not filtered.
1236 * For lower-level entries, it inherits parent's
1237 * filter bit so that lower level entries of a
1238 * non-filtered entry won't set the filter marker.
1241 he->filtered |= (1 << type);
1243 he->filtered |= (parent->filtered & (1 << type));
1247 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1249 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1250 check_thread_entry);
1252 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1253 perf_hpp__is_dso_entry);
1255 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1256 perf_hpp__is_sym_entry);
1258 hists__apply_filters(he->hists, he);
1261 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1262 struct rb_root *root,
1263 struct hist_entry *he,
1264 struct hist_entry *parent_he,
1265 struct perf_hpp_list *hpp_list)
1267 struct rb_node **p = &root->rb_node;
1268 struct rb_node *parent = NULL;
1269 struct hist_entry *iter, *new;
1270 struct perf_hpp_fmt *fmt;
1273 while (*p != NULL) {
1275 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1278 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1279 cmp = fmt->collapse(fmt, iter, he);
1285 he_stat__add_stat(&iter->stat, &he->stat);
1290 p = &parent->rb_left;
1292 p = &parent->rb_right;
1295 new = hist_entry__new(he, true);
1299 hists->nr_entries++;
1301 /* save related format list for output */
1302 new->hpp_list = hpp_list;
1303 new->parent_he = parent_he;
1305 hist_entry__apply_hierarchy_filters(new);
1307 /* some fields are now passed to 'new' */
1308 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1309 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1310 he->trace_output = NULL;
1312 new->trace_output = NULL;
1314 if (perf_hpp__is_srcline_entry(fmt))
1317 new->srcline = NULL;
1319 if (perf_hpp__is_srcfile_entry(fmt))
1322 new->srcfile = NULL;
1325 rb_link_node(&new->rb_node_in, parent, p);
1326 rb_insert_color(&new->rb_node_in, root);
1330 static int hists__hierarchy_insert_entry(struct hists *hists,
1331 struct rb_root *root,
1332 struct hist_entry *he)
1334 struct perf_hpp_list_node *node;
1335 struct hist_entry *new_he = NULL;
1336 struct hist_entry *parent = NULL;
1340 list_for_each_entry(node, &hists->hpp_formats, list) {
1341 /* skip period (overhead) and elided columns */
1342 if (node->level == 0 || node->skip)
1345 /* insert copy of 'he' for each fmt into the hierarchy */
1346 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1347 if (new_he == NULL) {
1352 root = &new_he->hroot_in;
1353 new_he->depth = depth++;
1358 new_he->leaf = true;
1360 if (symbol_conf.use_callchain) {
1361 callchain_cursor_reset(&callchain_cursor);
1362 if (callchain_merge(&callchain_cursor,
1369 /* 'he' is no longer used */
1370 hist_entry__delete(he);
1372 /* return 0 (or -1) since it already applied filters */
1376 static int hists__collapse_insert_entry(struct hists *hists,
1377 struct rb_root *root,
1378 struct hist_entry *he)
1380 struct rb_node **p = &root->rb_node;
1381 struct rb_node *parent = NULL;
1382 struct hist_entry *iter;
1385 if (symbol_conf.report_hierarchy)
1386 return hists__hierarchy_insert_entry(hists, root, he);
1388 while (*p != NULL) {
1390 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1392 cmp = hist_entry__collapse(iter, he);
1397 he_stat__add_stat(&iter->stat, &he->stat);
1398 if (symbol_conf.cumulate_callchain)
1399 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1401 if (symbol_conf.use_callchain) {
1402 callchain_cursor_reset(&callchain_cursor);
1403 if (callchain_merge(&callchain_cursor,
1408 hist_entry__delete(he);
1415 p = &(*p)->rb_right;
1417 hists->nr_entries++;
1419 rb_link_node(&he->rb_node_in, parent, p);
1420 rb_insert_color(&he->rb_node_in, root);
1424 struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
1426 struct rb_root *root;
1428 pthread_mutex_lock(&hists->lock);
1430 root = hists->entries_in;
1431 if (++hists->entries_in > &hists->entries_in_array[1])
1432 hists->entries_in = &hists->entries_in_array[0];
1434 pthread_mutex_unlock(&hists->lock);
1439 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1441 hists__filter_entry_by_dso(hists, he);
1442 hists__filter_entry_by_thread(hists, he);
1443 hists__filter_entry_by_symbol(hists, he);
1444 hists__filter_entry_by_socket(hists, he);
1447 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1449 struct rb_root *root;
1450 struct rb_node *next;
1451 struct hist_entry *n;
1454 if (!hists__has(hists, need_collapse))
1457 hists->nr_entries = 0;
1459 root = hists__get_rotate_entries_in(hists);
1461 next = rb_first(root);
1466 n = rb_entry(next, struct hist_entry, rb_node_in);
1467 next = rb_next(&n->rb_node_in);
1469 rb_erase(&n->rb_node_in, root);
1470 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1476 * If it wasn't combined with one of the entries already
1477 * collapsed, we need to apply the filters that may have
1478 * been set by, say, the hist_browser.
1480 hists__apply_filters(hists, n);
1483 ui_progress__update(prog, 1);
1488 static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1490 struct hists *hists = a->hists;
1491 struct perf_hpp_fmt *fmt;
1494 hists__for_each_sort_list(hists, fmt) {
1495 if (perf_hpp__should_skip(fmt, a->hists))
1498 cmp = fmt->sort(fmt, a, b);
1506 static void hists__reset_filter_stats(struct hists *hists)
1508 hists->nr_non_filtered_entries = 0;
1509 hists->stats.total_non_filtered_period = 0;
1512 void hists__reset_stats(struct hists *hists)
1514 hists->nr_entries = 0;
1515 hists->stats.total_period = 0;
1517 hists__reset_filter_stats(hists);
1520 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1522 hists->nr_non_filtered_entries++;
1523 hists->stats.total_non_filtered_period += h->stat.period;
1526 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1529 hists__inc_filter_stats(hists, h);
1531 hists->nr_entries++;
1532 hists->stats.total_period += h->stat.period;
1535 static void hierarchy_recalc_total_periods(struct hists *hists)
1537 struct rb_node *node;
1538 struct hist_entry *he;
1540 node = rb_first(&hists->entries);
1542 hists->stats.total_period = 0;
1543 hists->stats.total_non_filtered_period = 0;
1546 * recalculate total period using top-level entries only
1547 * since lower level entries only see non-filtered entries
1548 * but upper level entries have sum of both entries.
1551 he = rb_entry(node, struct hist_entry, rb_node);
1552 node = rb_next(node);
1554 hists->stats.total_period += he->stat.period;
1556 hists->stats.total_non_filtered_period += he->stat.period;
1560 static void hierarchy_insert_output_entry(struct rb_root *root,
1561 struct hist_entry *he)
1563 struct rb_node **p = &root->rb_node;
1564 struct rb_node *parent = NULL;
1565 struct hist_entry *iter;
1566 struct perf_hpp_fmt *fmt;
1568 while (*p != NULL) {
1570 iter = rb_entry(parent, struct hist_entry, rb_node);
1572 if (hist_entry__sort(he, iter) > 0)
1573 p = &parent->rb_left;
1575 p = &parent->rb_right;
1578 rb_link_node(&he->rb_node, parent, p);
1579 rb_insert_color(&he->rb_node, root);
1581 /* update column width of dynamic entry */
1582 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1583 if (perf_hpp__is_dynamic_entry(fmt))
1584 fmt->sort(fmt, he, NULL);
1588 static void hists__hierarchy_output_resort(struct hists *hists,
1589 struct ui_progress *prog,
1590 struct rb_root *root_in,
1591 struct rb_root *root_out,
1592 u64 min_callchain_hits,
1595 struct rb_node *node;
1596 struct hist_entry *he;
1598 *root_out = RB_ROOT;
1599 node = rb_first(root_in);
1602 he = rb_entry(node, struct hist_entry, rb_node_in);
1603 node = rb_next(node);
1605 hierarchy_insert_output_entry(root_out, he);
1608 ui_progress__update(prog, 1);
1610 hists->nr_entries++;
1611 if (!he->filtered) {
1612 hists->nr_non_filtered_entries++;
1613 hists__calc_col_len(hists, he);
1617 hists__hierarchy_output_resort(hists, prog,
1628 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1629 u64 total = he->stat.period;
1631 if (symbol_conf.cumulate_callchain)
1632 total = he->stat_acc->period;
1634 min_callchain_hits = total * (callchain_param.min_percent / 100);
1637 callchain_param.sort(&he->sorted_chain, he->callchain,
1638 min_callchain_hits, &callchain_param);
1642 static void __hists__insert_output_entry(struct rb_root *entries,
1643 struct hist_entry *he,
1644 u64 min_callchain_hits,
1647 struct rb_node **p = &entries->rb_node;
1648 struct rb_node *parent = NULL;
1649 struct hist_entry *iter;
1650 struct perf_hpp_fmt *fmt;
1652 if (use_callchain) {
1653 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1654 u64 total = he->stat.period;
1656 if (symbol_conf.cumulate_callchain)
1657 total = he->stat_acc->period;
1659 min_callchain_hits = total * (callchain_param.min_percent / 100);
1661 callchain_param.sort(&he->sorted_chain, he->callchain,
1662 min_callchain_hits, &callchain_param);
1665 while (*p != NULL) {
1667 iter = rb_entry(parent, struct hist_entry, rb_node);
1669 if (hist_entry__sort(he, iter) > 0)
1672 p = &(*p)->rb_right;
1675 rb_link_node(&he->rb_node, parent, p);
1676 rb_insert_color(&he->rb_node, entries);
1678 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1679 if (perf_hpp__is_dynamic_entry(fmt) &&
1680 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1681 fmt->sort(fmt, he, NULL); /* update column width */
1685 static void output_resort(struct hists *hists, struct ui_progress *prog,
1686 bool use_callchain, hists__resort_cb_t cb)
1688 struct rb_root *root;
1689 struct rb_node *next;
1690 struct hist_entry *n;
1691 u64 callchain_total;
1692 u64 min_callchain_hits;
1694 callchain_total = hists->callchain_period;
1695 if (symbol_conf.filter_relative)
1696 callchain_total = hists->callchain_non_filtered_period;
1698 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1700 hists__reset_stats(hists);
1701 hists__reset_col_len(hists);
1703 if (symbol_conf.report_hierarchy) {
1704 hists__hierarchy_output_resort(hists, prog,
1705 &hists->entries_collapsed,
1709 hierarchy_recalc_total_periods(hists);
1713 if (hists__has(hists, need_collapse))
1714 root = &hists->entries_collapsed;
1716 root = hists->entries_in;
1718 next = rb_first(root);
1719 hists->entries = RB_ROOT;
1722 n = rb_entry(next, struct hist_entry, rb_node_in);
1723 next = rb_next(&n->rb_node_in);
1728 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1729 hists__inc_stats(hists, n);
1732 hists__calc_col_len(hists, n);
1735 ui_progress__update(prog, 1);
1739 void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
1743 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1744 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1746 use_callchain = symbol_conf.use_callchain;
1748 output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
1751 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1753 output_resort(hists, prog, symbol_conf.use_callchain, NULL);
1756 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1757 hists__resort_cb_t cb)
1759 output_resort(hists, prog, symbol_conf.use_callchain, cb);
1762 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1764 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1767 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1773 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1775 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1777 while (can_goto_child(he, HMD_NORMAL)) {
1778 node = rb_last(&he->hroot_out);
1779 he = rb_entry(node, struct hist_entry, rb_node);
1784 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1786 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1788 if (can_goto_child(he, hmd))
1789 node = rb_first(&he->hroot_out);
1791 node = rb_next(node);
1793 while (node == NULL) {
1798 node = rb_next(&he->rb_node);
1803 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1805 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1807 node = rb_prev(node);
1809 return rb_hierarchy_last(node);
1815 return &he->rb_node;
1818 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1820 struct rb_node *node;
1821 struct hist_entry *child;
1827 node = rb_first(&he->hroot_out);
1828 child = rb_entry(node, struct hist_entry, rb_node);
1830 while (node && child->filtered) {
1831 node = rb_next(node);
1832 child = rb_entry(node, struct hist_entry, rb_node);
1836 percent = hist_entry__get_percent_limit(child);
1840 return node && percent >= limit;
1843 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1844 enum hist_filter filter)
1846 h->filtered &= ~(1 << filter);
1848 if (symbol_conf.report_hierarchy) {
1849 struct hist_entry *parent = h->parent_he;
1852 he_stat__add_stat(&parent->stat, &h->stat);
1854 parent->filtered &= ~(1 << filter);
1856 if (parent->filtered)
1859 /* force fold unfiltered entry for simplicity */
1860 parent->unfolded = false;
1861 parent->has_no_entry = false;
1862 parent->row_offset = 0;
1863 parent->nr_rows = 0;
1865 parent = parent->parent_he;
1872 /* force fold unfiltered entry for simplicity */
1873 h->unfolded = false;
1874 h->has_no_entry = false;
1878 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1880 hists__inc_filter_stats(hists, h);
1881 hists__calc_col_len(hists, h);
1885 static bool hists__filter_entry_by_dso(struct hists *hists,
1886 struct hist_entry *he)
1888 if (hists->dso_filter != NULL &&
1889 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1890 he->filtered |= (1 << HIST_FILTER__DSO);
1897 static bool hists__filter_entry_by_thread(struct hists *hists,
1898 struct hist_entry *he)
1900 if (hists->thread_filter != NULL &&
1901 he->thread != hists->thread_filter) {
1902 he->filtered |= (1 << HIST_FILTER__THREAD);
1909 static bool hists__filter_entry_by_symbol(struct hists *hists,
1910 struct hist_entry *he)
1912 if (hists->symbol_filter_str != NULL &&
1913 (!he->ms.sym || strstr(he->ms.sym->name,
1914 hists->symbol_filter_str) == NULL)) {
1915 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1922 static bool hists__filter_entry_by_socket(struct hists *hists,
1923 struct hist_entry *he)
1925 if ((hists->socket_filter > -1) &&
1926 (he->socket != hists->socket_filter)) {
1927 he->filtered |= (1 << HIST_FILTER__SOCKET);
1934 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1936 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
1940 hists->stats.nr_non_filtered_samples = 0;
1942 hists__reset_filter_stats(hists);
1943 hists__reset_col_len(hists);
1945 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1946 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1948 if (filter(hists, h))
1951 hists__remove_entry_filter(hists, h, type);
1955 static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1957 struct rb_node **p = &root->rb_node;
1958 struct rb_node *parent = NULL;
1959 struct hist_entry *iter;
1960 struct rb_root new_root = RB_ROOT;
1963 while (*p != NULL) {
1965 iter = rb_entry(parent, struct hist_entry, rb_node);
1967 if (hist_entry__sort(he, iter) > 0)
1970 p = &(*p)->rb_right;
1973 rb_link_node(&he->rb_node, parent, p);
1974 rb_insert_color(&he->rb_node, root);
1976 if (he->leaf || he->filtered)
1979 nd = rb_first(&he->hroot_out);
1981 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1984 rb_erase(&h->rb_node, &he->hroot_out);
1986 resort_filtered_entry(&new_root, h);
1989 he->hroot_out = new_root;
1992 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
1995 struct rb_root new_root = RB_ROOT;
1997 hists->stats.nr_non_filtered_samples = 0;
1999 hists__reset_filter_stats(hists);
2000 hists__reset_col_len(hists);
2002 nd = rb_first(&hists->entries);
2004 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2007 ret = hist_entry__filter(h, type, arg);
2010 * case 1. non-matching type
2011 * zero out the period, set filter marker and move to child
2014 memset(&h->stat, 0, sizeof(h->stat));
2015 h->filtered |= (1 << type);
2017 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2020 * case 2. matched type (filter out)
2021 * set filter marker and move to next
2023 else if (ret == 1) {
2024 h->filtered |= (1 << type);
2026 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2029 * case 3. ok (not filtered)
2030 * add period to hists and parents, erase the filter marker
2031 * and move to next sibling
2034 hists__remove_entry_filter(hists, h, type);
2036 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2040 hierarchy_recalc_total_periods(hists);
2043 * resort output after applying a new filter since filter in a lower
2044 * hierarchy can change periods in a upper hierarchy.
2046 nd = rb_first(&hists->entries);
2048 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2051 rb_erase(&h->rb_node, &hists->entries);
2053 resort_filtered_entry(&new_root, h);
2056 hists->entries = new_root;
2059 void hists__filter_by_thread(struct hists *hists)
2061 if (symbol_conf.report_hierarchy)
2062 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2063 hists->thread_filter);
2065 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2066 hists__filter_entry_by_thread);
2069 void hists__filter_by_dso(struct hists *hists)
2071 if (symbol_conf.report_hierarchy)
2072 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2075 hists__filter_by_type(hists, HIST_FILTER__DSO,
2076 hists__filter_entry_by_dso);
2079 void hists__filter_by_symbol(struct hists *hists)
2081 if (symbol_conf.report_hierarchy)
2082 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2083 hists->symbol_filter_str);
2085 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2086 hists__filter_entry_by_symbol);
2089 void hists__filter_by_socket(struct hists *hists)
2091 if (symbol_conf.report_hierarchy)
2092 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2093 &hists->socket_filter);
2095 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2096 hists__filter_entry_by_socket);
2099 void events_stats__inc(struct events_stats *stats, u32 type)
2101 ++stats->nr_events[0];
2102 ++stats->nr_events[type];
2105 void hists__inc_nr_events(struct hists *hists, u32 type)
2107 events_stats__inc(&hists->stats, type);
2110 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2112 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2114 hists->stats.nr_non_filtered_samples++;
2117 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2118 struct hist_entry *pair)
2120 struct rb_root *root;
2122 struct rb_node *parent = NULL;
2123 struct hist_entry *he;
2126 if (hists__has(hists, need_collapse))
2127 root = &hists->entries_collapsed;
2129 root = hists->entries_in;
2133 while (*p != NULL) {
2135 he = rb_entry(parent, struct hist_entry, rb_node_in);
2137 cmp = hist_entry__collapse(he, pair);
2145 p = &(*p)->rb_right;
2148 he = hist_entry__new(pair, true);
2150 memset(&he->stat, 0, sizeof(he->stat));
2152 if (symbol_conf.cumulate_callchain)
2153 memset(he->stat_acc, 0, sizeof(he->stat));
2154 rb_link_node(&he->rb_node_in, parent, p);
2155 rb_insert_color(&he->rb_node_in, root);
2156 hists__inc_stats(hists, he);
2163 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2164 struct rb_root *root,
2165 struct hist_entry *pair)
2168 struct rb_node *parent = NULL;
2169 struct hist_entry *he;
2170 struct perf_hpp_fmt *fmt;
2173 while (*p != NULL) {
2177 he = rb_entry(parent, struct hist_entry, rb_node_in);
2179 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2180 cmp = fmt->collapse(fmt, he, pair);
2188 p = &parent->rb_left;
2190 p = &parent->rb_right;
2193 he = hist_entry__new(pair, true);
2195 rb_link_node(&he->rb_node_in, parent, p);
2196 rb_insert_color(&he->rb_node_in, root);
2200 memset(&he->stat, 0, sizeof(he->stat));
2201 hists__inc_stats(hists, he);
2207 static struct hist_entry *hists__find_entry(struct hists *hists,
2208 struct hist_entry *he)
2212 if (hists__has(hists, need_collapse))
2213 n = hists->entries_collapsed.rb_node;
2215 n = hists->entries_in->rb_node;
2218 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2219 int64_t cmp = hist_entry__collapse(iter, he);
2232 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
2233 struct hist_entry *he)
2235 struct rb_node *n = root->rb_node;
2238 struct hist_entry *iter;
2239 struct perf_hpp_fmt *fmt;
2242 iter = rb_entry(n, struct hist_entry, rb_node_in);
2243 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2244 cmp = fmt->collapse(fmt, iter, he);
2260 static void hists__match_hierarchy(struct rb_root *leader_root,
2261 struct rb_root *other_root)
2264 struct hist_entry *pos, *pair;
2266 for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
2267 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2268 pair = hists__find_hierarchy_entry(other_root, pos);
2271 hist_entry__add_pair(pair, pos);
2272 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2278 * Look for pairs to link to the leader buckets (hist_entries):
2280 void hists__match(struct hists *leader, struct hists *other)
2282 struct rb_root *root;
2284 struct hist_entry *pos, *pair;
2286 if (symbol_conf.report_hierarchy) {
2287 /* hierarchy report always collapses entries */
2288 return hists__match_hierarchy(&leader->entries_collapsed,
2289 &other->entries_collapsed);
2292 if (hists__has(leader, need_collapse))
2293 root = &leader->entries_collapsed;
2295 root = leader->entries_in;
2297 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2298 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2299 pair = hists__find_entry(other, pos);
2302 hist_entry__add_pair(pair, pos);
2306 static int hists__link_hierarchy(struct hists *leader_hists,
2307 struct hist_entry *parent,
2308 struct rb_root *leader_root,
2309 struct rb_root *other_root)
2312 struct hist_entry *pos, *leader;
2314 for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
2315 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2317 if (hist_entry__has_pairs(pos)) {
2320 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2321 if (leader->hists == leader_hists) {
2329 leader = add_dummy_hierarchy_entry(leader_hists,
2334 /* do not point parent in the pos */
2335 leader->parent_he = parent;
2337 hist_entry__add_pair(pos, leader);
2341 if (hists__link_hierarchy(leader_hists, leader,
2343 &pos->hroot_in) < 0)
2351 * Look for entries in the other hists that are not present in the leader, if
2352 * we find them, just add a dummy entry on the leader hists, with period=0,
2353 * nr_events=0, to serve as the list header.
2355 int hists__link(struct hists *leader, struct hists *other)
2357 struct rb_root *root;
2359 struct hist_entry *pos, *pair;
2361 if (symbol_conf.report_hierarchy) {
2362 /* hierarchy report always collapses entries */
2363 return hists__link_hierarchy(leader, NULL,
2364 &leader->entries_collapsed,
2365 &other->entries_collapsed);
2368 if (hists__has(other, need_collapse))
2369 root = &other->entries_collapsed;
2371 root = other->entries_in;
2373 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2374 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2376 if (!hist_entry__has_pairs(pos)) {
2377 pair = hists__add_dummy_entry(leader, pos);
2380 hist_entry__add_pair(pos, pair);
2387 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2388 struct perf_sample *sample, bool nonany_branch_mode)
2390 struct branch_info *bi;
2392 /* If we have branch cycles always annotate them. */
2393 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2396 bi = sample__resolve_bstack(sample, al);
2398 struct addr_map_symbol *prev = NULL;
2401 * Ignore errors, still want to process the
2404 * For non standard branch modes always
2405 * force no IPC (prev == NULL)
2407 * Note that perf stores branches reversed from
2410 for (i = bs->nr - 1; i >= 0; i--) {
2411 addr_map_symbol__account_cycles(&bi[i].from,
2412 nonany_branch_mode ? NULL : prev,
2413 bi[i].flags.cycles);
2421 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2423 struct perf_evsel *pos;
2426 evlist__for_each_entry(evlist, pos) {
2427 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2428 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2435 u64 hists__total_period(struct hists *hists)
2437 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2438 hists->stats.total_period;
2441 int parse_filter_percentage(const struct option *opt __maybe_unused,
2442 const char *arg, int unset __maybe_unused)
2444 if (!strcmp(arg, "relative"))
2445 symbol_conf.filter_relative = true;
2446 else if (!strcmp(arg, "absolute"))
2447 symbol_conf.filter_relative = false;
2454 int perf_hist_config(const char *var, const char *value)
2456 if (!strcmp(var, "hist.percentage"))
2457 return parse_filter_percentage(NULL, value, 0);
2462 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2464 memset(hists, 0, sizeof(*hists));
2465 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2466 hists->entries_in = &hists->entries_in_array[0];
2467 hists->entries_collapsed = RB_ROOT;
2468 hists->entries = RB_ROOT;
2469 pthread_mutex_init(&hists->lock, NULL);
2470 hists->socket_filter = -1;
2471 hists->hpp_list = hpp_list;
2472 INIT_LIST_HEAD(&hists->hpp_formats);
2476 static void hists__delete_remaining_entries(struct rb_root *root)
2478 struct rb_node *node;
2479 struct hist_entry *he;
2481 while (!RB_EMPTY_ROOT(root)) {
2482 node = rb_first(root);
2483 rb_erase(node, root);
2485 he = rb_entry(node, struct hist_entry, rb_node_in);
2486 hist_entry__delete(he);
2490 static void hists__delete_all_entries(struct hists *hists)
2492 hists__delete_entries(hists);
2493 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2494 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2495 hists__delete_remaining_entries(&hists->entries_collapsed);
2498 static void hists_evsel__exit(struct perf_evsel *evsel)
2500 struct hists *hists = evsel__hists(evsel);
2501 struct perf_hpp_fmt *fmt, *pos;
2502 struct perf_hpp_list_node *node, *tmp;
2504 hists__delete_all_entries(hists);
2506 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2507 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2508 list_del(&fmt->list);
2511 list_del(&node->list);
2516 static int hists_evsel__init(struct perf_evsel *evsel)
2518 struct hists *hists = evsel__hists(evsel);
2520 __hists__init(hists, &perf_hpp_list);
2525 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2526 * stored in the rbtree...
2529 int hists__init(void)
2531 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
2535 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2540 void perf_hpp_list__init(struct perf_hpp_list *list)
2542 INIT_LIST_HEAD(&list->fields);
2543 INIT_LIST_HEAD(&list->sorts);