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 len = thread__comm_len(h->thread);
55 if (hists__new_col_len(self, HISTC_COMM, len))
56 hists__set_col_len(self, HISTC_THREAD, len + 6);
59 len = dso__name_len(h->ms.map->dso);
60 hists__new_col_len(self, HISTC_DSO, len);
64 static void hist_entry__add_cpumode_period(struct hist_entry *self,
65 unsigned int cpumode, u64 period)
68 case PERF_RECORD_MISC_KERNEL:
69 self->period_sys += period;
71 case PERF_RECORD_MISC_USER:
72 self->period_us += period;
74 case PERF_RECORD_MISC_GUEST_KERNEL:
75 self->period_guest_sys += period;
77 case PERF_RECORD_MISC_GUEST_USER:
78 self->period_guest_us += period;
86 * histogram, sorted on item, collects periods
89 static struct hist_entry *hist_entry__new(struct hist_entry *template)
91 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
92 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
98 self->ms.map->referenced = true;
99 if (symbol_conf.use_callchain)
100 callchain_init(self->callchain);
106 static void hists__inc_nr_entries(struct hists *self, struct hist_entry *h)
109 hists__calc_col_len(self, h);
114 static u8 symbol__parent_filter(const struct symbol *parent)
116 if (symbol_conf.exclude_other && parent == NULL)
117 return 1 << HIST_FILTER__PARENT;
121 struct hist_entry *__hists__add_entry(struct hists *self,
122 struct addr_location *al,
123 struct symbol *sym_parent, u64 period)
125 struct rb_node **p = &self->entries.rb_node;
126 struct rb_node *parent = NULL;
127 struct hist_entry *he;
128 struct hist_entry entry = {
129 .thread = al->thread,
138 .parent = sym_parent,
139 .filtered = symbol__parent_filter(sym_parent),
145 he = rb_entry(parent, struct hist_entry, rb_node);
147 cmp = hist_entry__cmp(&entry, he);
150 he->period += period;
161 he = hist_entry__new(&entry);
164 rb_link_node(&he->rb_node, parent, p);
165 rb_insert_color(&he->rb_node, &self->entries);
166 hists__inc_nr_entries(self, he);
168 hist_entry__add_cpumode_period(he, al->cpumode, period);
173 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
175 struct sort_entry *se;
178 list_for_each_entry(se, &hist_entry__sort_list, list) {
179 cmp = se->se_cmp(left, right);
188 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
190 struct sort_entry *se;
193 list_for_each_entry(se, &hist_entry__sort_list, list) {
194 int64_t (*f)(struct hist_entry *, struct hist_entry *);
196 f = se->se_collapse ?: se->se_cmp;
198 cmp = f(left, right);
206 void hist_entry__free(struct hist_entry *he)
212 * collapse the histogram
215 static bool hists__collapse_insert_entry(struct hists *self,
216 struct rb_root *root,
217 struct hist_entry *he)
219 struct rb_node **p = &root->rb_node;
220 struct rb_node *parent = NULL;
221 struct hist_entry *iter;
226 iter = rb_entry(parent, struct hist_entry, rb_node);
228 cmp = hist_entry__collapse(iter, he);
231 iter->period += he->period;
232 if (symbol_conf.use_callchain) {
233 callchain_cursor_reset(&self->callchain_cursor);
234 callchain_merge(&self->callchain_cursor, iter->callchain,
237 hist_entry__free(he);
247 rb_link_node(&he->rb_node, parent, p);
248 rb_insert_color(&he->rb_node, root);
252 void hists__collapse_resort(struct hists *self)
255 struct rb_node *next;
256 struct hist_entry *n;
258 if (!sort__need_collapse)
262 next = rb_first(&self->entries);
263 self->nr_entries = 0;
264 hists__reset_col_len(self);
267 n = rb_entry(next, struct hist_entry, rb_node);
268 next = rb_next(&n->rb_node);
270 rb_erase(&n->rb_node, &self->entries);
271 if (hists__collapse_insert_entry(self, &tmp, n))
272 hists__inc_nr_entries(self, n);
279 * reverse the map, sort on period.
282 static void __hists__insert_output_entry(struct rb_root *entries,
283 struct hist_entry *he,
284 u64 min_callchain_hits)
286 struct rb_node **p = &entries->rb_node;
287 struct rb_node *parent = NULL;
288 struct hist_entry *iter;
290 if (symbol_conf.use_callchain)
291 callchain_param.sort(&he->sorted_chain, he->callchain,
292 min_callchain_hits, &callchain_param);
296 iter = rb_entry(parent, struct hist_entry, rb_node);
298 if (he->period > iter->period)
304 rb_link_node(&he->rb_node, parent, p);
305 rb_insert_color(&he->rb_node, entries);
308 void hists__output_resort(struct hists *self)
311 struct rb_node *next;
312 struct hist_entry *n;
313 u64 min_callchain_hits;
315 min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
318 next = rb_first(&self->entries);
320 self->nr_entries = 0;
321 hists__reset_col_len(self);
324 n = rb_entry(next, struct hist_entry, rb_node);
325 next = rb_next(&n->rb_node);
327 rb_erase(&n->rb_node, &self->entries);
328 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
329 hists__inc_nr_entries(self, n);
335 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
338 int ret = fprintf(fp, " ");
340 for (i = 0; i < left_margin; i++)
341 ret += fprintf(fp, " ");
346 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
350 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
352 for (i = 0; i < depth; i++)
353 if (depth_mask & (1 << i))
354 ret += fprintf(fp, "| ");
356 ret += fprintf(fp, " ");
358 ret += fprintf(fp, "\n");
363 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
364 int depth, int depth_mask, int period,
365 u64 total_samples, u64 hits,
371 ret += callchain__fprintf_left_margin(fp, left_margin);
372 for (i = 0; i < depth; i++) {
373 if (depth_mask & (1 << i))
374 ret += fprintf(fp, "|");
376 ret += fprintf(fp, " ");
377 if (!period && i == depth - 1) {
380 percent = hits * 100.0 / total_samples;
381 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
383 ret += fprintf(fp, "%s", " ");
386 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
388 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
393 static struct symbol *rem_sq_bracket;
394 static struct callchain_list rem_hits;
396 static void init_rem_hits(void)
398 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
399 if (!rem_sq_bracket) {
400 fprintf(stderr, "Not enough memory to display remaining hits\n");
404 strcpy(rem_sq_bracket->name, "[...]");
405 rem_hits.ms.sym = rem_sq_bracket;
408 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
409 u64 total_samples, int depth,
410 int depth_mask, int left_margin)
412 struct rb_node *node, *next;
413 struct callchain_node *child;
414 struct callchain_list *chain;
415 int new_depth_mask = depth_mask;
420 uint entries_printed = 0;
422 if (callchain_param.mode == CHAIN_GRAPH_REL)
423 new_total = self->children_hit;
425 new_total = total_samples;
427 remaining = new_total;
429 node = rb_first(&self->rb_root);
433 child = rb_entry(node, struct callchain_node, rb_node);
434 cumul = callchain_cumul_hits(child);
438 * The depth mask manages the output of pipes that show
439 * the depth. We don't want to keep the pipes of the current
440 * level for the last child of this depth.
441 * Except if we have remaining filtered hits. They will
442 * supersede the last child
444 next = rb_next(node);
445 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
446 new_depth_mask &= ~(1 << (depth - 1));
449 * But we keep the older depth mask for the line separator
450 * to keep the level link until we reach the last child
452 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
455 list_for_each_entry(chain, &child->val, list) {
456 ret += ipchain__fprintf_graph(fp, chain, depth,
462 ret += __callchain__fprintf_graph(fp, child, new_total,
464 new_depth_mask | (1 << depth),
467 if (++entries_printed == callchain_param.print_limit)
471 if (callchain_param.mode == CHAIN_GRAPH_REL &&
472 remaining && remaining != new_total) {
477 new_depth_mask &= ~(1 << (depth - 1));
479 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
480 new_depth_mask, 0, new_total,
481 remaining, left_margin);
487 static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
488 u64 total_samples, int left_margin)
490 struct callchain_list *chain;
491 bool printed = false;
494 u32 entries_printed = 0;
496 list_for_each_entry(chain, &self->val, list) {
497 if (!i++ && sort__first_dimension == SORT_SYM)
501 ret += callchain__fprintf_left_margin(fp, left_margin);
502 ret += fprintf(fp, "|\n");
503 ret += callchain__fprintf_left_margin(fp, left_margin);
504 ret += fprintf(fp, "---");
509 ret += callchain__fprintf_left_margin(fp, left_margin);
512 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
514 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
516 if (++entries_printed == callchain_param.print_limit)
520 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
525 static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
528 struct callchain_list *chain;
534 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
537 list_for_each_entry(chain, &self->val, list) {
538 if (chain->ip >= PERF_CONTEXT_MAX)
541 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
543 ret += fprintf(fp, " %p\n",
544 (void *)(long)chain->ip);
550 static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
551 u64 total_samples, int left_margin)
553 struct rb_node *rb_node;
554 struct callchain_node *chain;
556 u32 entries_printed = 0;
558 rb_node = rb_first(&self->sorted_chain);
562 chain = rb_entry(rb_node, struct callchain_node, rb_node);
563 percent = chain->hit * 100.0 / total_samples;
564 switch (callchain_param.mode) {
566 ret += percent_color_fprintf(fp, " %6.2f%%\n",
568 ret += callchain__fprintf_flat(fp, chain, total_samples);
570 case CHAIN_GRAPH_ABS: /* Falldown */
571 case CHAIN_GRAPH_REL:
572 ret += callchain__fprintf_graph(fp, chain, total_samples,
578 ret += fprintf(fp, "\n");
579 if (++entries_printed == callchain_param.print_limit)
581 rb_node = rb_next(rb_node);
587 int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
588 struct hists *hists, struct hists *pair_hists,
589 bool show_displacement, long displacement,
590 bool color, u64 session_total)
592 struct sort_entry *se;
593 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
594 const char *sep = symbol_conf.field_sep;
597 if (symbol_conf.exclude_other && !self->parent)
601 period = self->pair ? self->pair->period : 0;
602 total = pair_hists->stats.total_period;
603 period_sys = self->pair ? self->pair->period_sys : 0;
604 period_us = self->pair ? self->pair->period_us : 0;
605 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
606 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
608 period = self->period;
609 total = session_total;
610 period_sys = self->period_sys;
611 period_us = self->period_us;
612 period_guest_sys = self->period_guest_sys;
613 period_guest_us = self->period_guest_us;
618 ret = percent_color_snprintf(s, size,
619 sep ? "%.2f" : " %6.2f%%",
620 (period * 100.0) / total);
622 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
623 (period * 100.0) / total);
624 if (symbol_conf.show_cpu_utilization) {
625 ret += percent_color_snprintf(s + ret, size - ret,
626 sep ? "%.2f" : " %6.2f%%",
627 (period_sys * 100.0) / total);
628 ret += percent_color_snprintf(s + ret, size - ret,
629 sep ? "%.2f" : " %6.2f%%",
630 (period_us * 100.0) / total);
632 ret += percent_color_snprintf(s + ret,
634 sep ? "%.2f" : " %6.2f%%",
635 (period_guest_sys * 100.0) /
637 ret += percent_color_snprintf(s + ret,
639 sep ? "%.2f" : " %6.2f%%",
640 (period_guest_us * 100.0) /
645 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
647 if (symbol_conf.show_nr_samples) {
649 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
651 ret += snprintf(s + ret, size - ret, "%11" PRIu64, period);
656 double old_percent = 0, new_percent = 0, diff;
659 old_percent = (period * 100.0) / total;
660 if (session_total > 0)
661 new_percent = (self->period * 100.0) / session_total;
663 diff = new_percent - old_percent;
665 if (fabs(diff) >= 0.01)
666 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
668 snprintf(bf, sizeof(bf), " ");
671 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
673 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
675 if (show_displacement) {
677 snprintf(bf, sizeof(bf), "%+4ld", displacement);
679 snprintf(bf, sizeof(bf), " ");
682 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
684 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
688 list_for_each_entry(se, &hist_entry__sort_list, list) {
692 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
693 ret += se->se_snprintf(self, s + ret, size - ret,
694 hists__col_len(hists, se->se_width_idx));
700 int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
701 struct hists *pair_hists, bool show_displacement,
702 long displacement, FILE *fp, u64 session_total)
705 hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
706 show_displacement, displacement,
707 true, session_total);
708 return fprintf(fp, "%s\n", bf);
711 static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
712 struct hists *hists, FILE *fp,
717 if (sort__first_dimension == SORT_COMM) {
718 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
720 left_margin = hists__col_len(hists, se->se_width_idx);
721 left_margin -= thread__comm_len(self->thread);
724 return hist_entry_callchain__fprintf(fp, self, session_total,
728 size_t hists__fprintf(struct hists *self, struct hists *pair,
729 bool show_displacement, FILE *fp)
731 struct sort_entry *se;
734 unsigned long position = 1;
735 long displacement = 0;
737 const char *sep = symbol_conf.field_sep;
738 const char *col_width = symbol_conf.col_width_list_str;
742 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
744 if (symbol_conf.show_nr_samples) {
746 fprintf(fp, "%cSamples", *sep);
748 fputs(" Samples ", fp);
751 if (symbol_conf.show_cpu_utilization) {
753 ret += fprintf(fp, "%csys", *sep);
754 ret += fprintf(fp, "%cus", *sep);
756 ret += fprintf(fp, "%cguest sys", *sep);
757 ret += fprintf(fp, "%cguest us", *sep);
760 ret += fprintf(fp, " sys ");
761 ret += fprintf(fp, " us ");
763 ret += fprintf(fp, " guest sys ");
764 ret += fprintf(fp, " guest us ");
771 ret += fprintf(fp, "%cDelta", *sep);
773 ret += fprintf(fp, " Delta ");
775 if (show_displacement) {
777 ret += fprintf(fp, "%cDisplacement", *sep);
779 ret += fprintf(fp, " Displ");
783 list_for_each_entry(se, &hist_entry__sort_list, list) {
787 fprintf(fp, "%c%s", *sep, se->se_header);
790 width = strlen(se->se_header);
791 if (symbol_conf.col_width_list_str) {
793 hists__set_col_len(self, se->se_width_idx,
795 col_width = strchr(col_width, ',');
800 if (!hists__new_col_len(self, se->se_width_idx, width))
801 width = hists__col_len(self, se->se_width_idx);
802 fprintf(fp, " %*s", width, se->se_header);
809 fprintf(fp, "# ........");
810 if (symbol_conf.show_nr_samples)
811 fprintf(fp, " ..........");
813 fprintf(fp, " ..........");
814 if (show_displacement)
815 fprintf(fp, " .....");
817 list_for_each_entry(se, &hist_entry__sort_list, list) {
824 width = hists__col_len(self, se->se_width_idx);
826 width = strlen(se->se_header);
827 for (i = 0; i < width; i++)
831 fprintf(fp, "\n#\n");
834 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
835 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
837 if (show_displacement) {
839 displacement = ((long)h->pair->position -
845 ret += hist_entry__fprintf(h, self, pair, show_displacement,
846 displacement, fp, self->stats.total_period);
848 if (symbol_conf.use_callchain)
849 ret += hist_entry__fprintf_callchain(h, self, fp,
850 self->stats.total_period);
851 if (h->ms.map == NULL && verbose > 1) {
852 __map_groups__fprintf_maps(&h->thread->mg,
853 MAP__FUNCTION, verbose, fp);
854 fprintf(fp, "%.10s end\n", graph_dotted_line);
858 free(rem_sq_bracket);
864 * See hists__fprintf to match the column widths
866 unsigned int hists__sort_list_width(struct hists *self)
868 struct sort_entry *se;
869 int ret = 9; /* total % */
871 if (symbol_conf.show_cpu_utilization) {
872 ret += 7; /* count_sys % */
873 ret += 6; /* count_us % */
875 ret += 13; /* count_guest_sys % */
876 ret += 12; /* count_guest_us % */
880 if (symbol_conf.show_nr_samples)
883 list_for_each_entry(se, &hist_entry__sort_list, list)
885 ret += 2 + hists__col_len(self, se->se_width_idx);
887 if (verbose) /* Addr + origin */
888 ret += 3 + BITS_PER_LONG / 4;
893 static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
894 enum hist_filter filter)
896 h->filtered &= ~(1 << filter);
902 self->nr_entries += h->nr_rows;
904 self->stats.total_period += h->period;
905 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
907 hists__calc_col_len(self, h);
910 void hists__filter_by_dso(struct hists *self, const struct dso *dso)
914 self->nr_entries = self->stats.total_period = 0;
915 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
916 hists__reset_col_len(self);
918 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
919 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
921 if (symbol_conf.exclude_other && !h->parent)
924 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
925 h->filtered |= (1 << HIST_FILTER__DSO);
929 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
933 void hists__filter_by_thread(struct hists *self, const struct thread *thread)
937 self->nr_entries = self->stats.total_period = 0;
938 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
939 hists__reset_col_len(self);
941 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
942 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
944 if (thread != NULL && h->thread != thread) {
945 h->filtered |= (1 << HIST_FILTER__THREAD);
949 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
953 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
955 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
958 int hist_entry__annotate(struct hist_entry *he, struct list_head *head,
961 return symbol__annotate(he->ms.sym, he->ms.map, head, privsize);
964 void hists__inc_nr_events(struct hists *self, u32 type)
966 ++self->stats.nr_events[0];
967 ++self->stats.nr_events[type];
970 size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
975 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
976 const char *name = perf_event__name(i);
978 if (!strcmp(name, "UNKNOWN"))
981 ret += fprintf(fp, "%16s events: %10d\n", name,
982 self->stats.nr_events[i]);