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 static int __hpp__fmt(struct perf_hpp
*hpp
, struct hist_entry
*he
,
19 hpp_field_fn get_field
, const char *fmt
, int len
,
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
, len
, percent
);
37 ret
= hpp__call_print_fn(hpp
, print_fn
, fmt
, len
, 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
, len
,
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 int hpp__fmt(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
108 struct hist_entry
*he
, hpp_field_fn get_field
,
109 const char *fmtstr
, hpp_snprint_fn print_fn
, bool fmt_percent
)
111 int len
= fmt
->user_len
?: fmt
->len
;
113 if (symbol_conf
.field_sep
) {
114 return __hpp__fmt(hpp
, he
, get_field
, fmtstr
, 1,
115 print_fn
, fmt_percent
);
119 len
-= 2; /* 2 for a space and a % sign */
123 return __hpp__fmt(hpp
, he
, get_field
, fmtstr
, len
, print_fn
, fmt_percent
);
126 int hpp__fmt_acc(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
127 struct hist_entry
*he
, hpp_field_fn get_field
,
128 const char *fmtstr
, hpp_snprint_fn print_fn
, bool fmt_percent
)
130 if (!symbol_conf
.cumulate_callchain
) {
131 int len
= fmt
->user_len
?: fmt
->len
;
132 return snprintf(hpp
->buf
, hpp
->size
, " %*s", len
- 1, "N/A");
135 return hpp__fmt(fmt
, hpp
, he
, get_field
, fmtstr
, print_fn
, fmt_percent
);
138 static int field_cmp(u64 field_a
, u64 field_b
)
140 if (field_a
> field_b
)
142 if (field_a
< field_b
)
147 static int __hpp__sort(struct hist_entry
*a
, struct hist_entry
*b
,
148 hpp_field_fn get_field
)
152 struct perf_evsel
*evsel
;
153 struct hist_entry
*pair
;
154 u64
*fields_a
, *fields_b
;
156 ret
= field_cmp(get_field(a
), get_field(b
));
157 if (ret
|| !symbol_conf
.event_group
)
160 evsel
= hists_to_evsel(a
->hists
);
161 if (!perf_evsel__is_group_event(evsel
))
164 nr_members
= evsel
->nr_members
;
165 fields_a
= calloc(nr_members
, sizeof(*fields_a
));
166 fields_b
= calloc(nr_members
, sizeof(*fields_b
));
168 if (!fields_a
|| !fields_b
)
171 list_for_each_entry(pair
, &a
->pairs
.head
, pairs
.node
) {
172 evsel
= hists_to_evsel(pair
->hists
);
173 fields_a
[perf_evsel__group_idx(evsel
)] = get_field(pair
);
176 list_for_each_entry(pair
, &b
->pairs
.head
, pairs
.node
) {
177 evsel
= hists_to_evsel(pair
->hists
);
178 fields_b
[perf_evsel__group_idx(evsel
)] = get_field(pair
);
181 for (i
= 1; i
< nr_members
; i
++) {
182 ret
= field_cmp(fields_a
[i
], fields_b
[i
]);
194 static int __hpp__sort_acc(struct hist_entry
*a
, struct hist_entry
*b
,
195 hpp_field_fn get_field
)
199 if (symbol_conf
.cumulate_callchain
) {
201 * Put caller above callee when they have equal period.
203 ret
= field_cmp(get_field(a
), get_field(b
));
207 ret
= b
->callchain
->max_depth
- a
->callchain
->max_depth
;
212 static int hpp__width_fn(struct perf_hpp_fmt
*fmt
,
213 struct perf_hpp
*hpp __maybe_unused
,
214 struct perf_evsel
*evsel
)
216 int len
= fmt
->user_len
?: fmt
->len
;
218 if (symbol_conf
.event_group
)
219 len
= max(len
, evsel
->nr_members
* fmt
->len
);
221 if (len
< (int)strlen(fmt
->name
))
222 len
= strlen(fmt
->name
);
227 static int hpp__header_fn(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
228 struct perf_evsel
*evsel
)
230 int len
= hpp__width_fn(fmt
, hpp
, evsel
);
231 return scnprintf(hpp
->buf
, hpp
->size
, "%*s", len
, fmt
->name
);
234 static int hpp_color_scnprintf(struct perf_hpp
*hpp
, const char *fmt
, ...)
237 ssize_t ssize
= hpp
->size
;
242 len
= va_arg(args
, int);
243 percent
= va_arg(args
, double);
244 ret
= percent_color_len_snprintf(hpp
->buf
, hpp
->size
, fmt
, len
, percent
);
247 return (ret
>= ssize
) ? (ssize
- 1) : ret
;
250 static int hpp_entry_scnprintf(struct perf_hpp
*hpp
, const char *fmt
, ...)
253 ssize_t ssize
= hpp
->size
;
257 ret
= vsnprintf(hpp
->buf
, hpp
->size
, fmt
, args
);
260 return (ret
>= ssize
) ? (ssize
- 1) : ret
;
263 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
264 static u64 he_get_##_field(struct hist_entry *he) \
266 return he->stat._field; \
269 static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
270 struct perf_hpp *hpp, struct hist_entry *he) \
272 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
273 hpp_color_scnprintf, true); \
276 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
277 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
278 struct perf_hpp *hpp, struct hist_entry *he) \
280 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
281 hpp_entry_scnprintf, true); \
284 #define __HPP_SORT_FN(_type, _field) \
285 static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
287 return __hpp__sort(a, b, he_get_##_field); \
290 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
291 static u64 he_get_acc_##_field(struct hist_entry *he) \
293 return he->stat_acc->_field; \
296 static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
297 struct perf_hpp *hpp, struct hist_entry *he) \
299 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
300 hpp_color_scnprintf, true); \
303 #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
304 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
305 struct perf_hpp *hpp, struct hist_entry *he) \
307 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
308 hpp_entry_scnprintf, true); \
311 #define __HPP_SORT_ACC_FN(_type, _field) \
312 static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
314 return __hpp__sort_acc(a, b, he_get_acc_##_field); \
317 #define __HPP_ENTRY_RAW_FN(_type, _field) \
318 static u64 he_get_raw_##_field(struct hist_entry *he) \
320 return he->stat._field; \
323 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
324 struct perf_hpp *hpp, struct hist_entry *he) \
326 return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \
327 hpp_entry_scnprintf, false); \
330 #define __HPP_SORT_RAW_FN(_type, _field) \
331 static int64_t hpp__sort_##_type(struct hist_entry *a, struct hist_entry *b) \
333 return __hpp__sort(a, b, he_get_raw_##_field); \
337 #define HPP_PERCENT_FNS(_type, _field) \
338 __HPP_COLOR_PERCENT_FN(_type, _field) \
339 __HPP_ENTRY_PERCENT_FN(_type, _field) \
340 __HPP_SORT_FN(_type, _field)
342 #define HPP_PERCENT_ACC_FNS(_type, _field) \
343 __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
344 __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
345 __HPP_SORT_ACC_FN(_type, _field)
347 #define HPP_RAW_FNS(_type, _field) \
348 __HPP_ENTRY_RAW_FN(_type, _field) \
349 __HPP_SORT_RAW_FN(_type, _field)
351 HPP_PERCENT_FNS(overhead
, period
)
352 HPP_PERCENT_FNS(overhead_sys
, period_sys
)
353 HPP_PERCENT_FNS(overhead_us
, period_us
)
354 HPP_PERCENT_FNS(overhead_guest_sys
, period_guest_sys
)
355 HPP_PERCENT_FNS(overhead_guest_us
, period_guest_us
)
356 HPP_PERCENT_ACC_FNS(overhead_acc
, period
)
358 HPP_RAW_FNS(samples
, nr_events
)
359 HPP_RAW_FNS(period
, period
)
361 static int64_t hpp__nop_cmp(struct hist_entry
*a __maybe_unused
,
362 struct hist_entry
*b __maybe_unused
)
367 #define HPP__COLOR_PRINT_FNS(_name, _fn) \
370 .header = hpp__header_fn, \
371 .width = hpp__width_fn, \
372 .color = hpp__color_ ## _fn, \
373 .entry = hpp__entry_ ## _fn, \
374 .cmp = hpp__nop_cmp, \
375 .collapse = hpp__nop_cmp, \
376 .sort = hpp__sort_ ## _fn, \
379 #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn) \
382 .header = hpp__header_fn, \
383 .width = hpp__width_fn, \
384 .color = hpp__color_ ## _fn, \
385 .entry = hpp__entry_ ## _fn, \
386 .cmp = hpp__nop_cmp, \
387 .collapse = hpp__nop_cmp, \
388 .sort = hpp__sort_ ## _fn, \
391 #define HPP__PRINT_FNS(_name, _fn) \
394 .header = hpp__header_fn, \
395 .width = hpp__width_fn, \
396 .entry = hpp__entry_ ## _fn, \
397 .cmp = hpp__nop_cmp, \
398 .collapse = hpp__nop_cmp, \
399 .sort = hpp__sort_ ## _fn, \
402 struct perf_hpp_fmt perf_hpp__format
[] = {
403 HPP__COLOR_PRINT_FNS("Overhead", overhead
),
404 HPP__COLOR_PRINT_FNS("sys", overhead_sys
),
405 HPP__COLOR_PRINT_FNS("usr", overhead_us
),
406 HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys
),
407 HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us
),
408 HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc
),
409 HPP__PRINT_FNS("Samples", samples
),
410 HPP__PRINT_FNS("Period", period
)
413 LIST_HEAD(perf_hpp__list
);
414 LIST_HEAD(perf_hpp__sort_list
);
417 #undef HPP__COLOR_PRINT_FNS
418 #undef HPP__COLOR_ACC_PRINT_FNS
419 #undef HPP__PRINT_FNS
421 #undef HPP_PERCENT_FNS
422 #undef HPP_PERCENT_ACC_FNS
425 #undef __HPP_HEADER_FN
426 #undef __HPP_WIDTH_FN
427 #undef __HPP_COLOR_PERCENT_FN
428 #undef __HPP_ENTRY_PERCENT_FN
429 #undef __HPP_COLOR_ACC_PERCENT_FN
430 #undef __HPP_ENTRY_ACC_PERCENT_FN
431 #undef __HPP_ENTRY_RAW_FN
433 #undef __HPP_SORT_ACC_FN
434 #undef __HPP_SORT_RAW_FN
437 void perf_hpp__init(void)
439 struct list_head
*list
;
442 for (i
= 0; i
< PERF_HPP__MAX_INDEX
; i
++) {
443 struct perf_hpp_fmt
*fmt
= &perf_hpp__format
[i
];
445 INIT_LIST_HEAD(&fmt
->list
);
447 /* sort_list may be linked by setup_sorting() */
448 if (fmt
->sort_list
.next
== NULL
)
449 INIT_LIST_HEAD(&fmt
->sort_list
);
453 * If user specified field order, no need to setup default fields.
455 if (is_strict_order(field_order
))
458 if (symbol_conf
.cumulate_callchain
) {
459 perf_hpp__column_enable(PERF_HPP__OVERHEAD_ACC
);
460 perf_hpp__format
[PERF_HPP__OVERHEAD
].name
= "Self";
463 perf_hpp__column_enable(PERF_HPP__OVERHEAD
);
465 if (symbol_conf
.show_cpu_utilization
) {
466 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS
);
467 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US
);
470 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS
);
471 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US
);
475 if (symbol_conf
.show_nr_samples
)
476 perf_hpp__column_enable(PERF_HPP__SAMPLES
);
478 if (symbol_conf
.show_total_period
)
479 perf_hpp__column_enable(PERF_HPP__PERIOD
);
481 /* prepend overhead field for backward compatiblity. */
482 list
= &perf_hpp__format
[PERF_HPP__OVERHEAD
].sort_list
;
483 if (list_empty(list
))
484 list_add(list
, &perf_hpp__sort_list
);
486 if (symbol_conf
.cumulate_callchain
) {
487 list
= &perf_hpp__format
[PERF_HPP__OVERHEAD_ACC
].sort_list
;
488 if (list_empty(list
))
489 list_add(list
, &perf_hpp__sort_list
);
493 void perf_hpp__column_register(struct perf_hpp_fmt
*format
)
495 list_add_tail(&format
->list
, &perf_hpp__list
);
498 void perf_hpp__column_unregister(struct perf_hpp_fmt
*format
)
500 list_del(&format
->list
);
503 void perf_hpp__register_sort_field(struct perf_hpp_fmt
*format
)
505 list_add_tail(&format
->sort_list
, &perf_hpp__sort_list
);
508 void perf_hpp__column_enable(unsigned col
)
510 BUG_ON(col
>= PERF_HPP__MAX_INDEX
);
511 perf_hpp__column_register(&perf_hpp__format
[col
]);
514 void perf_hpp__column_disable(unsigned col
)
516 BUG_ON(col
>= PERF_HPP__MAX_INDEX
);
517 perf_hpp__column_unregister(&perf_hpp__format
[col
]);
520 void perf_hpp__cancel_cumulate(void)
522 if (is_strict_order(field_order
))
525 perf_hpp__column_disable(PERF_HPP__OVERHEAD_ACC
);
526 perf_hpp__format
[PERF_HPP__OVERHEAD
].name
= "Overhead";
529 void perf_hpp__setup_output_field(void)
531 struct perf_hpp_fmt
*fmt
;
533 /* append sort keys to output field */
534 perf_hpp__for_each_sort_list(fmt
) {
535 if (!list_empty(&fmt
->list
))
539 * sort entry fields are dynamically created,
540 * so they can share a same sort key even though
543 if (perf_hpp__is_sort_entry(fmt
)) {
544 struct perf_hpp_fmt
*pos
;
546 perf_hpp__for_each_format(pos
) {
547 if (perf_hpp__same_sort_entry(pos
, fmt
))
552 perf_hpp__column_register(fmt
);
558 void perf_hpp__append_sort_keys(void)
560 struct perf_hpp_fmt
*fmt
;
562 /* append output fields to sort keys */
563 perf_hpp__for_each_format(fmt
) {
564 if (!list_empty(&fmt
->sort_list
))
568 * sort entry fields are dynamically created,
569 * so they can share a same sort key even though
572 if (perf_hpp__is_sort_entry(fmt
)) {
573 struct perf_hpp_fmt
*pos
;
575 perf_hpp__for_each_sort_list(pos
) {
576 if (perf_hpp__same_sort_entry(pos
, fmt
))
581 perf_hpp__register_sort_field(fmt
);
587 void perf_hpp__reset_output_field(void)
589 struct perf_hpp_fmt
*fmt
, *tmp
;
591 /* reset output fields */
592 perf_hpp__for_each_format_safe(fmt
, tmp
) {
593 list_del_init(&fmt
->list
);
594 list_del_init(&fmt
->sort_list
);
597 /* reset sort keys */
598 perf_hpp__for_each_sort_list_safe(fmt
, tmp
) {
599 list_del_init(&fmt
->list
);
600 list_del_init(&fmt
->sort_list
);
605 * See hists__fprintf to match the column widths
607 unsigned int hists__sort_list_width(struct hists
*hists
)
609 struct perf_hpp_fmt
*fmt
;
612 struct perf_hpp dummy_hpp
;
614 perf_hpp__for_each_format(fmt
) {
615 if (perf_hpp__should_skip(fmt
))
623 ret
+= fmt
->width(fmt
, &dummy_hpp
, hists_to_evsel(hists
));
626 if (verbose
&& sort__has_sym
) /* Addr + origin */
627 ret
+= 3 + BITS_PER_LONG
/ 4;
632 void perf_hpp__reset_width(struct perf_hpp_fmt
*fmt
, struct hists
*hists
)
636 if (perf_hpp__is_sort_entry(fmt
))
637 return perf_hpp__reset_sort_width(fmt
, hists
);
639 for (idx
= 0; idx
< PERF_HPP__MAX_INDEX
; idx
++) {
640 if (fmt
== &perf_hpp__format
[idx
])
644 if (idx
== PERF_HPP__MAX_INDEX
)
648 case PERF_HPP__OVERHEAD
:
649 case PERF_HPP__OVERHEAD_SYS
:
650 case PERF_HPP__OVERHEAD_US
:
651 case PERF_HPP__OVERHEAD_ACC
:
655 case PERF_HPP__OVERHEAD_GUEST_SYS
:
656 case PERF_HPP__OVERHEAD_GUEST_US
:
660 case PERF_HPP__SAMPLES
:
661 case PERF_HPP__PERIOD
:
670 void perf_hpp__set_user_width(const char *width_list_str
)
672 struct perf_hpp_fmt
*fmt
;
673 const char *ptr
= width_list_str
;
675 perf_hpp__for_each_format(fmt
) {
678 int len
= strtol(ptr
, &p
, 10);