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 rb_root *root,
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;
621 uint entries_printed = 0;
623 remaining = total_samples;
625 node = rb_first(root);
630 child = rb_entry(node, struct callchain_node, rb_node);
631 cumul = callchain_cumul_hits(child);
635 * The depth mask manages the output of pipes that show
636 * the depth. We don't want to keep the pipes of the current
637 * level for the last child of this depth.
638 * Except if we have remaining filtered hits. They will
639 * supersede the last child
641 next = rb_next(node);
642 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
643 new_depth_mask &= ~(1 << (depth - 1));
646 * But we keep the older depth mask for the line separator
647 * to keep the level link until we reach the last child
649 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
652 list_for_each_entry(chain, &child->val, list) {
653 ret += ipchain__fprintf_graph(fp, chain, depth,
660 if (callchain_param.mode == CHAIN_GRAPH_REL)
661 new_total = child->children_hit;
663 new_total = total_samples;
665 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
667 new_depth_mask | (1 << depth),
670 if (++entries_printed == callchain_param.print_limit)
674 if (callchain_param.mode == CHAIN_GRAPH_REL &&
675 remaining && remaining != total_samples) {
680 new_depth_mask &= ~(1 << (depth - 1));
681 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
682 new_depth_mask, 0, total_samples,
683 remaining, left_margin);
689 static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
690 u64 total_samples, int left_margin)
692 struct callchain_node *cnode;
693 struct callchain_list *chain;
694 u32 entries_printed = 0;
695 bool printed = false;
696 struct rb_node *node;
701 * If have one single callchain root, don't bother printing
702 * its percentage (100 % in fractal mode and the same percentage
703 * than the hist in graph mode). This also avoid one level of column.
705 node = rb_first(root);
706 if (node && !rb_next(node)) {
707 cnode = rb_entry(node, struct callchain_node, rb_node);
708 list_for_each_entry(chain, &cnode->val, list) {
710 * If we sort by symbol, the first entry is the same than
711 * the symbol. No need to print it otherwise it appears as
714 if (!i++ && sort__first_dimension == SORT_SYM)
717 ret += callchain__fprintf_left_margin(fp, left_margin);
718 ret += fprintf(fp, "|\n");
719 ret += callchain__fprintf_left_margin(fp, left_margin);
720 ret += fprintf(fp, "---");
724 ret += callchain__fprintf_left_margin(fp, left_margin);
727 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
729 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
731 if (++entries_printed == callchain_param.print_limit)
734 root = &cnode->rb_root;
737 return __callchain__fprintf_graph(fp, root, total_samples,
741 static size_t __callchain__fprintf_flat(FILE *fp,
742 struct callchain_node *self,
745 struct callchain_list *chain;
751 ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
754 list_for_each_entry(chain, &self->val, list) {
755 if (chain->ip >= PERF_CONTEXT_MAX)
758 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
760 ret += fprintf(fp, " %p\n",
761 (void *)(long)chain->ip);
767 static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
771 u32 entries_printed = 0;
772 struct rb_node *rb_node;
773 struct callchain_node *chain;
775 rb_node = rb_first(self);
779 chain = rb_entry(rb_node, struct callchain_node, rb_node);
780 percent = chain->hit * 100.0 / total_samples;
782 ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
783 ret += __callchain__fprintf_flat(fp, chain, total_samples);
784 ret += fprintf(fp, "\n");
785 if (++entries_printed == callchain_param.print_limit)
788 rb_node = rb_next(rb_node);
794 static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
795 u64 total_samples, int left_margin,
798 switch (callchain_param.mode) {
799 case CHAIN_GRAPH_REL:
800 return callchain__fprintf_graph(fp, &he->sorted_chain, he->period,
803 case CHAIN_GRAPH_ABS:
804 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
808 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
813 pr_err("Bad callchain mode\n");
819 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
821 struct rb_node *next = rb_first(&hists->entries);
822 struct hist_entry *n;
825 hists__reset_col_len(hists);
827 while (next && row++ < max_rows) {
828 n = rb_entry(next, struct hist_entry, rb_node);
830 hists__calc_col_len(hists, n);
831 next = rb_next(&n->rb_node);
835 static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
836 size_t size, struct hists *pair_hists,
837 bool show_displacement, long displacement,
838 bool color, u64 total_period)
840 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
842 const char *sep = symbol_conf.field_sep;
845 if (symbol_conf.exclude_other && !he->parent)
849 period = he->pair ? he->pair->period : 0;
850 nr_events = he->pair ? he->pair->nr_events : 0;
851 total = pair_hists->stats.total_period;
852 period_sys = he->pair ? he->pair->period_sys : 0;
853 period_us = he->pair ? he->pair->period_us : 0;
854 period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
855 period_guest_us = he->pair ? he->pair->period_guest_us : 0;
858 nr_events = he->nr_events;
859 total = total_period;
860 period_sys = he->period_sys;
861 period_us = he->period_us;
862 period_guest_sys = he->period_guest_sys;
863 period_guest_us = he->period_guest_us;
868 ret = percent_color_snprintf(s, size,
869 sep ? "%.2f" : " %6.2f%%",
870 (period * 100.0) / total);
872 ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
873 (period * 100.0) / total);
874 if (symbol_conf.show_cpu_utilization) {
875 ret += percent_color_snprintf(s + ret, size - ret,
876 sep ? "%.2f" : " %6.2f%%",
877 (period_sys * 100.0) / total);
878 ret += percent_color_snprintf(s + ret, size - ret,
879 sep ? "%.2f" : " %6.2f%%",
880 (period_us * 100.0) / total);
882 ret += percent_color_snprintf(s + ret,
884 sep ? "%.2f" : " %6.2f%%",
885 (period_guest_sys * 100.0) /
887 ret += percent_color_snprintf(s + ret,
889 sep ? "%.2f" : " %6.2f%%",
890 (period_guest_us * 100.0) /
895 ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
897 if (symbol_conf.show_nr_samples) {
899 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
901 ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
904 if (symbol_conf.show_total_period) {
906 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
908 ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
913 double old_percent = 0, new_percent = 0, diff;
916 old_percent = (period * 100.0) / total;
917 if (total_period > 0)
918 new_percent = (he->period * 100.0) / total_period;
920 diff = new_percent - old_percent;
922 if (fabs(diff) >= 0.01)
923 scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
925 scnprintf(bf, sizeof(bf), " ");
928 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
930 ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
932 if (show_displacement) {
934 scnprintf(bf, sizeof(bf), "%+4ld", displacement);
936 scnprintf(bf, sizeof(bf), " ");
939 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
941 ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
948 int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
951 const char *sep = symbol_conf.field_sep;
952 struct sort_entry *se;
955 list_for_each_entry(se, &hist_entry__sort_list, list) {
959 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
960 ret += se->se_snprintf(he, s + ret, size - ret,
961 hists__col_len(hists, se->se_width_idx));
967 static int hist_entry__fprintf(struct hist_entry *he, size_t size,
968 struct hists *hists, struct hists *pair_hists,
969 bool show_displacement, long displacement,
970 u64 total_period, FILE *fp)
975 if (size == 0 || size > sizeof(bf))
978 ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
979 show_displacement, displacement,
981 hist_entry__snprintf(he, bf + ret, size - ret, hists);
982 return fprintf(fp, "%s\n", bf);
985 static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
987 u64 total_period, FILE *fp)
991 if (sort__first_dimension == SORT_COMM) {
992 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
994 left_margin = hists__col_len(hists, se->se_width_idx);
995 left_margin -= thread__comm_len(he->thread);
998 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
1001 size_t hists__fprintf(struct hists *hists, struct hists *pair,
1002 bool show_displacement, bool show_header, int max_rows,
1003 int max_cols, FILE *fp)
1005 struct sort_entry *se;
1009 unsigned long position = 1;
1010 long displacement = 0;
1012 const char *sep = symbol_conf.field_sep;
1013 const char *col_width = symbol_conf.col_width_list_str;
1021 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
1023 if (symbol_conf.show_cpu_utilization) {
1025 ret += fprintf(fp, "%csys", *sep);
1026 ret += fprintf(fp, "%cus", *sep);
1028 ret += fprintf(fp, "%cguest sys", *sep);
1029 ret += fprintf(fp, "%cguest us", *sep);
1032 ret += fprintf(fp, " sys ");
1033 ret += fprintf(fp, " us ");
1035 ret += fprintf(fp, " guest sys ");
1036 ret += fprintf(fp, " guest us ");
1041 if (symbol_conf.show_nr_samples) {
1043 fprintf(fp, "%cSamples", *sep);
1045 fputs(" Samples ", fp);
1048 if (symbol_conf.show_total_period) {
1050 ret += fprintf(fp, "%cPeriod", *sep);
1052 ret += fprintf(fp, " Period ");
1057 ret += fprintf(fp, "%cDelta", *sep);
1059 ret += fprintf(fp, " Delta ");
1061 if (show_displacement) {
1063 ret += fprintf(fp, "%cDisplacement", *sep);
1065 ret += fprintf(fp, " Displ");
1069 list_for_each_entry(se, &hist_entry__sort_list, list) {
1073 fprintf(fp, "%c%s", *sep, se->se_header);
1076 width = strlen(se->se_header);
1077 if (symbol_conf.col_width_list_str) {
1079 hists__set_col_len(hists, se->se_width_idx,
1081 col_width = strchr(col_width, ',');
1086 if (!hists__new_col_len(hists, se->se_width_idx, width))
1087 width = hists__col_len(hists, se->se_width_idx);
1088 fprintf(fp, " %*s", width, se->se_header);
1092 if (max_rows && ++nr_rows >= max_rows)
1098 fprintf(fp, "# ........");
1099 if (symbol_conf.show_cpu_utilization)
1100 fprintf(fp, " ....... .......");
1101 if (symbol_conf.show_nr_samples)
1102 fprintf(fp, " ..........");
1103 if (symbol_conf.show_total_period)
1104 fprintf(fp, " ............");
1106 fprintf(fp, " ..........");
1107 if (show_displacement)
1108 fprintf(fp, " .....");
1110 list_for_each_entry(se, &hist_entry__sort_list, list) {
1117 width = hists__col_len(hists, se->se_width_idx);
1119 width = strlen(se->se_header);
1120 for (i = 0; i < width; i++)
1125 if (max_rows && ++nr_rows >= max_rows)
1129 if (max_rows && ++nr_rows >= max_rows)
1133 total_period = hists->stats.total_period;
1135 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1136 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1141 if (show_displacement) {
1142 if (h->pair != NULL)
1143 displacement = ((long)h->pair->position -
1149 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
1150 displacement, total_period, fp);
1152 if (symbol_conf.use_callchain)
1153 ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
1154 if (max_rows && ++nr_rows >= max_rows)
1157 if (h->ms.map == NULL && verbose > 1) {
1158 __map_groups__fprintf_maps(&h->thread->mg,
1159 MAP__FUNCTION, verbose, fp);
1160 fprintf(fp, "%.10s end\n", graph_dotted_line);
1164 free(rem_sq_bracket);
1170 * See hists__fprintf to match the column widths
1172 unsigned int hists__sort_list_width(struct hists *hists)
1174 struct sort_entry *se;
1175 int ret = 9; /* total % */
1177 if (symbol_conf.show_cpu_utilization) {
1178 ret += 7; /* count_sys % */
1179 ret += 6; /* count_us % */
1181 ret += 13; /* count_guest_sys % */
1182 ret += 12; /* count_guest_us % */
1186 if (symbol_conf.show_nr_samples)
1189 if (symbol_conf.show_total_period)
1192 list_for_each_entry(se, &hist_entry__sort_list, list)
1194 ret += 2 + hists__col_len(hists, se->se_width_idx);
1196 if (verbose) /* Addr + origin */
1197 ret += 3 + BITS_PER_LONG / 4;
1202 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1203 enum hist_filter filter)
1205 h->filtered &= ~(1 << filter);
1209 ++hists->nr_entries;
1211 hists->nr_entries += h->nr_rows;
1213 hists->stats.total_period += h->period;
1214 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
1216 hists__calc_col_len(hists, h);
1220 static bool hists__filter_entry_by_dso(struct hists *hists,
1221 struct hist_entry *he)
1223 if (hists->dso_filter != NULL &&
1224 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1225 he->filtered |= (1 << HIST_FILTER__DSO);
1232 void hists__filter_by_dso(struct hists *hists)
1236 hists->nr_entries = hists->stats.total_period = 0;
1237 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1238 hists__reset_col_len(hists);
1240 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1241 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1243 if (symbol_conf.exclude_other && !h->parent)
1246 if (hists__filter_entry_by_dso(hists, h))
1249 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1253 static bool hists__filter_entry_by_thread(struct hists *hists,
1254 struct hist_entry *he)
1256 if (hists->thread_filter != NULL &&
1257 he->thread != hists->thread_filter) {
1258 he->filtered |= (1 << HIST_FILTER__THREAD);
1265 void hists__filter_by_thread(struct hists *hists)
1269 hists->nr_entries = hists->stats.total_period = 0;
1270 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1271 hists__reset_col_len(hists);
1273 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1274 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1276 if (hists__filter_entry_by_thread(hists, h))
1279 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1283 static bool hists__filter_entry_by_symbol(struct hists *hists,
1284 struct hist_entry *he)
1286 if (hists->symbol_filter_str != NULL &&
1287 (!he->ms.sym || strstr(he->ms.sym->name,
1288 hists->symbol_filter_str) == NULL)) {
1289 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1296 void hists__filter_by_symbol(struct hists *hists)
1300 hists->nr_entries = hists->stats.total_period = 0;
1301 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1302 hists__reset_col_len(hists);
1304 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1305 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1307 if (hists__filter_entry_by_symbol(hists, h))
1310 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1314 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
1316 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
1319 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
1321 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
1324 void hists__inc_nr_events(struct hists *hists, u32 type)
1326 ++hists->stats.nr_events[0];
1327 ++hists->stats.nr_events[type];
1330 size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
1335 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
1338 if (hists->stats.nr_events[i] == 0)
1341 name = perf_event__name(i);
1342 if (!strcmp(name, "UNKNOWN"))
1345 ret += fprintf(fp, "%16s events: %10d\n", name,
1346 hists->stats.nr_events[i]);