9 static bool hists__filter_entry_by_dso(struct hists *hists,
10 struct hist_entry *he);
11 static bool hists__filter_entry_by_thread(struct hists *hists,
12 struct hist_entry *he);
13 static bool hists__filter_entry_by_symbol(struct hists *hists,
14 struct hist_entry *he);
23 struct callchain_param callchain_param = {
24 .mode = CHAIN_GRAPH_REL,
29 u16 hists__col_len(struct hists *hists, enum hist_column col)
31 return hists->col_len[col];
34 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
36 hists->col_len[col] = len;
39 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
41 if (len > hists__col_len(hists, col)) {
42 hists__set_col_len(hists, col, len);
48 static void hists__reset_col_len(struct hists *hists)
52 for (col = 0; col < HISTC_NR_COLS; ++col)
53 hists__set_col_len(hists, col, 0);
56 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
58 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
60 if (hists__col_len(hists, dso) < unresolved_col_width &&
61 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
62 !symbol_conf.dso_list)
63 hists__set_col_len(hists, dso, unresolved_col_width);
66 static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
68 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
72 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
74 hists__set_unres_dso_col_len(hists, HISTC_DSO);
76 len = thread__comm_len(h->thread);
77 if (hists__new_col_len(hists, HISTC_COMM, len))
78 hists__set_col_len(hists, HISTC_THREAD, len + 6);
81 len = dso__name_len(h->ms.map->dso);
82 hists__new_col_len(hists, HISTC_DSO, len);
88 * +4 accounts for '[x] ' priv level info
89 * +2 account of 0x prefix on raw addresses
91 if (h->branch_info->from.sym) {
92 symlen = (int)h->branch_info->from.sym->namelen + 4;
93 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
95 symlen = dso__name_len(h->branch_info->from.map->dso);
96 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
98 symlen = unresolved_col_width + 4 + 2;
99 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
100 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
103 if (h->branch_info->to.sym) {
104 symlen = (int)h->branch_info->to.sym->namelen + 4;
105 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
107 symlen = dso__name_len(h->branch_info->to.map->dso);
108 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
110 symlen = unresolved_col_width + 4 + 2;
111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
117 static void hist_entry__add_cpumode_period(struct hist_entry *he,
118 unsigned int cpumode, u64 period)
121 case PERF_RECORD_MISC_KERNEL:
122 he->period_sys += period;
124 case PERF_RECORD_MISC_USER:
125 he->period_us += period;
127 case PERF_RECORD_MISC_GUEST_KERNEL:
128 he->period_guest_sys += period;
130 case PERF_RECORD_MISC_GUEST_USER:
131 he->period_guest_us += period;
138 static void hist_entry__decay(struct hist_entry *he)
140 he->period = (he->period * 7) / 8;
141 he->nr_events = (he->nr_events * 7) / 8;
144 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
146 u64 prev_period = he->period;
148 if (prev_period == 0)
151 hist_entry__decay(he);
154 hists->stats.total_period -= prev_period - he->period;
156 return he->period == 0;
159 static void __hists__decay_entries(struct hists *hists, bool zap_user,
160 bool zap_kernel, bool threaded)
162 struct rb_node *next = rb_first(&hists->entries);
163 struct hist_entry *n;
166 n = rb_entry(next, struct hist_entry, rb_node);
167 next = rb_next(&n->rb_node);
169 * We may be annotating this, for instance, so keep it here in
170 * case some it gets new samples, we'll eventually free it when
171 * the user stops browsing and it agains gets fully decayed.
173 if (((zap_user && n->level == '.') ||
174 (zap_kernel && n->level != '.') ||
175 hists__decay_entry(hists, n)) &&
177 rb_erase(&n->rb_node, &hists->entries);
179 if (sort__need_collapse || threaded)
180 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
188 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
190 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
193 void hists__decay_entries_threaded(struct hists *hists,
194 bool zap_user, bool zap_kernel)
196 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
200 * histogram, sorted on item, collects periods
203 static struct hist_entry *hist_entry__new(struct hist_entry *template)
205 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
206 struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
212 he->ms.map->referenced = true;
213 if (symbol_conf.use_callchain)
214 callchain_init(he->callchain);
220 static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
223 hists__calc_col_len(hists, h);
225 hists->stats.total_period += h->period;
229 static u8 symbol__parent_filter(const struct symbol *parent)
231 if (symbol_conf.exclude_other && parent == NULL)
232 return 1 << HIST_FILTER__PARENT;
236 static struct hist_entry *add_hist_entry(struct hists *hists,
237 struct hist_entry *entry,
238 struct addr_location *al,
242 struct rb_node *parent = NULL;
243 struct hist_entry *he;
246 pthread_mutex_lock(&hists->lock);
248 p = &hists->entries_in->rb_node;
252 he = rb_entry(parent, struct hist_entry, rb_node_in);
254 cmp = hist_entry__cmp(entry, he);
257 he->period += period;
268 he = hist_entry__new(entry);
272 rb_link_node(&he->rb_node_in, parent, p);
273 rb_insert_color(&he->rb_node_in, hists->entries_in);
275 hist_entry__add_cpumode_period(he, al->cpumode, period);
277 pthread_mutex_unlock(&hists->lock);
281 struct hist_entry *__hists__add_branch_entry(struct hists *self,
282 struct addr_location *al,
283 struct symbol *sym_parent,
284 struct branch_info *bi,
287 struct hist_entry entry = {
288 .thread = al->thread,
297 .parent = sym_parent,
298 .filtered = symbol__parent_filter(sym_parent),
302 return add_hist_entry(self, &entry, al, period);
305 struct hist_entry *__hists__add_entry(struct hists *self,
306 struct addr_location *al,
307 struct symbol *sym_parent, u64 period)
309 struct hist_entry entry = {
310 .thread = al->thread,
319 .parent = sym_parent,
320 .filtered = symbol__parent_filter(sym_parent),
323 return add_hist_entry(self, &entry, al, period);
327 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
329 struct sort_entry *se;
332 list_for_each_entry(se, &hist_entry__sort_list, list) {
333 cmp = se->se_cmp(left, right);
342 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
344 struct sort_entry *se;
347 list_for_each_entry(se, &hist_entry__sort_list, list) {
348 int64_t (*f)(struct hist_entry *, struct hist_entry *);
350 f = se->se_collapse ?: se->se_cmp;
352 cmp = f(left, right);
360 void hist_entry__free(struct hist_entry *he)
366 * collapse the histogram
369 static bool hists__collapse_insert_entry(struct hists *hists,
370 struct rb_root *root,
371 struct hist_entry *he)
373 struct rb_node **p = &root->rb_node;
374 struct rb_node *parent = NULL;
375 struct hist_entry *iter;
380 iter = rb_entry(parent, struct hist_entry, rb_node_in);
382 cmp = hist_entry__collapse(iter, he);
385 iter->period += he->period;
386 iter->nr_events += he->nr_events;
387 if (symbol_conf.use_callchain) {
388 callchain_cursor_reset(&hists->callchain_cursor);
389 callchain_merge(&hists->callchain_cursor, iter->callchain,
392 hist_entry__free(he);
402 rb_link_node(&he->rb_node_in, parent, p);
403 rb_insert_color(&he->rb_node_in, root);
407 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
409 struct rb_root *root;
411 pthread_mutex_lock(&hists->lock);
413 root = hists->entries_in;
414 if (++hists->entries_in > &hists->entries_in_array[1])
415 hists->entries_in = &hists->entries_in_array[0];
417 pthread_mutex_unlock(&hists->lock);
422 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
424 hists__filter_entry_by_dso(hists, he);
425 hists__filter_entry_by_thread(hists, he);
426 hists__filter_entry_by_symbol(hists, he);
429 static void __hists__collapse_resort(struct hists *hists, bool threaded)
431 struct rb_root *root;
432 struct rb_node *next;
433 struct hist_entry *n;
435 if (!sort__need_collapse && !threaded)
438 root = hists__get_rotate_entries_in(hists);
439 next = rb_first(root);
442 n = rb_entry(next, struct hist_entry, rb_node_in);
443 next = rb_next(&n->rb_node_in);
445 rb_erase(&n->rb_node_in, root);
446 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
448 * If it wasn't combined with one of the entries already
449 * collapsed, we need to apply the filters that may have
450 * been set by, say, the hist_browser.
452 hists__apply_filters(hists, n);
457 void hists__collapse_resort(struct hists *hists)
459 return __hists__collapse_resort(hists, false);
462 void hists__collapse_resort_threaded(struct hists *hists)
464 return __hists__collapse_resort(hists, true);
468 * reverse the map, sort on period.
471 static void __hists__insert_output_entry(struct rb_root *entries,
472 struct hist_entry *he,
473 u64 min_callchain_hits)
475 struct rb_node **p = &entries->rb_node;
476 struct rb_node *parent = NULL;
477 struct hist_entry *iter;
479 if (symbol_conf.use_callchain)
480 callchain_param.sort(&he->sorted_chain, he->callchain,
481 min_callchain_hits, &callchain_param);
485 iter = rb_entry(parent, struct hist_entry, rb_node);
487 if (he->period > iter->period)
493 rb_link_node(&he->rb_node, parent, p);
494 rb_insert_color(&he->rb_node, entries);
497 static void __hists__output_resort(struct hists *hists, bool threaded)
499 struct rb_root *root;
500 struct rb_node *next;
501 struct hist_entry *n;
502 u64 min_callchain_hits;
504 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
506 if (sort__need_collapse || threaded)
507 root = &hists->entries_collapsed;
509 root = hists->entries_in;
511 next = rb_first(root);
512 hists->entries = RB_ROOT;
514 hists->nr_entries = 0;
515 hists->stats.total_period = 0;
516 hists__reset_col_len(hists);
519 n = rb_entry(next, struct hist_entry, rb_node_in);
520 next = rb_next(&n->rb_node_in);
522 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
523 hists__inc_nr_entries(hists, n);
527 void hists__output_resort(struct hists *hists)
529 return __hists__output_resort(hists, false);
532 void hists__output_resort_threaded(struct hists *hists)
534 return __hists__output_resort(hists, true);
537 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
540 int ret = fprintf(fp, " ");
542 for (i = 0; i < left_margin; i++)
543 ret += fprintf(fp, " ");
548 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
552 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
554 for (i = 0; i < depth; i++)
555 if (depth_mask & (1 << i))
556 ret += fprintf(fp, "| ");
558 ret += fprintf(fp, " ");
560 ret += fprintf(fp, "\n");
565 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
566 int depth, int depth_mask, int period,
567 u64 total_samples, u64 hits,
573 ret += callchain__fprintf_left_margin(fp, left_margin);
574 for (i = 0; i < depth; i++) {
575 if (depth_mask & (1 << i))
576 ret += fprintf(fp, "|");
578 ret += fprintf(fp, " ");
579 if (!period && i == depth - 1) {
582 percent = hits * 100.0 / total_samples;
583 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
585 ret += fprintf(fp, "%s", " ");
588 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
590 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
595 static struct symbol *rem_sq_bracket;
596 static struct callchain_list rem_hits;
598 static void init_rem_hits(void)
600 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
601 if (!rem_sq_bracket) {
602 fprintf(stderr, "Not enough memory to display remaining hits\n");
606 strcpy(rem_sq_bracket->name, "[...]");
607 rem_hits.ms.sym = rem_sq_bracket;
610 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
611 u64 total_samples, int depth,
612 int depth_mask, int left_margin)
614 struct rb_node *node, *next;
615 struct callchain_node *child;
616 struct callchain_list *chain;
617 int new_depth_mask = depth_mask;
622 uint entries_printed = 0;
624 if (callchain_param.mode == CHAIN_GRAPH_REL)
625 new_total = self->children_hit;
627 new_total = total_samples;
629 remaining = new_total;
631 node = rb_first(&self->rb_root);
635 child = rb_entry(node, struct callchain_node, rb_node);
636 cumul = callchain_cumul_hits(child);
640 * The depth mask manages the output of pipes that show
641 * the depth. We don't want to keep the pipes of the current
642 * level for the last child of this depth.
643 * Except if we have remaining filtered hits. They will
644 * supersede the last child
646 next = rb_next(node);
647 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
648 new_depth_mask &= ~(1 << (depth - 1));
651 * But we keep the older depth mask for the line separator
652 * to keep the level link until we reach the last child
654 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
657 list_for_each_entry(chain, &child->val, list) {
658 ret += ipchain__fprintf_graph(fp, chain, depth,
664 ret += __callchain__fprintf_graph(fp, child, new_total,
666 new_depth_mask | (1 << depth),
669 if (++entries_printed == callchain_param.print_limit)
673 if (callchain_param.mode == CHAIN_GRAPH_REL &&
674 remaining && remaining != new_total) {
679 new_depth_mask &= ~(1 << (depth - 1));
681 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
682 new_depth_mask, 0, new_total,
683 remaining, left_margin);
689 static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
690 u64 total_samples, int left_margin)
692 struct callchain_list *chain;
693 bool printed = false;
696 u32 entries_printed = 0;
698 list_for_each_entry(chain, &self->val, list) {
699 if (!i++ && sort__first_dimension == SORT_SYM)
703 ret += callchain__fprintf_left_margin(fp, left_margin);
704 ret += fprintf(fp, "|\n");
705 ret += callchain__fprintf_left_margin(fp, left_margin);
706 ret += fprintf(fp, "---");
711 ret += callchain__fprintf_left_margin(fp, left_margin);
714 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
716 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
718 if (++entries_printed == callchain_param.print_limit)
722 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
727 static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
730 struct callchain_list *chain;
736 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
739 list_for_each_entry(chain, &self->val, list) {
740 if (chain->ip >= PERF_CONTEXT_MAX)
743 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
745 ret += fprintf(fp, " %p\n",
746 (void *)(long)chain->ip);
752 static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
753 u64 total_samples, int left_margin,
756 struct rb_node *rb_node;
757 struct callchain_node *chain;
759 u32 entries_printed = 0;
761 rb_node = rb_first(&he->sorted_chain);
765 chain = rb_entry(rb_node, struct callchain_node, rb_node);
766 percent = chain->hit * 100.0 / total_samples;
767 switch (callchain_param.mode) {
769 ret += percent_color_fprintf(fp, " %6.2f%%\n",
771 ret += callchain__fprintf_flat(fp, chain, total_samples);
773 case CHAIN_GRAPH_ABS: /* Falldown */
774 case CHAIN_GRAPH_REL:
775 ret += callchain__fprintf_graph(fp, chain, total_samples,
781 ret += fprintf(fp, "\n");
782 if (++entries_printed == callchain_param.print_limit)
784 rb_node = rb_next(rb_node);
790 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
792 struct rb_node *next = rb_first(&hists->entries);
793 struct hist_entry *n;
796 hists__reset_col_len(hists);
798 while (next && row++ < max_rows) {
799 n = rb_entry(next, struct hist_entry, rb_node);
801 hists__calc_col_len(hists, n);
802 next = rb_next(&n->rb_node);
806 static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
807 size_t size, struct hists *pair_hists,
808 bool show_displacement, long displacement,
809 bool color, u64 total_period)
811 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
813 const char *sep = symbol_conf.field_sep;
816 if (symbol_conf.exclude_other && !he->parent)
820 period = he->pair ? he->pair->period : 0;
821 nr_events = he->pair ? he->pair->nr_events : 0;
822 total = pair_hists->stats.total_period;
823 period_sys = he->pair ? he->pair->period_sys : 0;
824 period_us = he->pair ? he->pair->period_us : 0;
825 period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
826 period_guest_us = he->pair ? he->pair->period_guest_us : 0;
829 nr_events = he->nr_events;
830 total = total_period;
831 period_sys = he->period_sys;
832 period_us = he->period_us;
833 period_guest_sys = he->period_guest_sys;
834 period_guest_us = he->period_guest_us;
839 ret = percent_color_snprintf(s, size,
840 sep ? "%.2f" : " %6.2f%%",
841 (period * 100.0) / total);
843 ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
844 (period * 100.0) / total);
845 if (symbol_conf.show_cpu_utilization) {
846 ret += percent_color_snprintf(s + ret, size - ret,
847 sep ? "%.2f" : " %6.2f%%",
848 (period_sys * 100.0) / total);
849 ret += percent_color_snprintf(s + ret, size - ret,
850 sep ? "%.2f" : " %6.2f%%",
851 (period_us * 100.0) / total);
853 ret += percent_color_snprintf(s + ret,
855 sep ? "%.2f" : " %6.2f%%",
856 (period_guest_sys * 100.0) /
858 ret += percent_color_snprintf(s + ret,
860 sep ? "%.2f" : " %6.2f%%",
861 (period_guest_us * 100.0) /
866 ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
868 if (symbol_conf.show_nr_samples) {
870 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
872 ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
875 if (symbol_conf.show_total_period) {
877 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
879 ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
884 double old_percent = 0, new_percent = 0, diff;
887 old_percent = (period * 100.0) / total;
888 if (total_period > 0)
889 new_percent = (he->period * 100.0) / total_period;
891 diff = new_percent - old_percent;
893 if (fabs(diff) >= 0.01)
894 ret += scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
896 ret += scnprintf(bf, sizeof(bf), " ");
899 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
901 ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
903 if (show_displacement) {
905 ret += scnprintf(bf, sizeof(bf), "%+4ld", displacement);
907 ret += scnprintf(bf, sizeof(bf), " ");
910 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
912 ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
919 int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
922 const char *sep = symbol_conf.field_sep;
923 struct sort_entry *se;
926 list_for_each_entry(se, &hist_entry__sort_list, list) {
930 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
931 ret += se->se_snprintf(he, s + ret, size - ret,
932 hists__col_len(hists, se->se_width_idx));
938 static int hist_entry__fprintf(struct hist_entry *he, size_t size,
939 struct hists *hists, struct hists *pair_hists,
940 bool show_displacement, long displacement,
941 u64 total_period, FILE *fp)
946 if (size == 0 || size > sizeof(bf))
949 ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
950 show_displacement, displacement,
952 hist_entry__snprintf(he, bf + ret, size - ret, hists);
953 return fprintf(fp, "%s\n", bf);
956 static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
958 u64 total_period, FILE *fp)
962 if (sort__first_dimension == SORT_COMM) {
963 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
965 left_margin = hists__col_len(hists, se->se_width_idx);
966 left_margin -= thread__comm_len(he->thread);
969 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
972 size_t hists__fprintf(struct hists *hists, struct hists *pair,
973 bool show_displacement, bool show_header, int max_rows,
974 int max_cols, FILE *fp)
976 struct sort_entry *se;
980 unsigned long position = 1;
981 long displacement = 0;
983 const char *sep = symbol_conf.field_sep;
984 const char *col_width = symbol_conf.col_width_list_str;
992 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
994 if (symbol_conf.show_cpu_utilization) {
996 ret += fprintf(fp, "%csys", *sep);
997 ret += fprintf(fp, "%cus", *sep);
999 ret += fprintf(fp, "%cguest sys", *sep);
1000 ret += fprintf(fp, "%cguest us", *sep);
1003 ret += fprintf(fp, " sys ");
1004 ret += fprintf(fp, " us ");
1006 ret += fprintf(fp, " guest sys ");
1007 ret += fprintf(fp, " guest us ");
1012 if (symbol_conf.show_nr_samples) {
1014 fprintf(fp, "%cSamples", *sep);
1016 fputs(" Samples ", fp);
1019 if (symbol_conf.show_total_period) {
1021 ret += fprintf(fp, "%cPeriod", *sep);
1023 ret += fprintf(fp, " Period ");
1028 ret += fprintf(fp, "%cDelta", *sep);
1030 ret += fprintf(fp, " Delta ");
1032 if (show_displacement) {
1034 ret += fprintf(fp, "%cDisplacement", *sep);
1036 ret += fprintf(fp, " Displ");
1040 list_for_each_entry(se, &hist_entry__sort_list, list) {
1044 fprintf(fp, "%c%s", *sep, se->se_header);
1047 width = strlen(se->se_header);
1048 if (symbol_conf.col_width_list_str) {
1050 hists__set_col_len(hists, se->se_width_idx,
1052 col_width = strchr(col_width, ',');
1057 if (!hists__new_col_len(hists, se->se_width_idx, width))
1058 width = hists__col_len(hists, se->se_width_idx);
1059 fprintf(fp, " %*s", width, se->se_header);
1063 if (max_rows && ++nr_rows >= max_rows)
1069 fprintf(fp, "# ........");
1070 if (symbol_conf.show_cpu_utilization)
1071 fprintf(fp, " ....... .......");
1072 if (symbol_conf.show_nr_samples)
1073 fprintf(fp, " ..........");
1074 if (symbol_conf.show_total_period)
1075 fprintf(fp, " ............");
1077 fprintf(fp, " ..........");
1078 if (show_displacement)
1079 fprintf(fp, " .....");
1081 list_for_each_entry(se, &hist_entry__sort_list, list) {
1088 width = hists__col_len(hists, se->se_width_idx);
1090 width = strlen(se->se_header);
1091 for (i = 0; i < width; i++)
1096 if (max_rows && ++nr_rows >= max_rows)
1100 if (max_rows && ++nr_rows >= max_rows)
1104 total_period = hists->stats.total_period;
1106 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1107 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1112 if (show_displacement) {
1113 if (h->pair != NULL)
1114 displacement = ((long)h->pair->position -
1120 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
1121 displacement, total_period, fp);
1123 if (symbol_conf.use_callchain)
1124 ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
1125 if (max_rows && ++nr_rows >= max_rows)
1128 if (h->ms.map == NULL && verbose > 1) {
1129 __map_groups__fprintf_maps(&h->thread->mg,
1130 MAP__FUNCTION, verbose, fp);
1131 fprintf(fp, "%.10s end\n", graph_dotted_line);
1135 free(rem_sq_bracket);
1141 * See hists__fprintf to match the column widths
1143 unsigned int hists__sort_list_width(struct hists *hists)
1145 struct sort_entry *se;
1146 int ret = 9; /* total % */
1148 if (symbol_conf.show_cpu_utilization) {
1149 ret += 7; /* count_sys % */
1150 ret += 6; /* count_us % */
1152 ret += 13; /* count_guest_sys % */
1153 ret += 12; /* count_guest_us % */
1157 if (symbol_conf.show_nr_samples)
1160 if (symbol_conf.show_total_period)
1163 list_for_each_entry(se, &hist_entry__sort_list, list)
1165 ret += 2 + hists__col_len(hists, se->se_width_idx);
1167 if (verbose) /* Addr + origin */
1168 ret += 3 + BITS_PER_LONG / 4;
1173 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1174 enum hist_filter filter)
1176 h->filtered &= ~(1 << filter);
1180 ++hists->nr_entries;
1182 hists->nr_entries += h->nr_rows;
1184 hists->stats.total_period += h->period;
1185 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
1187 hists__calc_col_len(hists, h);
1191 static bool hists__filter_entry_by_dso(struct hists *hists,
1192 struct hist_entry *he)
1194 if (hists->dso_filter != NULL &&
1195 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1196 he->filtered |= (1 << HIST_FILTER__DSO);
1203 void hists__filter_by_dso(struct hists *hists)
1207 hists->nr_entries = hists->stats.total_period = 0;
1208 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1209 hists__reset_col_len(hists);
1211 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1212 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1214 if (symbol_conf.exclude_other && !h->parent)
1217 if (hists__filter_entry_by_dso(hists, h))
1220 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1224 static bool hists__filter_entry_by_thread(struct hists *hists,
1225 struct hist_entry *he)
1227 if (hists->thread_filter != NULL &&
1228 he->thread != hists->thread_filter) {
1229 he->filtered |= (1 << HIST_FILTER__THREAD);
1236 void hists__filter_by_thread(struct hists *hists)
1240 hists->nr_entries = hists->stats.total_period = 0;
1241 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1242 hists__reset_col_len(hists);
1244 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1245 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1247 if (hists__filter_entry_by_thread(hists, h))
1250 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1254 static bool hists__filter_entry_by_symbol(struct hists *hists,
1255 struct hist_entry *he)
1257 if (hists->symbol_filter_str != NULL &&
1258 (!he->ms.sym || strstr(he->ms.sym->name,
1259 hists->symbol_filter_str) == NULL)) {
1260 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1267 void hists__filter_by_symbol(struct hists *hists)
1271 hists->nr_entries = hists->stats.total_period = 0;
1272 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1273 hists__reset_col_len(hists);
1275 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1276 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1278 if (hists__filter_entry_by_symbol(hists, h))
1281 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1285 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
1287 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
1290 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
1292 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
1295 void hists__inc_nr_events(struct hists *hists, u32 type)
1297 ++hists->stats.nr_events[0];
1298 ++hists->stats.nr_events[type];
1301 size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
1306 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
1309 if (hists->stats.nr_events[i] == 0)
1312 name = perf_event__name(i);
1313 if (!strcmp(name, "UNKNOWN"))
1316 ret += fprintf(fp, "%16s events: %10d\n", name,
1317 hists->stats.nr_events[i]);