2 #include <linux/compiler.h>
4 #include "../util/hist.h"
5 #include "../util/util.h"
6 #include "../util/sort.h"
7 #include "../util/evsel.h"
9 /* hist period print (hpp) functions */
11 typedef int (*hpp_snprint_fn
)(char *buf
, size_t size
, const char *fmt
, ...);
13 static int __hpp__fmt(struct perf_hpp
*hpp
, struct hist_entry
*he
,
14 u64 (*get_field
)(struct hist_entry
*),
15 const char *fmt
, hpp_snprint_fn print_fn
,
19 struct hists
*hists
= he
->hists
;
20 struct perf_evsel
*evsel
= hists_to_evsel(hists
);
25 if (hists
->stats
.total_period
)
26 percent
= 100.0 * get_field(he
) /
27 hists
->stats
.total_period
;
29 ret
= print_fn(hpp
->buf
, hpp
->size
, fmt
, percent
);
31 ret
= print_fn(hpp
->buf
, hpp
->size
, fmt
, get_field(he
));
33 if (perf_evsel__is_group_event(evsel
)) {
34 int prev_idx
, idx_delta
;
35 struct hist_entry
*pair
;
36 int nr_members
= evsel
->nr_members
;
38 prev_idx
= perf_evsel__group_idx(evsel
);
40 list_for_each_entry(pair
, &he
->pairs
.head
, pairs
.node
) {
41 u64 period
= get_field(pair
);
42 u64 total
= pair
->hists
->stats
.total_period
;
47 evsel
= hists_to_evsel(pair
->hists
);
48 idx_delta
= perf_evsel__group_idx(evsel
) - prev_idx
- 1;
52 * zero-fill group members in the middle which
55 ret
+= print_fn(hpp
->buf
+ ret
, hpp
->size
- ret
,
60 ret
+= print_fn(hpp
->buf
+ ret
, hpp
->size
- ret
,
61 fmt
, 100.0 * period
/ total
);
63 ret
+= print_fn(hpp
->buf
+ ret
, hpp
->size
- ret
,
66 prev_idx
= perf_evsel__group_idx(evsel
);
69 idx_delta
= nr_members
- prev_idx
- 1;
73 * zero-fill group members at last which have no sample
75 ret
+= print_fn(hpp
->buf
+ ret
, hpp
->size
- ret
,
82 #define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
83 static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
84 struct perf_hpp *hpp) \
86 int len = _min_width; \
88 if (symbol_conf.event_group) { \
89 struct perf_evsel *evsel = hpp->ptr; \
91 len = max(len, evsel->nr_members * _unit_width); \
93 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
96 #define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
97 static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
98 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_fmt *fmt __maybe_unused, \
117 struct perf_hpp *hpp, struct hist_entry *he) \
119 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
120 (hpp_snprint_fn)percent_color_snprintf, true); \
123 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
124 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
125 struct perf_hpp *hpp, struct hist_entry *he) \
127 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
128 return __hpp__fmt(hpp, he, he_get_##_field, fmt, \
132 #define __HPP_ENTRY_RAW_FN(_type, _field) \
133 static u64 he_get_raw_##_field(struct hist_entry *he) \
135 return he->stat._field; \
138 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
139 struct perf_hpp *hpp, struct hist_entry *he) \
141 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
142 return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, scnprintf, false); \
145 #define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
146 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
147 __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
148 __HPP_COLOR_PERCENT_FN(_type, _field) \
149 __HPP_ENTRY_PERCENT_FN(_type, _field)
151 #define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width) \
152 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
153 __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
154 __HPP_ENTRY_RAW_FN(_type, _field)
157 HPP_PERCENT_FNS(overhead
, "Overhead", period
, 8, 8)
158 HPP_PERCENT_FNS(overhead_sys
, "sys", period_sys
, 8, 8)
159 HPP_PERCENT_FNS(overhead_us
, "usr", period_us
, 8, 8)
160 HPP_PERCENT_FNS(overhead_guest_sys
, "guest sys", period_guest_sys
, 9, 8)
161 HPP_PERCENT_FNS(overhead_guest_us
, "guest usr", period_guest_us
, 9, 8)
163 HPP_RAW_FNS(samples
, "Samples", nr_events
, 12, 12)
164 HPP_RAW_FNS(period
, "Period", period
, 12, 12)
167 static int hpp__header_baseline(struct perf_hpp_fmt
*fmt __maybe_unused
,
168 struct perf_hpp
*hpp
)
170 return scnprintf(hpp
->buf
, hpp
->size
, "Baseline");
173 static int hpp__width_baseline(struct perf_hpp_fmt
*fmt __maybe_unused
,
174 struct perf_hpp
*hpp __maybe_unused
)
179 static double baseline_percent(struct hist_entry
*he
)
181 struct hist_entry
*pair
= hist_entry__next_pair(he
);
182 struct hists
*pair_hists
= pair
? pair
->hists
: NULL
;
183 double percent
= 0.0;
186 u64 total_period
= pair_hists
->stats
.total_period
;
187 u64 base_period
= pair
->stat
.period
;
189 percent
= 100.0 * base_period
/ total_period
;
195 static int hpp__color_baseline(struct perf_hpp_fmt
*fmt __maybe_unused
,
196 struct perf_hpp
*hpp
, struct hist_entry
*he
)
198 double percent
= baseline_percent(he
);
200 if (hist_entry__has_pairs(he
) || symbol_conf
.field_sep
)
201 return percent_color_snprintf(hpp
->buf
, hpp
->size
, " %6.2f%%", percent
);
203 return scnprintf(hpp
->buf
, hpp
->size
, " ");
206 static int hpp__entry_baseline(struct perf_hpp_fmt
*_fmt __maybe_unused
,
207 struct perf_hpp
*hpp
, struct hist_entry
*he
)
209 double percent
= baseline_percent(he
);
210 const char *fmt
= symbol_conf
.field_sep
? "%.2f" : " %6.2f%%";
212 if (hist_entry__has_pairs(he
) || symbol_conf
.field_sep
)
213 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, percent
);
215 return scnprintf(hpp
->buf
, hpp
->size
, " ");
218 static int hpp__header_period_baseline(struct perf_hpp_fmt
*_fmt __maybe_unused
,
219 struct perf_hpp
*hpp
)
221 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%12s";
223 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Period Base");
226 static int hpp__width_period_baseline(struct perf_hpp_fmt
*fmt __maybe_unused
,
227 struct perf_hpp
*hpp __maybe_unused
)
232 static int hpp__entry_period_baseline(struct perf_hpp_fmt
*_fmt __maybe_unused
,
233 struct perf_hpp
*hpp
, struct hist_entry
*he
)
235 struct hist_entry
*pair
= hist_entry__next_pair(he
);
236 u64 period
= pair
? pair
->stat
.period
: 0;
237 const char *fmt
= symbol_conf
.field_sep
? "%" PRIu64
: "%12" PRIu64
;
239 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, period
);
242 static int hpp__header_delta(struct perf_hpp_fmt
*_fmt __maybe_unused
,
243 struct perf_hpp
*hpp
)
245 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%7s";
247 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Delta");
250 static int hpp__width_delta(struct perf_hpp_fmt
*fmt __maybe_unused
,
251 struct perf_hpp
*hpp __maybe_unused
)
256 static int hpp__entry_delta(struct perf_hpp_fmt
*_fmt __maybe_unused
,
257 struct perf_hpp
*hpp
, struct hist_entry
*he
)
259 struct hist_entry
*pair
= hist_entry__next_pair(he
);
260 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%7.7s";
265 if (he
->diff
.computed
)
266 diff
= he
->diff
.period_ratio_delta
;
268 diff
= perf_diff__compute_delta(he
, pair
);
270 diff
= perf_diff__period_percent(he
, he
->stat
.period
);
272 if (fabs(diff
) >= 0.01)
273 scnprintf(buf
, sizeof(buf
), "%+4.2F%%", diff
);
275 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, buf
);
278 static int hpp__header_ratio(struct perf_hpp_fmt
*_fmt __maybe_unused
,
279 struct perf_hpp
*hpp
)
281 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%14s";
283 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Ratio");
286 static int hpp__width_ratio(struct perf_hpp_fmt
*fmt __maybe_unused
,
287 struct perf_hpp
*hpp __maybe_unused
)
292 static int hpp__entry_ratio(struct perf_hpp_fmt
*_fmt __maybe_unused
,
293 struct perf_hpp
*hpp
, struct hist_entry
*he
)
295 struct hist_entry
*pair
= hist_entry__next_pair(he
);
296 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%14s";
301 if (he
->diff
.computed
)
302 ratio
= he
->diff
.period_ratio
;
304 ratio
= perf_diff__compute_ratio(he
, pair
);
308 scnprintf(buf
, sizeof(buf
), "%+14.6F", ratio
);
310 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, buf
);
313 static int hpp__header_wdiff(struct perf_hpp_fmt
*_fmt __maybe_unused
,
314 struct perf_hpp
*hpp
)
316 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%14s";
318 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Weighted diff");
321 static int hpp__width_wdiff(struct perf_hpp_fmt
*fmt __maybe_unused
,
322 struct perf_hpp
*hpp __maybe_unused
)
327 static int hpp__entry_wdiff(struct perf_hpp_fmt
*_fmt __maybe_unused
,
328 struct perf_hpp
*hpp
, struct hist_entry
*he
)
330 struct hist_entry
*pair
= hist_entry__next_pair(he
);
331 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%14s";
336 if (he
->diff
.computed
)
337 wdiff
= he
->diff
.wdiff
;
339 wdiff
= perf_diff__compute_wdiff(he
, pair
);
343 scnprintf(buf
, sizeof(buf
), "%14ld", wdiff
);
345 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, buf
);
348 static int hpp__header_formula(struct perf_hpp_fmt
*_fmt __maybe_unused
,
349 struct perf_hpp
*hpp
)
351 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%70s";
353 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, "Formula");
356 static int hpp__width_formula(struct perf_hpp_fmt
*fmt __maybe_unused
,
357 struct perf_hpp
*hpp __maybe_unused
)
362 static int hpp__entry_formula(struct perf_hpp_fmt
*_fmt __maybe_unused
,
363 struct perf_hpp
*hpp
, struct hist_entry
*he
)
365 struct hist_entry
*pair
= hist_entry__next_pair(he
);
366 const char *fmt
= symbol_conf
.field_sep
? "%s" : "%-70s";
370 perf_diff__formula(he
, pair
, buf
, sizeof(buf
));
372 return scnprintf(hpp
->buf
, hpp
->size
, fmt
, buf
);
375 #define HPP__COLOR_PRINT_FNS(_name) \
377 .header = hpp__header_ ## _name, \
378 .width = hpp__width_ ## _name, \
379 .color = hpp__color_ ## _name, \
380 .entry = hpp__entry_ ## _name \
383 #define HPP__PRINT_FNS(_name) \
385 .header = hpp__header_ ## _name, \
386 .width = hpp__width_ ## _name, \
387 .entry = hpp__entry_ ## _name \
390 struct perf_hpp_fmt perf_hpp__format
[] = {
391 HPP__COLOR_PRINT_FNS(baseline
),
392 HPP__COLOR_PRINT_FNS(overhead
),
393 HPP__COLOR_PRINT_FNS(overhead_sys
),
394 HPP__COLOR_PRINT_FNS(overhead_us
),
395 HPP__COLOR_PRINT_FNS(overhead_guest_sys
),
396 HPP__COLOR_PRINT_FNS(overhead_guest_us
),
397 HPP__PRINT_FNS(samples
),
398 HPP__PRINT_FNS(period
),
399 HPP__PRINT_FNS(period_baseline
),
400 HPP__PRINT_FNS(delta
),
401 HPP__PRINT_FNS(ratio
),
402 HPP__PRINT_FNS(wdiff
),
403 HPP__PRINT_FNS(formula
)
406 LIST_HEAD(perf_hpp__list
);
409 #undef HPP__COLOR_PRINT_FNS
410 #undef HPP__PRINT_FNS
412 #undef HPP_PERCENT_FNS
415 #undef __HPP_HEADER_FN
416 #undef __HPP_WIDTH_FN
417 #undef __HPP_COLOR_PERCENT_FN
418 #undef __HPP_ENTRY_PERCENT_FN
419 #undef __HPP_ENTRY_RAW_FN
422 void perf_hpp__init(void)
424 if (symbol_conf
.show_cpu_utilization
) {
425 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS
);
426 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US
);
429 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS
);
430 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US
);
434 if (symbol_conf
.show_nr_samples
)
435 perf_hpp__column_enable(PERF_HPP__SAMPLES
);
437 if (symbol_conf
.show_total_period
)
438 perf_hpp__column_enable(PERF_HPP__PERIOD
);
441 void perf_hpp__column_register(struct perf_hpp_fmt
*format
)
443 list_add_tail(&format
->list
, &perf_hpp__list
);
446 void perf_hpp__column_enable(unsigned col
)
448 BUG_ON(col
>= PERF_HPP__MAX_INDEX
);
449 perf_hpp__column_register(&perf_hpp__format
[col
]);
452 static inline void advance_hpp(struct perf_hpp
*hpp
, int inc
)
458 int hist_entry__period_snprintf(struct perf_hpp
*hpp
, struct hist_entry
*he
,
461 const char *sep
= symbol_conf
.field_sep
;
462 struct perf_hpp_fmt
*fmt
;
463 char *start
= hpp
->buf
;
467 if (symbol_conf
.exclude_other
&& !he
->parent
)
470 perf_hpp__for_each_format(fmt
) {
472 * If there's no field_sep, we still need
473 * to display initial ' '.
475 if (!sep
|| !first
) {
476 ret
= scnprintf(hpp
->buf
, hpp
->size
, "%s", sep
?: " ");
477 advance_hpp(hpp
, ret
);
481 if (color
&& fmt
->color
)
482 ret
= fmt
->color(fmt
, hpp
, he
);
484 ret
= fmt
->entry(fmt
, hpp
, he
);
486 advance_hpp(hpp
, ret
);
489 return hpp
->buf
- start
;
492 int hist_entry__sort_snprintf(struct hist_entry
*he
, char *s
, size_t size
,
495 const char *sep
= symbol_conf
.field_sep
;
496 struct sort_entry
*se
;
499 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
503 ret
+= scnprintf(s
+ ret
, size
- ret
, "%s", sep
?: " ");
504 ret
+= se
->se_snprintf(he
, s
+ ret
, size
- ret
,
505 hists__col_len(hists
, se
->se_width_idx
));
512 * See hists__fprintf to match the column widths
514 unsigned int hists__sort_list_width(struct hists
*hists
)
516 struct perf_hpp_fmt
*fmt
;
517 struct sort_entry
*se
;
519 struct perf_hpp dummy_hpp
= {
520 .ptr
= hists_to_evsel(hists
),
523 perf_hpp__for_each_format(fmt
) {
527 ret
+= fmt
->width(fmt
, &dummy_hpp
);
530 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
532 ret
+= 2 + hists__col_len(hists
, se
->se_width_idx
);
534 if (verbose
) /* Addr + origin */
535 ret
+= 3 + BITS_PER_LONG
/ 4;