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;
162 /* If the map of an existing hist_entry has
163 * become out-of-date due to an exec() or
164 * similar, update it. Otherwise we will
165 * mis-adjust symbol addresses when computing
166 * the history counter to increment.
168 if (he->ms.map != entry->ms.map) {
169 he->ms.map = entry->ms.map;
171 he->ms.map->referenced = true;
182 he = hist_entry__new(&entry);
185 rb_link_node(&he->rb_node, parent, p);
186 rb_insert_color(&he->rb_node, &self->entries);
187 hists__inc_nr_entries(self, he);
189 hist_entry__add_cpumode_period(he, al->cpumode, period);
194 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
196 struct sort_entry *se;
199 list_for_each_entry(se, &hist_entry__sort_list, list) {
200 cmp = se->se_cmp(left, right);
209 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
211 struct sort_entry *se;
214 list_for_each_entry(se, &hist_entry__sort_list, list) {
215 int64_t (*f)(struct hist_entry *, struct hist_entry *);
217 f = se->se_collapse ?: se->se_cmp;
219 cmp = f(left, right);
227 void hist_entry__free(struct hist_entry *he)
233 * collapse the histogram
236 static bool hists__collapse_insert_entry(struct hists *self,
237 struct rb_root *root,
238 struct hist_entry *he)
240 struct rb_node **p = &root->rb_node;
241 struct rb_node *parent = NULL;
242 struct hist_entry *iter;
247 iter = rb_entry(parent, struct hist_entry, rb_node);
249 cmp = hist_entry__collapse(iter, he);
252 iter->period += he->period;
253 if (symbol_conf.use_callchain) {
254 callchain_cursor_reset(&self->callchain_cursor);
255 callchain_merge(&self->callchain_cursor, iter->callchain,
258 hist_entry__free(he);
268 rb_link_node(&he->rb_node, parent, p);
269 rb_insert_color(&he->rb_node, root);
273 void hists__collapse_resort(struct hists *self)
276 struct rb_node *next;
277 struct hist_entry *n;
279 if (!sort__need_collapse)
283 next = rb_first(&self->entries);
284 self->nr_entries = 0;
285 hists__reset_col_len(self);
288 n = rb_entry(next, struct hist_entry, rb_node);
289 next = rb_next(&n->rb_node);
291 rb_erase(&n->rb_node, &self->entries);
292 if (hists__collapse_insert_entry(self, &tmp, n))
293 hists__inc_nr_entries(self, n);
300 * reverse the map, sort on period.
303 static void __hists__insert_output_entry(struct rb_root *entries,
304 struct hist_entry *he,
305 u64 min_callchain_hits)
307 struct rb_node **p = &entries->rb_node;
308 struct rb_node *parent = NULL;
309 struct hist_entry *iter;
311 if (symbol_conf.use_callchain)
312 callchain_param.sort(&he->sorted_chain, he->callchain,
313 min_callchain_hits, &callchain_param);
317 iter = rb_entry(parent, struct hist_entry, rb_node);
319 if (he->period > iter->period)
325 rb_link_node(&he->rb_node, parent, p);
326 rb_insert_color(&he->rb_node, entries);
329 void hists__output_resort(struct hists *self)
332 struct rb_node *next;
333 struct hist_entry *n;
334 u64 min_callchain_hits;
336 min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
339 next = rb_first(&self->entries);
341 self->nr_entries = 0;
342 hists__reset_col_len(self);
345 n = rb_entry(next, struct hist_entry, rb_node);
346 next = rb_next(&n->rb_node);
348 rb_erase(&n->rb_node, &self->entries);
349 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
350 hists__inc_nr_entries(self, n);
356 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
359 int ret = fprintf(fp, " ");
361 for (i = 0; i < left_margin; i++)
362 ret += fprintf(fp, " ");
367 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
371 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
373 for (i = 0; i < depth; i++)
374 if (depth_mask & (1 << i))
375 ret += fprintf(fp, "| ");
377 ret += fprintf(fp, " ");
379 ret += fprintf(fp, "\n");
384 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
385 int depth, int depth_mask, int period,
386 u64 total_samples, u64 hits,
392 ret += callchain__fprintf_left_margin(fp, left_margin);
393 for (i = 0; i < depth; i++) {
394 if (depth_mask & (1 << i))
395 ret += fprintf(fp, "|");
397 ret += fprintf(fp, " ");
398 if (!period && i == depth - 1) {
401 percent = hits * 100.0 / total_samples;
402 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
404 ret += fprintf(fp, "%s", " ");
407 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
409 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
414 static struct symbol *rem_sq_bracket;
415 static struct callchain_list rem_hits;
417 static void init_rem_hits(void)
419 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
420 if (!rem_sq_bracket) {
421 fprintf(stderr, "Not enough memory to display remaining hits\n");
425 strcpy(rem_sq_bracket->name, "[...]");
426 rem_hits.ms.sym = rem_sq_bracket;
429 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
430 u64 total_samples, int depth,
431 int depth_mask, int left_margin)
433 struct rb_node *node, *next;
434 struct callchain_node *child;
435 struct callchain_list *chain;
436 int new_depth_mask = depth_mask;
441 uint entries_printed = 0;
443 if (callchain_param.mode == CHAIN_GRAPH_REL)
444 new_total = self->children_hit;
446 new_total = total_samples;
448 remaining = new_total;
450 node = rb_first(&self->rb_root);
454 child = rb_entry(node, struct callchain_node, rb_node);
455 cumul = callchain_cumul_hits(child);
459 * The depth mask manages the output of pipes that show
460 * the depth. We don't want to keep the pipes of the current
461 * level for the last child of this depth.
462 * Except if we have remaining filtered hits. They will
463 * supersede the last child
465 next = rb_next(node);
466 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
467 new_depth_mask &= ~(1 << (depth - 1));
470 * But we keep the older depth mask for the line separator
471 * to keep the level link until we reach the last child
473 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
476 list_for_each_entry(chain, &child->val, list) {
477 ret += ipchain__fprintf_graph(fp, chain, depth,
483 ret += __callchain__fprintf_graph(fp, child, new_total,
485 new_depth_mask | (1 << depth),
488 if (++entries_printed == callchain_param.print_limit)
492 if (callchain_param.mode == CHAIN_GRAPH_REL &&
493 remaining && remaining != new_total) {
498 new_depth_mask &= ~(1 << (depth - 1));
500 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
501 new_depth_mask, 0, new_total,
502 remaining, left_margin);
508 static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
509 u64 total_samples, int left_margin)
511 struct callchain_list *chain;
512 bool printed = false;
515 u32 entries_printed = 0;
517 list_for_each_entry(chain, &self->val, list) {
518 if (!i++ && sort__first_dimension == SORT_SYM)
522 ret += callchain__fprintf_left_margin(fp, left_margin);
523 ret += fprintf(fp, "|\n");
524 ret += callchain__fprintf_left_margin(fp, left_margin);
525 ret += fprintf(fp, "---");
530 ret += callchain__fprintf_left_margin(fp, left_margin);
533 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
535 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
537 if (++entries_printed == callchain_param.print_limit)
541 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
546 static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
549 struct callchain_list *chain;
555 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
558 list_for_each_entry(chain, &self->val, list) {
559 if (chain->ip >= PERF_CONTEXT_MAX)
562 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
564 ret += fprintf(fp, " %p\n",
565 (void *)(long)chain->ip);
571 static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
572 u64 total_samples, int left_margin)
574 struct rb_node *rb_node;
575 struct callchain_node *chain;
577 u32 entries_printed = 0;
579 rb_node = rb_first(&self->sorted_chain);
583 chain = rb_entry(rb_node, struct callchain_node, rb_node);
584 percent = chain->hit * 100.0 / total_samples;
585 switch (callchain_param.mode) {
587 ret += percent_color_fprintf(fp, " %6.2f%%\n",
589 ret += callchain__fprintf_flat(fp, chain, total_samples);
591 case CHAIN_GRAPH_ABS: /* Falldown */
592 case CHAIN_GRAPH_REL:
593 ret += callchain__fprintf_graph(fp, chain, total_samples,
599 ret += fprintf(fp, "\n");
600 if (++entries_printed == callchain_param.print_limit)
602 rb_node = rb_next(rb_node);
608 int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
609 struct hists *hists, struct hists *pair_hists,
610 bool show_displacement, long displacement,
611 bool color, u64 session_total)
613 struct sort_entry *se;
614 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
616 const char *sep = symbol_conf.field_sep;
619 if (symbol_conf.exclude_other && !self->parent)
623 period = self->pair ? self->pair->period : 0;
624 nr_events = self->pair ? self->pair->nr_events : 0;
625 total = pair_hists->stats.total_period;
626 period_sys = self->pair ? self->pair->period_sys : 0;
627 period_us = self->pair ? self->pair->period_us : 0;
628 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
629 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
631 period = self->period;
632 nr_events = self->nr_events;
633 total = session_total;
634 period_sys = self->period_sys;
635 period_us = self->period_us;
636 period_guest_sys = self->period_guest_sys;
637 period_guest_us = self->period_guest_us;
642 ret = percent_color_snprintf(s, size,
643 sep ? "%.2f" : " %6.2f%%",
644 (period * 100.0) / total);
646 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
647 (period * 100.0) / total);
648 if (symbol_conf.show_cpu_utilization) {
649 ret += percent_color_snprintf(s + ret, size - ret,
650 sep ? "%.2f" : " %6.2f%%",
651 (period_sys * 100.0) / total);
652 ret += percent_color_snprintf(s + ret, size - ret,
653 sep ? "%.2f" : " %6.2f%%",
654 (period_us * 100.0) / total);
656 ret += percent_color_snprintf(s + ret,
658 sep ? "%.2f" : " %6.2f%%",
659 (period_guest_sys * 100.0) /
661 ret += percent_color_snprintf(s + ret,
663 sep ? "%.2f" : " %6.2f%%",
664 (period_guest_us * 100.0) /
669 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
671 if (symbol_conf.show_nr_samples) {
673 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
675 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
680 double old_percent = 0, new_percent = 0, diff;
683 old_percent = (period * 100.0) / total;
684 if (session_total > 0)
685 new_percent = (self->period * 100.0) / session_total;
687 diff = new_percent - old_percent;
689 if (fabs(diff) >= 0.01)
690 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
692 snprintf(bf, sizeof(bf), " ");
695 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
697 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
699 if (show_displacement) {
701 snprintf(bf, sizeof(bf), "%+4ld", displacement);
703 snprintf(bf, sizeof(bf), " ");
706 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
708 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
712 list_for_each_entry(se, &hist_entry__sort_list, list) {
716 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
717 ret += se->se_snprintf(self, s + ret, size - ret,
718 hists__col_len(hists, se->se_width_idx));
724 int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
725 struct hists *pair_hists, bool show_displacement,
726 long displacement, FILE *fp, u64 session_total)
729 hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
730 show_displacement, displacement,
731 true, session_total);
732 return fprintf(fp, "%s\n", bf);
735 static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
736 struct hists *hists, FILE *fp,
741 if (sort__first_dimension == SORT_COMM) {
742 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
744 left_margin = hists__col_len(hists, se->se_width_idx);
745 left_margin -= thread__comm_len(self->thread);
748 return hist_entry_callchain__fprintf(fp, self, session_total,
752 size_t hists__fprintf(struct hists *self, struct hists *pair,
753 bool show_displacement, FILE *fp)
755 struct sort_entry *se;
758 unsigned long position = 1;
759 long displacement = 0;
761 const char *sep = symbol_conf.field_sep;
762 const char *col_width = symbol_conf.col_width_list_str;
766 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
768 if (symbol_conf.show_nr_samples) {
770 fprintf(fp, "%cSamples", *sep);
772 fputs(" Samples ", fp);
775 if (symbol_conf.show_cpu_utilization) {
777 ret += fprintf(fp, "%csys", *sep);
778 ret += fprintf(fp, "%cus", *sep);
780 ret += fprintf(fp, "%cguest sys", *sep);
781 ret += fprintf(fp, "%cguest us", *sep);
784 ret += fprintf(fp, " sys ");
785 ret += fprintf(fp, " us ");
787 ret += fprintf(fp, " guest sys ");
788 ret += fprintf(fp, " guest us ");
795 ret += fprintf(fp, "%cDelta", *sep);
797 ret += fprintf(fp, " Delta ");
799 if (show_displacement) {
801 ret += fprintf(fp, "%cDisplacement", *sep);
803 ret += fprintf(fp, " Displ");
807 list_for_each_entry(se, &hist_entry__sort_list, list) {
811 fprintf(fp, "%c%s", *sep, se->se_header);
814 width = strlen(se->se_header);
815 if (symbol_conf.col_width_list_str) {
817 hists__set_col_len(self, se->se_width_idx,
819 col_width = strchr(col_width, ',');
824 if (!hists__new_col_len(self, se->se_width_idx, width))
825 width = hists__col_len(self, se->se_width_idx);
826 fprintf(fp, " %*s", width, se->se_header);
833 fprintf(fp, "# ........");
834 if (symbol_conf.show_nr_samples)
835 fprintf(fp, " ..........");
837 fprintf(fp, " ..........");
838 if (show_displacement)
839 fprintf(fp, " .....");
841 list_for_each_entry(se, &hist_entry__sort_list, list) {
848 width = hists__col_len(self, se->se_width_idx);
850 width = strlen(se->se_header);
851 for (i = 0; i < width; i++)
855 fprintf(fp, "\n#\n");
858 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
859 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
861 if (show_displacement) {
863 displacement = ((long)h->pair->position -
869 ret += hist_entry__fprintf(h, self, pair, show_displacement,
870 displacement, fp, self->stats.total_period);
872 if (symbol_conf.use_callchain)
873 ret += hist_entry__fprintf_callchain(h, self, fp,
874 self->stats.total_period);
875 if (h->ms.map == NULL && verbose > 1) {
876 __map_groups__fprintf_maps(&h->thread->mg,
877 MAP__FUNCTION, verbose, fp);
878 fprintf(fp, "%.10s end\n", graph_dotted_line);
882 free(rem_sq_bracket);
888 * See hists__fprintf to match the column widths
890 unsigned int hists__sort_list_width(struct hists *self)
892 struct sort_entry *se;
893 int ret = 9; /* total % */
895 if (symbol_conf.show_cpu_utilization) {
896 ret += 7; /* count_sys % */
897 ret += 6; /* count_us % */
899 ret += 13; /* count_guest_sys % */
900 ret += 12; /* count_guest_us % */
904 if (symbol_conf.show_nr_samples)
907 list_for_each_entry(se, &hist_entry__sort_list, list)
909 ret += 2 + hists__col_len(self, se->se_width_idx);
911 if (verbose) /* Addr + origin */
912 ret += 3 + BITS_PER_LONG / 4;
917 static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
918 enum hist_filter filter)
920 h->filtered &= ~(1 << filter);
926 self->nr_entries += h->nr_rows;
928 self->stats.total_period += h->period;
929 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
931 hists__calc_col_len(self, h);
934 void hists__filter_by_dso(struct hists *self, const struct dso *dso)
938 self->nr_entries = self->stats.total_period = 0;
939 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
940 hists__reset_col_len(self);
942 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
943 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
945 if (symbol_conf.exclude_other && !h->parent)
948 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
949 h->filtered |= (1 << HIST_FILTER__DSO);
953 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
957 void hists__filter_by_thread(struct hists *self, const struct thread *thread)
961 self->nr_entries = self->stats.total_period = 0;
962 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
963 hists__reset_col_len(self);
965 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
966 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
968 if (thread != NULL && h->thread != thread) {
969 h->filtered |= (1 << HIST_FILTER__THREAD);
973 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
977 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
979 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
982 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
984 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
987 void hists__inc_nr_events(struct hists *self, u32 type)
989 ++self->stats.nr_events[0];
990 ++self->stats.nr_events[type];
993 size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
998 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
1001 if (self->stats.nr_events[i] == 0)
1004 name = perf_event__name(i);
1005 if (!strcmp(name, "UNKNOWN"))
1008 ret += fprintf(fp, "%16s events: %10d\n", name,
1009 self->stats.nr_events[i]);