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 if (a
->thread
!= b
->thread
|| !symbol_conf
.use_callchain
)
210 ret
= b
->callchain
->max_depth
- a
->callchain
->max_depth
;
215 static int hpp__width_fn(struct perf_hpp_fmt
*fmt
,
216 struct perf_hpp
*hpp __maybe_unused
,
217 struct perf_evsel
*evsel
)
219 int len
= fmt
->user_len
?: fmt
->len
;
221 if (symbol_conf
.event_group
)
222 len
= max(len
, evsel
->nr_members
* fmt
->len
);
224 if (len
< (int)strlen(fmt
->name
))
225 len
= strlen(fmt
->name
);
230 static int hpp__header_fn(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
231 struct perf_evsel
*evsel
)
233 int len
= hpp__width_fn(fmt
, hpp
, evsel
);
234 return scnprintf(hpp
->buf
, hpp
->size
, "%*s", len
, fmt
->name
);
237 static int hpp_color_scnprintf(struct perf_hpp
*hpp
, const char *fmt
, ...)
240 ssize_t ssize
= hpp
->size
;
245 len
= va_arg(args
, int);
246 percent
= va_arg(args
, double);
247 ret
= percent_color_len_snprintf(hpp
->buf
, hpp
->size
, fmt
, len
, percent
);
250 return (ret
>= ssize
) ? (ssize
- 1) : ret
;
253 static int hpp_entry_scnprintf(struct perf_hpp
*hpp
, const char *fmt
, ...)
256 ssize_t ssize
= hpp
->size
;
260 ret
= vsnprintf(hpp
->buf
, hpp
->size
, fmt
, args
);
263 return (ret
>= ssize
) ? (ssize
- 1) : ret
;
266 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
267 static u64 he_get_##_field(struct hist_entry *he) \
269 return he->stat._field; \
272 static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
273 struct perf_hpp *hpp, struct hist_entry *he) \
275 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
276 hpp_color_scnprintf, true); \
279 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
280 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
281 struct perf_hpp *hpp, struct hist_entry *he) \
283 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
284 hpp_entry_scnprintf, true); \
287 #define __HPP_SORT_FN(_type, _field) \
288 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
289 struct hist_entry *a, struct hist_entry *b) \
291 return __hpp__sort(a, b, he_get_##_field); \
294 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
295 static u64 he_get_acc_##_field(struct hist_entry *he) \
297 return he->stat_acc->_field; \
300 static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
301 struct perf_hpp *hpp, struct hist_entry *he) \
303 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
304 hpp_color_scnprintf, true); \
307 #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
308 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
309 struct perf_hpp *hpp, struct hist_entry *he) \
311 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
312 hpp_entry_scnprintf, true); \
315 #define __HPP_SORT_ACC_FN(_type, _field) \
316 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
317 struct hist_entry *a, struct hist_entry *b) \
319 return __hpp__sort_acc(a, b, he_get_acc_##_field); \
322 #define __HPP_ENTRY_RAW_FN(_type, _field) \
323 static u64 he_get_raw_##_field(struct hist_entry *he) \
325 return he->stat._field; \
328 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
329 struct perf_hpp *hpp, struct hist_entry *he) \
331 return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \
332 hpp_entry_scnprintf, false); \
335 #define __HPP_SORT_RAW_FN(_type, _field) \
336 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
337 struct hist_entry *a, struct hist_entry *b) \
339 return __hpp__sort(a, b, he_get_raw_##_field); \
343 #define HPP_PERCENT_FNS(_type, _field) \
344 __HPP_COLOR_PERCENT_FN(_type, _field) \
345 __HPP_ENTRY_PERCENT_FN(_type, _field) \
346 __HPP_SORT_FN(_type, _field)
348 #define HPP_PERCENT_ACC_FNS(_type, _field) \
349 __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
350 __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
351 __HPP_SORT_ACC_FN(_type, _field)
353 #define HPP_RAW_FNS(_type, _field) \
354 __HPP_ENTRY_RAW_FN(_type, _field) \
355 __HPP_SORT_RAW_FN(_type, _field)
357 HPP_PERCENT_FNS(overhead
, period
)
358 HPP_PERCENT_FNS(overhead_sys
, period_sys
)
359 HPP_PERCENT_FNS(overhead_us
, period_us
)
360 HPP_PERCENT_FNS(overhead_guest_sys
, period_guest_sys
)
361 HPP_PERCENT_FNS(overhead_guest_us
, period_guest_us
)
362 HPP_PERCENT_ACC_FNS(overhead_acc
, period
)
364 HPP_RAW_FNS(samples
, nr_events
)
365 HPP_RAW_FNS(period
, period
)
367 static int64_t hpp__nop_cmp(struct perf_hpp_fmt
*fmt __maybe_unused
,
368 struct hist_entry
*a __maybe_unused
,
369 struct hist_entry
*b __maybe_unused
)
374 static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt
*a
)
376 return a
->header
== hpp__header_fn
;
379 static bool hpp__equal(struct perf_hpp_fmt
*a
, struct perf_hpp_fmt
*b
)
381 if (!perf_hpp__is_hpp_entry(a
) || !perf_hpp__is_hpp_entry(b
))
384 return a
->idx
== b
->idx
;
387 #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx) \
390 .header = hpp__header_fn, \
391 .width = hpp__width_fn, \
392 .color = hpp__color_ ## _fn, \
393 .entry = hpp__entry_ ## _fn, \
394 .cmp = hpp__nop_cmp, \
395 .collapse = hpp__nop_cmp, \
396 .sort = hpp__sort_ ## _fn, \
397 .idx = PERF_HPP__ ## _idx, \
398 .equal = hpp__equal, \
401 #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx) \
404 .header = hpp__header_fn, \
405 .width = hpp__width_fn, \
406 .color = hpp__color_ ## _fn, \
407 .entry = hpp__entry_ ## _fn, \
408 .cmp = hpp__nop_cmp, \
409 .collapse = hpp__nop_cmp, \
410 .sort = hpp__sort_ ## _fn, \
411 .idx = PERF_HPP__ ## _idx, \
412 .equal = hpp__equal, \
415 #define HPP__PRINT_FNS(_name, _fn, _idx) \
418 .header = hpp__header_fn, \
419 .width = hpp__width_fn, \
420 .entry = hpp__entry_ ## _fn, \
421 .cmp = hpp__nop_cmp, \
422 .collapse = hpp__nop_cmp, \
423 .sort = hpp__sort_ ## _fn, \
424 .idx = PERF_HPP__ ## _idx, \
425 .equal = hpp__equal, \
428 struct perf_hpp_fmt perf_hpp__format
[] = {
429 HPP__COLOR_PRINT_FNS("Overhead", overhead
, OVERHEAD
),
430 HPP__COLOR_PRINT_FNS("sys", overhead_sys
, OVERHEAD_SYS
),
431 HPP__COLOR_PRINT_FNS("usr", overhead_us
, OVERHEAD_US
),
432 HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys
, OVERHEAD_GUEST_SYS
),
433 HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us
, OVERHEAD_GUEST_US
),
434 HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc
, OVERHEAD_ACC
),
435 HPP__PRINT_FNS("Samples", samples
, SAMPLES
),
436 HPP__PRINT_FNS("Period", period
, PERIOD
)
439 struct perf_hpp_list perf_hpp_list
= {
440 .fields
= LIST_HEAD_INIT(perf_hpp_list
.fields
),
441 .sorts
= LIST_HEAD_INIT(perf_hpp_list
.sorts
),
444 #undef HPP__COLOR_PRINT_FNS
445 #undef HPP__COLOR_ACC_PRINT_FNS
446 #undef HPP__PRINT_FNS
448 #undef HPP_PERCENT_FNS
449 #undef HPP_PERCENT_ACC_FNS
452 #undef __HPP_HEADER_FN
453 #undef __HPP_WIDTH_FN
454 #undef __HPP_COLOR_PERCENT_FN
455 #undef __HPP_ENTRY_PERCENT_FN
456 #undef __HPP_COLOR_ACC_PERCENT_FN
457 #undef __HPP_ENTRY_ACC_PERCENT_FN
458 #undef __HPP_ENTRY_RAW_FN
460 #undef __HPP_SORT_ACC_FN
461 #undef __HPP_SORT_RAW_FN
464 void perf_hpp__init(void)
468 for (i
= 0; i
< PERF_HPP__MAX_INDEX
; i
++) {
469 struct perf_hpp_fmt
*fmt
= &perf_hpp__format
[i
];
471 INIT_LIST_HEAD(&fmt
->list
);
473 /* sort_list may be linked by setup_sorting() */
474 if (fmt
->sort_list
.next
== NULL
)
475 INIT_LIST_HEAD(&fmt
->sort_list
);
479 * If user specified field order, no need to setup default fields.
481 if (is_strict_order(field_order
))
484 if (symbol_conf
.cumulate_callchain
) {
485 hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC
);
486 perf_hpp__format
[PERF_HPP__OVERHEAD
].name
= "Self";
489 hpp_dimension__add_output(PERF_HPP__OVERHEAD
);
491 if (symbol_conf
.show_cpu_utilization
) {
492 hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS
);
493 hpp_dimension__add_output(PERF_HPP__OVERHEAD_US
);
496 hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS
);
497 hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US
);
501 if (symbol_conf
.show_nr_samples
)
502 hpp_dimension__add_output(PERF_HPP__SAMPLES
);
504 if (symbol_conf
.show_total_period
)
505 hpp_dimension__add_output(PERF_HPP__PERIOD
);
508 void perf_hpp_list__column_register(struct perf_hpp_list
*list
,
509 struct perf_hpp_fmt
*format
)
511 list_add_tail(&format
->list
, &list
->fields
);
514 void perf_hpp_list__register_sort_field(struct perf_hpp_list
*list
,
515 struct perf_hpp_fmt
*format
)
517 if (perf_hpp__is_sort_entry(format
) || perf_hpp__is_dynamic_entry(format
))
518 list
->nr_sort_keys
++;
520 list_add_tail(&format
->sort_list
, &list
->sorts
);
523 void perf_hpp__column_unregister(struct perf_hpp_fmt
*format
)
525 list_del(&format
->list
);
528 void perf_hpp__cancel_cumulate(void)
530 struct perf_hpp_fmt
*fmt
, *acc
, *ovh
, *tmp
;
532 if (is_strict_order(field_order
))
535 ovh
= &perf_hpp__format
[PERF_HPP__OVERHEAD
];
536 acc
= &perf_hpp__format
[PERF_HPP__OVERHEAD_ACC
];
538 perf_hpp_list__for_each_format_safe(&perf_hpp_list
, fmt
, tmp
) {
539 if (acc
->equal(acc
, fmt
)) {
540 perf_hpp__column_unregister(fmt
);
544 if (ovh
->equal(ovh
, fmt
))
545 fmt
->name
= "Overhead";
549 static bool fmt_equal(struct perf_hpp_fmt
*a
, struct perf_hpp_fmt
*b
)
551 return a
->equal
&& a
->equal(a
, b
);
554 void perf_hpp__setup_output_field(struct perf_hpp_list
*list
)
556 struct perf_hpp_fmt
*fmt
;
558 /* append sort keys to output field */
559 perf_hpp_list__for_each_sort_list(list
, fmt
) {
560 struct perf_hpp_fmt
*pos
;
562 perf_hpp_list__for_each_format(list
, pos
) {
563 if (fmt_equal(fmt
, pos
))
567 perf_hpp__column_register(fmt
);
573 void perf_hpp__append_sort_keys(struct perf_hpp_list
*list
)
575 struct perf_hpp_fmt
*fmt
;
577 /* append output fields to sort keys */
578 perf_hpp_list__for_each_format(list
, fmt
) {
579 struct perf_hpp_fmt
*pos
;
581 perf_hpp_list__for_each_sort_list(list
, pos
) {
582 if (fmt_equal(fmt
, pos
))
586 perf_hpp__register_sort_field(fmt
);
593 static void fmt_free(struct perf_hpp_fmt
*fmt
)
599 void perf_hpp__reset_output_field(struct perf_hpp_list
*list
)
601 struct perf_hpp_fmt
*fmt
, *tmp
;
603 /* reset output fields */
604 perf_hpp_list__for_each_format_safe(list
, fmt
, tmp
) {
605 list_del_init(&fmt
->list
);
606 list_del_init(&fmt
->sort_list
);
610 /* reset sort keys */
611 perf_hpp_list__for_each_sort_list_safe(list
, fmt
, tmp
) {
612 list_del_init(&fmt
->list
);
613 list_del_init(&fmt
->sort_list
);
619 * See hists__fprintf to match the column widths
621 unsigned int hists__sort_list_width(struct hists
*hists
)
623 struct perf_hpp_fmt
*fmt
;
626 struct perf_hpp dummy_hpp
;
628 hists__for_each_format(hists
, fmt
) {
629 if (perf_hpp__should_skip(fmt
, hists
))
637 ret
+= fmt
->width(fmt
, &dummy_hpp
, hists_to_evsel(hists
));
640 if (verbose
&& sort__has_sym
) /* Addr + origin */
641 ret
+= 3 + BITS_PER_LONG
/ 4;
646 void perf_hpp__reset_width(struct perf_hpp_fmt
*fmt
, struct hists
*hists
)
648 if (perf_hpp__is_sort_entry(fmt
))
649 return perf_hpp__reset_sort_width(fmt
, hists
);
651 if (perf_hpp__is_dynamic_entry(fmt
))
654 BUG_ON(fmt
->idx
>= PERF_HPP__MAX_INDEX
);
657 case PERF_HPP__OVERHEAD
:
658 case PERF_HPP__OVERHEAD_SYS
:
659 case PERF_HPP__OVERHEAD_US
:
660 case PERF_HPP__OVERHEAD_ACC
:
664 case PERF_HPP__OVERHEAD_GUEST_SYS
:
665 case PERF_HPP__OVERHEAD_GUEST_US
:
669 case PERF_HPP__SAMPLES
:
670 case PERF_HPP__PERIOD
:
679 void perf_hpp__set_user_width(const char *width_list_str
)
681 struct perf_hpp_fmt
*fmt
;
682 const char *ptr
= width_list_str
;
684 perf_hpp_list__for_each_format(&perf_hpp_list
, fmt
) {
687 int len
= strtol(ptr
, &p
, 10);