1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/compiler.h>
8 #include "../util/callchain.h"
9 #include "../util/debug.h"
10 #include "../util/hist.h"
11 #include "../util/util.h"
12 #include "../util/sort.h"
13 #include "../util/evsel.h"
14 #include "../util/evlist.h"
17 /* hist period print (hpp) functions */
19 #define hpp__call_print_fn(hpp, fn, fmt, ...) \
21 int __ret = fn(hpp, fmt, ##__VA_ARGS__); \
22 advance_hpp(hpp, __ret); \
26 static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
27 hpp_field_fn get_field, const char *fmt, int len,
28 hpp_snprint_fn print_fn, bool fmt_percent)
31 struct hists *hists = he->hists;
32 struct evsel *evsel = hists_to_evsel(hists);
34 size_t size = hpp->size;
38 u64 total = hists__total_period(hists);
41 percent = 100.0 * get_field(he) / total;
43 ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
45 ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
47 if (perf_evsel__is_group_event(evsel)) {
48 int prev_idx, idx_delta;
49 struct hist_entry *pair;
50 int nr_members = evsel->core.nr_members;
52 prev_idx = perf_evsel__group_idx(evsel);
54 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
55 u64 period = get_field(pair);
56 u64 total = hists__total_period(pair->hists);
61 evsel = hists_to_evsel(pair->hists);
62 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
66 * zero-fill group members in the middle which
70 ret += hpp__call_print_fn(hpp, print_fn,
73 ret += hpp__call_print_fn(hpp, print_fn,
79 ret += hpp__call_print_fn(hpp, print_fn, fmt, len,
80 100.0 * period / total);
82 ret += hpp__call_print_fn(hpp, print_fn, fmt,
86 prev_idx = perf_evsel__group_idx(evsel);
89 idx_delta = nr_members - prev_idx - 1;
93 * zero-fill group members at last which have no sample
96 ret += hpp__call_print_fn(hpp, print_fn,
99 ret += hpp__call_print_fn(hpp, print_fn,
106 * Restore original buf and size as it's where caller expects
107 * the result will be saved.
115 int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
116 struct hist_entry *he, hpp_field_fn get_field,
117 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
119 int len = fmt->user_len ?: fmt->len;
121 if (symbol_conf.field_sep) {
122 return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
123 print_fn, fmt_percent);
127 len -= 2; /* 2 for a space and a % sign */
131 return __hpp__fmt(hpp, he, get_field, fmtstr, len, print_fn, fmt_percent);
134 int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
135 struct hist_entry *he, hpp_field_fn get_field,
136 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
138 if (!symbol_conf.cumulate_callchain) {
139 int len = fmt->user_len ?: fmt->len;
140 return snprintf(hpp->buf, hpp->size, " %*s", len - 1, "N/A");
143 return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent);
146 static int field_cmp(u64 field_a, u64 field_b)
148 if (field_a > field_b)
150 if (field_a < field_b)
155 static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
156 hpp_field_fn get_field)
161 struct hist_entry *pair;
162 u64 *fields_a, *fields_b;
164 ret = field_cmp(get_field(a), get_field(b));
165 if (ret || !symbol_conf.event_group)
168 evsel = hists_to_evsel(a->hists);
169 if (!perf_evsel__is_group_event(evsel))
172 nr_members = evsel->core.nr_members;
173 fields_a = calloc(nr_members, sizeof(*fields_a));
174 fields_b = calloc(nr_members, sizeof(*fields_b));
176 if (!fields_a || !fields_b)
179 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
180 evsel = hists_to_evsel(pair->hists);
181 fields_a[perf_evsel__group_idx(evsel)] = get_field(pair);
184 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
185 evsel = hists_to_evsel(pair->hists);
186 fields_b[perf_evsel__group_idx(evsel)] = get_field(pair);
189 for (i = 1; i < nr_members; i++) {
190 ret = field_cmp(fields_a[i], fields_b[i]);
202 static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
203 hpp_field_fn get_field)
207 if (symbol_conf.cumulate_callchain) {
209 * Put caller above callee when they have equal period.
211 ret = field_cmp(get_field(a), get_field(b));
215 if (a->thread != b->thread || !hist_entry__has_callchains(a) || !symbol_conf.use_callchain)
218 ret = b->callchain->max_depth - a->callchain->max_depth;
219 if (callchain_param.order == ORDER_CALLER)
225 static int hpp__width_fn(struct perf_hpp_fmt *fmt,
226 struct perf_hpp *hpp __maybe_unused,
229 int len = fmt->user_len ?: fmt->len;
230 struct evsel *evsel = hists_to_evsel(hists);
232 if (symbol_conf.event_group)
233 len = max(len, evsel->core.nr_members * fmt->len);
235 if (len < (int)strlen(fmt->name))
236 len = strlen(fmt->name);
241 static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
242 struct hists *hists, int line __maybe_unused,
243 int *span __maybe_unused)
245 int len = hpp__width_fn(fmt, hpp, hists);
246 return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
249 int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
252 ssize_t ssize = hpp->size;
257 len = va_arg(args, int);
258 percent = va_arg(args, double);
259 ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
262 return (ret >= ssize) ? (ssize - 1) : ret;
265 static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
268 ssize_t ssize = hpp->size;
272 ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
275 return (ret >= ssize) ? (ssize - 1) : ret;
278 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
279 static u64 he_get_##_field(struct hist_entry *he) \
281 return he->stat._field; \
284 static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
285 struct perf_hpp *hpp, struct hist_entry *he) \
287 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
288 hpp_color_scnprintf, true); \
291 #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
292 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
293 struct perf_hpp *hpp, struct hist_entry *he) \
295 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
296 hpp_entry_scnprintf, true); \
299 #define __HPP_SORT_FN(_type, _field) \
300 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
301 struct hist_entry *a, struct hist_entry *b) \
303 return __hpp__sort(a, b, he_get_##_field); \
306 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
307 static u64 he_get_acc_##_field(struct hist_entry *he) \
309 return he->stat_acc->_field; \
312 static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
313 struct perf_hpp *hpp, struct hist_entry *he) \
315 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
316 hpp_color_scnprintf, true); \
319 #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
320 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
321 struct perf_hpp *hpp, struct hist_entry *he) \
323 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
324 hpp_entry_scnprintf, true); \
327 #define __HPP_SORT_ACC_FN(_type, _field) \
328 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
329 struct hist_entry *a, struct hist_entry *b) \
331 return __hpp__sort_acc(a, b, he_get_acc_##_field); \
334 #define __HPP_ENTRY_RAW_FN(_type, _field) \
335 static u64 he_get_raw_##_field(struct hist_entry *he) \
337 return he->stat._field; \
340 static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
341 struct perf_hpp *hpp, struct hist_entry *he) \
343 return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \
344 hpp_entry_scnprintf, false); \
347 #define __HPP_SORT_RAW_FN(_type, _field) \
348 static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
349 struct hist_entry *a, struct hist_entry *b) \
351 return __hpp__sort(a, b, he_get_raw_##_field); \
355 #define HPP_PERCENT_FNS(_type, _field) \
356 __HPP_COLOR_PERCENT_FN(_type, _field) \
357 __HPP_ENTRY_PERCENT_FN(_type, _field) \
358 __HPP_SORT_FN(_type, _field)
360 #define HPP_PERCENT_ACC_FNS(_type, _field) \
361 __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
362 __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
363 __HPP_SORT_ACC_FN(_type, _field)
365 #define HPP_RAW_FNS(_type, _field) \
366 __HPP_ENTRY_RAW_FN(_type, _field) \
367 __HPP_SORT_RAW_FN(_type, _field)
369 HPP_PERCENT_FNS(overhead, period)
370 HPP_PERCENT_FNS(overhead_sys, period_sys)
371 HPP_PERCENT_FNS(overhead_us, period_us)
372 HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys)
373 HPP_PERCENT_FNS(overhead_guest_us, period_guest_us)
374 HPP_PERCENT_ACC_FNS(overhead_acc, period)
376 HPP_RAW_FNS(samples, nr_events)
377 HPP_RAW_FNS(period, period)
379 static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
380 struct hist_entry *a __maybe_unused,
381 struct hist_entry *b __maybe_unused)
386 static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a)
388 return a->header == hpp__header_fn;
391 static bool hpp__equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
393 if (!perf_hpp__is_hpp_entry(a) || !perf_hpp__is_hpp_entry(b))
396 return a->idx == b->idx;
399 #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx) \
402 .header = hpp__header_fn, \
403 .width = hpp__width_fn, \
404 .color = hpp__color_ ## _fn, \
405 .entry = hpp__entry_ ## _fn, \
406 .cmp = hpp__nop_cmp, \
407 .collapse = hpp__nop_cmp, \
408 .sort = hpp__sort_ ## _fn, \
409 .idx = PERF_HPP__ ## _idx, \
410 .equal = hpp__equal, \
413 #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx) \
416 .header = hpp__header_fn, \
417 .width = hpp__width_fn, \
418 .color = hpp__color_ ## _fn, \
419 .entry = hpp__entry_ ## _fn, \
420 .cmp = hpp__nop_cmp, \
421 .collapse = hpp__nop_cmp, \
422 .sort = hpp__sort_ ## _fn, \
423 .idx = PERF_HPP__ ## _idx, \
424 .equal = hpp__equal, \
427 #define HPP__PRINT_FNS(_name, _fn, _idx) \
430 .header = hpp__header_fn, \
431 .width = hpp__width_fn, \
432 .entry = hpp__entry_ ## _fn, \
433 .cmp = hpp__nop_cmp, \
434 .collapse = hpp__nop_cmp, \
435 .sort = hpp__sort_ ## _fn, \
436 .idx = PERF_HPP__ ## _idx, \
437 .equal = hpp__equal, \
440 struct perf_hpp_fmt perf_hpp__format[] = {
441 HPP__COLOR_PRINT_FNS("Overhead", overhead, OVERHEAD),
442 HPP__COLOR_PRINT_FNS("sys", overhead_sys, OVERHEAD_SYS),
443 HPP__COLOR_PRINT_FNS("usr", overhead_us, OVERHEAD_US),
444 HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys, OVERHEAD_GUEST_SYS),
445 HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us, OVERHEAD_GUEST_US),
446 HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
447 HPP__PRINT_FNS("Samples", samples, SAMPLES),
448 HPP__PRINT_FNS("Period", period, PERIOD)
451 struct perf_hpp_list perf_hpp_list = {
452 .fields = LIST_HEAD_INIT(perf_hpp_list.fields),
453 .sorts = LIST_HEAD_INIT(perf_hpp_list.sorts),
454 .nr_header_lines = 1,
457 #undef HPP__COLOR_PRINT_FNS
458 #undef HPP__COLOR_ACC_PRINT_FNS
459 #undef HPP__PRINT_FNS
461 #undef HPP_PERCENT_FNS
462 #undef HPP_PERCENT_ACC_FNS
465 #undef __HPP_HEADER_FN
466 #undef __HPP_WIDTH_FN
467 #undef __HPP_COLOR_PERCENT_FN
468 #undef __HPP_ENTRY_PERCENT_FN
469 #undef __HPP_COLOR_ACC_PERCENT_FN
470 #undef __HPP_ENTRY_ACC_PERCENT_FN
471 #undef __HPP_ENTRY_RAW_FN
473 #undef __HPP_SORT_ACC_FN
474 #undef __HPP_SORT_RAW_FN
477 void perf_hpp__init(void)
481 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
482 struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
484 INIT_LIST_HEAD(&fmt->list);
486 /* sort_list may be linked by setup_sorting() */
487 if (fmt->sort_list.next == NULL)
488 INIT_LIST_HEAD(&fmt->sort_list);
492 * If user specified field order, no need to setup default fields.
494 if (is_strict_order(field_order))
497 if (symbol_conf.cumulate_callchain) {
498 hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
499 perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
502 hpp_dimension__add_output(PERF_HPP__OVERHEAD);
504 if (symbol_conf.show_cpu_utilization) {
505 hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
506 hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
509 hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
510 hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
514 if (symbol_conf.show_nr_samples)
515 hpp_dimension__add_output(PERF_HPP__SAMPLES);
517 if (symbol_conf.show_total_period)
518 hpp_dimension__add_output(PERF_HPP__PERIOD);
521 void perf_hpp_list__column_register(struct perf_hpp_list *list,
522 struct perf_hpp_fmt *format)
524 list_add_tail(&format->list, &list->fields);
527 void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
528 struct perf_hpp_fmt *format)
530 list_add_tail(&format->sort_list, &list->sorts);
533 void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
534 struct perf_hpp_fmt *format)
536 list_add(&format->sort_list, &list->sorts);
539 void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
541 list_del_init(&format->list);
544 void perf_hpp__cancel_cumulate(void)
546 struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp;
548 if (is_strict_order(field_order))
551 ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
552 acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
554 perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) {
555 if (acc->equal(acc, fmt)) {
556 perf_hpp__column_unregister(fmt);
560 if (ovh->equal(ovh, fmt))
561 fmt->name = "Overhead";
565 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
567 return a->equal && a->equal(a, b);
570 void perf_hpp__setup_output_field(struct perf_hpp_list *list)
572 struct perf_hpp_fmt *fmt;
574 /* append sort keys to output field */
575 perf_hpp_list__for_each_sort_list(list, fmt) {
576 struct perf_hpp_fmt *pos;
578 /* skip sort-only fields ("sort_compute" in perf diff) */
579 if (!fmt->entry && !fmt->color)
582 perf_hpp_list__for_each_format(list, pos) {
583 if (fmt_equal(fmt, pos))
587 perf_hpp__column_register(fmt);
593 void perf_hpp__append_sort_keys(struct perf_hpp_list *list)
595 struct perf_hpp_fmt *fmt;
597 /* append output fields to sort keys */
598 perf_hpp_list__for_each_format(list, fmt) {
599 struct perf_hpp_fmt *pos;
601 perf_hpp_list__for_each_sort_list(list, pos) {
602 if (fmt_equal(fmt, pos))
606 perf_hpp__register_sort_field(fmt);
613 static void fmt_free(struct perf_hpp_fmt *fmt)
616 * At this point fmt should be completely
617 * unhooked, if not it's a bug.
619 BUG_ON(!list_empty(&fmt->list));
620 BUG_ON(!list_empty(&fmt->sort_list));
626 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
628 struct perf_hpp_fmt *fmt, *tmp;
630 /* reset output fields */
631 perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
632 list_del_init(&fmt->list);
633 list_del_init(&fmt->sort_list);
637 /* reset sort keys */
638 perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
639 list_del_init(&fmt->list);
640 list_del_init(&fmt->sort_list);
646 * See hists__fprintf to match the column widths
648 unsigned int hists__sort_list_width(struct hists *hists)
650 struct perf_hpp_fmt *fmt;
653 struct perf_hpp dummy_hpp;
655 hists__for_each_format(hists, fmt) {
656 if (perf_hpp__should_skip(fmt, hists))
664 ret += fmt->width(fmt, &dummy_hpp, hists);
667 if (verbose > 0 && hists__has(hists, sym)) /* Addr + origin */
668 ret += 3 + BITS_PER_LONG / 4;
673 unsigned int hists__overhead_width(struct hists *hists)
675 struct perf_hpp_fmt *fmt;
678 struct perf_hpp dummy_hpp;
680 hists__for_each_format(hists, fmt) {
681 if (perf_hpp__is_sort_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
689 ret += fmt->width(fmt, &dummy_hpp, hists);
695 void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
697 if (perf_hpp__is_sort_entry(fmt))
698 return perf_hpp__reset_sort_width(fmt, hists);
700 if (perf_hpp__is_dynamic_entry(fmt))
703 BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
706 case PERF_HPP__OVERHEAD:
707 case PERF_HPP__OVERHEAD_SYS:
708 case PERF_HPP__OVERHEAD_US:
709 case PERF_HPP__OVERHEAD_ACC:
713 case PERF_HPP__OVERHEAD_GUEST_SYS:
714 case PERF_HPP__OVERHEAD_GUEST_US:
718 case PERF_HPP__SAMPLES:
719 case PERF_HPP__PERIOD:
728 void hists__reset_column_width(struct hists *hists)
730 struct perf_hpp_fmt *fmt;
731 struct perf_hpp_list_node *node;
733 hists__for_each_format(hists, fmt)
734 perf_hpp__reset_width(fmt, hists);
736 /* hierarchy entries have their own hpp list */
737 list_for_each_entry(node, &hists->hpp_formats, list) {
738 perf_hpp_list__for_each_format(&node->hpp, fmt)
739 perf_hpp__reset_width(fmt, hists);
743 void perf_hpp__set_user_width(const char *width_list_str)
745 struct perf_hpp_fmt *fmt;
746 const char *ptr = width_list_str;
748 perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
751 int len = strtol(ptr, &p, 10);
761 static int add_hierarchy_fmt(struct hists *hists, struct perf_hpp_fmt *fmt)
763 struct perf_hpp_list_node *node = NULL;
764 struct perf_hpp_fmt *fmt_copy;
766 bool skip = perf_hpp__should_skip(fmt, hists);
768 list_for_each_entry(node, &hists->hpp_formats, list) {
769 if (node->level == fmt->level) {
776 node = malloc(sizeof(*node));
781 node->level = fmt->level;
782 perf_hpp_list__init(&node->hpp);
784 hists->nr_hpp_node++;
785 list_add_tail(&node->list, &hists->hpp_formats);
788 fmt_copy = perf_hpp_fmt__dup(fmt);
789 if (fmt_copy == NULL)
795 list_add_tail(&fmt_copy->list, &node->hpp.fields);
796 list_add_tail(&fmt_copy->sort_list, &node->hpp.sorts);
801 int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
802 struct evlist *evlist)
805 struct perf_hpp_fmt *fmt;
809 if (!symbol_conf.report_hierarchy)
812 evlist__for_each_entry(evlist, evsel) {
813 hists = evsel__hists(evsel);
815 perf_hpp_list__for_each_sort_list(list, fmt) {
816 if (perf_hpp__is_dynamic_entry(fmt) &&
817 !perf_hpp__defined_dynamic_entry(fmt, hists))
820 ret = add_hierarchy_fmt(hists, fmt);