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 #define hpp__call_print_fn(hpp, fn, fmt, ...) \
13 int __ret = fn(hpp, fmt, ##__VA_ARGS__); \
14 advance_hpp(hpp, __ret); \
18 int __hpp__fmt(struct perf_hpp
*hpp
, struct hist_entry
*he
,
19 hpp_field_fn get_field
, const char *fmt
,
20 hpp_snprint_fn print_fn
, bool fmt_percent
)
23 struct hists
*hists
= he
->hists
;
24 struct perf_evsel
*evsel
= hists_to_evsel(hists
);
26 size_t size
= hpp
->size
;
30 u64 total
= hists__total_period(hists
);
33 percent
= 100.0 * get_field(he
) / total
;
35 ret
= hpp__call_print_fn(hpp
, print_fn
, fmt
, percent
);
37 ret
= hpp__call_print_fn(hpp
, print_fn
, fmt
, get_field(he
));
39 if (perf_evsel__is_group_event(evsel
)) {
40 int prev_idx
, idx_delta
;
41 struct hist_entry
*pair
;
42 int nr_members
= evsel
->nr_members
;
44 prev_idx
= perf_evsel__group_idx(evsel
);
46 list_for_each_entry(pair
, &he
->pairs
.head
, pairs
.node
) {
47 u64 period
= get_field(pair
);
48 u64 total
= hists__total_period(pair
->hists
);
53 evsel
= hists_to_evsel(pair
->hists
);
54 idx_delta
= perf_evsel__group_idx(evsel
) - prev_idx
- 1;
58 * zero-fill group members in the middle which
62 ret
+= hpp__call_print_fn(hpp
, print_fn
,
65 ret
+= hpp__call_print_fn(hpp
, print_fn
,
71 ret
+= hpp__call_print_fn(hpp
, print_fn
, fmt
,
72 100.0 * period
/ total
);
74 ret
+= hpp__call_print_fn(hpp
, print_fn
, fmt
,
78 prev_idx
= perf_evsel__group_idx(evsel
);
81 idx_delta
= nr_members
- prev_idx
- 1;
85 * zero-fill group members at last which have no sample
88 ret
+= hpp__call_print_fn(hpp
, print_fn
,
91 ret
+= hpp__call_print_fn(hpp
, print_fn
,
98 * Restore original buf and size as it's where caller expects
99 * the result will be saved.
107 static int field_cmp(u64 field_a
, u64 field_b
)
109 if (field_a
> field_b
)
111 if (field_a
< field_b
)
116 static int __hpp__sort(struct hist_entry
*a
, struct hist_entry
*b
,
117 hpp_field_fn get_field
)
121 struct perf_evsel
*evsel
;
122 struct hist_entry
*pair
;
123 u64
*fields_a
, *fields_b
;
125 ret
= field_cmp(get_field(a
), get_field(b
));
126 if (ret
|| !symbol_conf
.event_group
)
129 evsel
= hists_to_evsel(a
->hists
);
130 if (!perf_evsel__is_group_event(evsel
))
133 nr_members
= evsel
->nr_members
;
134 fields_a
= calloc(sizeof(*fields_a
), nr_members
);
135 fields_b
= calloc(sizeof(*fields_b
), nr_members
);
137 if (!fields_a
|| !fields_b
)
140 list_for_each_entry(pair
, &a
->pairs
.head
, pairs
.node
) {
141 evsel
= hists_to_evsel(pair
->hists
);
142 fields_a
[perf_evsel__group_idx(evsel
)] = get_field(pair
);
145 list_for_each_entry(pair
, &b
->pairs
.head
, pairs
.node
) {
146 evsel
= hists_to_evsel(pair
->hists
);
147 fields_b
[perf_evsel__group_idx(evsel
)] = get_field(pair
);
150 for (i
= 1; i
< nr_members
; i
++) {
151 ret
= field_cmp(fields_a
[i
], fields_b
[i
]);
163 #define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
164 static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
165 struct perf_hpp *hpp, \
166 struct perf_evsel *evsel) \
168 int len = _min_width; \
170 if (symbol_conf.event_group) \
171 len = max(len, evsel->nr_members * _unit_width); \
173 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
176 #define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
177 static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
178 struct perf_hpp *hpp __maybe_unused, \
179 struct perf_evsel *evsel) \
181 int len = _min_width; \
183 if (symbol_conf.event_group) \
184 len = max(len, evsel->nr_members * _unit_width); \
189 static int hpp_color_scnprintf(struct perf_hpp
*hpp
, const char *fmt
, ...)
192 ssize_t ssize
= hpp
->size
;
197 percent
= va_arg(args
, double);
198 ret
= value_color_snprintf(hpp
->buf
, hpp
->size
, fmt
, percent
);
201 return (ret
>= ssize
) ? (ssize
- 1) : ret
;
204 static int hpp_entry_scnprintf(struct perf_hpp
*hpp
, const char *fmt
, ...)
207 ssize_t ssize
= hpp
->size
;
211 ret
= vsnprintf(hpp
->buf
, hpp
->size
, fmt
, args
);
214 return (ret
>= ssize
) ? (ssize
- 1) : ret
;
217 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
218 static u64 he_get_##_field(struct hist_entry *he) \
220 return he->stat._field; \
223 static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
224 struct perf_hpp *hpp, struct hist_entry *he) \
226 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
227 hpp_color_scnprintf, true); \
230 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
231 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
232 struct perf_hpp *hpp, struct hist_entry *he) \
234 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
235 return __hpp__fmt(hpp, he, he_get_##_field, fmt, \
236 hpp_entry_scnprintf, true); \
239 #define __HPP_SORT_FN(_type, _field) \
240 static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
242 return __hpp__sort(a, b, he_get_##_field); \
245 #define __HPP_ENTRY_RAW_FN(_type, _field) \
246 static u64 he_get_raw_##_field(struct hist_entry *he) \
248 return he->stat._field; \
251 static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
252 struct perf_hpp *hpp, struct hist_entry *he) \
254 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
255 return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, \
256 hpp_entry_scnprintf, false); \
259 #define __HPP_SORT_RAW_FN(_type, _field) \
260 static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
262 return __hpp__sort(a, b, he_get_raw_##_field); \
266 #define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
267 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
268 __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
269 __HPP_COLOR_PERCENT_FN(_type, _field) \
270 __HPP_ENTRY_PERCENT_FN(_type, _field) \
271 __HPP_SORT_FN(_type, _field)
273 #define HPP_RAW_FNS(_type, _str, _field, _min_width, _unit_width) \
274 __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
275 __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
276 __HPP_ENTRY_RAW_FN(_type, _field) \
277 __HPP_SORT_RAW_FN(_type, _field)
280 HPP_PERCENT_FNS(overhead
, "Overhead", period
, 8, 8)
281 HPP_PERCENT_FNS(overhead_sys
, "sys", period_sys
, 8, 8)
282 HPP_PERCENT_FNS(overhead_us
, "usr", period_us
, 8, 8)
283 HPP_PERCENT_FNS(overhead_guest_sys
, "guest sys", period_guest_sys
, 9, 8)
284 HPP_PERCENT_FNS(overhead_guest_us
, "guest usr", period_guest_us
, 9, 8)
286 HPP_RAW_FNS(samples
, "Samples", nr_events
, 12, 12)
287 HPP_RAW_FNS(period
, "Period", period
, 12, 12)
289 static int64_t hpp__nop_cmp(struct hist_entry
*a __maybe_unused
,
290 struct hist_entry
*b __maybe_unused
)
295 #define HPP__COLOR_PRINT_FNS(_name) \
297 .header = hpp__header_ ## _name, \
298 .width = hpp__width_ ## _name, \
299 .color = hpp__color_ ## _name, \
300 .entry = hpp__entry_ ## _name, \
301 .cmp = hpp__nop_cmp, \
302 .collapse = hpp__nop_cmp, \
303 .sort = hpp__sort_ ## _name, \
306 #define HPP__PRINT_FNS(_name) \
308 .header = hpp__header_ ## _name, \
309 .width = hpp__width_ ## _name, \
310 .entry = hpp__entry_ ## _name, \
311 .cmp = hpp__nop_cmp, \
312 .collapse = hpp__nop_cmp, \
313 .sort = hpp__sort_ ## _name, \
316 struct perf_hpp_fmt perf_hpp__format
[] = {
317 HPP__COLOR_PRINT_FNS(overhead
),
318 HPP__COLOR_PRINT_FNS(overhead_sys
),
319 HPP__COLOR_PRINT_FNS(overhead_us
),
320 HPP__COLOR_PRINT_FNS(overhead_guest_sys
),
321 HPP__COLOR_PRINT_FNS(overhead_guest_us
),
322 HPP__PRINT_FNS(samples
),
323 HPP__PRINT_FNS(period
)
326 LIST_HEAD(perf_hpp__list
);
327 LIST_HEAD(perf_hpp__sort_list
);
330 #undef HPP__COLOR_PRINT_FNS
331 #undef HPP__PRINT_FNS
333 #undef HPP_PERCENT_FNS
336 #undef __HPP_HEADER_FN
337 #undef __HPP_WIDTH_FN
338 #undef __HPP_COLOR_PERCENT_FN
339 #undef __HPP_ENTRY_PERCENT_FN
340 #undef __HPP_ENTRY_RAW_FN
343 void perf_hpp__init(void)
345 struct list_head
*list
;
348 for (i
= 0; i
< PERF_HPP__MAX_INDEX
; i
++) {
349 struct perf_hpp_fmt
*fmt
= &perf_hpp__format
[i
];
351 INIT_LIST_HEAD(&fmt
->list
);
353 /* sort_list may be linked by setup_sorting() */
354 if (fmt
->sort_list
.next
== NULL
)
355 INIT_LIST_HEAD(&fmt
->sort_list
);
359 * If user specified field order, no need to setup default fields.
364 perf_hpp__column_enable(PERF_HPP__OVERHEAD
);
366 if (symbol_conf
.show_cpu_utilization
) {
367 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS
);
368 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US
);
371 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS
);
372 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US
);
376 if (symbol_conf
.show_nr_samples
)
377 perf_hpp__column_enable(PERF_HPP__SAMPLES
);
379 if (symbol_conf
.show_total_period
)
380 perf_hpp__column_enable(PERF_HPP__PERIOD
);
382 /* prepend overhead field for backward compatiblity. */
383 list
= &perf_hpp__format
[PERF_HPP__OVERHEAD
].sort_list
;
384 if (list_empty(list
))
385 list_add(list
, &perf_hpp__sort_list
);
388 void perf_hpp__column_register(struct perf_hpp_fmt
*format
)
390 list_add_tail(&format
->list
, &perf_hpp__list
);
393 void perf_hpp__register_sort_field(struct perf_hpp_fmt
*format
)
395 list_add_tail(&format
->sort_list
, &perf_hpp__sort_list
);
398 void perf_hpp__column_enable(unsigned col
)
400 BUG_ON(col
>= PERF_HPP__MAX_INDEX
);
401 perf_hpp__column_register(&perf_hpp__format
[col
]);
404 void perf_hpp__setup_output_field(void)
406 struct perf_hpp_fmt
*fmt
;
408 /* append sort keys to output field */
409 perf_hpp__for_each_sort_list(fmt
) {
410 if (!list_empty(&fmt
->list
))
414 * sort entry fields are dynamically created,
415 * so they can share a same sort key even though
418 if (perf_hpp__is_sort_entry(fmt
)) {
419 struct perf_hpp_fmt
*pos
;
421 perf_hpp__for_each_format(pos
) {
422 if (perf_hpp__same_sort_entry(pos
, fmt
))
427 perf_hpp__column_register(fmt
);
433 void perf_hpp__append_sort_keys(void)
435 struct perf_hpp_fmt
*fmt
;
437 /* append output fields to sort keys */
438 perf_hpp__for_each_format(fmt
) {
439 if (!list_empty(&fmt
->sort_list
))
443 * sort entry fields are dynamically created,
444 * so they can share a same sort key even though
447 if (perf_hpp__is_sort_entry(fmt
)) {
448 struct perf_hpp_fmt
*pos
;
450 perf_hpp__for_each_sort_list(pos
) {
451 if (perf_hpp__same_sort_entry(pos
, fmt
))
456 perf_hpp__register_sort_field(fmt
);
462 void perf_hpp__reset_output_field(void)
464 struct perf_hpp_fmt
*fmt
, *tmp
;
466 /* reset output fields */
467 perf_hpp__for_each_format_safe(fmt
, tmp
) {
468 list_del_init(&fmt
->list
);
469 list_del_init(&fmt
->sort_list
);
472 /* reset sort keys */
473 perf_hpp__for_each_sort_list_safe(fmt
, tmp
) {
474 list_del_init(&fmt
->list
);
475 list_del_init(&fmt
->sort_list
);
480 * See hists__fprintf to match the column widths
482 unsigned int hists__sort_list_width(struct hists
*hists
)
484 struct perf_hpp_fmt
*fmt
;
487 struct perf_hpp dummy_hpp
;
489 perf_hpp__for_each_format(fmt
) {
490 if (perf_hpp__should_skip(fmt
))
498 ret
+= fmt
->width(fmt
, &dummy_hpp
, hists_to_evsel(hists
));
501 if (verbose
&& sort__has_sym
) /* Addr + origin */
502 ret
+= 3 + BITS_PER_LONG
/ 4;