4 * Builtin diff command: Analyze two perf.data input files, look up and read
5 * DSOs and symbol information, sort them and produce a diff.
9 #include "util/debug.h"
10 #include "util/event.h"
11 #include "util/hist.h"
12 #include "util/evsel.h"
13 #include "util/evlist.h"
14 #include "util/session.h"
15 #include "util/tool.h"
16 #include "util/sort.h"
17 #include "util/symbol.h"
18 #include "util/util.h"
19 #include "util/data.h"
20 #include "util/config.h"
27 /* Diff command specific HPP columns. */
29 PERF_HPP_DIFF__BASELINE
,
30 PERF_HPP_DIFF__PERIOD
,
31 PERF_HPP_DIFF__PERIOD_BASELINE
,
34 PERF_HPP_DIFF__WEIGHTED_DIFF
,
35 PERF_HPP_DIFF__FORMULA
,
36 PERF_HPP_DIFF__DELTA_ABS
,
38 PERF_HPP_DIFF__MAX_INDEX
42 struct perf_hpp_fmt fmt
;
49 struct perf_session
*session
;
50 struct perf_data_file file
;
53 struct diff_hpp_fmt fmt
[PERF_HPP_DIFF__MAX_INDEX
];
56 static struct data__file
*data__files
;
57 static int data__files_cnt
;
59 #define data__for_each_file_start(i, d, s) \
60 for (i = s, d = &data__files[s]; \
61 i < data__files_cnt; \
62 i++, d = &data__files[i])
64 #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
65 #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
68 static bool show_period
;
69 static bool show_formula
;
70 static bool show_baseline_only
;
71 static unsigned int sort_compute
= 1;
73 static s64 compute_wdiff_w1
;
74 static s64 compute_wdiff_w2
;
79 COMPUTE_WEIGHTED_DIFF
,
84 const char *compute_names
[COMPUTE_MAX
] = {
85 [COMPUTE_DELTA
] = "delta",
86 [COMPUTE_DELTA_ABS
] = "delta-abs",
87 [COMPUTE_RATIO
] = "ratio",
88 [COMPUTE_WEIGHTED_DIFF
] = "wdiff",
91 static int compute
= COMPUTE_DELTA_ABS
;
93 static int compute_2_hpp
[COMPUTE_MAX
] = {
94 [COMPUTE_DELTA
] = PERF_HPP_DIFF__DELTA
,
95 [COMPUTE_DELTA_ABS
] = PERF_HPP_DIFF__DELTA_ABS
,
96 [COMPUTE_RATIO
] = PERF_HPP_DIFF__RATIO
,
97 [COMPUTE_WEIGHTED_DIFF
] = PERF_HPP_DIFF__WEIGHTED_DIFF
,
100 #define MAX_COL_WIDTH 70
102 static struct header_column
{
105 } columns
[PERF_HPP_DIFF__MAX_INDEX
] = {
106 [PERF_HPP_DIFF__BASELINE
] = {
109 [PERF_HPP_DIFF__PERIOD
] = {
113 [PERF_HPP_DIFF__PERIOD_BASELINE
] = {
114 .name
= "Base period",
117 [PERF_HPP_DIFF__DELTA
] = {
121 [PERF_HPP_DIFF__DELTA_ABS
] = {
125 [PERF_HPP_DIFF__RATIO
] = {
129 [PERF_HPP_DIFF__WEIGHTED_DIFF
] = {
130 .name
= "Weighted diff",
133 [PERF_HPP_DIFF__FORMULA
] = {
135 .width
= MAX_COL_WIDTH
,
139 static int setup_compute_opt_wdiff(char *opt
)
149 w2_str
= strchr(opt
, ',');
157 compute_wdiff_w1
= strtol(w1_str
, NULL
, 10);
158 compute_wdiff_w2
= strtol(w2_str
, NULL
, 10);
160 if (!compute_wdiff_w1
|| !compute_wdiff_w2
)
163 pr_debug("compute wdiff w1(%" PRId64
") w2(%" PRId64
")\n",
164 compute_wdiff_w1
, compute_wdiff_w2
);
170 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
175 static int setup_compute_opt(char *opt
)
177 if (compute
== COMPUTE_WEIGHTED_DIFF
)
178 return setup_compute_opt_wdiff(opt
);
181 pr_err("Failed: extra option specified '%s'", opt
);
188 static int setup_compute(const struct option
*opt
, const char *str
,
189 int unset __maybe_unused
)
191 int *cp
= (int *) opt
->value
;
192 char *cstr
= (char *) str
;
202 option
= strchr(str
, ':');
204 unsigned len
= option
++ - str
;
207 * The str data are not writeable, so we need
208 * to use another buffer.
211 /* No option value is longer. */
212 if (len
>= sizeof(buf
))
215 strncpy(buf
, str
, len
);
220 for (i
= 0; i
< COMPUTE_MAX
; i
++)
221 if (!strcmp(cstr
, compute_names
[i
])) {
223 return setup_compute_opt(option
);
226 pr_err("Failed: '%s' is not computation method "
227 "(use 'delta','ratio' or 'wdiff')\n", str
);
231 static double period_percent(struct hist_entry
*he
, u64 period
)
233 u64 total
= hists__total_period(he
->hists
);
235 return (period
* 100.0) / total
;
238 static double compute_delta(struct hist_entry
*he
, struct hist_entry
*pair
)
240 double old_percent
= period_percent(he
, he
->stat
.period
);
241 double new_percent
= period_percent(pair
, pair
->stat
.period
);
243 pair
->diff
.period_ratio_delta
= new_percent
- old_percent
;
244 pair
->diff
.computed
= true;
245 return pair
->diff
.period_ratio_delta
;
248 static double compute_ratio(struct hist_entry
*he
, struct hist_entry
*pair
)
250 double old_period
= he
->stat
.period
?: 1;
251 double new_period
= pair
->stat
.period
;
253 pair
->diff
.computed
= true;
254 pair
->diff
.period_ratio
= new_period
/ old_period
;
255 return pair
->diff
.period_ratio
;
258 static s64
compute_wdiff(struct hist_entry
*he
, struct hist_entry
*pair
)
260 u64 old_period
= he
->stat
.period
;
261 u64 new_period
= pair
->stat
.period
;
263 pair
->diff
.computed
= true;
264 pair
->diff
.wdiff
= new_period
* compute_wdiff_w2
-
265 old_period
* compute_wdiff_w1
;
267 return pair
->diff
.wdiff
;
270 static int formula_delta(struct hist_entry
*he
, struct hist_entry
*pair
,
271 char *buf
, size_t size
)
273 u64 he_total
= he
->hists
->stats
.total_period
;
274 u64 pair_total
= pair
->hists
->stats
.total_period
;
276 if (symbol_conf
.filter_relative
) {
277 he_total
= he
->hists
->stats
.total_non_filtered_period
;
278 pair_total
= pair
->hists
->stats
.total_non_filtered_period
;
280 return scnprintf(buf
, size
,
281 "(%" PRIu64
" * 100 / %" PRIu64
") - "
282 "(%" PRIu64
" * 100 / %" PRIu64
")",
283 pair
->stat
.period
, pair_total
,
284 he
->stat
.period
, he_total
);
287 static int formula_ratio(struct hist_entry
*he
, struct hist_entry
*pair
,
288 char *buf
, size_t size
)
290 double old_period
= he
->stat
.period
;
291 double new_period
= pair
->stat
.period
;
293 return scnprintf(buf
, size
, "%.0F / %.0F", new_period
, old_period
);
296 static int formula_wdiff(struct hist_entry
*he
, struct hist_entry
*pair
,
297 char *buf
, size_t size
)
299 u64 old_period
= he
->stat
.period
;
300 u64 new_period
= pair
->stat
.period
;
302 return scnprintf(buf
, size
,
303 "(%" PRIu64
" * " "%" PRId64
") - (%" PRIu64
" * " "%" PRId64
")",
304 new_period
, compute_wdiff_w2
, old_period
, compute_wdiff_w1
);
307 static int formula_fprintf(struct hist_entry
*he
, struct hist_entry
*pair
,
308 char *buf
, size_t size
)
312 case COMPUTE_DELTA_ABS
:
313 return formula_delta(he
, pair
, buf
, size
);
315 return formula_ratio(he
, pair
, buf
, size
);
316 case COMPUTE_WEIGHTED_DIFF
:
317 return formula_wdiff(he
, pair
, buf
, size
);
325 static int diff__process_sample_event(struct perf_tool
*tool __maybe_unused
,
326 union perf_event
*event
,
327 struct perf_sample
*sample
,
328 struct perf_evsel
*evsel
,
329 struct machine
*machine
)
331 struct addr_location al
;
332 struct hists
*hists
= evsel__hists(evsel
);
335 if (machine__resolve(machine
, &al
, sample
) < 0) {
336 pr_warning("problem processing %d event, skipping it.\n",
341 if (!hists__add_entry(hists
, &al
, NULL
, NULL
, NULL
, sample
, true)) {
342 pr_warning("problem incrementing symbol period, skipping event\n");
347 * The total_period is updated here before going to the output
348 * tree since normally only the baseline hists will call
349 * hists__output_resort() and precompute needs the total
350 * period in order to sort entries by percentage delta.
352 hists
->stats
.total_period
+= sample
->period
;
354 hists
->stats
.total_non_filtered_period
+= sample
->period
;
357 addr_location__put(&al
);
361 static struct perf_tool tool
= {
362 .sample
= diff__process_sample_event
,
363 .mmap
= perf_event__process_mmap
,
364 .mmap2
= perf_event__process_mmap2
,
365 .comm
= perf_event__process_comm
,
366 .exit
= perf_event__process_exit
,
367 .fork
= perf_event__process_fork
,
368 .lost
= perf_event__process_lost
,
369 .namespaces
= perf_event__process_namespaces
,
370 .ordered_events
= true,
371 .ordering_requires_timestamps
= true,
374 static struct perf_evsel
*evsel_match(struct perf_evsel
*evsel
,
375 struct perf_evlist
*evlist
)
377 struct perf_evsel
*e
;
379 evlist__for_each_entry(evlist
, e
) {
380 if (perf_evsel__match2(evsel
, e
))
387 static void perf_evlist__collapse_resort(struct perf_evlist
*evlist
)
389 struct perf_evsel
*evsel
;
391 evlist__for_each_entry(evlist
, evsel
) {
392 struct hists
*hists
= evsel__hists(evsel
);
394 hists__collapse_resort(hists
, NULL
);
398 static struct data__file
*fmt_to_data_file(struct perf_hpp_fmt
*fmt
)
400 struct diff_hpp_fmt
*dfmt
= container_of(fmt
, struct diff_hpp_fmt
, fmt
);
401 void *ptr
= dfmt
- dfmt
->idx
;
402 struct data__file
*d
= container_of(ptr
, struct data__file
, fmt
);
407 static struct hist_entry
*
408 get_pair_data(struct hist_entry
*he
, struct data__file
*d
)
410 if (hist_entry__has_pairs(he
)) {
411 struct hist_entry
*pair
;
413 list_for_each_entry(pair
, &he
->pairs
.head
, pairs
.node
)
414 if (pair
->hists
== d
->hists
)
421 static struct hist_entry
*
422 get_pair_fmt(struct hist_entry
*he
, struct diff_hpp_fmt
*dfmt
)
424 struct data__file
*d
= fmt_to_data_file(&dfmt
->fmt
);
426 return get_pair_data(he
, d
);
429 static void hists__baseline_only(struct hists
*hists
)
431 struct rb_root
*root
;
432 struct rb_node
*next
;
434 if (hists__has(hists
, need_collapse
))
435 root
= &hists
->entries_collapsed
;
437 root
= hists
->entries_in
;
439 next
= rb_first(root
);
440 while (next
!= NULL
) {
441 struct hist_entry
*he
= rb_entry(next
, struct hist_entry
, rb_node_in
);
443 next
= rb_next(&he
->rb_node_in
);
444 if (!hist_entry__next_pair(he
)) {
445 rb_erase(&he
->rb_node_in
, root
);
446 hist_entry__delete(he
);
451 static void hists__precompute(struct hists
*hists
)
453 struct rb_root
*root
;
454 struct rb_node
*next
;
456 if (hists__has(hists
, need_collapse
))
457 root
= &hists
->entries_collapsed
;
459 root
= hists
->entries_in
;
461 next
= rb_first(root
);
462 while (next
!= NULL
) {
463 struct hist_entry
*he
, *pair
;
464 struct data__file
*d
;
467 he
= rb_entry(next
, struct hist_entry
, rb_node_in
);
468 next
= rb_next(&he
->rb_node_in
);
470 data__for_each_file_new(i
, d
) {
471 pair
= get_pair_data(he
, d
);
477 case COMPUTE_DELTA_ABS
:
478 compute_delta(he
, pair
);
481 compute_ratio(he
, pair
);
483 case COMPUTE_WEIGHTED_DIFF
:
484 compute_wdiff(he
, pair
);
493 static int64_t cmp_doubles(double l
, double r
)
504 __hist_entry__cmp_compute(struct hist_entry
*left
, struct hist_entry
*right
,
510 double l
= left
->diff
.period_ratio_delta
;
511 double r
= right
->diff
.period_ratio_delta
;
513 return cmp_doubles(l
, r
);
515 case COMPUTE_DELTA_ABS
:
517 double l
= fabs(left
->diff
.period_ratio_delta
);
518 double r
= fabs(right
->diff
.period_ratio_delta
);
520 return cmp_doubles(l
, r
);
524 double l
= left
->diff
.period_ratio
;
525 double r
= right
->diff
.period_ratio
;
527 return cmp_doubles(l
, r
);
529 case COMPUTE_WEIGHTED_DIFF
:
531 s64 l
= left
->diff
.wdiff
;
532 s64 r
= right
->diff
.wdiff
;
544 hist_entry__cmp_compute(struct hist_entry
*left
, struct hist_entry
*right
,
547 bool pairs_left
= hist_entry__has_pairs(left
);
548 bool pairs_right
= hist_entry__has_pairs(right
);
549 struct hist_entry
*p_right
, *p_left
;
551 if (!pairs_left
&& !pairs_right
)
554 if (!pairs_left
|| !pairs_right
)
555 return pairs_left
? -1 : 1;
557 p_left
= get_pair_data(left
, &data__files
[sort_idx
]);
558 p_right
= get_pair_data(right
, &data__files
[sort_idx
]);
560 if (!p_left
&& !p_right
)
563 if (!p_left
|| !p_right
)
564 return p_left
? -1 : 1;
567 * We have 2 entries of same kind, let's
568 * make the data comparison.
570 return __hist_entry__cmp_compute(p_left
, p_right
, c
);
574 hist_entry__cmp_compute_idx(struct hist_entry
*left
, struct hist_entry
*right
,
577 struct hist_entry
*p_right
, *p_left
;
579 p_left
= get_pair_data(left
, &data__files
[sort_idx
]);
580 p_right
= get_pair_data(right
, &data__files
[sort_idx
]);
582 if (!p_left
&& !p_right
)
585 if (!p_left
|| !p_right
)
586 return p_left
? -1 : 1;
588 if (c
!= COMPUTE_DELTA
&& c
!= COMPUTE_DELTA_ABS
) {
590 * The delta can be computed without the baseline, but
591 * others are not. Put those entries which have no
594 if (left
->dummy
&& right
->dummy
)
597 if (left
->dummy
|| right
->dummy
)
598 return left
->dummy
? 1 : -1;
601 return __hist_entry__cmp_compute(p_left
, p_right
, c
);
605 hist_entry__cmp_nop(struct perf_hpp_fmt
*fmt __maybe_unused
,
606 struct hist_entry
*left __maybe_unused
,
607 struct hist_entry
*right __maybe_unused
)
613 hist_entry__cmp_baseline(struct perf_hpp_fmt
*fmt __maybe_unused
,
614 struct hist_entry
*left
, struct hist_entry
*right
)
616 if (left
->stat
.period
== right
->stat
.period
)
618 return left
->stat
.period
> right
->stat
.period
? 1 : -1;
622 hist_entry__cmp_delta(struct perf_hpp_fmt
*fmt
,
623 struct hist_entry
*left
, struct hist_entry
*right
)
625 struct data__file
*d
= fmt_to_data_file(fmt
);
627 return hist_entry__cmp_compute(right
, left
, COMPUTE_DELTA
, d
->idx
);
631 hist_entry__cmp_delta_abs(struct perf_hpp_fmt
*fmt
,
632 struct hist_entry
*left
, struct hist_entry
*right
)
634 struct data__file
*d
= fmt_to_data_file(fmt
);
636 return hist_entry__cmp_compute(right
, left
, COMPUTE_DELTA_ABS
, d
->idx
);
640 hist_entry__cmp_ratio(struct perf_hpp_fmt
*fmt
,
641 struct hist_entry
*left
, struct hist_entry
*right
)
643 struct data__file
*d
= fmt_to_data_file(fmt
);
645 return hist_entry__cmp_compute(right
, left
, COMPUTE_RATIO
, d
->idx
);
649 hist_entry__cmp_wdiff(struct perf_hpp_fmt
*fmt
,
650 struct hist_entry
*left
, struct hist_entry
*right
)
652 struct data__file
*d
= fmt_to_data_file(fmt
);
654 return hist_entry__cmp_compute(right
, left
, COMPUTE_WEIGHTED_DIFF
, d
->idx
);
658 hist_entry__cmp_delta_idx(struct perf_hpp_fmt
*fmt __maybe_unused
,
659 struct hist_entry
*left
, struct hist_entry
*right
)
661 return hist_entry__cmp_compute_idx(right
, left
, COMPUTE_DELTA
,
666 hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt
*fmt __maybe_unused
,
667 struct hist_entry
*left
, struct hist_entry
*right
)
669 return hist_entry__cmp_compute_idx(right
, left
, COMPUTE_DELTA_ABS
,
674 hist_entry__cmp_ratio_idx(struct perf_hpp_fmt
*fmt __maybe_unused
,
675 struct hist_entry
*left
, struct hist_entry
*right
)
677 return hist_entry__cmp_compute_idx(right
, left
, COMPUTE_RATIO
,
682 hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt
*fmt __maybe_unused
,
683 struct hist_entry
*left
, struct hist_entry
*right
)
685 return hist_entry__cmp_compute_idx(right
, left
, COMPUTE_WEIGHTED_DIFF
,
689 static void hists__process(struct hists
*hists
)
691 if (show_baseline_only
)
692 hists__baseline_only(hists
);
694 hists__precompute(hists
);
695 hists__output_resort(hists
, NULL
);
697 hists__fprintf(hists
, !quiet
, 0, 0, 0, stdout
,
698 symbol_conf
.use_callchain
);
701 static void data__fprintf(void)
703 struct data__file
*d
;
706 fprintf(stdout
, "# Data files:\n");
708 data__for_each_file(i
, d
)
709 fprintf(stdout
, "# [%d] %s %s\n",
710 d
->idx
, d
->file
.path
,
711 !d
->idx
? "(Baseline)" : "");
713 fprintf(stdout
, "#\n");
716 static void data_process(void)
718 struct perf_evlist
*evlist_base
= data__files
[0].session
->evlist
;
719 struct perf_evsel
*evsel_base
;
722 evlist__for_each_entry(evlist_base
, evsel_base
) {
723 struct hists
*hists_base
= evsel__hists(evsel_base
);
724 struct data__file
*d
;
727 data__for_each_file_new(i
, d
) {
728 struct perf_evlist
*evlist
= d
->session
->evlist
;
729 struct perf_evsel
*evsel
;
732 evsel
= evsel_match(evsel_base
, evlist
);
736 hists
= evsel__hists(evsel
);
739 hists__match(hists_base
, hists
);
741 if (!show_baseline_only
)
742 hists__link(hists_base
, hists
);
746 fprintf(stdout
, "%s# Event '%s'\n#\n", first
? "" : "\n",
747 perf_evsel__name(evsel_base
));
752 if (verbose
> 0 || ((data__files_cnt
> 2) && !quiet
))
755 /* Don't sort callchain for perf diff */
756 perf_evsel__reset_sample_bit(evsel_base
, CALLCHAIN
);
758 hists__process(hists_base
);
762 static void data__free(struct data__file
*d
)
766 for (col
= 0; col
< PERF_HPP_DIFF__MAX_INDEX
; col
++) {
767 struct diff_hpp_fmt
*fmt
= &d
->fmt
[col
];
773 static int __cmd_diff(void)
775 struct data__file
*d
;
776 int ret
= -EINVAL
, i
;
778 data__for_each_file(i
, d
) {
779 d
->session
= perf_session__new(&d
->file
, false, &tool
);
781 pr_err("Failed to open %s\n", d
->file
.path
);
786 ret
= perf_session__process_events(d
->session
);
788 pr_err("Failed to process %s\n", d
->file
.path
);
792 perf_evlist__collapse_resort(d
->session
->evlist
);
798 data__for_each_file(i
, d
) {
799 perf_session__delete(d
->session
);
807 static const char * const diff_usage
[] = {
808 "perf diff [<options>] [old_file] [new_file]",
812 static const struct option options
[] = {
813 OPT_INCR('v', "verbose", &verbose
,
814 "be more verbose (show symbol address, etc)"),
815 OPT_BOOLEAN('q', "quiet", &quiet
, "Do not show any message"),
816 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only
,
817 "Show only items with match in baseline"),
818 OPT_CALLBACK('c', "compute", &compute
,
819 "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs)",
820 "Entries differential computation selection",
822 OPT_BOOLEAN('p', "period", &show_period
,
823 "Show period values."),
824 OPT_BOOLEAN('F', "formula", &show_formula
,
826 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
827 "dump raw trace in ASCII"),
828 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
829 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
830 "file", "kallsyms pathname"),
831 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
832 "load module symbols - WARNING: use only with -k and LIVE kernel"),
833 OPT_STRING('d', "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
834 "only consider symbols in these dsos"),
835 OPT_STRING('C', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
836 "only consider symbols in these comms"),
837 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
838 "only consider these symbols"),
839 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
840 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
841 " Please refer the man page for the complete list."),
842 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf
.field_sep
, "separator",
843 "separator for columns, no spaces will be added between "
844 "columns '.' is reserved."),
845 OPT_CALLBACK(0, "symfs", NULL
, "directory",
846 "Look for files with symbols relative to this directory",
847 symbol__config_symfs
),
848 OPT_UINTEGER('o', "order", &sort_compute
, "Specify compute sorting."),
849 OPT_CALLBACK(0, "percentage", NULL
, "relative|absolute",
850 "How to display percentage of filtered entries", parse_filter_percentage
),
854 static double baseline_percent(struct hist_entry
*he
)
856 u64 total
= hists__total_period(he
->hists
);
858 return 100.0 * he
->stat
.period
/ total
;
861 static int hpp__color_baseline(struct perf_hpp_fmt
*fmt
,
862 struct perf_hpp
*hpp
, struct hist_entry
*he
)
864 struct diff_hpp_fmt
*dfmt
=
865 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
866 double percent
= baseline_percent(he
);
870 scnprintf(pfmt
, 20, "%%%d.2f%%%%", dfmt
->header_width
- 1);
871 return percent_color_snprintf(hpp
->buf
, hpp
->size
,
874 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
875 dfmt
->header_width
, pfmt
);
878 static int hpp__entry_baseline(struct hist_entry
*he
, char *buf
, size_t size
)
880 double percent
= baseline_percent(he
);
881 const char *fmt
= symbol_conf
.field_sep
? "%.2f" : "%6.2f%%";
885 ret
= scnprintf(buf
, size
, fmt
, percent
);
890 static int __hpp__color_compare(struct perf_hpp_fmt
*fmt
,
891 struct perf_hpp
*hpp
, struct hist_entry
*he
,
892 int comparison_method
)
894 struct diff_hpp_fmt
*dfmt
=
895 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
896 struct hist_entry
*pair
= get_pair_fmt(he
, dfmt
);
904 switch (comparison_method
) {
906 if (pair
->diff
.computed
)
907 diff
= pair
->diff
.period_ratio_delta
;
909 diff
= compute_delta(he
, pair
);
911 scnprintf(pfmt
, 20, "%%%+d.2f%%%%", dfmt
->header_width
- 1);
912 return percent_color_snprintf(hpp
->buf
, hpp
->size
,
917 if (pair
->diff
.computed
)
918 diff
= pair
->diff
.period_ratio
;
920 diff
= compute_ratio(he
, pair
);
922 scnprintf(pfmt
, 20, "%%%d.6f", dfmt
->header_width
);
923 return value_color_snprintf(hpp
->buf
, hpp
->size
,
925 case COMPUTE_WEIGHTED_DIFF
:
928 if (pair
->diff
.computed
)
929 wdiff
= pair
->diff
.wdiff
;
931 wdiff
= compute_wdiff(he
, pair
);
933 scnprintf(pfmt
, 20, "%%14ld", dfmt
->header_width
);
934 return color_snprintf(hpp
->buf
, hpp
->size
,
935 get_percent_color(wdiff
),
941 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
942 dfmt
->header_width
, "N/A");
944 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
945 dfmt
->header_width
, pfmt
);
948 static int hpp__color_delta(struct perf_hpp_fmt
*fmt
,
949 struct perf_hpp
*hpp
, struct hist_entry
*he
)
951 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_DELTA
);
954 static int hpp__color_ratio(struct perf_hpp_fmt
*fmt
,
955 struct perf_hpp
*hpp
, struct hist_entry
*he
)
957 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_RATIO
);
960 static int hpp__color_wdiff(struct perf_hpp_fmt
*fmt
,
961 struct perf_hpp
*hpp
, struct hist_entry
*he
)
963 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_WEIGHTED_DIFF
);
967 hpp__entry_unpair(struct hist_entry
*he
, int idx
, char *buf
, size_t size
)
970 case PERF_HPP_DIFF__PERIOD_BASELINE
:
971 scnprintf(buf
, size
, "%" PRIu64
, he
->stat
.period
);
980 hpp__entry_pair(struct hist_entry
*he
, struct hist_entry
*pair
,
981 int idx
, char *buf
, size_t size
)
988 case PERF_HPP_DIFF__DELTA
:
989 case PERF_HPP_DIFF__DELTA_ABS
:
990 if (pair
->diff
.computed
)
991 diff
= pair
->diff
.period_ratio_delta
;
993 diff
= compute_delta(he
, pair
);
995 scnprintf(buf
, size
, "%+4.2F%%", diff
);
998 case PERF_HPP_DIFF__RATIO
:
999 /* No point for ratio number if we are dummy.. */
1001 scnprintf(buf
, size
, "N/A");
1005 if (pair
->diff
.computed
)
1006 ratio
= pair
->diff
.period_ratio
;
1008 ratio
= compute_ratio(he
, pair
);
1011 scnprintf(buf
, size
, "%14.6F", ratio
);
1014 case PERF_HPP_DIFF__WEIGHTED_DIFF
:
1015 /* No point for wdiff number if we are dummy.. */
1017 scnprintf(buf
, size
, "N/A");
1021 if (pair
->diff
.computed
)
1022 wdiff
= pair
->diff
.wdiff
;
1024 wdiff
= compute_wdiff(he
, pair
);
1027 scnprintf(buf
, size
, "%14ld", wdiff
);
1030 case PERF_HPP_DIFF__FORMULA
:
1031 formula_fprintf(he
, pair
, buf
, size
);
1034 case PERF_HPP_DIFF__PERIOD
:
1035 scnprintf(buf
, size
, "%" PRIu64
, pair
->stat
.period
);
1044 __hpp__entry_global(struct hist_entry
*he
, struct diff_hpp_fmt
*dfmt
,
1045 char *buf
, size_t size
)
1047 struct hist_entry
*pair
= get_pair_fmt(he
, dfmt
);
1048 int idx
= dfmt
->idx
;
1050 /* baseline is special */
1051 if (idx
== PERF_HPP_DIFF__BASELINE
)
1052 hpp__entry_baseline(he
, buf
, size
);
1055 hpp__entry_pair(he
, pair
, idx
, buf
, size
);
1057 hpp__entry_unpair(he
, idx
, buf
, size
);
1061 static int hpp__entry_global(struct perf_hpp_fmt
*_fmt
, struct perf_hpp
*hpp
,
1062 struct hist_entry
*he
)
1064 struct diff_hpp_fmt
*dfmt
=
1065 container_of(_fmt
, struct diff_hpp_fmt
, fmt
);
1066 char buf
[MAX_COL_WIDTH
] = " ";
1068 __hpp__entry_global(he
, dfmt
, buf
, MAX_COL_WIDTH
);
1070 if (symbol_conf
.field_sep
)
1071 return scnprintf(hpp
->buf
, hpp
->size
, "%s", buf
);
1073 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
1074 dfmt
->header_width
, buf
);
1077 static int hpp__header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1078 struct hists
*hists __maybe_unused
,
1079 int line __maybe_unused
,
1080 int *span __maybe_unused
)
1082 struct diff_hpp_fmt
*dfmt
=
1083 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
1085 BUG_ON(!dfmt
->header
);
1086 return scnprintf(hpp
->buf
, hpp
->size
, dfmt
->header
);
1089 static int hpp__width(struct perf_hpp_fmt
*fmt
,
1090 struct perf_hpp
*hpp __maybe_unused
,
1091 struct hists
*hists __maybe_unused
)
1093 struct diff_hpp_fmt
*dfmt
=
1094 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
1096 BUG_ON(dfmt
->header_width
<= 0);
1097 return dfmt
->header_width
;
1100 static void init_header(struct data__file
*d
, struct diff_hpp_fmt
*dfmt
)
1102 #define MAX_HEADER_NAME 100
1103 char buf_indent
[MAX_HEADER_NAME
];
1104 char buf
[MAX_HEADER_NAME
];
1105 const char *header
= NULL
;
1108 BUG_ON(dfmt
->idx
>= PERF_HPP_DIFF__MAX_INDEX
);
1109 header
= columns
[dfmt
->idx
].name
;
1110 width
= columns
[dfmt
->idx
].width
;
1112 /* Only our defined HPP fmts should appear here. */
1115 if (data__files_cnt
> 2)
1116 scnprintf(buf
, MAX_HEADER_NAME
, "%s/%d", header
, d
->idx
);
1118 #define NAME (data__files_cnt > 2 ? buf : header)
1119 dfmt
->header_width
= width
;
1120 width
= (int) strlen(NAME
);
1121 if (dfmt
->header_width
< width
)
1122 dfmt
->header_width
= width
;
1124 scnprintf(buf_indent
, MAX_HEADER_NAME
, "%*s",
1125 dfmt
->header_width
, NAME
);
1127 dfmt
->header
= strdup(buf_indent
);
1128 #undef MAX_HEADER_NAME
1132 static void data__hpp_register(struct data__file
*d
, int idx
)
1134 struct diff_hpp_fmt
*dfmt
= &d
->fmt
[idx
];
1135 struct perf_hpp_fmt
*fmt
= &dfmt
->fmt
;
1139 fmt
->header
= hpp__header
;
1140 fmt
->width
= hpp__width
;
1141 fmt
->entry
= hpp__entry_global
;
1142 fmt
->cmp
= hist_entry__cmp_nop
;
1143 fmt
->collapse
= hist_entry__cmp_nop
;
1145 /* TODO more colors */
1147 case PERF_HPP_DIFF__BASELINE
:
1148 fmt
->color
= hpp__color_baseline
;
1149 fmt
->sort
= hist_entry__cmp_baseline
;
1151 case PERF_HPP_DIFF__DELTA
:
1152 fmt
->color
= hpp__color_delta
;
1153 fmt
->sort
= hist_entry__cmp_delta
;
1155 case PERF_HPP_DIFF__RATIO
:
1156 fmt
->color
= hpp__color_ratio
;
1157 fmt
->sort
= hist_entry__cmp_ratio
;
1159 case PERF_HPP_DIFF__WEIGHTED_DIFF
:
1160 fmt
->color
= hpp__color_wdiff
;
1161 fmt
->sort
= hist_entry__cmp_wdiff
;
1163 case PERF_HPP_DIFF__DELTA_ABS
:
1164 fmt
->color
= hpp__color_delta
;
1165 fmt
->sort
= hist_entry__cmp_delta_abs
;
1168 fmt
->sort
= hist_entry__cmp_nop
;
1172 init_header(d
, dfmt
);
1173 perf_hpp__column_register(fmt
);
1174 perf_hpp__register_sort_field(fmt
);
1177 static int ui_init(void)
1179 struct data__file
*d
;
1180 struct perf_hpp_fmt
*fmt
;
1183 data__for_each_file(i
, d
) {
1186 * Baseline or compute realted columns:
1188 * PERF_HPP_DIFF__BASELINE
1189 * PERF_HPP_DIFF__DELTA
1190 * PERF_HPP_DIFF__RATIO
1191 * PERF_HPP_DIFF__WEIGHTED_DIFF
1193 data__hpp_register(d
, i
? compute_2_hpp
[compute
] :
1194 PERF_HPP_DIFF__BASELINE
);
1199 * PERF_HPP_DIFF__FORMULA
1200 * PERF_HPP_DIFF__PERIOD
1201 * PERF_HPP_DIFF__PERIOD_BASELINE
1203 if (show_formula
&& i
)
1204 data__hpp_register(d
, PERF_HPP_DIFF__FORMULA
);
1207 data__hpp_register(d
, i
? PERF_HPP_DIFF__PERIOD
:
1208 PERF_HPP_DIFF__PERIOD_BASELINE
);
1215 * Prepend an fmt to sort on columns at 'sort_compute' first.
1216 * This fmt is added only to the sort list but not to the
1217 * output fields list.
1219 * Note that this column (data) can be compared twice - one
1220 * for this 'sort_compute' fmt and another for the normal
1221 * diff_hpp_fmt. But it shouldn't a problem as most entries
1222 * will be sorted out by first try or baseline and comparing
1223 * is not a costly operation.
1225 fmt
= zalloc(sizeof(*fmt
));
1227 pr_err("Memory allocation failed\n");
1231 fmt
->cmp
= hist_entry__cmp_nop
;
1232 fmt
->collapse
= hist_entry__cmp_nop
;
1236 fmt
->sort
= hist_entry__cmp_delta_idx
;
1239 fmt
->sort
= hist_entry__cmp_ratio_idx
;
1241 case COMPUTE_WEIGHTED_DIFF
:
1242 fmt
->sort
= hist_entry__cmp_wdiff_idx
;
1244 case COMPUTE_DELTA_ABS
:
1245 fmt
->sort
= hist_entry__cmp_delta_abs_idx
;
1251 perf_hpp__prepend_sort_field(fmt
);
1255 static int data_init(int argc
, const char **argv
)
1257 struct data__file
*d
;
1258 static const char *defaults
[] = {
1262 bool use_default
= true;
1265 data__files_cnt
= 2;
1269 defaults
[1] = argv
[0];
1271 data__files_cnt
= argc
;
1272 use_default
= false;
1274 } else if (perf_guest
) {
1275 defaults
[0] = "perf.data.host";
1276 defaults
[1] = "perf.data.guest";
1279 if (sort_compute
>= (unsigned int) data__files_cnt
) {
1280 pr_err("Order option out of limit.\n");
1284 data__files
= zalloc(sizeof(*data__files
) * data__files_cnt
);
1288 data__for_each_file(i
, d
) {
1289 struct perf_data_file
*file
= &d
->file
;
1291 file
->path
= use_default
? defaults
[i
] : argv
[i
];
1292 file
->mode
= PERF_DATA_MODE_READ
,
1293 file
->force
= force
,
1301 static int diff__config(const char *var
, const char *value
,
1302 void *cb __maybe_unused
)
1304 if (!strcmp(var
, "diff.order")) {
1306 if (perf_config_int(&ret
, var
, value
) < 0)
1311 if (!strcmp(var
, "diff.compute")) {
1312 if (!strcmp(value
, "delta")) {
1313 compute
= COMPUTE_DELTA
;
1314 } else if (!strcmp(value
, "delta-abs")) {
1315 compute
= COMPUTE_DELTA_ABS
;
1316 } else if (!strcmp(value
, "ratio")) {
1317 compute
= COMPUTE_RATIO
;
1318 } else if (!strcmp(value
, "wdiff")) {
1319 compute
= COMPUTE_WEIGHTED_DIFF
;
1321 pr_err("Invalid compute method: %s\n", value
);
1329 int cmd_diff(int argc
, const char **argv
)
1331 int ret
= hists__init();
1336 perf_config(diff__config
, NULL
);
1338 argc
= parse_options(argc
, argv
, options
, diff_usage
, 0);
1341 perf_quiet_option();
1343 if (symbol__init(NULL
) < 0)
1346 if (data_init(argc
, argv
) < 0)
1352 sort__mode
= SORT_MODE__DIFF
;
1354 if (setup_sorting(NULL
) < 0)
1355 usage_with_options(diff_usage
, options
);
1359 sort__setup_elide(NULL
);
1361 return __cmd_diff();