1 // SPDX-License-Identifier: GPL-2.0
5 * Builtin diff command: Analyze two perf.data input files, look up and read
6 * DSOs and symbol information, sort them and produce a diff.
10 #include "util/debug.h"
11 #include "util/event.h"
12 #include "util/hist.h"
13 #include "util/evsel.h"
14 #include "util/evlist.h"
15 #include "util/session.h"
16 #include "util/tool.h"
17 #include "util/sort.h"
18 #include "util/symbol.h"
19 #include "util/util.h"
20 #include "util/data.h"
21 #include "util/config.h"
22 #include "util/time-utils.h"
30 struct perf_tool tool
;
32 struct perf_time_interval
*ptime_range
;
37 /* Diff command specific HPP columns. */
39 PERF_HPP_DIFF__BASELINE
,
40 PERF_HPP_DIFF__PERIOD
,
41 PERF_HPP_DIFF__PERIOD_BASELINE
,
44 PERF_HPP_DIFF__WEIGHTED_DIFF
,
45 PERF_HPP_DIFF__FORMULA
,
46 PERF_HPP_DIFF__DELTA_ABS
,
48 PERF_HPP_DIFF__MAX_INDEX
52 struct perf_hpp_fmt fmt
;
59 struct perf_session
*session
;
60 struct perf_data data
;
63 struct diff_hpp_fmt fmt
[PERF_HPP_DIFF__MAX_INDEX
];
66 static struct data__file
*data__files
;
67 static int data__files_cnt
;
69 #define data__for_each_file_start(i, d, s) \
70 for (i = s, d = &data__files[s]; \
71 i < data__files_cnt; \
72 i++, d = &data__files[i])
74 #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
75 #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
78 static bool show_period
;
79 static bool show_formula
;
80 static bool show_baseline_only
;
81 static unsigned int sort_compute
= 1;
83 static s64 compute_wdiff_w1
;
84 static s64 compute_wdiff_w2
;
86 static const char *cpu_list
;
87 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
92 COMPUTE_WEIGHTED_DIFF
,
97 const char *compute_names
[COMPUTE_MAX
] = {
98 [COMPUTE_DELTA
] = "delta",
99 [COMPUTE_DELTA_ABS
] = "delta-abs",
100 [COMPUTE_RATIO
] = "ratio",
101 [COMPUTE_WEIGHTED_DIFF
] = "wdiff",
104 static int compute
= COMPUTE_DELTA_ABS
;
106 static int compute_2_hpp
[COMPUTE_MAX
] = {
107 [COMPUTE_DELTA
] = PERF_HPP_DIFF__DELTA
,
108 [COMPUTE_DELTA_ABS
] = PERF_HPP_DIFF__DELTA_ABS
,
109 [COMPUTE_RATIO
] = PERF_HPP_DIFF__RATIO
,
110 [COMPUTE_WEIGHTED_DIFF
] = PERF_HPP_DIFF__WEIGHTED_DIFF
,
113 #define MAX_COL_WIDTH 70
115 static struct header_column
{
118 } columns
[PERF_HPP_DIFF__MAX_INDEX
] = {
119 [PERF_HPP_DIFF__BASELINE
] = {
122 [PERF_HPP_DIFF__PERIOD
] = {
126 [PERF_HPP_DIFF__PERIOD_BASELINE
] = {
127 .name
= "Base period",
130 [PERF_HPP_DIFF__DELTA
] = {
134 [PERF_HPP_DIFF__DELTA_ABS
] = {
138 [PERF_HPP_DIFF__RATIO
] = {
142 [PERF_HPP_DIFF__WEIGHTED_DIFF
] = {
143 .name
= "Weighted diff",
146 [PERF_HPP_DIFF__FORMULA
] = {
148 .width
= MAX_COL_WIDTH
,
152 static int setup_compute_opt_wdiff(char *opt
)
162 w2_str
= strchr(opt
, ',');
170 compute_wdiff_w1
= strtol(w1_str
, NULL
, 10);
171 compute_wdiff_w2
= strtol(w2_str
, NULL
, 10);
173 if (!compute_wdiff_w1
|| !compute_wdiff_w2
)
176 pr_debug("compute wdiff w1(%" PRId64
") w2(%" PRId64
")\n",
177 compute_wdiff_w1
, compute_wdiff_w2
);
183 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
188 static int setup_compute_opt(char *opt
)
190 if (compute
== COMPUTE_WEIGHTED_DIFF
)
191 return setup_compute_opt_wdiff(opt
);
194 pr_err("Failed: extra option specified '%s'", opt
);
201 static int setup_compute(const struct option
*opt
, const char *str
,
202 int unset __maybe_unused
)
204 int *cp
= (int *) opt
->value
;
205 char *cstr
= (char *) str
;
215 option
= strchr(str
, ':');
217 unsigned len
= option
++ - str
;
220 * The str data are not writeable, so we need
221 * to use another buffer.
224 /* No option value is longer. */
225 if (len
>= sizeof(buf
))
228 strncpy(buf
, str
, len
);
233 for (i
= 0; i
< COMPUTE_MAX
; i
++)
234 if (!strcmp(cstr
, compute_names
[i
])) {
236 return setup_compute_opt(option
);
239 pr_err("Failed: '%s' is not computation method "
240 "(use 'delta','ratio' or 'wdiff')\n", str
);
244 static double period_percent(struct hist_entry
*he
, u64 period
)
246 u64 total
= hists__total_period(he
->hists
);
248 return (period
* 100.0) / total
;
251 static double compute_delta(struct hist_entry
*he
, struct hist_entry
*pair
)
253 double old_percent
= period_percent(he
, he
->stat
.period
);
254 double new_percent
= period_percent(pair
, pair
->stat
.period
);
256 pair
->diff
.period_ratio_delta
= new_percent
- old_percent
;
257 pair
->diff
.computed
= true;
258 return pair
->diff
.period_ratio_delta
;
261 static double compute_ratio(struct hist_entry
*he
, struct hist_entry
*pair
)
263 double old_period
= he
->stat
.period
?: 1;
264 double new_period
= pair
->stat
.period
;
266 pair
->diff
.computed
= true;
267 pair
->diff
.period_ratio
= new_period
/ old_period
;
268 return pair
->diff
.period_ratio
;
271 static s64
compute_wdiff(struct hist_entry
*he
, struct hist_entry
*pair
)
273 u64 old_period
= he
->stat
.period
;
274 u64 new_period
= pair
->stat
.period
;
276 pair
->diff
.computed
= true;
277 pair
->diff
.wdiff
= new_period
* compute_wdiff_w2
-
278 old_period
* compute_wdiff_w1
;
280 return pair
->diff
.wdiff
;
283 static int formula_delta(struct hist_entry
*he
, struct hist_entry
*pair
,
284 char *buf
, size_t size
)
286 u64 he_total
= he
->hists
->stats
.total_period
;
287 u64 pair_total
= pair
->hists
->stats
.total_period
;
289 if (symbol_conf
.filter_relative
) {
290 he_total
= he
->hists
->stats
.total_non_filtered_period
;
291 pair_total
= pair
->hists
->stats
.total_non_filtered_period
;
293 return scnprintf(buf
, size
,
294 "(%" PRIu64
" * 100 / %" PRIu64
") - "
295 "(%" PRIu64
" * 100 / %" PRIu64
")",
296 pair
->stat
.period
, pair_total
,
297 he
->stat
.period
, he_total
);
300 static int formula_ratio(struct hist_entry
*he
, struct hist_entry
*pair
,
301 char *buf
, size_t size
)
303 double old_period
= he
->stat
.period
;
304 double new_period
= pair
->stat
.period
;
306 return scnprintf(buf
, size
, "%.0F / %.0F", new_period
, old_period
);
309 static int formula_wdiff(struct hist_entry
*he
, struct hist_entry
*pair
,
310 char *buf
, size_t size
)
312 u64 old_period
= he
->stat
.period
;
313 u64 new_period
= pair
->stat
.period
;
315 return scnprintf(buf
, size
,
316 "(%" PRIu64
" * " "%" PRId64
") - (%" PRIu64
" * " "%" PRId64
")",
317 new_period
, compute_wdiff_w2
, old_period
, compute_wdiff_w1
);
320 static int formula_fprintf(struct hist_entry
*he
, struct hist_entry
*pair
,
321 char *buf
, size_t size
)
325 case COMPUTE_DELTA_ABS
:
326 return formula_delta(he
, pair
, buf
, size
);
328 return formula_ratio(he
, pair
, buf
, size
);
329 case COMPUTE_WEIGHTED_DIFF
:
330 return formula_wdiff(he
, pair
, buf
, size
);
338 static int diff__process_sample_event(struct perf_tool
*tool
,
339 union perf_event
*event
,
340 struct perf_sample
*sample
,
341 struct perf_evsel
*evsel
,
342 struct machine
*machine
)
344 struct perf_diff
*pdiff
= container_of(tool
, struct perf_diff
, tool
);
345 struct addr_location al
;
346 struct hists
*hists
= evsel__hists(evsel
);
349 if (perf_time__ranges_skip_sample(pdiff
->ptime_range
, pdiff
->range_num
,
354 if (machine__resolve(machine
, &al
, sample
) < 0) {
355 pr_warning("problem processing %d event, skipping it.\n",
360 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
)) {
365 if (!hists__add_entry(hists
, &al
, NULL
, NULL
, NULL
, sample
, true)) {
366 pr_warning("problem incrementing symbol period, skipping event\n");
371 * The total_period is updated here before going to the output
372 * tree since normally only the baseline hists will call
373 * hists__output_resort() and precompute needs the total
374 * period in order to sort entries by percentage delta.
376 hists
->stats
.total_period
+= sample
->period
;
378 hists
->stats
.total_non_filtered_period
+= sample
->period
;
381 addr_location__put(&al
);
385 static struct perf_diff pdiff
= {
387 .sample
= diff__process_sample_event
,
388 .mmap
= perf_event__process_mmap
,
389 .mmap2
= perf_event__process_mmap2
,
390 .comm
= perf_event__process_comm
,
391 .exit
= perf_event__process_exit
,
392 .fork
= perf_event__process_fork
,
393 .lost
= perf_event__process_lost
,
394 .namespaces
= perf_event__process_namespaces
,
395 .ordered_events
= true,
396 .ordering_requires_timestamps
= true,
400 static struct perf_evsel
*evsel_match(struct perf_evsel
*evsel
,
401 struct perf_evlist
*evlist
)
403 struct perf_evsel
*e
;
405 evlist__for_each_entry(evlist
, e
) {
406 if (perf_evsel__match2(evsel
, e
))
413 static void perf_evlist__collapse_resort(struct perf_evlist
*evlist
)
415 struct perf_evsel
*evsel
;
417 evlist__for_each_entry(evlist
, evsel
) {
418 struct hists
*hists
= evsel__hists(evsel
);
420 hists__collapse_resort(hists
, NULL
);
424 static struct data__file
*fmt_to_data_file(struct perf_hpp_fmt
*fmt
)
426 struct diff_hpp_fmt
*dfmt
= container_of(fmt
, struct diff_hpp_fmt
, fmt
);
427 void *ptr
= dfmt
- dfmt
->idx
;
428 struct data__file
*d
= container_of(ptr
, struct data__file
, fmt
);
433 static struct hist_entry
*
434 get_pair_data(struct hist_entry
*he
, struct data__file
*d
)
436 if (hist_entry__has_pairs(he
)) {
437 struct hist_entry
*pair
;
439 list_for_each_entry(pair
, &he
->pairs
.head
, pairs
.node
)
440 if (pair
->hists
== d
->hists
)
447 static struct hist_entry
*
448 get_pair_fmt(struct hist_entry
*he
, struct diff_hpp_fmt
*dfmt
)
450 struct data__file
*d
= fmt_to_data_file(&dfmt
->fmt
);
452 return get_pair_data(he
, d
);
455 static void hists__baseline_only(struct hists
*hists
)
457 struct rb_root_cached
*root
;
458 struct rb_node
*next
;
460 if (hists__has(hists
, need_collapse
))
461 root
= &hists
->entries_collapsed
;
463 root
= hists
->entries_in
;
465 next
= rb_first_cached(root
);
466 while (next
!= NULL
) {
467 struct hist_entry
*he
= rb_entry(next
, struct hist_entry
, rb_node_in
);
469 next
= rb_next(&he
->rb_node_in
);
470 if (!hist_entry__next_pair(he
)) {
471 rb_erase_cached(&he
->rb_node_in
, root
);
472 hist_entry__delete(he
);
477 static void hists__precompute(struct hists
*hists
)
479 struct rb_root_cached
*root
;
480 struct rb_node
*next
;
482 if (hists__has(hists
, need_collapse
))
483 root
= &hists
->entries_collapsed
;
485 root
= hists
->entries_in
;
487 next
= rb_first_cached(root
);
488 while (next
!= NULL
) {
489 struct hist_entry
*he
, *pair
;
490 struct data__file
*d
;
493 he
= rb_entry(next
, struct hist_entry
, rb_node_in
);
494 next
= rb_next(&he
->rb_node_in
);
496 data__for_each_file_new(i
, d
) {
497 pair
= get_pair_data(he
, d
);
503 case COMPUTE_DELTA_ABS
:
504 compute_delta(he
, pair
);
507 compute_ratio(he
, pair
);
509 case COMPUTE_WEIGHTED_DIFF
:
510 compute_wdiff(he
, pair
);
519 static int64_t cmp_doubles(double l
, double r
)
530 __hist_entry__cmp_compute(struct hist_entry
*left
, struct hist_entry
*right
,
536 double l
= left
->diff
.period_ratio_delta
;
537 double r
= right
->diff
.period_ratio_delta
;
539 return cmp_doubles(l
, r
);
541 case COMPUTE_DELTA_ABS
:
543 double l
= fabs(left
->diff
.period_ratio_delta
);
544 double r
= fabs(right
->diff
.period_ratio_delta
);
546 return cmp_doubles(l
, r
);
550 double l
= left
->diff
.period_ratio
;
551 double r
= right
->diff
.period_ratio
;
553 return cmp_doubles(l
, r
);
555 case COMPUTE_WEIGHTED_DIFF
:
557 s64 l
= left
->diff
.wdiff
;
558 s64 r
= right
->diff
.wdiff
;
570 hist_entry__cmp_compute(struct hist_entry
*left
, struct hist_entry
*right
,
573 bool pairs_left
= hist_entry__has_pairs(left
);
574 bool pairs_right
= hist_entry__has_pairs(right
);
575 struct hist_entry
*p_right
, *p_left
;
577 if (!pairs_left
&& !pairs_right
)
580 if (!pairs_left
|| !pairs_right
)
581 return pairs_left
? -1 : 1;
583 p_left
= get_pair_data(left
, &data__files
[sort_idx
]);
584 p_right
= get_pair_data(right
, &data__files
[sort_idx
]);
586 if (!p_left
&& !p_right
)
589 if (!p_left
|| !p_right
)
590 return p_left
? -1 : 1;
593 * We have 2 entries of same kind, let's
594 * make the data comparison.
596 return __hist_entry__cmp_compute(p_left
, p_right
, c
);
600 hist_entry__cmp_compute_idx(struct hist_entry
*left
, struct hist_entry
*right
,
603 struct hist_entry
*p_right
, *p_left
;
605 p_left
= get_pair_data(left
, &data__files
[sort_idx
]);
606 p_right
= get_pair_data(right
, &data__files
[sort_idx
]);
608 if (!p_left
&& !p_right
)
611 if (!p_left
|| !p_right
)
612 return p_left
? -1 : 1;
614 if (c
!= COMPUTE_DELTA
&& c
!= COMPUTE_DELTA_ABS
) {
616 * The delta can be computed without the baseline, but
617 * others are not. Put those entries which have no
620 if (left
->dummy
&& right
->dummy
)
623 if (left
->dummy
|| right
->dummy
)
624 return left
->dummy
? 1 : -1;
627 return __hist_entry__cmp_compute(p_left
, p_right
, c
);
631 hist_entry__cmp_nop(struct perf_hpp_fmt
*fmt __maybe_unused
,
632 struct hist_entry
*left __maybe_unused
,
633 struct hist_entry
*right __maybe_unused
)
639 hist_entry__cmp_baseline(struct perf_hpp_fmt
*fmt __maybe_unused
,
640 struct hist_entry
*left
, struct hist_entry
*right
)
642 if (left
->stat
.period
== right
->stat
.period
)
644 return left
->stat
.period
> right
->stat
.period
? 1 : -1;
648 hist_entry__cmp_delta(struct perf_hpp_fmt
*fmt
,
649 struct hist_entry
*left
, struct hist_entry
*right
)
651 struct data__file
*d
= fmt_to_data_file(fmt
);
653 return hist_entry__cmp_compute(right
, left
, COMPUTE_DELTA
, d
->idx
);
657 hist_entry__cmp_delta_abs(struct perf_hpp_fmt
*fmt
,
658 struct hist_entry
*left
, struct hist_entry
*right
)
660 struct data__file
*d
= fmt_to_data_file(fmt
);
662 return hist_entry__cmp_compute(right
, left
, COMPUTE_DELTA_ABS
, d
->idx
);
666 hist_entry__cmp_ratio(struct perf_hpp_fmt
*fmt
,
667 struct hist_entry
*left
, struct hist_entry
*right
)
669 struct data__file
*d
= fmt_to_data_file(fmt
);
671 return hist_entry__cmp_compute(right
, left
, COMPUTE_RATIO
, d
->idx
);
675 hist_entry__cmp_wdiff(struct perf_hpp_fmt
*fmt
,
676 struct hist_entry
*left
, struct hist_entry
*right
)
678 struct data__file
*d
= fmt_to_data_file(fmt
);
680 return hist_entry__cmp_compute(right
, left
, COMPUTE_WEIGHTED_DIFF
, d
->idx
);
684 hist_entry__cmp_delta_idx(struct perf_hpp_fmt
*fmt __maybe_unused
,
685 struct hist_entry
*left
, struct hist_entry
*right
)
687 return hist_entry__cmp_compute_idx(right
, left
, COMPUTE_DELTA
,
692 hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt
*fmt __maybe_unused
,
693 struct hist_entry
*left
, struct hist_entry
*right
)
695 return hist_entry__cmp_compute_idx(right
, left
, COMPUTE_DELTA_ABS
,
700 hist_entry__cmp_ratio_idx(struct perf_hpp_fmt
*fmt __maybe_unused
,
701 struct hist_entry
*left
, struct hist_entry
*right
)
703 return hist_entry__cmp_compute_idx(right
, left
, COMPUTE_RATIO
,
708 hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt
*fmt __maybe_unused
,
709 struct hist_entry
*left
, struct hist_entry
*right
)
711 return hist_entry__cmp_compute_idx(right
, left
, COMPUTE_WEIGHTED_DIFF
,
715 static void hists__process(struct hists
*hists
)
717 if (show_baseline_only
)
718 hists__baseline_only(hists
);
720 hists__precompute(hists
);
721 hists__output_resort(hists
, NULL
);
723 hists__fprintf(hists
, !quiet
, 0, 0, 0, stdout
,
724 !symbol_conf
.use_callchain
);
727 static void data__fprintf(void)
729 struct data__file
*d
;
732 fprintf(stdout
, "# Data files:\n");
734 data__for_each_file(i
, d
)
735 fprintf(stdout
, "# [%d] %s %s\n",
736 d
->idx
, d
->data
.path
,
737 !d
->idx
? "(Baseline)" : "");
739 fprintf(stdout
, "#\n");
742 static void data_process(void)
744 struct perf_evlist
*evlist_base
= data__files
[0].session
->evlist
;
745 struct perf_evsel
*evsel_base
;
748 evlist__for_each_entry(evlist_base
, evsel_base
) {
749 struct hists
*hists_base
= evsel__hists(evsel_base
);
750 struct data__file
*d
;
753 data__for_each_file_new(i
, d
) {
754 struct perf_evlist
*evlist
= d
->session
->evlist
;
755 struct perf_evsel
*evsel
;
758 evsel
= evsel_match(evsel_base
, evlist
);
762 hists
= evsel__hists(evsel
);
765 hists__match(hists_base
, hists
);
767 if (!show_baseline_only
)
768 hists__link(hists_base
, hists
);
772 fprintf(stdout
, "%s# Event '%s'\n#\n", first
? "" : "\n",
773 perf_evsel__name(evsel_base
));
778 if (verbose
> 0 || ((data__files_cnt
> 2) && !quiet
))
781 /* Don't sort callchain for perf diff */
782 perf_evsel__reset_sample_bit(evsel_base
, CALLCHAIN
);
784 hists__process(hists_base
);
788 static void data__free(struct data__file
*d
)
792 for (col
= 0; col
< PERF_HPP_DIFF__MAX_INDEX
; col
++) {
793 struct diff_hpp_fmt
*fmt
= &d
->fmt
[col
];
799 static int abstime_str_dup(char **pstr
)
803 if (pdiff
.time_str
&& strchr(pdiff
.time_str
, ':')) {
804 str
= strdup(pdiff
.time_str
);
813 static int parse_absolute_time(struct data__file
*d
, char **pstr
)
819 * Absolute timestamp for one file has the format: a.b,c.d
820 * For multiple files, the format is: a.b,c.d:a.b,c.d
822 p
= strchr(*pstr
, ':');
825 pr_err("Invalid time string\n");
832 pr_err("Invalid time string\n");
837 ret
= perf_time__parse_for_ranges(*pstr
, d
->session
,
852 static int parse_percent_time(struct data__file
*d
)
856 ret
= perf_time__parse_for_ranges(pdiff
.time_str
, d
->session
,
863 static int parse_time_str(struct data__file
*d
, char *abstime_ostr
,
869 ret
= parse_absolute_time(d
, pabstime_tmp
);
870 else if (pdiff
.time_str
)
871 ret
= parse_percent_time(d
);
876 static int __cmd_diff(void)
878 struct data__file
*d
;
880 char *abstime_ostr
, *abstime_tmp
;
882 ret
= abstime_str_dup(&abstime_ostr
);
886 abstime_tmp
= abstime_ostr
;
889 data__for_each_file(i
, d
) {
890 d
->session
= perf_session__new(&d
->data
, false, &pdiff
.tool
);
892 pr_err("Failed to open %s\n", d
->data
.path
);
897 if (pdiff
.time_str
) {
898 ret
= parse_time_str(d
, abstime_ostr
, &abstime_tmp
);
904 ret
= perf_session__cpu_bitmap(d
->session
, cpu_list
,
910 ret
= perf_session__process_events(d
->session
);
912 pr_err("Failed to process %s\n", d
->data
.path
);
916 perf_evlist__collapse_resort(d
->session
->evlist
);
918 if (pdiff
.ptime_range
)
919 zfree(&pdiff
.ptime_range
);
925 data__for_each_file(i
, d
) {
926 perf_session__delete(d
->session
);
932 if (pdiff
.ptime_range
)
933 zfree(&pdiff
.ptime_range
);
941 static const char * const diff_usage
[] = {
942 "perf diff [<options>] [old_file] [new_file]",
946 static const struct option options
[] = {
947 OPT_INCR('v', "verbose", &verbose
,
948 "be more verbose (show symbol address, etc)"),
949 OPT_BOOLEAN('q', "quiet", &quiet
, "Do not show any message"),
950 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only
,
951 "Show only items with match in baseline"),
952 OPT_CALLBACK('c', "compute", &compute
,
953 "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs)",
954 "Entries differential computation selection",
956 OPT_BOOLEAN('p', "period", &show_period
,
957 "Show period values."),
958 OPT_BOOLEAN('F', "formula", &show_formula
,
960 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
961 "dump raw trace in ASCII"),
962 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
963 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
964 "file", "kallsyms pathname"),
965 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
966 "load module symbols - WARNING: use only with -k and LIVE kernel"),
967 OPT_STRING('d', "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
968 "only consider symbols in these dsos"),
969 OPT_STRING('C', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
970 "only consider symbols in these comms"),
971 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
972 "only consider these symbols"),
973 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
974 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
975 " Please refer the man page for the complete list."),
976 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf
.field_sep
, "separator",
977 "separator for columns, no spaces will be added between "
978 "columns '.' is reserved."),
979 OPT_CALLBACK(0, "symfs", NULL
, "directory",
980 "Look for files with symbols relative to this directory",
981 symbol__config_symfs
),
982 OPT_UINTEGER('o', "order", &sort_compute
, "Specify compute sorting."),
983 OPT_CALLBACK(0, "percentage", NULL
, "relative|absolute",
984 "How to display percentage of filtered entries", parse_filter_percentage
),
985 OPT_STRING(0, "time", &pdiff
.time_str
, "str",
986 "Time span (time percent or absolute timestamp)"),
987 OPT_STRING(0, "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
988 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
989 "only consider symbols in these pids"),
990 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
991 "only consider symbols in these tids"),
995 static double baseline_percent(struct hist_entry
*he
)
997 u64 total
= hists__total_period(he
->hists
);
999 return 100.0 * he
->stat
.period
/ total
;
1002 static int hpp__color_baseline(struct perf_hpp_fmt
*fmt
,
1003 struct perf_hpp
*hpp
, struct hist_entry
*he
)
1005 struct diff_hpp_fmt
*dfmt
=
1006 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
1007 double percent
= baseline_percent(he
);
1008 char pfmt
[20] = " ";
1011 scnprintf(pfmt
, 20, "%%%d.2f%%%%", dfmt
->header_width
- 1);
1012 return percent_color_snprintf(hpp
->buf
, hpp
->size
,
1015 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
1016 dfmt
->header_width
, pfmt
);
1019 static int hpp__entry_baseline(struct hist_entry
*he
, char *buf
, size_t size
)
1021 double percent
= baseline_percent(he
);
1022 const char *fmt
= symbol_conf
.field_sep
? "%.2f" : "%6.2f%%";
1026 ret
= scnprintf(buf
, size
, fmt
, percent
);
1031 static int __hpp__color_compare(struct perf_hpp_fmt
*fmt
,
1032 struct perf_hpp
*hpp
, struct hist_entry
*he
,
1033 int comparison_method
)
1035 struct diff_hpp_fmt
*dfmt
=
1036 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
1037 struct hist_entry
*pair
= get_pair_fmt(he
, dfmt
);
1040 char pfmt
[20] = " ";
1045 switch (comparison_method
) {
1047 if (pair
->diff
.computed
)
1048 diff
= pair
->diff
.period_ratio_delta
;
1050 diff
= compute_delta(he
, pair
);
1052 scnprintf(pfmt
, 20, "%%%+d.2f%%%%", dfmt
->header_width
- 1);
1053 return percent_color_snprintf(hpp
->buf
, hpp
->size
,
1058 if (pair
->diff
.computed
)
1059 diff
= pair
->diff
.period_ratio
;
1061 diff
= compute_ratio(he
, pair
);
1063 scnprintf(pfmt
, 20, "%%%d.6f", dfmt
->header_width
);
1064 return value_color_snprintf(hpp
->buf
, hpp
->size
,
1066 case COMPUTE_WEIGHTED_DIFF
:
1069 if (pair
->diff
.computed
)
1070 wdiff
= pair
->diff
.wdiff
;
1072 wdiff
= compute_wdiff(he
, pair
);
1074 scnprintf(pfmt
, 20, "%%14ld", dfmt
->header_width
);
1075 return color_snprintf(hpp
->buf
, hpp
->size
,
1076 get_percent_color(wdiff
),
1082 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
1083 dfmt
->header_width
, "N/A");
1085 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
1086 dfmt
->header_width
, pfmt
);
1089 static int hpp__color_delta(struct perf_hpp_fmt
*fmt
,
1090 struct perf_hpp
*hpp
, struct hist_entry
*he
)
1092 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_DELTA
);
1095 static int hpp__color_ratio(struct perf_hpp_fmt
*fmt
,
1096 struct perf_hpp
*hpp
, struct hist_entry
*he
)
1098 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_RATIO
);
1101 static int hpp__color_wdiff(struct perf_hpp_fmt
*fmt
,
1102 struct perf_hpp
*hpp
, struct hist_entry
*he
)
1104 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_WEIGHTED_DIFF
);
1108 hpp__entry_unpair(struct hist_entry
*he
, int idx
, char *buf
, size_t size
)
1111 case PERF_HPP_DIFF__PERIOD_BASELINE
:
1112 scnprintf(buf
, size
, "%" PRIu64
, he
->stat
.period
);
1121 hpp__entry_pair(struct hist_entry
*he
, struct hist_entry
*pair
,
1122 int idx
, char *buf
, size_t size
)
1129 case PERF_HPP_DIFF__DELTA
:
1130 case PERF_HPP_DIFF__DELTA_ABS
:
1131 if (pair
->diff
.computed
)
1132 diff
= pair
->diff
.period_ratio_delta
;
1134 diff
= compute_delta(he
, pair
);
1136 scnprintf(buf
, size
, "%+4.2F%%", diff
);
1139 case PERF_HPP_DIFF__RATIO
:
1140 /* No point for ratio number if we are dummy.. */
1142 scnprintf(buf
, size
, "N/A");
1146 if (pair
->diff
.computed
)
1147 ratio
= pair
->diff
.period_ratio
;
1149 ratio
= compute_ratio(he
, pair
);
1152 scnprintf(buf
, size
, "%14.6F", ratio
);
1155 case PERF_HPP_DIFF__WEIGHTED_DIFF
:
1156 /* No point for wdiff number if we are dummy.. */
1158 scnprintf(buf
, size
, "N/A");
1162 if (pair
->diff
.computed
)
1163 wdiff
= pair
->diff
.wdiff
;
1165 wdiff
= compute_wdiff(he
, pair
);
1168 scnprintf(buf
, size
, "%14ld", wdiff
);
1171 case PERF_HPP_DIFF__FORMULA
:
1172 formula_fprintf(he
, pair
, buf
, size
);
1175 case PERF_HPP_DIFF__PERIOD
:
1176 scnprintf(buf
, size
, "%" PRIu64
, pair
->stat
.period
);
1185 __hpp__entry_global(struct hist_entry
*he
, struct diff_hpp_fmt
*dfmt
,
1186 char *buf
, size_t size
)
1188 struct hist_entry
*pair
= get_pair_fmt(he
, dfmt
);
1189 int idx
= dfmt
->idx
;
1191 /* baseline is special */
1192 if (idx
== PERF_HPP_DIFF__BASELINE
)
1193 hpp__entry_baseline(he
, buf
, size
);
1196 hpp__entry_pair(he
, pair
, idx
, buf
, size
);
1198 hpp__entry_unpair(he
, idx
, buf
, size
);
1202 static int hpp__entry_global(struct perf_hpp_fmt
*_fmt
, struct perf_hpp
*hpp
,
1203 struct hist_entry
*he
)
1205 struct diff_hpp_fmt
*dfmt
=
1206 container_of(_fmt
, struct diff_hpp_fmt
, fmt
);
1207 char buf
[MAX_COL_WIDTH
] = " ";
1209 __hpp__entry_global(he
, dfmt
, buf
, MAX_COL_WIDTH
);
1211 if (symbol_conf
.field_sep
)
1212 return scnprintf(hpp
->buf
, hpp
->size
, "%s", buf
);
1214 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
1215 dfmt
->header_width
, buf
);
1218 static int hpp__header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
1219 struct hists
*hists __maybe_unused
,
1220 int line __maybe_unused
,
1221 int *span __maybe_unused
)
1223 struct diff_hpp_fmt
*dfmt
=
1224 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
1226 BUG_ON(!dfmt
->header
);
1227 return scnprintf(hpp
->buf
, hpp
->size
, dfmt
->header
);
1230 static int hpp__width(struct perf_hpp_fmt
*fmt
,
1231 struct perf_hpp
*hpp __maybe_unused
,
1232 struct hists
*hists __maybe_unused
)
1234 struct diff_hpp_fmt
*dfmt
=
1235 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
1237 BUG_ON(dfmt
->header_width
<= 0);
1238 return dfmt
->header_width
;
1241 static void init_header(struct data__file
*d
, struct diff_hpp_fmt
*dfmt
)
1243 #define MAX_HEADER_NAME 100
1244 char buf_indent
[MAX_HEADER_NAME
];
1245 char buf
[MAX_HEADER_NAME
];
1246 const char *header
= NULL
;
1249 BUG_ON(dfmt
->idx
>= PERF_HPP_DIFF__MAX_INDEX
);
1250 header
= columns
[dfmt
->idx
].name
;
1251 width
= columns
[dfmt
->idx
].width
;
1253 /* Only our defined HPP fmts should appear here. */
1256 if (data__files_cnt
> 2)
1257 scnprintf(buf
, MAX_HEADER_NAME
, "%s/%d", header
, d
->idx
);
1259 #define NAME (data__files_cnt > 2 ? buf : header)
1260 dfmt
->header_width
= width
;
1261 width
= (int) strlen(NAME
);
1262 if (dfmt
->header_width
< width
)
1263 dfmt
->header_width
= width
;
1265 scnprintf(buf_indent
, MAX_HEADER_NAME
, "%*s",
1266 dfmt
->header_width
, NAME
);
1268 dfmt
->header
= strdup(buf_indent
);
1269 #undef MAX_HEADER_NAME
1273 static void data__hpp_register(struct data__file
*d
, int idx
)
1275 struct diff_hpp_fmt
*dfmt
= &d
->fmt
[idx
];
1276 struct perf_hpp_fmt
*fmt
= &dfmt
->fmt
;
1280 fmt
->header
= hpp__header
;
1281 fmt
->width
= hpp__width
;
1282 fmt
->entry
= hpp__entry_global
;
1283 fmt
->cmp
= hist_entry__cmp_nop
;
1284 fmt
->collapse
= hist_entry__cmp_nop
;
1286 /* TODO more colors */
1288 case PERF_HPP_DIFF__BASELINE
:
1289 fmt
->color
= hpp__color_baseline
;
1290 fmt
->sort
= hist_entry__cmp_baseline
;
1292 case PERF_HPP_DIFF__DELTA
:
1293 fmt
->color
= hpp__color_delta
;
1294 fmt
->sort
= hist_entry__cmp_delta
;
1296 case PERF_HPP_DIFF__RATIO
:
1297 fmt
->color
= hpp__color_ratio
;
1298 fmt
->sort
= hist_entry__cmp_ratio
;
1300 case PERF_HPP_DIFF__WEIGHTED_DIFF
:
1301 fmt
->color
= hpp__color_wdiff
;
1302 fmt
->sort
= hist_entry__cmp_wdiff
;
1304 case PERF_HPP_DIFF__DELTA_ABS
:
1305 fmt
->color
= hpp__color_delta
;
1306 fmt
->sort
= hist_entry__cmp_delta_abs
;
1309 fmt
->sort
= hist_entry__cmp_nop
;
1313 init_header(d
, dfmt
);
1314 perf_hpp__column_register(fmt
);
1315 perf_hpp__register_sort_field(fmt
);
1318 static int ui_init(void)
1320 struct data__file
*d
;
1321 struct perf_hpp_fmt
*fmt
;
1324 data__for_each_file(i
, d
) {
1327 * Baseline or compute realted columns:
1329 * PERF_HPP_DIFF__BASELINE
1330 * PERF_HPP_DIFF__DELTA
1331 * PERF_HPP_DIFF__RATIO
1332 * PERF_HPP_DIFF__WEIGHTED_DIFF
1334 data__hpp_register(d
, i
? compute_2_hpp
[compute
] :
1335 PERF_HPP_DIFF__BASELINE
);
1340 * PERF_HPP_DIFF__FORMULA
1341 * PERF_HPP_DIFF__PERIOD
1342 * PERF_HPP_DIFF__PERIOD_BASELINE
1344 if (show_formula
&& i
)
1345 data__hpp_register(d
, PERF_HPP_DIFF__FORMULA
);
1348 data__hpp_register(d
, i
? PERF_HPP_DIFF__PERIOD
:
1349 PERF_HPP_DIFF__PERIOD_BASELINE
);
1356 * Prepend an fmt to sort on columns at 'sort_compute' first.
1357 * This fmt is added only to the sort list but not to the
1358 * output fields list.
1360 * Note that this column (data) can be compared twice - one
1361 * for this 'sort_compute' fmt and another for the normal
1362 * diff_hpp_fmt. But it shouldn't a problem as most entries
1363 * will be sorted out by first try or baseline and comparing
1364 * is not a costly operation.
1366 fmt
= zalloc(sizeof(*fmt
));
1368 pr_err("Memory allocation failed\n");
1372 fmt
->cmp
= hist_entry__cmp_nop
;
1373 fmt
->collapse
= hist_entry__cmp_nop
;
1377 fmt
->sort
= hist_entry__cmp_delta_idx
;
1380 fmt
->sort
= hist_entry__cmp_ratio_idx
;
1382 case COMPUTE_WEIGHTED_DIFF
:
1383 fmt
->sort
= hist_entry__cmp_wdiff_idx
;
1385 case COMPUTE_DELTA_ABS
:
1386 fmt
->sort
= hist_entry__cmp_delta_abs_idx
;
1392 perf_hpp__prepend_sort_field(fmt
);
1396 static int data_init(int argc
, const char **argv
)
1398 struct data__file
*d
;
1399 static const char *defaults
[] = {
1403 bool use_default
= true;
1406 data__files_cnt
= 2;
1410 defaults
[1] = argv
[0];
1412 data__files_cnt
= argc
;
1413 use_default
= false;
1415 } else if (perf_guest
) {
1416 defaults
[0] = "perf.data.host";
1417 defaults
[1] = "perf.data.guest";
1420 if (sort_compute
>= (unsigned int) data__files_cnt
) {
1421 pr_err("Order option out of limit.\n");
1425 data__files
= zalloc(sizeof(*data__files
) * data__files_cnt
);
1429 data__for_each_file(i
, d
) {
1430 struct perf_data
*data
= &d
->data
;
1432 data
->path
= use_default
? defaults
[i
] : argv
[i
];
1433 data
->mode
= PERF_DATA_MODE_READ
,
1434 data
->force
= force
,
1442 static int diff__config(const char *var
, const char *value
,
1443 void *cb __maybe_unused
)
1445 if (!strcmp(var
, "diff.order")) {
1447 if (perf_config_int(&ret
, var
, value
) < 0)
1452 if (!strcmp(var
, "diff.compute")) {
1453 if (!strcmp(value
, "delta")) {
1454 compute
= COMPUTE_DELTA
;
1455 } else if (!strcmp(value
, "delta-abs")) {
1456 compute
= COMPUTE_DELTA_ABS
;
1457 } else if (!strcmp(value
, "ratio")) {
1458 compute
= COMPUTE_RATIO
;
1459 } else if (!strcmp(value
, "wdiff")) {
1460 compute
= COMPUTE_WEIGHTED_DIFF
;
1462 pr_err("Invalid compute method: %s\n", value
);
1470 int cmd_diff(int argc
, const char **argv
)
1472 int ret
= hists__init();
1477 perf_config(diff__config
, NULL
);
1479 argc
= parse_options(argc
, argv
, options
, diff_usage
, 0);
1482 perf_quiet_option();
1484 if (symbol__init(NULL
) < 0)
1487 if (data_init(argc
, argv
) < 0)
1493 sort__mode
= SORT_MODE__DIFF
;
1495 if (setup_sorting(NULL
) < 0)
1496 usage_with_options(diff_usage
, options
);
1500 sort__setup_elide(NULL
);
1502 return __cmd_diff();