3 #include "../util/hist.h"
4 #include "../util/util.h"
5 #include "../util/sort.h"
8 /* hist period print (hpp) functions */
9 static int hpp__header_overhead(struct perf_hpp *hpp)
11 return scnprintf(hpp->buf, hpp->size, "Overhead");
14 static int hpp__width_overhead(struct perf_hpp *hpp __maybe_unused)
19 static int hpp__color_overhead(struct perf_hpp *hpp, struct hist_entry *he)
21 struct hists *hists = he->hists;
22 double percent = 100.0 * he->stat.period / hists->stats.total_period;
24 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
27 static int hpp__entry_overhead(struct perf_hpp *hpp, struct hist_entry *he)
29 struct hists *hists = he->hists;
30 double percent = 100.0 * he->stat.period / hists->stats.total_period;
31 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
33 return scnprintf(hpp->buf, hpp->size, fmt, percent);
36 static int hpp__header_overhead_sys(struct perf_hpp *hpp)
38 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
40 return scnprintf(hpp->buf, hpp->size, fmt, "sys");
43 static int hpp__width_overhead_sys(struct perf_hpp *hpp __maybe_unused)
48 static int hpp__color_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
50 struct hists *hists = he->hists;
51 double percent = 100.0 * he->stat.period_sys / hists->stats.total_period;
53 return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
56 static int hpp__entry_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
58 struct hists *hists = he->hists;
59 double percent = 100.0 * he->stat.period_sys / hists->stats.total_period;
60 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
62 return scnprintf(hpp->buf, hpp->size, fmt, percent);
65 static int hpp__header_overhead_us(struct perf_hpp *hpp)
67 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
69 return scnprintf(hpp->buf, hpp->size, fmt, "user");
72 static int hpp__width_overhead_us(struct perf_hpp *hpp __maybe_unused)
77 static int hpp__color_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
79 struct hists *hists = he->hists;
80 double percent = 100.0 * he->stat.period_us / hists->stats.total_period;
82 return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
85 static int hpp__entry_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
87 struct hists *hists = he->hists;
88 double percent = 100.0 * he->stat.period_us / hists->stats.total_period;
89 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
91 return scnprintf(hpp->buf, hpp->size, fmt, percent);
94 static int hpp__header_overhead_guest_sys(struct perf_hpp *hpp)
96 return scnprintf(hpp->buf, hpp->size, "guest sys");
99 static int hpp__width_overhead_guest_sys(struct perf_hpp *hpp __maybe_unused)
104 static int hpp__color_overhead_guest_sys(struct perf_hpp *hpp,
105 struct hist_entry *he)
107 struct hists *hists = he->hists;
108 double percent = 100.0 * he->stat.period_guest_sys / hists->stats.total_period;
110 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
113 static int hpp__entry_overhead_guest_sys(struct perf_hpp *hpp,
114 struct hist_entry *he)
116 struct hists *hists = he->hists;
117 double percent = 100.0 * he->stat.period_guest_sys / hists->stats.total_period;
118 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
120 return scnprintf(hpp->buf, hpp->size, fmt, percent);
123 static int hpp__header_overhead_guest_us(struct perf_hpp *hpp)
125 return scnprintf(hpp->buf, hpp->size, "guest usr");
128 static int hpp__width_overhead_guest_us(struct perf_hpp *hpp __maybe_unused)
133 static int hpp__color_overhead_guest_us(struct perf_hpp *hpp,
134 struct hist_entry *he)
136 struct hists *hists = he->hists;
137 double percent = 100.0 * he->stat.period_guest_us / hists->stats.total_period;
139 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
142 static int hpp__entry_overhead_guest_us(struct perf_hpp *hpp,
143 struct hist_entry *he)
145 struct hists *hists = he->hists;
146 double percent = 100.0 * he->stat.period_guest_us / hists->stats.total_period;
147 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
149 return scnprintf(hpp->buf, hpp->size, fmt, percent);
152 static int hpp__header_baseline(struct perf_hpp *hpp)
154 return scnprintf(hpp->buf, hpp->size, "Baseline");
157 static int hpp__width_baseline(struct perf_hpp *hpp __maybe_unused)
162 static double baseline_percent(struct hist_entry *he)
164 struct hist_entry *pair = hist_entry__next_pair(he);
165 struct hists *pair_hists = pair ? pair->hists : NULL;
166 double percent = 0.0;
169 u64 total_period = pair_hists->stats.total_period;
170 u64 base_period = pair->stat.period;
172 percent = 100.0 * base_period / total_period;
178 static int hpp__color_baseline(struct perf_hpp *hpp, struct hist_entry *he)
180 double percent = baseline_percent(he);
182 if (hist_entry__has_pairs(he))
183 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
185 return scnprintf(hpp->buf, hpp->size, " ");
188 static int hpp__entry_baseline(struct perf_hpp *hpp, struct hist_entry *he)
190 double percent = baseline_percent(he);
191 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
193 if (hist_entry__has_pairs(he) || symbol_conf.field_sep)
194 return scnprintf(hpp->buf, hpp->size, fmt, percent);
196 return scnprintf(hpp->buf, hpp->size, " ");
199 static int hpp__header_samples(struct perf_hpp *hpp)
201 const char *fmt = symbol_conf.field_sep ? "%s" : "%11s";
203 return scnprintf(hpp->buf, hpp->size, fmt, "Samples");
206 static int hpp__width_samples(struct perf_hpp *hpp __maybe_unused)
211 static int hpp__entry_samples(struct perf_hpp *hpp, struct hist_entry *he)
213 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%11" PRIu64;
215 return scnprintf(hpp->buf, hpp->size, fmt, he->stat.nr_events);
218 static int hpp__header_period(struct perf_hpp *hpp)
220 const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
222 return scnprintf(hpp->buf, hpp->size, fmt, "Period");
225 static int hpp__width_period(struct perf_hpp *hpp __maybe_unused)
230 static int hpp__entry_period(struct perf_hpp *hpp, struct hist_entry *he)
232 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
234 return scnprintf(hpp->buf, hpp->size, fmt, he->stat.period);
237 static int hpp__header_period_baseline(struct perf_hpp *hpp)
239 const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
241 return scnprintf(hpp->buf, hpp->size, fmt, "Period Base");
244 static int hpp__width_period_baseline(struct perf_hpp *hpp __maybe_unused)
249 static int hpp__entry_period_baseline(struct perf_hpp *hpp, struct hist_entry *he)
251 struct hist_entry *pair = hist_entry__next_pair(he);
252 u64 period = pair ? pair->stat.period : 0;
253 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
255 return scnprintf(hpp->buf, hpp->size, fmt, period);
257 static int hpp__header_delta(struct perf_hpp *hpp)
259 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
261 return scnprintf(hpp->buf, hpp->size, fmt, "Delta");
264 static int hpp__width_delta(struct perf_hpp *hpp __maybe_unused)
269 static int hpp__entry_delta(struct perf_hpp *hpp, struct hist_entry *he)
271 const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
275 if (he->diff.computed)
276 diff = he->diff.period_ratio_delta;
278 diff = perf_diff__compute_delta(he);
280 if (fabs(diff) >= 0.01)
281 scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
283 return scnprintf(hpp->buf, hpp->size, fmt, buf);
286 static int hpp__header_ratio(struct perf_hpp *hpp)
288 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
290 return scnprintf(hpp->buf, hpp->size, fmt, "Ratio");
293 static int hpp__width_ratio(struct perf_hpp *hpp __maybe_unused)
298 static int hpp__entry_ratio(struct perf_hpp *hpp, struct hist_entry *he)
300 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
304 if (he->diff.computed)
305 ratio = he->diff.period_ratio;
307 ratio = perf_diff__compute_ratio(he);
310 scnprintf(buf, sizeof(buf), "%+14.6F", ratio);
312 return scnprintf(hpp->buf, hpp->size, fmt, buf);
315 static int hpp__header_wdiff(struct perf_hpp *hpp)
317 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
319 return scnprintf(hpp->buf, hpp->size, fmt, "Weighted diff");
322 static int hpp__width_wdiff(struct perf_hpp *hpp __maybe_unused)
327 static int hpp__entry_wdiff(struct perf_hpp *hpp, struct hist_entry *he)
329 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
333 if (he->diff.computed)
334 wdiff = he->diff.wdiff;
336 wdiff = perf_diff__compute_wdiff(he);
339 scnprintf(buf, sizeof(buf), "%14ld", wdiff);
341 return scnprintf(hpp->buf, hpp->size, fmt, buf);
344 static int hpp__header_displ(struct perf_hpp *hpp)
346 return scnprintf(hpp->buf, hpp->size, "Displ.");
349 static int hpp__width_displ(struct perf_hpp *hpp __maybe_unused)
354 static int hpp__entry_displ(struct perf_hpp *hpp,
355 struct hist_entry *he)
357 struct hist_entry *pair = hist_entry__next_pair(he);
358 long displacement = pair ? pair->position - he->position : 0;
359 const char *fmt = symbol_conf.field_sep ? "%s" : "%6.6s";
363 scnprintf(buf, sizeof(buf), "%+4ld", displacement);
365 return scnprintf(hpp->buf, hpp->size, fmt, buf);
368 static int hpp__header_formula(struct perf_hpp *hpp)
370 const char *fmt = symbol_conf.field_sep ? "%s" : "%70s";
372 return scnprintf(hpp->buf, hpp->size, fmt, "Formula");
375 static int hpp__width_formula(struct perf_hpp *hpp __maybe_unused)
380 static int hpp__entry_formula(struct perf_hpp *hpp, struct hist_entry *he)
382 const char *fmt = symbol_conf.field_sep ? "%s" : "%-70s";
385 perf_diff__formula(buf, sizeof(buf), he);
386 return scnprintf(hpp->buf, hpp->size, fmt, buf);
389 #define HPP__COLOR_PRINT_FNS(_name) \
390 .header = hpp__header_ ## _name, \
391 .width = hpp__width_ ## _name, \
392 .color = hpp__color_ ## _name, \
393 .entry = hpp__entry_ ## _name
395 #define HPP__PRINT_FNS(_name) \
396 .header = hpp__header_ ## _name, \
397 .width = hpp__width_ ## _name, \
398 .entry = hpp__entry_ ## _name
400 struct perf_hpp_fmt perf_hpp__format[] = {
401 { .cond = false, HPP__COLOR_PRINT_FNS(baseline) },
402 { .cond = true, HPP__COLOR_PRINT_FNS(overhead) },
403 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_sys) },
404 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_us) },
405 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_sys) },
406 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_us) },
407 { .cond = false, HPP__PRINT_FNS(samples) },
408 { .cond = false, HPP__PRINT_FNS(period) },
409 { .cond = false, HPP__PRINT_FNS(period_baseline) },
410 { .cond = false, HPP__PRINT_FNS(delta) },
411 { .cond = false, HPP__PRINT_FNS(ratio) },
412 { .cond = false, HPP__PRINT_FNS(wdiff) },
413 { .cond = false, HPP__PRINT_FNS(displ) },
414 { .cond = false, HPP__PRINT_FNS(formula) }
417 #undef HPP__COLOR_PRINT_FNS
418 #undef HPP__PRINT_FNS
420 void perf_hpp__init(void)
422 if (symbol_conf.show_cpu_utilization) {
423 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].cond = true;
424 perf_hpp__format[PERF_HPP__OVERHEAD_US].cond = true;
427 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].cond = true;
428 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].cond = true;
432 if (symbol_conf.show_nr_samples)
433 perf_hpp__format[PERF_HPP__SAMPLES].cond = true;
435 if (symbol_conf.show_total_period)
436 perf_hpp__format[PERF_HPP__PERIOD].cond = true;
439 void perf_hpp__column_enable(unsigned col, bool enable)
441 BUG_ON(col >= PERF_HPP__MAX_INDEX);
442 perf_hpp__format[col].cond = enable;
445 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
451 int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
454 const char *sep = symbol_conf.field_sep;
455 char *start = hpp->buf;
459 if (symbol_conf.exclude_other && !he->parent)
462 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
463 if (!perf_hpp__format[i].cond)
466 if (!sep || !first) {
467 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
468 advance_hpp(hpp, ret);
472 if (color && perf_hpp__format[i].color)
473 ret = perf_hpp__format[i].color(hpp, he);
475 ret = perf_hpp__format[i].entry(hpp, he);
477 advance_hpp(hpp, ret);
480 return hpp->buf - start;
483 int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
486 const char *sep = symbol_conf.field_sep;
487 struct sort_entry *se;
490 list_for_each_entry(se, &hist_entry__sort_list, list) {
494 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
495 ret += se->se_snprintf(he, s + ret, size - ret,
496 hists__col_len(hists, se->se_width_idx));
503 * See hists__fprintf to match the column widths
505 unsigned int hists__sort_list_width(struct hists *hists)
507 struct sort_entry *se;
510 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
511 if (!perf_hpp__format[i].cond)
516 ret += perf_hpp__format[i].width(NULL);
519 list_for_each_entry(se, &hist_entry__sort_list, list)
521 ret += 2 + hists__col_len(hists, se->se_width_idx);
523 if (verbose) /* Addr + origin */
524 ret += 3 + BITS_PER_LONG / 4;