6 struct callchain_param callchain_param = {
7 .mode = CHAIN_GRAPH_REL,
12 * histogram, sorted on item, collects counts
15 struct hist_entry *__perf_session__add_hist_entry(struct rb_root *hists,
16 struct addr_location *al,
17 struct symbol *sym_parent,
20 struct rb_node **p = &hists->rb_node;
21 struct rb_node *parent = NULL;
22 struct hist_entry *he;
23 struct hist_entry entry = {
38 he = rb_entry(parent, struct hist_entry, rb_node);
40 cmp = hist_entry__cmp(&entry, he);
53 he = malloc(sizeof(*he));
57 rb_link_node(&he->rb_node, parent, p);
58 rb_insert_color(&he->rb_node, hists);
64 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
66 struct sort_entry *se;
69 list_for_each_entry(se, &hist_entry__sort_list, list) {
70 cmp = se->cmp(left, right);
79 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
81 struct sort_entry *se;
84 list_for_each_entry(se, &hist_entry__sort_list, list) {
85 int64_t (*f)(struct hist_entry *, struct hist_entry *);
87 f = se->collapse ?: se->cmp;
97 void hist_entry__free(struct hist_entry *he)
103 * collapse the histogram
106 static void collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
108 struct rb_node **p = &root->rb_node;
109 struct rb_node *parent = NULL;
110 struct hist_entry *iter;
115 iter = rb_entry(parent, struct hist_entry, rb_node);
117 cmp = hist_entry__collapse(iter, he);
120 iter->count += he->count;
121 hist_entry__free(he);
131 rb_link_node(&he->rb_node, parent, p);
132 rb_insert_color(&he->rb_node, root);
135 void perf_session__collapse_resort(struct rb_root *hists)
138 struct rb_node *next;
139 struct hist_entry *n;
141 if (!sort__need_collapse)
145 next = rb_first(hists);
148 n = rb_entry(next, struct hist_entry, rb_node);
149 next = rb_next(&n->rb_node);
151 rb_erase(&n->rb_node, hists);
152 collapse__insert_entry(&tmp, n);
159 * reverse the map, sort on count.
162 static void perf_session__insert_output_hist_entry(struct rb_root *root,
163 struct hist_entry *he,
164 u64 min_callchain_hits)
166 struct rb_node **p = &root->rb_node;
167 struct rb_node *parent = NULL;
168 struct hist_entry *iter;
170 if (symbol_conf.use_callchain)
171 callchain_param.sort(&he->sorted_chain, &he->callchain,
172 min_callchain_hits, &callchain_param);
176 iter = rb_entry(parent, struct hist_entry, rb_node);
178 if (he->count > iter->count)
184 rb_link_node(&he->rb_node, parent, p);
185 rb_insert_color(&he->rb_node, root);
188 void perf_session__output_resort(struct rb_root *hists, u64 total_samples)
191 struct rb_node *next;
192 struct hist_entry *n;
193 u64 min_callchain_hits;
196 total_samples * (callchain_param.min_percent / 100);
199 next = rb_first(hists);
202 n = rb_entry(next, struct hist_entry, rb_node);
203 next = rb_next(&n->rb_node);
205 rb_erase(&n->rb_node, hists);
206 perf_session__insert_output_hist_entry(&tmp, n,
213 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
216 int ret = fprintf(fp, " ");
218 for (i = 0; i < left_margin; i++)
219 ret += fprintf(fp, " ");
224 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
228 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
230 for (i = 0; i < depth; i++)
231 if (depth_mask & (1 << i))
232 ret += fprintf(fp, "| ");
234 ret += fprintf(fp, " ");
236 ret += fprintf(fp, "\n");
241 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
242 int depth, int depth_mask, int count,
243 u64 total_samples, int hits,
249 ret += callchain__fprintf_left_margin(fp, left_margin);
250 for (i = 0; i < depth; i++) {
251 if (depth_mask & (1 << i))
252 ret += fprintf(fp, "|");
254 ret += fprintf(fp, " ");
255 if (!count && i == depth - 1) {
258 percent = hits * 100.0 / total_samples;
259 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
261 ret += fprintf(fp, "%s", " ");
264 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
266 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
271 static struct symbol *rem_sq_bracket;
272 static struct callchain_list rem_hits;
274 static void init_rem_hits(void)
276 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
277 if (!rem_sq_bracket) {
278 fprintf(stderr, "Not enough memory to display remaining hits\n");
282 strcpy(rem_sq_bracket->name, "[...]");
283 rem_hits.ms.sym = rem_sq_bracket;
286 static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
287 u64 total_samples, int depth,
288 int depth_mask, int left_margin)
290 struct rb_node *node, *next;
291 struct callchain_node *child;
292 struct callchain_list *chain;
293 int new_depth_mask = depth_mask;
299 if (callchain_param.mode == CHAIN_GRAPH_REL)
300 new_total = self->children_hit;
302 new_total = total_samples;
304 remaining = new_total;
306 node = rb_first(&self->rb_root);
310 child = rb_entry(node, struct callchain_node, rb_node);
311 cumul = cumul_hits(child);
315 * The depth mask manages the output of pipes that show
316 * the depth. We don't want to keep the pipes of the current
317 * level for the last child of this depth.
318 * Except if we have remaining filtered hits. They will
319 * supersede the last child
321 next = rb_next(node);
322 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
323 new_depth_mask &= ~(1 << (depth - 1));
326 * But we keep the older depth mask for the line separator
327 * to keep the level link until we reach the last child
329 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
332 list_for_each_entry(chain, &child->val, list) {
333 ret += ipchain__fprintf_graph(fp, chain, depth,
339 ret += __callchain__fprintf_graph(fp, child, new_total,
341 new_depth_mask | (1 << depth),
346 if (callchain_param.mode == CHAIN_GRAPH_REL &&
347 remaining && remaining != new_total) {
352 new_depth_mask &= ~(1 << (depth - 1));
354 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
355 new_depth_mask, 0, new_total,
356 remaining, left_margin);
362 static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
363 u64 total_samples, int left_margin)
365 struct callchain_list *chain;
366 bool printed = false;
370 list_for_each_entry(chain, &self->val, list) {
371 if (!i++ && sort__first_dimension == SORT_SYM)
375 ret += callchain__fprintf_left_margin(fp, left_margin);
376 ret += fprintf(fp, "|\n");
377 ret += callchain__fprintf_left_margin(fp, left_margin);
378 ret += fprintf(fp, "---");
383 ret += callchain__fprintf_left_margin(fp, left_margin);
386 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
388 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
391 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
396 static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
399 struct callchain_list *chain;
405 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
408 list_for_each_entry(chain, &self->val, list) {
409 if (chain->ip >= PERF_CONTEXT_MAX)
412 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
414 ret += fprintf(fp, " %p\n",
415 (void *)(long)chain->ip);
421 static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
422 u64 total_samples, int left_margin)
424 struct rb_node *rb_node;
425 struct callchain_node *chain;
428 rb_node = rb_first(&self->sorted_chain);
432 chain = rb_entry(rb_node, struct callchain_node, rb_node);
433 percent = chain->hit * 100.0 / total_samples;
434 switch (callchain_param.mode) {
436 ret += percent_color_fprintf(fp, " %6.2f%%\n",
438 ret += callchain__fprintf_flat(fp, chain, total_samples);
440 case CHAIN_GRAPH_ABS: /* Falldown */
441 case CHAIN_GRAPH_REL:
442 ret += callchain__fprintf_graph(fp, chain, total_samples,
448 ret += fprintf(fp, "\n");
449 rb_node = rb_next(rb_node);
455 size_t hist_entry__fprintf(struct hist_entry *self,
456 struct perf_session *pair_session,
457 bool show_displacement,
458 long displacement, FILE *fp,
461 struct sort_entry *se;
463 const char *sep = symbol_conf.field_sep;
466 if (symbol_conf.exclude_other && !self->parent)
470 count = self->pair ? self->pair->count : 0;
471 total = pair_session->events_stats.total;
474 total = session_total;
478 ret = percent_color_fprintf(fp, sep ? "%.2f" : " %6.2f%%",
479 (count * 100.0) / total);
481 ret = fprintf(fp, sep ? "%lld" : "%12lld ", count);
483 if (symbol_conf.show_nr_samples) {
485 ret += fprintf(fp, "%c%lld", *sep, count);
487 ret += fprintf(fp, "%11lld", count);
492 double old_percent = 0, new_percent = 0, diff;
495 old_percent = (count * 100.0) / total;
496 if (session_total > 0)
497 new_percent = (self->count * 100.0) / session_total;
499 diff = new_percent - old_percent;
501 if (fabs(diff) >= 0.01)
502 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
504 snprintf(bf, sizeof(bf), " ");
507 ret += fprintf(fp, "%c%s", *sep, bf);
509 ret += fprintf(fp, "%11.11s", bf);
511 if (show_displacement) {
513 snprintf(bf, sizeof(bf), "%+4ld", displacement);
515 snprintf(bf, sizeof(bf), " ");
518 ret += fprintf(fp, "%c%s", *sep, bf);
520 ret += fprintf(fp, "%6.6s", bf);
524 list_for_each_entry(se, &hist_entry__sort_list, list) {
528 ret += fprintf(fp, "%s", sep ?: " ");
529 ret += se->print(fp, self, se->width ? *se->width : 0);
532 return ret + fprintf(fp, "\n");
535 static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
540 if (sort__first_dimension == SORT_COMM) {
541 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
543 left_margin = se->width ? *se->width : 0;
544 left_margin -= thread__comm_len(self->thread);
547 return hist_entry_callchain__fprintf(fp, self, session_total,
551 size_t perf_session__fprintf_hists(struct rb_root *hists,
552 struct perf_session *pair,
553 bool show_displacement, FILE *fp,
556 struct sort_entry *se;
559 unsigned long position = 1;
560 long displacement = 0;
562 const char *sep = symbol_conf.field_sep;
563 char *col_width = symbol_conf.col_width_list_str;
567 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
569 if (symbol_conf.show_nr_samples) {
571 fprintf(fp, "%cSamples", *sep);
573 fputs(" Samples ", fp);
578 ret += fprintf(fp, "%cDelta", *sep);
580 ret += fprintf(fp, " Delta ");
582 if (show_displacement) {
584 ret += fprintf(fp, "%cDisplacement", *sep);
586 ret += fprintf(fp, " Displ");
590 list_for_each_entry(se, &hist_entry__sort_list, list) {
594 fprintf(fp, "%c%s", *sep, se->header);
597 width = strlen(se->header);
599 if (symbol_conf.col_width_list_str) {
601 *se->width = atoi(col_width);
602 col_width = strchr(col_width, ',');
607 width = *se->width = max(*se->width, width);
609 fprintf(fp, " %*s", width, se->header);
616 fprintf(fp, "# ........");
617 if (symbol_conf.show_nr_samples)
618 fprintf(fp, " ..........");
620 fprintf(fp, " ..........");
621 if (show_displacement)
622 fprintf(fp, " .....");
624 list_for_each_entry(se, &hist_entry__sort_list, list) {
634 width = strlen(se->header);
635 for (i = 0; i < width; i++)
639 fprintf(fp, "\n#\n");
642 for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
643 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
645 if (show_displacement) {
647 displacement = ((long)h->pair->position -
653 ret += hist_entry__fprintf(h, pair, show_displacement,
654 displacement, fp, session_total);
656 if (symbol_conf.use_callchain)
657 ret += hist_entry__fprintf_callchain(h, fp, session_total);
659 if (h->ms.map == NULL && verbose > 1) {
660 __map_groups__fprintf_maps(&h->thread->mg,
661 MAP__FUNCTION, verbose, fp);
662 fprintf(fp, "%.10s end\n", graph_dotted_line);
666 free(rem_sq_bracket);