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__percent_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
;
20 if (hists
->stats
.total_period
)
21 percent
= 100.0 * get_field(he
) / hists
->stats
.total_period
;
23 ret
= print_fn(hpp
->buf
, hpp
->size
, fmt
, percent
);
27 static int __hpp__raw_fmt(struct perf_hpp
*hpp
, struct hist_entry
*he
,
28 u64 (*get_field
)(struct hist_entry
*),
29 const char *fmt
, hpp_snprint_fn print_fn
)
33 ret
= print_fn(hpp
->buf
, hpp
->size
, fmt
, get_field(he
));
38 #define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
39 static int hpp__header_##_type(struct perf_hpp *hpp) \
41 int len = _min_width; \
43 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
46 #define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
47 static int hpp__width_##_type(struct perf_hpp *hpp __maybe_unused) \
49 int len = _min_width; \
54 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
55 static u64 he_get_##_field(struct hist_entry *he) \
57 return he->stat._field; \
60 static int hpp__color_##_type(struct perf_hpp *hpp, struct hist_entry *he) \
62 return __hpp__percent_fmt(hpp, he, he_get_##_field, " %6.2f%%", \
63 (hpp_snprint_fn)percent_color_snprintf); \
66 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
67 static int hpp__entry_##_type(struct perf_hpp *hpp, struct hist_entry *he) \
69 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
70 return __hpp__percent_fmt(hpp, he, he_get_##_field, fmt, \
74 #define __HPP_ENTRY_RAW_FN(_type, _field) \
75 static u64 he_get_raw_##_field(struct hist_entry *he) \
77 return he->stat._field; \
80 static int hpp__entry_##_type(struct perf_hpp *hpp, struct hist_entry *he) \
82 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
83 return __hpp__raw_fmt(hpp, he, he_get_raw_##_field, fmt, scnprintf); \
86 #define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
87 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
88 __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
89 __HPP_COLOR_PERCENT_FN(_type, _field) \
90 __HPP_ENTRY_PERCENT_FN(_type, _field)
92 #define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width) \
93 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
94 __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
95 __HPP_ENTRY_RAW_FN(_type, _field)
98 HPP_PERCENT_FNS(overhead
, "Overhead", period
, 8, 8)
99 HPP_PERCENT_FNS(overhead_sys
, "sys", period_sys
, 8, 8)
100 HPP_PERCENT_FNS(overhead_us
, "usr", period_us
, 8, 8)
101 HPP_PERCENT_FNS(overhead_guest_sys
, "guest sys", period_guest_sys
, 9, 8)
102 HPP_PERCENT_FNS(overhead_guest_us
, "guest usr", period_guest_us
, 9, 8)
104 HPP_RAW_FNS(samples
, "Samples", nr_events
, 12, 12)
105 HPP_RAW_FNS(period
, "Period", period
, 12, 12)
108 static int hpp__header_baseline(struct perf_hpp
*hpp
)
110 return scnprintf(hpp
->buf
, hpp
->size
, "Baseline");
113 static int hpp__width_baseline(struct perf_hpp
*hpp __maybe_unused
)
118 static double baseline_percent(struct hist_entry
*he
)
120 struct hist_entry
*pair
= hist_entry__next_pair(he
);
121 struct hists
*pair_hists
= pair
? pair
->hists
: NULL
;
122 double percent
= 0.0;
125 u64 total_period
= pair_hists
->stats
.total_period
;
126 u64 base_period
= pair
->stat
.period
;
128 percent
= 100.0 * base_period
/ total_period
;
134 static int hpp__color_baseline(struct perf_hpp
*hpp
, struct hist_entry
*he
)
136 double percent
= baseline_percent(he
);
138 if (hist_entry__has_pairs(he
) || symbol_conf
.field_sep
)
139 return percent_color_snprintf(hpp
->buf
, hpp
->size
, " %6.2f%%", percent
);
141 return scnprintf(hpp
->buf
, hpp
->size
, " ");
144 static int hpp__entry_baseline(struct perf_hpp
*hpp
, struct hist_entry
*he
)
146 double percent
= baseline_percent(he
);
147 const char *fmt
= symbol_conf
.field_sep
? "%.2f" : " %6.2f%%";
149 if (hist_entry__has_pairs(he
) || symbol_conf
.field_sep
)
150 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, percent
);
152 return scnprintf(hpp
->buf
, hpp
->size
, " ");
155 static int hpp__header_period_baseline(struct perf_hpp
*hpp
)
157 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%12s";
159 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Period Base");
162 static int hpp__width_period_baseline(struct perf_hpp
*hpp __maybe_unused
)
167 static int hpp__entry_period_baseline(struct perf_hpp
*hpp
, struct hist_entry
*he
)
169 struct hist_entry
*pair
= hist_entry__next_pair(he
);
170 u64 period
= pair
? pair
->stat
.period
: 0;
171 const char *fmt
= symbol_conf
.field_sep
? "%" PRIu64
: "%12" PRIu64
;
173 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, period
);
176 static int hpp__header_delta(struct perf_hpp
*hpp
)
178 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%7s";
180 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Delta");
183 static int hpp__width_delta(struct perf_hpp
*hpp __maybe_unused
)
188 static int hpp__entry_delta(struct perf_hpp
*hpp
, struct hist_entry
*he
)
190 struct hist_entry
*pair
= hist_entry__next_pair(he
);
191 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%7.7s";
196 if (he
->diff
.computed
)
197 diff
= he
->diff
.period_ratio_delta
;
199 diff
= perf_diff__compute_delta(he
, pair
);
201 diff
= perf_diff__period_percent(he
, he
->stat
.period
);
203 if (fabs(diff
) >= 0.01)
204 scnprintf(buf
, sizeof(buf
), "%+4.2F%%", diff
);
206 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, buf
);
209 static int hpp__header_ratio(struct perf_hpp
*hpp
)
211 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%14s";
213 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Ratio");
216 static int hpp__width_ratio(struct perf_hpp
*hpp __maybe_unused
)
221 static int hpp__entry_ratio(struct perf_hpp
*hpp
, struct hist_entry
*he
)
223 struct hist_entry
*pair
= hist_entry__next_pair(he
);
224 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%14s";
229 if (he
->diff
.computed
)
230 ratio
= he
->diff
.period_ratio
;
232 ratio
= perf_diff__compute_ratio(he
, pair
);
236 scnprintf(buf
, sizeof(buf
), "%+14.6F", ratio
);
238 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, buf
);
241 static int hpp__header_wdiff(struct perf_hpp
*hpp
)
243 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%14s";
245 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Weighted diff");
248 static int hpp__width_wdiff(struct perf_hpp
*hpp __maybe_unused
)
253 static int hpp__entry_wdiff(struct perf_hpp
*hpp
, struct hist_entry
*he
)
255 struct hist_entry
*pair
= hist_entry__next_pair(he
);
256 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%14s";
261 if (he
->diff
.computed
)
262 wdiff
= he
->diff
.wdiff
;
264 wdiff
= perf_diff__compute_wdiff(he
, pair
);
268 scnprintf(buf
, sizeof(buf
), "%14ld", wdiff
);
270 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, buf
);
273 static int hpp__header_formula(struct perf_hpp
*hpp
)
275 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%70s";
277 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Formula");
280 static int hpp__width_formula(struct perf_hpp
*hpp __maybe_unused
)
285 static int hpp__entry_formula(struct perf_hpp
*hpp
, struct hist_entry
*he
)
287 struct hist_entry
*pair
= hist_entry__next_pair(he
);
288 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%-70s";
292 perf_diff__formula(he
, pair
, buf
, sizeof(buf
));
294 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, buf
);
297 #define HPP__COLOR_PRINT_FNS(_name) \
299 .header = hpp__header_ ## _name, \
300 .width = hpp__width_ ## _name, \
301 .color = hpp__color_ ## _name, \
302 .entry = hpp__entry_ ## _name \
305 #define HPP__PRINT_FNS(_name) \
307 .header = hpp__header_ ## _name, \
308 .width = hpp__width_ ## _name, \
309 .entry = hpp__entry_ ## _name \
312 struct perf_hpp_fmt perf_hpp__format
[] = {
313 HPP__COLOR_PRINT_FNS(baseline
),
314 HPP__COLOR_PRINT_FNS(overhead
),
315 HPP__COLOR_PRINT_FNS(overhead_sys
),
316 HPP__COLOR_PRINT_FNS(overhead_us
),
317 HPP__COLOR_PRINT_FNS(overhead_guest_sys
),
318 HPP__COLOR_PRINT_FNS(overhead_guest_us
),
319 HPP__PRINT_FNS(samples
),
320 HPP__PRINT_FNS(period
),
321 HPP__PRINT_FNS(period_baseline
),
322 HPP__PRINT_FNS(delta
),
323 HPP__PRINT_FNS(ratio
),
324 HPP__PRINT_FNS(wdiff
),
325 HPP__PRINT_FNS(formula
)
328 LIST_HEAD(perf_hpp__list
);
331 #undef HPP__COLOR_PRINT_FNS
332 #undef HPP__PRINT_FNS
334 #undef HPP_PERCENT_FNS
337 #undef __HPP_HEADER_FN
338 #undef __HPP_WIDTH_FN
339 #undef __HPP_COLOR_PERCENT_FN
340 #undef __HPP_ENTRY_PERCENT_FN
341 #undef __HPP_ENTRY_RAW_FN
344 void perf_hpp__init(void)
346 if (symbol_conf
.show_cpu_utilization
) {
347 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS
);
348 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US
);
351 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS
);
352 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US
);
356 if (symbol_conf
.show_nr_samples
)
357 perf_hpp__column_enable(PERF_HPP__SAMPLES
);
359 if (symbol_conf
.show_total_period
)
360 perf_hpp__column_enable(PERF_HPP__PERIOD
);
363 void perf_hpp__column_register(struct perf_hpp_fmt
*format
)
365 list_add_tail(&format
->list
, &perf_hpp__list
);
368 void perf_hpp__column_enable(unsigned col
)
370 BUG_ON(col
>= PERF_HPP__MAX_INDEX
);
371 perf_hpp__column_register(&perf_hpp__format
[col
]);
374 static inline void advance_hpp(struct perf_hpp
*hpp
, int inc
)
380 int hist_entry__period_snprintf(struct perf_hpp
*hpp
, struct hist_entry
*he
,
383 const char *sep
= symbol_conf
.field_sep
;
384 struct perf_hpp_fmt
*fmt
;
385 char *start
= hpp
->buf
;
389 if (symbol_conf
.exclude_other
&& !he
->parent
)
392 perf_hpp__for_each_format(fmt
) {
394 * If there's no field_sep, we still need
395 * to display initial ' '.
397 if (!sep
|| !first
) {
398 ret
= scnprintf(hpp
->buf
, hpp
->size
, "%s", sep
?: " ");
399 advance_hpp(hpp
, ret
);
403 if (color
&& fmt
->color
)
404 ret
= fmt
->color(hpp
, he
);
406 ret
= fmt
->entry(hpp
, he
);
408 advance_hpp(hpp
, ret
);
411 return hpp
->buf
- start
;
414 int hist_entry__sort_snprintf(struct hist_entry
*he
, char *s
, size_t size
,
417 const char *sep
= symbol_conf
.field_sep
;
418 struct sort_entry
*se
;
421 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
425 ret
+= scnprintf(s
+ ret
, size
- ret
, "%s", sep
?: " ");
426 ret
+= se
->se_snprintf(he
, s
+ ret
, size
- ret
,
427 hists__col_len(hists
, se
->se_width_idx
));
434 * See hists__fprintf to match the column widths
436 unsigned int hists__sort_list_width(struct hists
*hists
)
438 struct perf_hpp_fmt
*fmt
;
439 struct sort_entry
*se
;
442 perf_hpp__for_each_format(fmt
) {
446 ret
+= fmt
->width(NULL
);
449 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
451 ret
+= 2 + hists__col_len(hists
, se
->se_width_idx
);
453 if (verbose
) /* Addr + origin */
454 ret
+= 3 + BITS_PER_LONG
/ 4;