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"
24 /* Diff command specific HPP columns. */
26 PERF_HPP_DIFF__BASELINE
,
27 PERF_HPP_DIFF__PERIOD
,
28 PERF_HPP_DIFF__PERIOD_BASELINE
,
31 PERF_HPP_DIFF__WEIGHTED_DIFF
,
32 PERF_HPP_DIFF__FORMULA
,
34 PERF_HPP_DIFF__MAX_INDEX
38 struct perf_hpp_fmt fmt
;
45 struct perf_session
*session
;
46 struct perf_data_file file
;
49 struct diff_hpp_fmt fmt
[PERF_HPP_DIFF__MAX_INDEX
];
52 static struct data__file
*data__files
;
53 static int data__files_cnt
;
55 #define data__for_each_file_start(i, d, s) \
56 for (i = s, d = &data__files[s]; \
57 i < data__files_cnt; \
58 i++, d = &data__files[i])
60 #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
61 #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
63 static char diff__default_sort_order
[] = "dso,symbol";
65 static bool show_period
;
66 static bool show_formula
;
67 static bool show_baseline_only
;
68 static unsigned int sort_compute
;
70 static s64 compute_wdiff_w1
;
71 static s64 compute_wdiff_w2
;
76 COMPUTE_WEIGHTED_DIFF
,
80 const char *compute_names
[COMPUTE_MAX
] = {
81 [COMPUTE_DELTA
] = "delta",
82 [COMPUTE_RATIO
] = "ratio",
83 [COMPUTE_WEIGHTED_DIFF
] = "wdiff",
88 static int compute_2_hpp
[COMPUTE_MAX
] = {
89 [COMPUTE_DELTA
] = PERF_HPP_DIFF__DELTA
,
90 [COMPUTE_RATIO
] = PERF_HPP_DIFF__RATIO
,
91 [COMPUTE_WEIGHTED_DIFF
] = PERF_HPP_DIFF__WEIGHTED_DIFF
,
94 #define MAX_COL_WIDTH 70
96 static struct header_column
{
99 } columns
[PERF_HPP_DIFF__MAX_INDEX
] = {
100 [PERF_HPP_DIFF__BASELINE
] = {
103 [PERF_HPP_DIFF__PERIOD
] = {
107 [PERF_HPP_DIFF__PERIOD_BASELINE
] = {
108 .name
= "Base period",
111 [PERF_HPP_DIFF__DELTA
] = {
115 [PERF_HPP_DIFF__RATIO
] = {
119 [PERF_HPP_DIFF__WEIGHTED_DIFF
] = {
120 .name
= "Weighted diff",
123 [PERF_HPP_DIFF__FORMULA
] = {
125 .width
= MAX_COL_WIDTH
,
129 static int setup_compute_opt_wdiff(char *opt
)
139 w2_str
= strchr(opt
, ',');
147 compute_wdiff_w1
= strtol(w1_str
, NULL
, 10);
148 compute_wdiff_w2
= strtol(w2_str
, NULL
, 10);
150 if (!compute_wdiff_w1
|| !compute_wdiff_w2
)
153 pr_debug("compute wdiff w1(%" PRId64
") w2(%" PRId64
")\n",
154 compute_wdiff_w1
, compute_wdiff_w2
);
160 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
165 static int setup_compute_opt(char *opt
)
167 if (compute
== COMPUTE_WEIGHTED_DIFF
)
168 return setup_compute_opt_wdiff(opt
);
171 pr_err("Failed: extra option specified '%s'", opt
);
178 static int setup_compute(const struct option
*opt
, const char *str
,
179 int unset __maybe_unused
)
181 int *cp
= (int *) opt
->value
;
182 char *cstr
= (char *) str
;
192 option
= strchr(str
, ':');
194 unsigned len
= option
++ - str
;
197 * The str data are not writeable, so we need
198 * to use another buffer.
201 /* No option value is longer. */
202 if (len
>= sizeof(buf
))
205 strncpy(buf
, str
, len
);
210 for (i
= 0; i
< COMPUTE_MAX
; i
++)
211 if (!strcmp(cstr
, compute_names
[i
])) {
213 return setup_compute_opt(option
);
216 pr_err("Failed: '%s' is not computation method "
217 "(use 'delta','ratio' or 'wdiff')\n", str
);
221 static double period_percent(struct hist_entry
*he
, u64 period
)
223 u64 total
= hists__total_period(he
->hists
);
225 return (period
* 100.0) / total
;
228 static double compute_delta(struct hist_entry
*he
, struct hist_entry
*pair
)
230 double old_percent
= period_percent(he
, he
->stat
.period
);
231 double new_percent
= period_percent(pair
, pair
->stat
.period
);
233 pair
->diff
.period_ratio_delta
= new_percent
- old_percent
;
234 pair
->diff
.computed
= true;
235 return pair
->diff
.period_ratio_delta
;
238 static double compute_ratio(struct hist_entry
*he
, struct hist_entry
*pair
)
240 double old_period
= he
->stat
.period
?: 1;
241 double new_period
= pair
->stat
.period
;
243 pair
->diff
.computed
= true;
244 pair
->diff
.period_ratio
= new_period
/ old_period
;
245 return pair
->diff
.period_ratio
;
248 static s64
compute_wdiff(struct hist_entry
*he
, struct hist_entry
*pair
)
250 u64 old_period
= he
->stat
.period
;
251 u64 new_period
= pair
->stat
.period
;
253 pair
->diff
.computed
= true;
254 pair
->diff
.wdiff
= new_period
* compute_wdiff_w2
-
255 old_period
* compute_wdiff_w1
;
257 return pair
->diff
.wdiff
;
260 static int formula_delta(struct hist_entry
*he
, struct hist_entry
*pair
,
261 char *buf
, size_t size
)
263 u64 he_total
= he
->hists
->stats
.total_period
;
264 u64 pair_total
= pair
->hists
->stats
.total_period
;
266 if (symbol_conf
.filter_relative
) {
267 he_total
= he
->hists
->stats
.total_non_filtered_period
;
268 pair_total
= pair
->hists
->stats
.total_non_filtered_period
;
270 return scnprintf(buf
, size
,
271 "(%" PRIu64
" * 100 / %" PRIu64
") - "
272 "(%" PRIu64
" * 100 / %" PRIu64
")",
273 pair
->stat
.period
, pair_total
,
274 he
->stat
.period
, he_total
);
277 static int formula_ratio(struct hist_entry
*he
, struct hist_entry
*pair
,
278 char *buf
, size_t size
)
280 double old_period
= he
->stat
.period
;
281 double new_period
= pair
->stat
.period
;
283 return scnprintf(buf
, size
, "%.0F / %.0F", new_period
, old_period
);
286 static int formula_wdiff(struct hist_entry
*he
, struct hist_entry
*pair
,
287 char *buf
, size_t size
)
289 u64 old_period
= he
->stat
.period
;
290 u64 new_period
= pair
->stat
.period
;
292 return scnprintf(buf
, size
,
293 "(%" PRIu64
" * " "%" PRId64
") - (%" PRIu64
" * " "%" PRId64
")",
294 new_period
, compute_wdiff_w2
, old_period
, compute_wdiff_w1
);
297 static int formula_fprintf(struct hist_entry
*he
, struct hist_entry
*pair
,
298 char *buf
, size_t size
)
302 return formula_delta(he
, pair
, buf
, size
);
304 return formula_ratio(he
, pair
, buf
, size
);
305 case COMPUTE_WEIGHTED_DIFF
:
306 return formula_wdiff(he
, pair
, buf
, size
);
314 static int hists__add_entry(struct hists
*hists
,
315 struct addr_location
*al
, u64 period
,
316 u64 weight
, u64 transaction
)
318 if (__hists__add_entry(hists
, al
, NULL
, NULL
, NULL
, period
, weight
,
319 transaction
) != NULL
)
324 static int diff__process_sample_event(struct perf_tool
*tool __maybe_unused
,
325 union perf_event
*event
,
326 struct perf_sample
*sample
,
327 struct perf_evsel
*evsel
,
328 struct machine
*machine
)
330 struct addr_location al
;
332 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
) < 0) {
333 pr_warning("problem processing %d event, skipping it.\n",
338 if (hists__add_entry(&evsel
->hists
, &al
, sample
->period
,
339 sample
->weight
, sample
->transaction
)) {
340 pr_warning("problem incrementing symbol period, skipping event\n");
344 if (al
.filtered
== 0) {
345 evsel
->hists
.stats
.total_non_filtered_period
+= sample
->period
;
346 evsel
->hists
.nr_non_filtered_entries
++;
348 evsel
->hists
.stats
.total_period
+= sample
->period
;
352 static struct perf_tool tool
= {
353 .sample
= diff__process_sample_event
,
354 .mmap
= perf_event__process_mmap
,
355 .comm
= perf_event__process_comm
,
356 .exit
= perf_event__process_exit
,
357 .fork
= perf_event__process_fork
,
358 .lost
= perf_event__process_lost
,
359 .ordered_samples
= true,
360 .ordering_requires_timestamps
= true,
363 static struct perf_evsel
*evsel_match(struct perf_evsel
*evsel
,
364 struct perf_evlist
*evlist
)
366 struct perf_evsel
*e
;
368 evlist__for_each(evlist
, e
) {
369 if (perf_evsel__match2(evsel
, e
))
376 static void perf_evlist__collapse_resort(struct perf_evlist
*evlist
)
378 struct perf_evsel
*evsel
;
380 evlist__for_each(evlist
, evsel
) {
381 struct hists
*hists
= &evsel
->hists
;
383 hists__collapse_resort(hists
, NULL
);
387 static struct hist_entry
*
388 get_pair_data(struct hist_entry
*he
, struct data__file
*d
)
390 if (hist_entry__has_pairs(he
)) {
391 struct hist_entry
*pair
;
393 list_for_each_entry(pair
, &he
->pairs
.head
, pairs
.node
)
394 if (pair
->hists
== d
->hists
)
401 static struct hist_entry
*
402 get_pair_fmt(struct hist_entry
*he
, struct diff_hpp_fmt
*dfmt
)
404 void *ptr
= dfmt
- dfmt
->idx
;
405 struct data__file
*d
= container_of(ptr
, struct data__file
, fmt
);
407 return get_pair_data(he
, d
);
410 static void hists__baseline_only(struct hists
*hists
)
412 struct rb_root
*root
;
413 struct rb_node
*next
;
415 if (sort__need_collapse
)
416 root
= &hists
->entries_collapsed
;
418 root
= hists
->entries_in
;
420 next
= rb_first(root
);
421 while (next
!= NULL
) {
422 struct hist_entry
*he
= rb_entry(next
, struct hist_entry
, rb_node_in
);
424 next
= rb_next(&he
->rb_node_in
);
425 if (!hist_entry__next_pair(he
)) {
426 rb_erase(&he
->rb_node_in
, root
);
427 hist_entry__free(he
);
432 static void hists__precompute(struct hists
*hists
)
434 struct rb_root
*root
;
435 struct rb_node
*next
;
437 if (sort__need_collapse
)
438 root
= &hists
->entries_collapsed
;
440 root
= hists
->entries_in
;
442 next
= rb_first(root
);
443 while (next
!= NULL
) {
444 struct hist_entry
*he
, *pair
;
446 he
= rb_entry(next
, struct hist_entry
, rb_node_in
);
447 next
= rb_next(&he
->rb_node_in
);
449 pair
= get_pair_data(he
, &data__files
[sort_compute
]);
455 compute_delta(he
, pair
);
458 compute_ratio(he
, pair
);
460 case COMPUTE_WEIGHTED_DIFF
:
461 compute_wdiff(he
, pair
);
469 static int64_t cmp_doubles(double l
, double r
)
480 __hist_entry__cmp_compute(struct hist_entry
*left
, struct hist_entry
*right
,
486 double l
= left
->diff
.period_ratio_delta
;
487 double r
= right
->diff
.period_ratio_delta
;
489 return cmp_doubles(l
, r
);
493 double l
= left
->diff
.period_ratio
;
494 double r
= right
->diff
.period_ratio
;
496 return cmp_doubles(l
, r
);
498 case COMPUTE_WEIGHTED_DIFF
:
500 s64 l
= left
->diff
.wdiff
;
501 s64 r
= right
->diff
.wdiff
;
513 hist_entry__cmp_compute(struct hist_entry
*left
, struct hist_entry
*right
,
516 bool pairs_left
= hist_entry__has_pairs(left
);
517 bool pairs_right
= hist_entry__has_pairs(right
);
518 struct hist_entry
*p_right
, *p_left
;
520 if (!pairs_left
&& !pairs_right
)
523 if (!pairs_left
|| !pairs_right
)
524 return pairs_left
? -1 : 1;
526 p_left
= get_pair_data(left
, &data__files
[sort_compute
]);
527 p_right
= get_pair_data(right
, &data__files
[sort_compute
]);
529 if (!p_left
&& !p_right
)
532 if (!p_left
|| !p_right
)
533 return p_left
? -1 : 1;
536 * We have 2 entries of same kind, let's
537 * make the data comparison.
539 return __hist_entry__cmp_compute(p_left
, p_right
, c
);
542 static void insert_hist_entry_by_compute(struct rb_root
*root
,
543 struct hist_entry
*he
,
546 struct rb_node
**p
= &root
->rb_node
;
547 struct rb_node
*parent
= NULL
;
548 struct hist_entry
*iter
;
552 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
553 if (hist_entry__cmp_compute(he
, iter
, c
) < 0)
559 rb_link_node(&he
->rb_node
, parent
, p
);
560 rb_insert_color(&he
->rb_node
, root
);
563 static void hists__compute_resort(struct hists
*hists
)
565 struct rb_root
*root
;
566 struct rb_node
*next
;
568 if (sort__need_collapse
)
569 root
= &hists
->entries_collapsed
;
571 root
= hists
->entries_in
;
573 hists
->entries
= RB_ROOT
;
574 next
= rb_first(root
);
576 hists__reset_stats(hists
);
577 hists__reset_col_len(hists
);
579 while (next
!= NULL
) {
580 struct hist_entry
*he
;
582 he
= rb_entry(next
, struct hist_entry
, rb_node_in
);
583 next
= rb_next(&he
->rb_node_in
);
585 insert_hist_entry_by_compute(&hists
->entries
, he
, compute
);
586 hists__inc_stats(hists
, he
);
589 hists__calc_col_len(hists
, he
);
593 static void hists__process(struct hists
*hists
)
595 if (show_baseline_only
)
596 hists__baseline_only(hists
);
599 hists__precompute(hists
);
600 hists__compute_resort(hists
);
602 hists__output_resort(hists
);
605 hists__fprintf(hists
, true, 0, 0, 0, stdout
);
608 static void data__fprintf(void)
610 struct data__file
*d
;
613 fprintf(stdout
, "# Data files:\n");
615 data__for_each_file(i
, d
)
616 fprintf(stdout
, "# [%d] %s %s\n",
617 d
->idx
, d
->file
.path
,
618 !d
->idx
? "(Baseline)" : "");
620 fprintf(stdout
, "#\n");
623 static void data_process(void)
625 struct perf_evlist
*evlist_base
= data__files
[0].session
->evlist
;
626 struct perf_evsel
*evsel_base
;
629 evlist__for_each(evlist_base
, evsel_base
) {
630 struct data__file
*d
;
633 data__for_each_file_new(i
, d
) {
634 struct perf_evlist
*evlist
= d
->session
->evlist
;
635 struct perf_evsel
*evsel
;
637 evsel
= evsel_match(evsel_base
, evlist
);
641 d
->hists
= &evsel
->hists
;
643 hists__match(&evsel_base
->hists
, &evsel
->hists
);
645 if (!show_baseline_only
)
646 hists__link(&evsel_base
->hists
,
650 fprintf(stdout
, "%s# Event '%s'\n#\n", first
? "" : "\n",
651 perf_evsel__name(evsel_base
));
655 if (verbose
|| data__files_cnt
> 2)
658 hists__process(&evsel_base
->hists
);
662 static void data__free(struct data__file
*d
)
666 for (col
= 0; col
< PERF_HPP_DIFF__MAX_INDEX
; col
++) {
667 struct diff_hpp_fmt
*fmt
= &d
->fmt
[col
];
673 static int __cmd_diff(void)
675 struct data__file
*d
;
676 int ret
= -EINVAL
, i
;
678 data__for_each_file(i
, d
) {
679 d
->session
= perf_session__new(&d
->file
, false, &tool
);
681 pr_err("Failed to open %s\n", d
->file
.path
);
686 ret
= perf_session__process_events(d
->session
, &tool
);
688 pr_err("Failed to process %s\n", d
->file
.path
);
692 perf_evlist__collapse_resort(d
->session
->evlist
);
698 data__for_each_file(i
, d
) {
700 perf_session__delete(d
->session
);
709 static const char * const diff_usage
[] = {
710 "perf diff [<options>] [old_file] [new_file]",
714 static const struct option options
[] = {
715 OPT_INCR('v', "verbose", &verbose
,
716 "be more verbose (show symbol address, etc)"),
717 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only
,
718 "Show only items with match in baseline"),
719 OPT_CALLBACK('c', "compute", &compute
,
720 "delta,ratio,wdiff:w1,w2 (default delta)",
721 "Entries differential computation selection",
723 OPT_BOOLEAN('p', "period", &show_period
,
724 "Show period values."),
725 OPT_BOOLEAN('F', "formula", &show_formula
,
727 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
728 "dump raw trace in ASCII"),
729 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
730 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
731 "load module symbols - WARNING: use only with -k and LIVE kernel"),
732 OPT_STRING('d', "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
733 "only consider symbols in these dsos"),
734 OPT_STRING('C', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
735 "only consider symbols in these comms"),
736 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
737 "only consider these symbols"),
738 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
739 "sort by key(s): pid, comm, dso, symbol, parent"),
740 OPT_STRING('t', "field-separator", &symbol_conf
.field_sep
, "separator",
741 "separator for columns, no spaces will be added between "
742 "columns '.' is reserved."),
743 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
744 "Look for files with symbols relative to this directory"),
745 OPT_UINTEGER('o', "order", &sort_compute
, "Specify compute sorting."),
746 OPT_CALLBACK(0, "percentage", NULL
, "relative|absolute",
747 "How to display percentage of filtered entries", parse_filter_percentage
),
751 static double baseline_percent(struct hist_entry
*he
)
753 u64 total
= hists__total_period(he
->hists
);
755 return 100.0 * he
->stat
.period
/ total
;
758 static int hpp__color_baseline(struct perf_hpp_fmt
*fmt
,
759 struct perf_hpp
*hpp
, struct hist_entry
*he
)
761 struct diff_hpp_fmt
*dfmt
=
762 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
763 double percent
= baseline_percent(he
);
767 scnprintf(pfmt
, 20, "%%%d.2f%%%%", dfmt
->header_width
- 1);
768 return percent_color_snprintf(hpp
->buf
, hpp
->size
,
771 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
772 dfmt
->header_width
, pfmt
);
775 static int hpp__entry_baseline(struct hist_entry
*he
, char *buf
, size_t size
)
777 double percent
= baseline_percent(he
);
778 const char *fmt
= symbol_conf
.field_sep
? "%.2f" : "%6.2f%%";
782 ret
= scnprintf(buf
, size
, fmt
, percent
);
787 static int __hpp__color_compare(struct perf_hpp_fmt
*fmt
,
788 struct perf_hpp
*hpp
, struct hist_entry
*he
,
789 int comparison_method
)
791 struct diff_hpp_fmt
*dfmt
=
792 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
793 struct hist_entry
*pair
= get_pair_fmt(he
, dfmt
);
801 switch (comparison_method
) {
803 if (pair
->diff
.computed
)
804 diff
= pair
->diff
.period_ratio_delta
;
806 diff
= compute_delta(he
, pair
);
808 if (fabs(diff
) < 0.01)
810 scnprintf(pfmt
, 20, "%%%+d.2f%%%%", dfmt
->header_width
- 1);
811 return percent_color_snprintf(hpp
->buf
, hpp
->size
,
816 if (pair
->diff
.computed
)
817 diff
= pair
->diff
.period_ratio
;
819 diff
= compute_ratio(he
, pair
);
821 scnprintf(pfmt
, 20, "%%%d.6f", dfmt
->header_width
);
822 return value_color_snprintf(hpp
->buf
, hpp
->size
,
824 case COMPUTE_WEIGHTED_DIFF
:
827 if (pair
->diff
.computed
)
828 wdiff
= pair
->diff
.wdiff
;
830 wdiff
= compute_wdiff(he
, pair
);
832 scnprintf(pfmt
, 20, "%%14ld", dfmt
->header_width
);
833 return color_snprintf(hpp
->buf
, hpp
->size
,
834 get_percent_color(wdiff
),
840 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
841 dfmt
->header_width
, pfmt
);
844 static int hpp__color_delta(struct perf_hpp_fmt
*fmt
,
845 struct perf_hpp
*hpp
, struct hist_entry
*he
)
847 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_DELTA
);
850 static int hpp__color_ratio(struct perf_hpp_fmt
*fmt
,
851 struct perf_hpp
*hpp
, struct hist_entry
*he
)
853 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_RATIO
);
856 static int hpp__color_wdiff(struct perf_hpp_fmt
*fmt
,
857 struct perf_hpp
*hpp
, struct hist_entry
*he
)
859 return __hpp__color_compare(fmt
, hpp
, he
, COMPUTE_WEIGHTED_DIFF
);
863 hpp__entry_unpair(struct hist_entry
*he
, int idx
, char *buf
, size_t size
)
866 case PERF_HPP_DIFF__PERIOD_BASELINE
:
867 scnprintf(buf
, size
, "%" PRIu64
, he
->stat
.period
);
876 hpp__entry_pair(struct hist_entry
*he
, struct hist_entry
*pair
,
877 int idx
, char *buf
, size_t size
)
884 case PERF_HPP_DIFF__DELTA
:
885 if (pair
->diff
.computed
)
886 diff
= pair
->diff
.period_ratio_delta
;
888 diff
= compute_delta(he
, pair
);
890 if (fabs(diff
) >= 0.01)
891 scnprintf(buf
, size
, "%+4.2F%%", diff
);
894 case PERF_HPP_DIFF__RATIO
:
895 /* No point for ratio number if we are dummy.. */
899 if (pair
->diff
.computed
)
900 ratio
= pair
->diff
.period_ratio
;
902 ratio
= compute_ratio(he
, pair
);
905 scnprintf(buf
, size
, "%14.6F", ratio
);
908 case PERF_HPP_DIFF__WEIGHTED_DIFF
:
909 /* No point for wdiff number if we are dummy.. */
913 if (pair
->diff
.computed
)
914 wdiff
= pair
->diff
.wdiff
;
916 wdiff
= compute_wdiff(he
, pair
);
919 scnprintf(buf
, size
, "%14ld", wdiff
);
922 case PERF_HPP_DIFF__FORMULA
:
923 formula_fprintf(he
, pair
, buf
, size
);
926 case PERF_HPP_DIFF__PERIOD
:
927 scnprintf(buf
, size
, "%" PRIu64
, pair
->stat
.period
);
936 __hpp__entry_global(struct hist_entry
*he
, struct diff_hpp_fmt
*dfmt
,
937 char *buf
, size_t size
)
939 struct hist_entry
*pair
= get_pair_fmt(he
, dfmt
);
942 /* baseline is special */
943 if (idx
== PERF_HPP_DIFF__BASELINE
)
944 hpp__entry_baseline(he
, buf
, size
);
947 hpp__entry_pair(he
, pair
, idx
, buf
, size
);
949 hpp__entry_unpair(he
, idx
, buf
, size
);
953 static int hpp__entry_global(struct perf_hpp_fmt
*_fmt
, struct perf_hpp
*hpp
,
954 struct hist_entry
*he
)
956 struct diff_hpp_fmt
*dfmt
=
957 container_of(_fmt
, struct diff_hpp_fmt
, fmt
);
958 char buf
[MAX_COL_WIDTH
] = " ";
960 __hpp__entry_global(he
, dfmt
, buf
, MAX_COL_WIDTH
);
962 if (symbol_conf
.field_sep
)
963 return scnprintf(hpp
->buf
, hpp
->size
, "%s", buf
);
965 return scnprintf(hpp
->buf
, hpp
->size
, "%*s",
966 dfmt
->header_width
, buf
);
969 static int hpp__header(struct perf_hpp_fmt
*fmt
, struct perf_hpp
*hpp
,
970 struct perf_evsel
*evsel __maybe_unused
)
972 struct diff_hpp_fmt
*dfmt
=
973 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
975 BUG_ON(!dfmt
->header
);
976 return scnprintf(hpp
->buf
, hpp
->size
, dfmt
->header
);
979 static int hpp__width(struct perf_hpp_fmt
*fmt
,
980 struct perf_hpp
*hpp __maybe_unused
,
981 struct perf_evsel
*evsel __maybe_unused
)
983 struct diff_hpp_fmt
*dfmt
=
984 container_of(fmt
, struct diff_hpp_fmt
, fmt
);
986 BUG_ON(dfmt
->header_width
<= 0);
987 return dfmt
->header_width
;
990 static void init_header(struct data__file
*d
, struct diff_hpp_fmt
*dfmt
)
992 #define MAX_HEADER_NAME 100
993 char buf_indent
[MAX_HEADER_NAME
];
994 char buf
[MAX_HEADER_NAME
];
995 const char *header
= NULL
;
998 BUG_ON(dfmt
->idx
>= PERF_HPP_DIFF__MAX_INDEX
);
999 header
= columns
[dfmt
->idx
].name
;
1000 width
= columns
[dfmt
->idx
].width
;
1002 /* Only our defined HPP fmts should appear here. */
1005 if (data__files_cnt
> 2)
1006 scnprintf(buf
, MAX_HEADER_NAME
, "%s/%d", header
, d
->idx
);
1008 #define NAME (data__files_cnt > 2 ? buf : header)
1009 dfmt
->header_width
= width
;
1010 width
= (int) strlen(NAME
);
1011 if (dfmt
->header_width
< width
)
1012 dfmt
->header_width
= width
;
1014 scnprintf(buf_indent
, MAX_HEADER_NAME
, "%*s",
1015 dfmt
->header_width
, NAME
);
1017 dfmt
->header
= strdup(buf_indent
);
1018 #undef MAX_HEADER_NAME
1022 static void data__hpp_register(struct data__file
*d
, int idx
)
1024 struct diff_hpp_fmt
*dfmt
= &d
->fmt
[idx
];
1025 struct perf_hpp_fmt
*fmt
= &dfmt
->fmt
;
1029 fmt
->header
= hpp__header
;
1030 fmt
->width
= hpp__width
;
1031 fmt
->entry
= hpp__entry_global
;
1033 /* TODO more colors */
1035 case PERF_HPP_DIFF__BASELINE
:
1036 fmt
->color
= hpp__color_baseline
;
1038 case PERF_HPP_DIFF__DELTA
:
1039 fmt
->color
= hpp__color_delta
;
1041 case PERF_HPP_DIFF__RATIO
:
1042 fmt
->color
= hpp__color_ratio
;
1044 case PERF_HPP_DIFF__WEIGHTED_DIFF
:
1045 fmt
->color
= hpp__color_wdiff
;
1051 init_header(d
, dfmt
);
1052 perf_hpp__column_register(fmt
);
1055 static void ui_init(void)
1057 struct data__file
*d
;
1060 data__for_each_file(i
, d
) {
1063 * Baseline or compute realted columns:
1065 * PERF_HPP_DIFF__BASELINE
1066 * PERF_HPP_DIFF__DELTA
1067 * PERF_HPP_DIFF__RATIO
1068 * PERF_HPP_DIFF__WEIGHTED_DIFF
1070 data__hpp_register(d
, i
? compute_2_hpp
[compute
] :
1071 PERF_HPP_DIFF__BASELINE
);
1076 * PERF_HPP_DIFF__FORMULA
1077 * PERF_HPP_DIFF__PERIOD
1078 * PERF_HPP_DIFF__PERIOD_BASELINE
1080 if (show_formula
&& i
)
1081 data__hpp_register(d
, PERF_HPP_DIFF__FORMULA
);
1084 data__hpp_register(d
, i
? PERF_HPP_DIFF__PERIOD
:
1085 PERF_HPP_DIFF__PERIOD_BASELINE
);
1089 static int data_init(int argc
, const char **argv
)
1091 struct data__file
*d
;
1092 static const char *defaults
[] = {
1096 bool use_default
= true;
1099 data__files_cnt
= 2;
1103 defaults
[1] = argv
[0];
1105 data__files_cnt
= argc
;
1106 use_default
= false;
1108 } else if (perf_guest
) {
1109 defaults
[0] = "perf.data.host";
1110 defaults
[1] = "perf.data.guest";
1113 if (sort_compute
>= (unsigned int) data__files_cnt
) {
1114 pr_err("Order option out of limit.\n");
1118 data__files
= zalloc(sizeof(*data__files
) * data__files_cnt
);
1122 data__for_each_file(i
, d
) {
1123 struct perf_data_file
*file
= &d
->file
;
1125 file
->path
= use_default
? defaults
[i
] : argv
[i
];
1126 file
->mode
= PERF_DATA_MODE_READ
,
1127 file
->force
= force
,
1135 int cmd_diff(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1137 perf_config(perf_default_config
, NULL
);
1139 sort_order
= diff__default_sort_order
;
1140 argc
= parse_options(argc
, argv
, options
, diff_usage
, 0);
1142 if (symbol__init() < 0)
1145 if (data_init(argc
, argv
) < 0)
1150 if (setup_sorting() < 0)
1151 usage_with_options(diff_usage
, options
);
1155 sort__setup_elide(NULL
);
1157 return __cmd_diff();