6 struct callchain_param callchain_param = {
7 .mode = CHAIN_GRAPH_REL,
11 static void hist_entry__add_cpumode_count(struct hist_entry *self,
12 unsigned int cpumode, u64 count)
15 case PERF_RECORD_MISC_KERNEL:
16 self->count_sys += count;
18 case PERF_RECORD_MISC_USER:
19 self->count_us += count;
21 case PERF_RECORD_MISC_GUEST_KERNEL:
22 self->count_guest_sys += count;
24 case PERF_RECORD_MISC_GUEST_USER:
25 self->count_guest_us += count;
33 * histogram, sorted on item, collects counts
36 static struct hist_entry *hist_entry__new(struct hist_entry *template)
38 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_node) : 0;
39 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
43 if (symbol_conf.use_callchain)
44 callchain_init(self->callchain);
50 struct hist_entry *__hists__add_entry(struct hists *self,
51 struct addr_location *al,
52 struct symbol *sym_parent, u64 count)
54 struct rb_node **p = &self->entries.rb_node;
55 struct rb_node *parent = NULL;
56 struct hist_entry *he;
57 struct hist_entry entry = {
72 he = rb_entry(parent, struct hist_entry, rb_node);
74 cmp = hist_entry__cmp(&entry, he);
87 he = hist_entry__new(&entry);
90 rb_link_node(&he->rb_node, parent, p);
91 rb_insert_color(&he->rb_node, &self->entries);
93 hist_entry__add_cpumode_count(he, al->cpumode, count);
98 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
100 struct sort_entry *se;
103 list_for_each_entry(se, &hist_entry__sort_list, list) {
104 cmp = se->se_cmp(left, right);
113 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
115 struct sort_entry *se;
118 list_for_each_entry(se, &hist_entry__sort_list, list) {
119 int64_t (*f)(struct hist_entry *, struct hist_entry *);
121 f = se->se_collapse ?: se->se_cmp;
123 cmp = f(left, right);
131 void hist_entry__free(struct hist_entry *he)
137 * collapse the histogram
140 static void collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
142 struct rb_node **p = &root->rb_node;
143 struct rb_node *parent = NULL;
144 struct hist_entry *iter;
149 iter = rb_entry(parent, struct hist_entry, rb_node);
151 cmp = hist_entry__collapse(iter, he);
154 iter->count += he->count;
155 hist_entry__free(he);
165 rb_link_node(&he->rb_node, parent, p);
166 rb_insert_color(&he->rb_node, root);
169 void hists__collapse_resort(struct hists *self)
172 struct rb_node *next;
173 struct hist_entry *n;
175 if (!sort__need_collapse)
179 next = rb_first(&self->entries);
182 n = rb_entry(next, struct hist_entry, rb_node);
183 next = rb_next(&n->rb_node);
185 rb_erase(&n->rb_node, &self->entries);
186 collapse__insert_entry(&tmp, n);
193 * reverse the map, sort on count.
196 static void __hists__insert_output_entry(struct rb_root *entries,
197 struct hist_entry *he,
198 u64 min_callchain_hits)
200 struct rb_node **p = &entries->rb_node;
201 struct rb_node *parent = NULL;
202 struct hist_entry *iter;
204 if (symbol_conf.use_callchain)
205 callchain_param.sort(&he->sorted_chain, he->callchain,
206 min_callchain_hits, &callchain_param);
210 iter = rb_entry(parent, struct hist_entry, rb_node);
212 if (he->count > iter->count)
218 rb_link_node(&he->rb_node, parent, p);
219 rb_insert_color(&he->rb_node, entries);
222 u64 hists__output_resort(struct hists *self)
225 struct rb_node *next;
226 struct hist_entry *n;
227 u64 min_callchain_hits;
230 min_callchain_hits = self->stats.total * (callchain_param.min_percent / 100);
233 next = rb_first(&self->entries);
236 n = rb_entry(next, struct hist_entry, rb_node);
237 next = rb_next(&n->rb_node);
239 rb_erase(&n->rb_node, &self->entries);
240 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
248 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
251 int ret = fprintf(fp, " ");
253 for (i = 0; i < left_margin; i++)
254 ret += fprintf(fp, " ");
259 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
263 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
265 for (i = 0; i < depth; i++)
266 if (depth_mask & (1 << i))
267 ret += fprintf(fp, "| ");
269 ret += fprintf(fp, " ");
271 ret += fprintf(fp, "\n");
276 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
277 int depth, int depth_mask, int count,
278 u64 total_samples, int hits,
284 ret += callchain__fprintf_left_margin(fp, left_margin);
285 for (i = 0; i < depth; i++) {
286 if (depth_mask & (1 << i))
287 ret += fprintf(fp, "|");
289 ret += fprintf(fp, " ");
290 if (!count && i == depth - 1) {
293 percent = hits * 100.0 / total_samples;
294 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
296 ret += fprintf(fp, "%s", " ");
299 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
301 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
306 static struct symbol *rem_sq_bracket;
307 static struct callchain_list rem_hits;
309 static void init_rem_hits(void)
311 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
312 if (!rem_sq_bracket) {
313 fprintf(stderr, "Not enough memory to display remaining hits\n");
317 strcpy(rem_sq_bracket->name, "[...]");
318 rem_hits.ms.sym = rem_sq_bracket;
321 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
322 u64 total_samples, int depth,
323 int depth_mask, int left_margin)
325 struct rb_node *node, *next;
326 struct callchain_node *child;
327 struct callchain_list *chain;
328 int new_depth_mask = depth_mask;
333 uint entries_printed = 0;
335 if (callchain_param.mode == CHAIN_GRAPH_REL)
336 new_total = self->children_hit;
338 new_total = total_samples;
340 remaining = new_total;
342 node = rb_first(&self->rb_root);
346 child = rb_entry(node, struct callchain_node, rb_node);
347 cumul = cumul_hits(child);
351 * The depth mask manages the output of pipes that show
352 * the depth. We don't want to keep the pipes of the current
353 * level for the last child of this depth.
354 * Except if we have remaining filtered hits. They will
355 * supersede the last child
357 next = rb_next(node);
358 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
359 new_depth_mask &= ~(1 << (depth - 1));
362 * But we keep the older depth mask for the line separator
363 * to keep the level link until we reach the last child
365 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
368 list_for_each_entry(chain, &child->val, list) {
369 ret += ipchain__fprintf_graph(fp, chain, depth,
375 ret += __callchain__fprintf_graph(fp, child, new_total,
377 new_depth_mask | (1 << depth),
380 if (++entries_printed == callchain_param.print_limit)
384 if (callchain_param.mode == CHAIN_GRAPH_REL &&
385 remaining && remaining != new_total) {
390 new_depth_mask &= ~(1 << (depth - 1));
392 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
393 new_depth_mask, 0, new_total,
394 remaining, left_margin);
400 static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
401 u64 total_samples, int left_margin)
403 struct callchain_list *chain;
404 bool printed = false;
407 u32 entries_printed = 0;
409 list_for_each_entry(chain, &self->val, list) {
410 if (!i++ && sort__first_dimension == SORT_SYM)
414 ret += callchain__fprintf_left_margin(fp, left_margin);
415 ret += fprintf(fp, "|\n");
416 ret += callchain__fprintf_left_margin(fp, left_margin);
417 ret += fprintf(fp, "---");
422 ret += callchain__fprintf_left_margin(fp, left_margin);
425 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
427 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
429 if (++entries_printed == callchain_param.print_limit)
433 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
438 static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
441 struct callchain_list *chain;
447 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
450 list_for_each_entry(chain, &self->val, list) {
451 if (chain->ip >= PERF_CONTEXT_MAX)
454 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
456 ret += fprintf(fp, " %p\n",
457 (void *)(long)chain->ip);
463 static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
464 u64 total_samples, int left_margin)
466 struct rb_node *rb_node;
467 struct callchain_node *chain;
469 u32 entries_printed = 0;
471 rb_node = rb_first(&self->sorted_chain);
475 chain = rb_entry(rb_node, struct callchain_node, rb_node);
476 percent = chain->hit * 100.0 / total_samples;
477 switch (callchain_param.mode) {
479 ret += percent_color_fprintf(fp, " %6.2f%%\n",
481 ret += callchain__fprintf_flat(fp, chain, total_samples);
483 case CHAIN_GRAPH_ABS: /* Falldown */
484 case CHAIN_GRAPH_REL:
485 ret += callchain__fprintf_graph(fp, chain, total_samples,
491 ret += fprintf(fp, "\n");
492 if (++entries_printed == callchain_param.print_limit)
494 rb_node = rb_next(rb_node);
500 int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
501 struct hists *pair_hists, bool show_displacement,
502 long displacement, bool color, u64 session_total)
504 struct sort_entry *se;
505 u64 count, total, count_sys, count_us, count_guest_sys, count_guest_us;
506 const char *sep = symbol_conf.field_sep;
509 if (symbol_conf.exclude_other && !self->parent)
513 count = self->pair ? self->pair->count : 0;
514 total = pair_hists->stats.total;
515 count_sys = self->pair ? self->pair->count_sys : 0;
516 count_us = self->pair ? self->pair->count_us : 0;
517 count_guest_sys = self->pair ? self->pair->count_guest_sys : 0;
518 count_guest_us = self->pair ? self->pair->count_guest_us : 0;
521 total = session_total;
522 count_sys = self->count_sys;
523 count_us = self->count_us;
524 count_guest_sys = self->count_guest_sys;
525 count_guest_us = self->count_guest_us;
530 ret = percent_color_snprintf(s, size,
531 sep ? "%.2f" : " %6.2f%%",
532 (count * 100.0) / total);
534 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
535 (count * 100.0) / total);
536 if (symbol_conf.show_cpu_utilization) {
537 ret += percent_color_snprintf(s + ret, size - ret,
538 sep ? "%.2f" : " %6.2f%%",
539 (count_sys * 100.0) / total);
540 ret += percent_color_snprintf(s + ret, size - ret,
541 sep ? "%.2f" : " %6.2f%%",
542 (count_us * 100.0) / total);
544 ret += percent_color_snprintf(s + ret,
546 sep ? "%.2f" : " %6.2f%%",
547 (count_guest_sys * 100.0) /
549 ret += percent_color_snprintf(s + ret,
551 sep ? "%.2f" : " %6.2f%%",
552 (count_guest_us * 100.0) /
557 ret = snprintf(s, size, sep ? "%lld" : "%12lld ", count);
559 if (symbol_conf.show_nr_samples) {
561 ret += snprintf(s + ret, size - ret, "%c%lld", *sep, count);
563 ret += snprintf(s + ret, size - ret, "%11lld", count);
568 double old_percent = 0, new_percent = 0, diff;
571 old_percent = (count * 100.0) / total;
572 if (session_total > 0)
573 new_percent = (self->count * 100.0) / session_total;
575 diff = new_percent - old_percent;
577 if (fabs(diff) >= 0.01)
578 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
580 snprintf(bf, sizeof(bf), " ");
583 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
585 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
587 if (show_displacement) {
589 snprintf(bf, sizeof(bf), "%+4ld", displacement);
591 snprintf(bf, sizeof(bf), " ");
594 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
596 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
600 list_for_each_entry(se, &hist_entry__sort_list, list) {
604 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
605 ret += se->se_snprintf(self, s + ret, size - ret,
606 se->se_width ? *se->se_width : 0);
612 int hist_entry__fprintf(struct hist_entry *self, struct hists *pair_hists,
613 bool show_displacement, long displacement, FILE *fp,
617 hist_entry__snprintf(self, bf, sizeof(bf), pair_hists,
618 show_displacement, displacement,
619 true, session_total);
620 return fprintf(fp, "%s\n", bf);
623 static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
628 if (sort__first_dimension == SORT_COMM) {
629 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
631 left_margin = se->se_width ? *se->se_width : 0;
632 left_margin -= thread__comm_len(self->thread);
635 return hist_entry_callchain__fprintf(fp, self, session_total,
639 size_t hists__fprintf(struct hists *self, struct hists *pair,
640 bool show_displacement, FILE *fp)
642 struct sort_entry *se;
645 unsigned long position = 1;
646 long displacement = 0;
648 const char *sep = symbol_conf.field_sep;
649 char *col_width = symbol_conf.col_width_list_str;
653 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
655 if (symbol_conf.show_nr_samples) {
657 fprintf(fp, "%cSamples", *sep);
659 fputs(" Samples ", fp);
662 if (symbol_conf.show_cpu_utilization) {
664 ret += fprintf(fp, "%csys", *sep);
665 ret += fprintf(fp, "%cus", *sep);
667 ret += fprintf(fp, "%cguest sys", *sep);
668 ret += fprintf(fp, "%cguest us", *sep);
671 ret += fprintf(fp, " sys ");
672 ret += fprintf(fp, " us ");
674 ret += fprintf(fp, " guest sys ");
675 ret += fprintf(fp, " guest us ");
682 ret += fprintf(fp, "%cDelta", *sep);
684 ret += fprintf(fp, " Delta ");
686 if (show_displacement) {
688 ret += fprintf(fp, "%cDisplacement", *sep);
690 ret += fprintf(fp, " Displ");
694 list_for_each_entry(se, &hist_entry__sort_list, list) {
698 fprintf(fp, "%c%s", *sep, se->se_header);
701 width = strlen(se->se_header);
703 if (symbol_conf.col_width_list_str) {
705 *se->se_width = atoi(col_width);
706 col_width = strchr(col_width, ',');
711 width = *se->se_width = max(*se->se_width, width);
713 fprintf(fp, " %*s", width, se->se_header);
720 fprintf(fp, "# ........");
721 if (symbol_conf.show_nr_samples)
722 fprintf(fp, " ..........");
724 fprintf(fp, " ..........");
725 if (show_displacement)
726 fprintf(fp, " .....");
728 list_for_each_entry(se, &hist_entry__sort_list, list) {
736 width = *se->se_width;
738 width = strlen(se->se_header);
739 for (i = 0; i < width; i++)
743 fprintf(fp, "\n#\n");
746 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
747 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
749 if (show_displacement) {
751 displacement = ((long)h->pair->position -
757 ret += hist_entry__fprintf(h, pair, show_displacement,
758 displacement, fp, self->stats.total);
760 if (symbol_conf.use_callchain)
761 ret += hist_entry__fprintf_callchain(h, fp, self->stats.total);
763 if (h->ms.map == NULL && verbose > 1) {
764 __map_groups__fprintf_maps(&h->thread->mg,
765 MAP__FUNCTION, verbose, fp);
766 fprintf(fp, "%.10s end\n", graph_dotted_line);
770 free(rem_sq_bracket);