3 #include "../util/hist.h"
4 #include "../util/util.h"
5 #include "../util/sort.h"
6 #include "../util/evsel.h"
8 /* hist period print (hpp) functions */
10 typedef int (*hpp_snprint_fn)(char *buf, size_t size, const char *fmt, ...);
12 static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
13 u64 (*get_field)(struct hist_entry *),
14 const char *fmt, hpp_snprint_fn print_fn,
18 struct hists *hists = he->hists;
23 if (hists->stats.total_period)
24 percent = 100.0 * get_field(he) /
25 hists->stats.total_period;
27 ret = print_fn(hpp->buf, hpp->size, fmt, percent);
29 ret = print_fn(hpp->buf, hpp->size, fmt, get_field(he));
31 if (symbol_conf.event_group) {
32 int prev_idx, idx_delta;
33 struct perf_evsel *evsel = hists_to_evsel(hists);
34 struct hist_entry *pair;
35 int nr_members = evsel->nr_members;
40 prev_idx = perf_evsel__group_idx(evsel);
42 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
43 u64 period = get_field(pair);
44 u64 total = pair->hists->stats.total_period;
49 evsel = hists_to_evsel(pair->hists);
50 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
54 * zero-fill group members in the middle which
57 ret += print_fn(hpp->buf + ret, hpp->size - ret,
62 ret += print_fn(hpp->buf + ret, hpp->size - ret,
63 fmt, 100.0 * period / total);
65 ret += print_fn(hpp->buf + ret, hpp->size - ret,
68 prev_idx = perf_evsel__group_idx(evsel);
71 idx_delta = nr_members - prev_idx - 1;
75 * zero-fill group members at last which have no sample
77 ret += print_fn(hpp->buf + ret, hpp->size - ret,
84 #define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
85 static int hpp__header_##_type(struct perf_hpp *hpp) \
87 int len = _min_width; \
89 if (symbol_conf.event_group) { \
90 struct perf_evsel *evsel = hpp->ptr; \
92 len = max(len, evsel->nr_members * _unit_width); \
94 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
97 #define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
98 static int hpp__width_##_type(struct perf_hpp *hpp __maybe_unused) \
100 int len = _min_width; \
102 if (symbol_conf.event_group) { \
103 struct perf_evsel *evsel = hpp->ptr; \
105 len = max(len, evsel->nr_members * _unit_width); \
110 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
111 static u64 he_get_##_field(struct hist_entry *he) \
113 return he->stat._field; \
116 static int hpp__color_##_type(struct perf_hpp *hpp, struct hist_entry *he) \
118 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
119 (hpp_snprint_fn)percent_color_snprintf, true); \
122 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
123 static int hpp__entry_##_type(struct perf_hpp *hpp, struct hist_entry *he) \
125 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
126 return __hpp__fmt(hpp, he, he_get_##_field, fmt, \
130 #define __HPP_ENTRY_RAW_FN(_type, _field) \
131 static u64 he_get_raw_##_field(struct hist_entry *he) \
133 return he->stat._field; \
136 static int hpp__entry_##_type(struct perf_hpp *hpp, struct hist_entry *he) \
138 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
139 return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, scnprintf, false); \
142 #define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
143 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
144 __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
145 __HPP_COLOR_PERCENT_FN(_type, _field) \
146 __HPP_ENTRY_PERCENT_FN(_type, _field)
148 #define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width) \
149 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
150 __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
151 __HPP_ENTRY_RAW_FN(_type, _field)
154 HPP_PERCENT_FNS(overhead, "Overhead", period, 8, 8)
155 HPP_PERCENT_FNS(overhead_sys, "sys", period_sys, 8, 8)
156 HPP_PERCENT_FNS(overhead_us, "usr", period_us, 8, 8)
157 HPP_PERCENT_FNS(overhead_guest_sys, "guest sys", period_guest_sys, 9, 8)
158 HPP_PERCENT_FNS(overhead_guest_us, "guest usr", period_guest_us, 9, 8)
160 HPP_RAW_FNS(samples, "Samples", nr_events, 12, 12)
161 HPP_RAW_FNS(period, "Period", period, 12, 12)
164 static int hpp__header_baseline(struct perf_hpp *hpp)
166 return scnprintf(hpp->buf, hpp->size, "Baseline");
169 static int hpp__width_baseline(struct perf_hpp *hpp __maybe_unused)
174 static double baseline_percent(struct hist_entry *he)
176 struct hist_entry *pair = hist_entry__next_pair(he);
177 struct hists *pair_hists = pair ? pair->hists : NULL;
178 double percent = 0.0;
181 u64 total_period = pair_hists->stats.total_period;
182 u64 base_period = pair->stat.period;
184 percent = 100.0 * base_period / total_period;
190 static int hpp__color_baseline(struct perf_hpp *hpp, struct hist_entry *he)
192 double percent = baseline_percent(he);
194 if (hist_entry__has_pairs(he) || symbol_conf.field_sep)
195 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
197 return scnprintf(hpp->buf, hpp->size, " ");
200 static int hpp__entry_baseline(struct perf_hpp *hpp, struct hist_entry *he)
202 double percent = baseline_percent(he);
203 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
205 if (hist_entry__has_pairs(he) || symbol_conf.field_sep)
206 return scnprintf(hpp->buf, hpp->size, fmt, percent);
208 return scnprintf(hpp->buf, hpp->size, " ");
211 static int hpp__header_period_baseline(struct perf_hpp *hpp)
213 const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
215 return scnprintf(hpp->buf, hpp->size, fmt, "Period Base");
218 static int hpp__width_period_baseline(struct perf_hpp *hpp __maybe_unused)
223 static int hpp__entry_period_baseline(struct perf_hpp *hpp, struct hist_entry *he)
225 struct hist_entry *pair = hist_entry__next_pair(he);
226 u64 period = pair ? pair->stat.period : 0;
227 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
229 return scnprintf(hpp->buf, hpp->size, fmt, period);
232 static int hpp__header_delta(struct perf_hpp *hpp)
234 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
236 return scnprintf(hpp->buf, hpp->size, fmt, "Delta");
239 static int hpp__width_delta(struct perf_hpp *hpp __maybe_unused)
244 static int hpp__entry_delta(struct perf_hpp *hpp, struct hist_entry *he)
246 struct hist_entry *pair = hist_entry__next_pair(he);
247 const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
252 if (he->diff.computed)
253 diff = he->diff.period_ratio_delta;
255 diff = perf_diff__compute_delta(he, pair);
257 diff = perf_diff__period_percent(he, he->stat.period);
259 if (fabs(diff) >= 0.01)
260 scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
262 return scnprintf(hpp->buf, hpp->size, fmt, buf);
265 static int hpp__header_ratio(struct perf_hpp *hpp)
267 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
269 return scnprintf(hpp->buf, hpp->size, fmt, "Ratio");
272 static int hpp__width_ratio(struct perf_hpp *hpp __maybe_unused)
277 static int hpp__entry_ratio(struct perf_hpp *hpp, struct hist_entry *he)
279 struct hist_entry *pair = hist_entry__next_pair(he);
280 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
285 if (he->diff.computed)
286 ratio = he->diff.period_ratio;
288 ratio = perf_diff__compute_ratio(he, pair);
292 scnprintf(buf, sizeof(buf), "%+14.6F", ratio);
294 return scnprintf(hpp->buf, hpp->size, fmt, buf);
297 static int hpp__header_wdiff(struct perf_hpp *hpp)
299 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
301 return scnprintf(hpp->buf, hpp->size, fmt, "Weighted diff");
304 static int hpp__width_wdiff(struct perf_hpp *hpp __maybe_unused)
309 static int hpp__entry_wdiff(struct perf_hpp *hpp, struct hist_entry *he)
311 struct hist_entry *pair = hist_entry__next_pair(he);
312 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
317 if (he->diff.computed)
318 wdiff = he->diff.wdiff;
320 wdiff = perf_diff__compute_wdiff(he, pair);
324 scnprintf(buf, sizeof(buf), "%14ld", wdiff);
326 return scnprintf(hpp->buf, hpp->size, fmt, buf);
329 static int hpp__header_formula(struct perf_hpp *hpp)
331 const char *fmt = symbol_conf.field_sep ? "%s" : "%70s";
333 return scnprintf(hpp->buf, hpp->size, fmt, "Formula");
336 static int hpp__width_formula(struct perf_hpp *hpp __maybe_unused)
341 static int hpp__entry_formula(struct perf_hpp *hpp, struct hist_entry *he)
343 struct hist_entry *pair = hist_entry__next_pair(he);
344 const char *fmt = symbol_conf.field_sep ? "%s" : "%-70s";
348 perf_diff__formula(he, pair, buf, sizeof(buf));
350 return scnprintf(hpp->buf, hpp->size, fmt, buf);
353 #define HPP__COLOR_PRINT_FNS(_name) \
355 .header = hpp__header_ ## _name, \
356 .width = hpp__width_ ## _name, \
357 .color = hpp__color_ ## _name, \
358 .entry = hpp__entry_ ## _name \
361 #define HPP__PRINT_FNS(_name) \
363 .header = hpp__header_ ## _name, \
364 .width = hpp__width_ ## _name, \
365 .entry = hpp__entry_ ## _name \
368 struct perf_hpp_fmt perf_hpp__format[] = {
369 HPP__COLOR_PRINT_FNS(baseline),
370 HPP__COLOR_PRINT_FNS(overhead),
371 HPP__COLOR_PRINT_FNS(overhead_sys),
372 HPP__COLOR_PRINT_FNS(overhead_us),
373 HPP__COLOR_PRINT_FNS(overhead_guest_sys),
374 HPP__COLOR_PRINT_FNS(overhead_guest_us),
375 HPP__PRINT_FNS(samples),
376 HPP__PRINT_FNS(period),
377 HPP__PRINT_FNS(period_baseline),
378 HPP__PRINT_FNS(delta),
379 HPP__PRINT_FNS(ratio),
380 HPP__PRINT_FNS(wdiff),
381 HPP__PRINT_FNS(formula)
384 LIST_HEAD(perf_hpp__list);
387 #undef HPP__COLOR_PRINT_FNS
388 #undef HPP__PRINT_FNS
390 #undef HPP_PERCENT_FNS
393 #undef __HPP_HEADER_FN
394 #undef __HPP_WIDTH_FN
395 #undef __HPP_COLOR_PERCENT_FN
396 #undef __HPP_ENTRY_PERCENT_FN
397 #undef __HPP_ENTRY_RAW_FN
400 void perf_hpp__init(void)
402 if (symbol_conf.show_cpu_utilization) {
403 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
404 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
407 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
408 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
412 if (symbol_conf.show_nr_samples)
413 perf_hpp__column_enable(PERF_HPP__SAMPLES);
415 if (symbol_conf.show_total_period)
416 perf_hpp__column_enable(PERF_HPP__PERIOD);
419 void perf_hpp__column_register(struct perf_hpp_fmt *format)
421 list_add_tail(&format->list, &perf_hpp__list);
424 void perf_hpp__column_enable(unsigned col)
426 BUG_ON(col >= PERF_HPP__MAX_INDEX);
427 perf_hpp__column_register(&perf_hpp__format[col]);
430 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
436 int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
439 const char *sep = symbol_conf.field_sep;
440 struct perf_hpp_fmt *fmt;
441 char *start = hpp->buf;
445 if (symbol_conf.exclude_other && !he->parent)
448 perf_hpp__for_each_format(fmt) {
450 * If there's no field_sep, we still need
451 * to display initial ' '.
453 if (!sep || !first) {
454 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
455 advance_hpp(hpp, ret);
459 if (color && fmt->color)
460 ret = fmt->color(hpp, he);
462 ret = fmt->entry(hpp, he);
464 advance_hpp(hpp, ret);
467 return hpp->buf - start;
470 int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
473 const char *sep = symbol_conf.field_sep;
474 struct sort_entry *se;
477 list_for_each_entry(se, &hist_entry__sort_list, list) {
481 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
482 ret += se->se_snprintf(he, s + ret, size - ret,
483 hists__col_len(hists, se->se_width_idx));
490 * See hists__fprintf to match the column widths
492 unsigned int hists__sort_list_width(struct hists *hists)
494 struct perf_hpp_fmt *fmt;
495 struct sort_entry *se;
497 struct perf_hpp dummy_hpp = {
498 .ptr = hists_to_evsel(hists),
501 perf_hpp__for_each_format(fmt) {
505 ret += fmt->width(&dummy_hpp);
508 list_for_each_entry(se, &hist_entry__sort_list, list)
510 ret += 2 + hists__col_len(hists, se->se_width_idx);
512 if (verbose) /* Addr + origin */
513 ret += 3 + BITS_PER_LONG / 4;