15 struct callchain_param callchain_param = {
16 .mode = CHAIN_GRAPH_REL,
20 u16 hists__col_len(struct hists *self, enum hist_column col)
22 return self->col_len[col];
25 void hists__set_col_len(struct hists *self, enum hist_column col, u16 len)
27 self->col_len[col] = len;
30 bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len)
32 if (len > hists__col_len(self, col)) {
33 hists__set_col_len(self, col, len);
39 static void hists__reset_col_len(struct hists *self)
43 for (col = 0; col < HISTC_NR_COLS; ++col)
44 hists__set_col_len(self, col, 0);
47 static void hists__calc_col_len(struct hists *self, struct hist_entry *h)
52 hists__new_col_len(self, HISTC_SYMBOL, h->ms.sym->namelen);
54 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
56 if (hists__col_len(self, HISTC_DSO) < unresolved_col_width &&
57 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
58 !symbol_conf.dso_list)
59 hists__set_col_len(self, HISTC_DSO,
60 unresolved_col_width);
63 len = thread__comm_len(h->thread);
64 if (hists__new_col_len(self, HISTC_COMM, len))
65 hists__set_col_len(self, HISTC_THREAD, len + 6);
68 len = dso__name_len(h->ms.map->dso);
69 hists__new_col_len(self, HISTC_DSO, len);
73 static void hist_entry__add_cpumode_period(struct hist_entry *self,
74 unsigned int cpumode, u64 period)
77 case PERF_RECORD_MISC_KERNEL:
78 self->period_sys += period;
80 case PERF_RECORD_MISC_USER:
81 self->period_us += period;
83 case PERF_RECORD_MISC_GUEST_KERNEL:
84 self->period_guest_sys += period;
86 case PERF_RECORD_MISC_GUEST_USER:
87 self->period_guest_us += period;
95 * histogram, sorted on item, collects periods
98 static struct hist_entry *hist_entry__new(struct hist_entry *template)
100 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
101 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
107 self->ms.map->referenced = true;
108 if (symbol_conf.use_callchain)
109 callchain_init(self->callchain);
115 static void hists__inc_nr_entries(struct hists *self, struct hist_entry *h)
118 hists__calc_col_len(self, h);
123 static u8 symbol__parent_filter(const struct symbol *parent)
125 if (symbol_conf.exclude_other && parent == NULL)
126 return 1 << HIST_FILTER__PARENT;
130 struct hist_entry *__hists__add_entry(struct hists *self,
131 struct addr_location *al,
132 struct symbol *sym_parent, u64 period)
134 struct rb_node **p = &self->entries.rb_node;
135 struct rb_node *parent = NULL;
136 struct hist_entry *he;
137 struct hist_entry entry = {
138 .thread = al->thread,
147 .parent = sym_parent,
148 .filtered = symbol__parent_filter(sym_parent),
154 he = rb_entry(parent, struct hist_entry, rb_node);
156 cmp = hist_entry__cmp(&entry, he);
159 he->period += period;
170 he = hist_entry__new(&entry);
173 rb_link_node(&he->rb_node, parent, p);
174 rb_insert_color(&he->rb_node, &self->entries);
175 hists__inc_nr_entries(self, he);
177 hist_entry__add_cpumode_period(he, al->cpumode, period);
182 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
184 struct sort_entry *se;
187 list_for_each_entry(se, &hist_entry__sort_list, list) {
188 cmp = se->se_cmp(left, right);
197 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
199 struct sort_entry *se;
202 list_for_each_entry(se, &hist_entry__sort_list, list) {
203 int64_t (*f)(struct hist_entry *, struct hist_entry *);
205 f = se->se_collapse ?: se->se_cmp;
207 cmp = f(left, right);
215 void hist_entry__free(struct hist_entry *he)
221 * collapse the histogram
224 static bool hists__collapse_insert_entry(struct hists *self,
225 struct rb_root *root,
226 struct hist_entry *he)
228 struct rb_node **p = &root->rb_node;
229 struct rb_node *parent = NULL;
230 struct hist_entry *iter;
235 iter = rb_entry(parent, struct hist_entry, rb_node);
237 cmp = hist_entry__collapse(iter, he);
240 iter->period += he->period;
241 if (symbol_conf.use_callchain) {
242 callchain_cursor_reset(&self->callchain_cursor);
243 callchain_merge(&self->callchain_cursor, iter->callchain,
246 hist_entry__free(he);
256 rb_link_node(&he->rb_node, parent, p);
257 rb_insert_color(&he->rb_node, root);
261 void hists__collapse_resort(struct hists *self)
264 struct rb_node *next;
265 struct hist_entry *n;
267 if (!sort__need_collapse)
271 next = rb_first(&self->entries);
272 self->nr_entries = 0;
273 hists__reset_col_len(self);
276 n = rb_entry(next, struct hist_entry, rb_node);
277 next = rb_next(&n->rb_node);
279 rb_erase(&n->rb_node, &self->entries);
280 if (hists__collapse_insert_entry(self, &tmp, n))
281 hists__inc_nr_entries(self, n);
288 * reverse the map, sort on period.
291 static void __hists__insert_output_entry(struct rb_root *entries,
292 struct hist_entry *he,
293 u64 min_callchain_hits)
295 struct rb_node **p = &entries->rb_node;
296 struct rb_node *parent = NULL;
297 struct hist_entry *iter;
299 if (symbol_conf.use_callchain)
300 callchain_param.sort(&he->sorted_chain, he->callchain,
301 min_callchain_hits, &callchain_param);
305 iter = rb_entry(parent, struct hist_entry, rb_node);
307 if (he->period > iter->period)
313 rb_link_node(&he->rb_node, parent, p);
314 rb_insert_color(&he->rb_node, entries);
317 void hists__output_resort(struct hists *self)
320 struct rb_node *next;
321 struct hist_entry *n;
322 u64 min_callchain_hits;
324 min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
327 next = rb_first(&self->entries);
329 self->nr_entries = 0;
330 hists__reset_col_len(self);
333 n = rb_entry(next, struct hist_entry, rb_node);
334 next = rb_next(&n->rb_node);
336 rb_erase(&n->rb_node, &self->entries);
337 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
338 hists__inc_nr_entries(self, n);
344 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
347 int ret = fprintf(fp, " ");
349 for (i = 0; i < left_margin; i++)
350 ret += fprintf(fp, " ");
355 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
359 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
361 for (i = 0; i < depth; i++)
362 if (depth_mask & (1 << i))
363 ret += fprintf(fp, "| ");
365 ret += fprintf(fp, " ");
367 ret += fprintf(fp, "\n");
372 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
373 int depth, int depth_mask, int period,
374 u64 total_samples, u64 hits,
380 ret += callchain__fprintf_left_margin(fp, left_margin);
381 for (i = 0; i < depth; i++) {
382 if (depth_mask & (1 << i))
383 ret += fprintf(fp, "|");
385 ret += fprintf(fp, " ");
386 if (!period && i == depth - 1) {
389 percent = hits * 100.0 / total_samples;
390 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
392 ret += fprintf(fp, "%s", " ");
395 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
397 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
402 static struct symbol *rem_sq_bracket;
403 static struct callchain_list rem_hits;
405 static void init_rem_hits(void)
407 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
408 if (!rem_sq_bracket) {
409 fprintf(stderr, "Not enough memory to display remaining hits\n");
413 strcpy(rem_sq_bracket->name, "[...]");
414 rem_hits.ms.sym = rem_sq_bracket;
417 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
418 u64 total_samples, int depth,
419 int depth_mask, int left_margin)
421 struct rb_node *node, *next;
422 struct callchain_node *child;
423 struct callchain_list *chain;
424 int new_depth_mask = depth_mask;
429 uint entries_printed = 0;
431 if (callchain_param.mode == CHAIN_GRAPH_REL)
432 new_total = self->children_hit;
434 new_total = total_samples;
436 remaining = new_total;
438 node = rb_first(&self->rb_root);
442 child = rb_entry(node, struct callchain_node, rb_node);
443 cumul = callchain_cumul_hits(child);
447 * The depth mask manages the output of pipes that show
448 * the depth. We don't want to keep the pipes of the current
449 * level for the last child of this depth.
450 * Except if we have remaining filtered hits. They will
451 * supersede the last child
453 next = rb_next(node);
454 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
455 new_depth_mask &= ~(1 << (depth - 1));
458 * But we keep the older depth mask for the line separator
459 * to keep the level link until we reach the last child
461 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
464 list_for_each_entry(chain, &child->val, list) {
465 ret += ipchain__fprintf_graph(fp, chain, depth,
471 ret += __callchain__fprintf_graph(fp, child, new_total,
473 new_depth_mask | (1 << depth),
476 if (++entries_printed == callchain_param.print_limit)
480 if (callchain_param.mode == CHAIN_GRAPH_REL &&
481 remaining && remaining != new_total) {
486 new_depth_mask &= ~(1 << (depth - 1));
488 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
489 new_depth_mask, 0, new_total,
490 remaining, left_margin);
496 static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
497 u64 total_samples, int left_margin)
499 struct callchain_list *chain;
500 bool printed = false;
503 u32 entries_printed = 0;
505 list_for_each_entry(chain, &self->val, list) {
506 if (!i++ && sort__first_dimension == SORT_SYM)
510 ret += callchain__fprintf_left_margin(fp, left_margin);
511 ret += fprintf(fp, "|\n");
512 ret += callchain__fprintf_left_margin(fp, left_margin);
513 ret += fprintf(fp, "---");
518 ret += callchain__fprintf_left_margin(fp, left_margin);
521 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
523 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
525 if (++entries_printed == callchain_param.print_limit)
529 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
534 static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
537 struct callchain_list *chain;
543 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
546 list_for_each_entry(chain, &self->val, list) {
547 if (chain->ip >= PERF_CONTEXT_MAX)
550 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
552 ret += fprintf(fp, " %p\n",
553 (void *)(long)chain->ip);
559 static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
560 u64 total_samples, int left_margin)
562 struct rb_node *rb_node;
563 struct callchain_node *chain;
565 u32 entries_printed = 0;
567 rb_node = rb_first(&self->sorted_chain);
571 chain = rb_entry(rb_node, struct callchain_node, rb_node);
572 percent = chain->hit * 100.0 / total_samples;
573 switch (callchain_param.mode) {
575 ret += percent_color_fprintf(fp, " %6.2f%%\n",
577 ret += callchain__fprintf_flat(fp, chain, total_samples);
579 case CHAIN_GRAPH_ABS: /* Falldown */
580 case CHAIN_GRAPH_REL:
581 ret += callchain__fprintf_graph(fp, chain, total_samples,
587 ret += fprintf(fp, "\n");
588 if (++entries_printed == callchain_param.print_limit)
590 rb_node = rb_next(rb_node);
596 int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
597 struct hists *hists, struct hists *pair_hists,
598 bool show_displacement, long displacement,
599 bool color, u64 session_total)
601 struct sort_entry *se;
602 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
604 const char *sep = symbol_conf.field_sep;
607 if (symbol_conf.exclude_other && !self->parent)
611 period = self->pair ? self->pair->period : 0;
612 nr_events = self->pair ? self->pair->nr_events : 0;
613 total = pair_hists->stats.total_period;
614 period_sys = self->pair ? self->pair->period_sys : 0;
615 period_us = self->pair ? self->pair->period_us : 0;
616 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
617 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
619 period = self->period;
620 nr_events = self->nr_events;
621 total = session_total;
622 period_sys = self->period_sys;
623 period_us = self->period_us;
624 period_guest_sys = self->period_guest_sys;
625 period_guest_us = self->period_guest_us;
630 ret = percent_color_snprintf(s, size,
631 sep ? "%.2f" : " %6.2f%%",
632 (period * 100.0) / total);
634 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
635 (period * 100.0) / total);
636 if (symbol_conf.show_cpu_utilization) {
637 ret += percent_color_snprintf(s + ret, size - ret,
638 sep ? "%.2f" : " %6.2f%%",
639 (period_sys * 100.0) / total);
640 ret += percent_color_snprintf(s + ret, size - ret,
641 sep ? "%.2f" : " %6.2f%%",
642 (period_us * 100.0) / total);
644 ret += percent_color_snprintf(s + ret,
646 sep ? "%.2f" : " %6.2f%%",
647 (period_guest_sys * 100.0) /
649 ret += percent_color_snprintf(s + ret,
651 sep ? "%.2f" : " %6.2f%%",
652 (period_guest_us * 100.0) /
657 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
659 if (symbol_conf.show_nr_samples) {
661 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
663 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
668 double old_percent = 0, new_percent = 0, diff;
671 old_percent = (period * 100.0) / total;
672 if (session_total > 0)
673 new_percent = (self->period * 100.0) / session_total;
675 diff = new_percent - old_percent;
677 if (fabs(diff) >= 0.01)
678 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
680 snprintf(bf, sizeof(bf), " ");
683 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
685 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
687 if (show_displacement) {
689 snprintf(bf, sizeof(bf), "%+4ld", displacement);
691 snprintf(bf, sizeof(bf), " ");
694 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
696 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
700 list_for_each_entry(se, &hist_entry__sort_list, list) {
704 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
705 ret += se->se_snprintf(self, s + ret, size - ret,
706 hists__col_len(hists, se->se_width_idx));
712 int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
713 struct hists *pair_hists, bool show_displacement,
714 long displacement, FILE *fp, u64 session_total)
717 hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
718 show_displacement, displacement,
719 true, session_total);
720 return fprintf(fp, "%s\n", bf);
723 static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
724 struct hists *hists, FILE *fp,
729 if (sort__first_dimension == SORT_COMM) {
730 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
732 left_margin = hists__col_len(hists, se->se_width_idx);
733 left_margin -= thread__comm_len(self->thread);
736 return hist_entry_callchain__fprintf(fp, self, session_total,
740 size_t hists__fprintf(struct hists *self, struct hists *pair,
741 bool show_displacement, FILE *fp)
743 struct sort_entry *se;
746 unsigned long position = 1;
747 long displacement = 0;
749 const char *sep = symbol_conf.field_sep;
750 const char *col_width = symbol_conf.col_width_list_str;
754 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
756 if (symbol_conf.show_nr_samples) {
758 fprintf(fp, "%cSamples", *sep);
760 fputs(" Samples ", fp);
763 if (symbol_conf.show_cpu_utilization) {
765 ret += fprintf(fp, "%csys", *sep);
766 ret += fprintf(fp, "%cus", *sep);
768 ret += fprintf(fp, "%cguest sys", *sep);
769 ret += fprintf(fp, "%cguest us", *sep);
772 ret += fprintf(fp, " sys ");
773 ret += fprintf(fp, " us ");
775 ret += fprintf(fp, " guest sys ");
776 ret += fprintf(fp, " guest us ");
783 ret += fprintf(fp, "%cDelta", *sep);
785 ret += fprintf(fp, " Delta ");
787 if (show_displacement) {
789 ret += fprintf(fp, "%cDisplacement", *sep);
791 ret += fprintf(fp, " Displ");
795 list_for_each_entry(se, &hist_entry__sort_list, list) {
799 fprintf(fp, "%c%s", *sep, se->se_header);
802 width = strlen(se->se_header);
803 if (symbol_conf.col_width_list_str) {
805 hists__set_col_len(self, se->se_width_idx,
807 col_width = strchr(col_width, ',');
812 if (!hists__new_col_len(self, se->se_width_idx, width))
813 width = hists__col_len(self, se->se_width_idx);
814 fprintf(fp, " %*s", width, se->se_header);
821 fprintf(fp, "# ........");
822 if (symbol_conf.show_nr_samples)
823 fprintf(fp, " ..........");
825 fprintf(fp, " ..........");
826 if (show_displacement)
827 fprintf(fp, " .....");
829 list_for_each_entry(se, &hist_entry__sort_list, list) {
836 width = hists__col_len(self, se->se_width_idx);
838 width = strlen(se->se_header);
839 for (i = 0; i < width; i++)
843 fprintf(fp, "\n#\n");
846 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
847 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
849 if (show_displacement) {
851 displacement = ((long)h->pair->position -
857 ret += hist_entry__fprintf(h, self, pair, show_displacement,
858 displacement, fp, self->stats.total_period);
860 if (symbol_conf.use_callchain)
861 ret += hist_entry__fprintf_callchain(h, self, fp,
862 self->stats.total_period);
863 if (h->ms.map == NULL && verbose > 1) {
864 __map_groups__fprintf_maps(&h->thread->mg,
865 MAP__FUNCTION, verbose, fp);
866 fprintf(fp, "%.10s end\n", graph_dotted_line);
870 free(rem_sq_bracket);
876 * See hists__fprintf to match the column widths
878 unsigned int hists__sort_list_width(struct hists *self)
880 struct sort_entry *se;
881 int ret = 9; /* total % */
883 if (symbol_conf.show_cpu_utilization) {
884 ret += 7; /* count_sys % */
885 ret += 6; /* count_us % */
887 ret += 13; /* count_guest_sys % */
888 ret += 12; /* count_guest_us % */
892 if (symbol_conf.show_nr_samples)
895 list_for_each_entry(se, &hist_entry__sort_list, list)
897 ret += 2 + hists__col_len(self, se->se_width_idx);
899 if (verbose) /* Addr + origin */
900 ret += 3 + BITS_PER_LONG / 4;
905 static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
906 enum hist_filter filter)
908 h->filtered &= ~(1 << filter);
914 self->nr_entries += h->nr_rows;
916 self->stats.total_period += h->period;
917 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
919 hists__calc_col_len(self, h);
922 void hists__filter_by_dso(struct hists *self, const struct dso *dso)
926 self->nr_entries = self->stats.total_period = 0;
927 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
928 hists__reset_col_len(self);
930 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
931 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
933 if (symbol_conf.exclude_other && !h->parent)
936 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
937 h->filtered |= (1 << HIST_FILTER__DSO);
941 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
945 void hists__filter_by_thread(struct hists *self, const struct thread *thread)
949 self->nr_entries = self->stats.total_period = 0;
950 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
951 hists__reset_col_len(self);
953 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
954 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
956 if (thread != NULL && h->thread != thread) {
957 h->filtered |= (1 << HIST_FILTER__THREAD);
961 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
965 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
967 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
970 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
972 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
975 void hists__inc_nr_events(struct hists *self, u32 type)
977 ++self->stats.nr_events[0];
978 ++self->stats.nr_events[type];
981 size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
986 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
989 if (self->stats.nr_events[i] == 0)
992 name = perf_event__name(i);
993 if (!strcmp(name, "UNKNOWN"))
996 ret += fprintf(fp, "%16s events: %10d\n", name,
997 self->stats.nr_events[i]);