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"
22 static char const *input_old
= "perf.data.old",
23 *input_new
= "perf.data";
24 static char diff__default_sort_order
[] = "dso,symbol";
26 static bool show_displacement
;
27 static bool show_period
;
28 static bool show_formula
;
29 static bool show_baseline_only
;
30 static bool sort_compute
;
32 static s64 compute_wdiff_w1
;
33 static s64 compute_wdiff_w2
;
38 COMPUTE_WEIGHTED_DIFF
,
42 const char *compute_names
[COMPUTE_MAX
] = {
43 [COMPUTE_DELTA
] = "delta",
44 [COMPUTE_RATIO
] = "ratio",
45 [COMPUTE_WEIGHTED_DIFF
] = "wdiff",
50 static int setup_compute_opt_wdiff(char *opt
)
60 w2_str
= strchr(opt
, ',');
68 compute_wdiff_w1
= strtol(w1_str
, NULL
, 10);
69 compute_wdiff_w2
= strtol(w2_str
, NULL
, 10);
71 if (!compute_wdiff_w1
|| !compute_wdiff_w2
)
74 pr_debug("compute wdiff w1(%" PRId64
") w2(%" PRId64
")\n",
75 compute_wdiff_w1
, compute_wdiff_w2
);
81 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
86 static int setup_compute_opt(char *opt
)
88 if (compute
== COMPUTE_WEIGHTED_DIFF
)
89 return setup_compute_opt_wdiff(opt
);
92 pr_err("Failed: extra option specified '%s'", opt
);
99 static int setup_compute(const struct option
*opt
, const char *str
,
100 int unset __maybe_unused
)
102 int *cp
= (int *) opt
->value
;
103 char *cstr
= (char *) str
;
115 cstr
= (char *) ++str
;
120 option
= strchr(str
, ':');
122 unsigned len
= option
++ - str
;
125 * The str data are not writeable, so we need
126 * to use another buffer.
129 /* No option value is longer. */
130 if (len
>= sizeof(buf
))
133 strncpy(buf
, str
, len
);
138 for (i
= 0; i
< COMPUTE_MAX
; i
++)
139 if (!strcmp(cstr
, compute_names
[i
])) {
141 return setup_compute_opt(option
);
144 pr_err("Failed: '%s' is not computation method "
145 "(use 'delta','ratio' or 'wdiff')\n", str
);
149 static double get_period_percent(struct hist_entry
*he
, u64 period
)
151 u64 total
= he
->hists
->stats
.total_period
;
152 return (period
* 100.0) / total
;
155 double perf_diff__compute_delta(struct hist_entry
*he
)
157 struct hist_entry
*pair
= hist_entry__next_pair(he
);
158 double new_percent
= get_period_percent(he
, he
->stat
.period
);
159 double old_percent
= pair
? get_period_percent(pair
, pair
->stat
.period
) : 0.0;
161 he
->diff
.period_ratio_delta
= new_percent
- old_percent
;
162 he
->diff
.computed
= true;
163 return he
->diff
.period_ratio_delta
;
166 double perf_diff__compute_ratio(struct hist_entry
*he
)
168 struct hist_entry
*pair
= hist_entry__next_pair(he
);
169 double new_period
= he
->stat
.period
;
170 double old_period
= pair
? pair
->stat
.period
: 0;
172 he
->diff
.computed
= true;
173 he
->diff
.period_ratio
= pair
? (new_period
/ old_period
) : 0;
174 return he
->diff
.period_ratio
;
177 s64
perf_diff__compute_wdiff(struct hist_entry
*he
)
179 struct hist_entry
*pair
= hist_entry__next_pair(he
);
180 u64 new_period
= he
->stat
.period
;
181 u64 old_period
= pair
? pair
->stat
.period
: 0;
183 he
->diff
.computed
= true;
188 he
->diff
.wdiff
= new_period
* compute_wdiff_w2
-
189 old_period
* compute_wdiff_w1
;
191 return he
->diff
.wdiff
;
194 static int formula_delta(struct hist_entry
*he
, char *buf
, size_t size
)
196 struct hist_entry
*pair
= hist_entry__next_pair(he
);
201 return scnprintf(buf
, size
,
202 "(%" PRIu64
" * 100 / %" PRIu64
") - "
203 "(%" PRIu64
" * 100 / %" PRIu64
")",
204 he
->stat
.period
, he
->hists
->stats
.total_period
,
205 pair
->stat
.period
, pair
->hists
->stats
.total_period
);
208 static int formula_ratio(struct hist_entry
*he
, char *buf
, size_t size
)
210 struct hist_entry
*pair
= hist_entry__next_pair(he
);
211 double new_period
= he
->stat
.period
;
212 double old_period
= pair
? pair
->stat
.period
: 0;
217 return scnprintf(buf
, size
, "%.0F / %.0F", new_period
, old_period
);
220 static int formula_wdiff(struct hist_entry
*he
, char *buf
, size_t size
)
222 struct hist_entry
*pair
= hist_entry__next_pair(he
);
223 u64 new_period
= he
->stat
.period
;
224 u64 old_period
= pair
? pair
->stat
.period
: 0;
229 return scnprintf(buf
, size
,
230 "(%" PRIu64
" * " "%" PRId64
") - (%" PRIu64
" * " "%" PRId64
")",
231 new_period
, compute_wdiff_w2
, old_period
, compute_wdiff_w1
);
234 int perf_diff__formula(char *buf
, size_t size
, struct hist_entry
*he
)
238 return formula_delta(he
, buf
, size
);
240 return formula_ratio(he
, buf
, size
);
241 case COMPUTE_WEIGHTED_DIFF
:
242 return formula_wdiff(he
, buf
, size
);
250 static int hists__add_entry(struct hists
*self
,
251 struct addr_location
*al
, u64 period
)
253 if (__hists__add_entry(self
, al
, NULL
, period
) != NULL
)
258 static int diff__process_sample_event(struct perf_tool
*tool __maybe_unused
,
259 union perf_event
*event
,
260 struct perf_sample
*sample
,
261 struct perf_evsel
*evsel
,
262 struct machine
*machine
)
264 struct addr_location al
;
266 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
, NULL
) < 0) {
267 pr_warning("problem processing %d event, skipping it.\n",
275 if (hists__add_entry(&evsel
->hists
, &al
, sample
->period
)) {
276 pr_warning("problem incrementing symbol period, skipping event\n");
280 evsel
->hists
.stats
.total_period
+= sample
->period
;
284 static struct perf_tool tool
= {
285 .sample
= diff__process_sample_event
,
286 .mmap
= perf_event__process_mmap
,
287 .comm
= perf_event__process_comm
,
288 .exit
= perf_event__process_exit
,
289 .fork
= perf_event__process_fork
,
290 .lost
= perf_event__process_lost
,
291 .ordered_samples
= true,
292 .ordering_requires_timestamps
= true,
295 static void insert_hist_entry_by_name(struct rb_root
*root
,
296 struct hist_entry
*he
)
298 struct rb_node
**p
= &root
->rb_node
;
299 struct rb_node
*parent
= NULL
;
300 struct hist_entry
*iter
;
304 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
305 if (hist_entry__cmp(he
, iter
) < 0)
311 rb_link_node(&he
->rb_node
, parent
, p
);
312 rb_insert_color(&he
->rb_node
, root
);
315 static void hists__name_resort(struct hists
*self
, bool sort
)
317 unsigned long position
= 1;
318 struct rb_root tmp
= RB_ROOT
;
319 struct rb_node
*next
= rb_first(&self
->entries
);
321 while (next
!= NULL
) {
322 struct hist_entry
*n
= rb_entry(next
, struct hist_entry
, rb_node
);
324 next
= rb_next(&n
->rb_node
);
325 n
->position
= position
++;
328 rb_erase(&n
->rb_node
, &self
->entries
);
329 insert_hist_entry_by_name(&tmp
, n
);
337 static struct perf_evsel
*evsel_match(struct perf_evsel
*evsel
,
338 struct perf_evlist
*evlist
)
340 struct perf_evsel
*e
;
342 list_for_each_entry(e
, &evlist
->entries
, node
)
343 if (perf_evsel__match2(evsel
, e
))
349 static void perf_evlist__resort_hists(struct perf_evlist
*evlist
, bool name
)
351 struct perf_evsel
*evsel
;
353 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
354 struct hists
*hists
= &evsel
->hists
;
356 hists__output_resort(hists
);
359 * The hists__name_resort only sets possition
362 if (name
|| ((!name
) && show_displacement
))
363 hists__name_resort(hists
, name
);
367 static void hists__baseline_only(struct hists
*hists
)
369 struct rb_node
*next
= rb_first(&hists
->entries
);
371 while (next
!= NULL
) {
372 struct hist_entry
*he
= rb_entry(next
, struct hist_entry
, rb_node
);
374 next
= rb_next(&he
->rb_node
);
375 if (!hist_entry__next_pair(he
)) {
376 rb_erase(&he
->rb_node
, &hists
->entries
);
377 hist_entry__free(he
);
382 static void hists__precompute(struct hists
*hists
)
384 struct rb_node
*next
= rb_first(&hists
->entries
);
386 while (next
!= NULL
) {
387 struct hist_entry
*he
= rb_entry(next
, struct hist_entry
, rb_node
);
389 next
= rb_next(&he
->rb_node
);
393 perf_diff__compute_delta(he
);
396 perf_diff__compute_ratio(he
);
398 case COMPUTE_WEIGHTED_DIFF
:
399 perf_diff__compute_wdiff(he
);
407 static int64_t cmp_doubles(double l
, double r
)
418 hist_entry__cmp_compute(struct hist_entry
*left
, struct hist_entry
*right
,
424 double l
= left
->diff
.period_ratio_delta
;
425 double r
= right
->diff
.period_ratio_delta
;
427 return cmp_doubles(l
, r
);
431 double l
= left
->diff
.period_ratio
;
432 double r
= right
->diff
.period_ratio
;
434 return cmp_doubles(l
, r
);
436 case COMPUTE_WEIGHTED_DIFF
:
438 s64 l
= left
->diff
.wdiff
;
439 s64 r
= right
->diff
.wdiff
;
450 static void insert_hist_entry_by_compute(struct rb_root
*root
,
451 struct hist_entry
*he
,
454 struct rb_node
**p
= &root
->rb_node
;
455 struct rb_node
*parent
= NULL
;
456 struct hist_entry
*iter
;
460 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
461 if (hist_entry__cmp_compute(he
, iter
, c
) < 0)
467 rb_link_node(&he
->rb_node
, parent
, p
);
468 rb_insert_color(&he
->rb_node
, root
);
471 static void hists__compute_resort(struct hists
*hists
)
473 struct rb_root tmp
= RB_ROOT
;
474 struct rb_node
*next
= rb_first(&hists
->entries
);
476 while (next
!= NULL
) {
477 struct hist_entry
*he
= rb_entry(next
, struct hist_entry
, rb_node
);
479 next
= rb_next(&he
->rb_node
);
481 rb_erase(&he
->rb_node
, &hists
->entries
);
482 insert_hist_entry_by_compute(&tmp
, he
, compute
);
485 hists
->entries
= tmp
;
488 static void hists__process(struct hists
*old
, struct hists
*new)
490 hists__match(new, old
);
492 if (show_baseline_only
)
493 hists__baseline_only(new);
495 hists__link(new, old
);
498 hists__precompute(new);
499 hists__compute_resort(new);
502 hists__fprintf(new, true, 0, 0, stdout
);
505 static int __cmd_diff(void)
508 #define older (session[0])
509 #define newer (session[1])
510 struct perf_session
*session
[2];
511 struct perf_evlist
*evlist_new
, *evlist_old
;
512 struct perf_evsel
*evsel
;
515 older
= perf_session__new(input_old
, O_RDONLY
, force
, false,
517 newer
= perf_session__new(input_new
, O_RDONLY
, force
, false,
519 if (session
[0] == NULL
|| session
[1] == NULL
)
522 for (i
= 0; i
< 2; ++i
) {
523 ret
= perf_session__process_events(session
[i
], &tool
);
528 evlist_old
= older
->evlist
;
529 evlist_new
= newer
->evlist
;
531 perf_evlist__resort_hists(evlist_old
, true);
532 perf_evlist__resort_hists(evlist_new
, false);
534 list_for_each_entry(evsel
, &evlist_new
->entries
, node
) {
535 struct perf_evsel
*evsel_old
;
537 evsel_old
= evsel_match(evsel
, evlist_old
);
541 fprintf(stdout
, "%s# Event '%s'\n#\n", first
? "" : "\n",
542 perf_evsel__name(evsel
));
546 hists__process(&evsel_old
->hists
, &evsel
->hists
);
550 for (i
= 0; i
< 2; ++i
)
551 perf_session__delete(session
[i
]);
557 static const char * const diff_usage
[] = {
558 "perf diff [<options>] [old_file] [new_file]",
562 static const struct option options
[] = {
563 OPT_INCR('v', "verbose", &verbose
,
564 "be more verbose (show symbol address, etc)"),
565 OPT_BOOLEAN('M', "displacement", &show_displacement
,
566 "Show position displacement relative to baseline"),
567 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only
,
568 "Show only items with match in baseline"),
569 OPT_CALLBACK('c', "compute", &compute
,
570 "delta,ratio,wdiff:w1,w2 (default delta)",
571 "Entries differential computation selection",
573 OPT_BOOLEAN('p', "period", &show_period
,
574 "Show period values."),
575 OPT_BOOLEAN('F', "formula", &show_formula
,
577 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
578 "dump raw trace in ASCII"),
579 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
580 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
581 "load module symbols - WARNING: use only with -k and LIVE kernel"),
582 OPT_STRING('d', "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
583 "only consider symbols in these dsos"),
584 OPT_STRING('C', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
585 "only consider symbols in these comms"),
586 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
587 "only consider these symbols"),
588 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
589 "sort by key(s): pid, comm, dso, symbol, parent"),
590 OPT_STRING('t', "field-separator", &symbol_conf
.field_sep
, "separator",
591 "separator for columns, no spaces will be added between "
592 "columns '.' is reserved."),
593 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
594 "Look for files with symbols relative to this directory"),
598 static void ui_init(void)
602 /* No overhead column. */
603 perf_hpp__column_enable(PERF_HPP__OVERHEAD
, false);
606 * Display baseline/delta/ratio/displacement/
607 * formula/periods columns.
609 perf_hpp__column_enable(PERF_HPP__BASELINE
, true);
613 perf_hpp__column_enable(PERF_HPP__DELTA
, true);
616 perf_hpp__column_enable(PERF_HPP__RATIO
, true);
618 case COMPUTE_WEIGHTED_DIFF
:
619 perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF
, true);
625 if (show_displacement
)
626 perf_hpp__column_enable(PERF_HPP__DISPL
, true);
629 perf_hpp__column_enable(PERF_HPP__FORMULA
, true);
632 perf_hpp__column_enable(PERF_HPP__PERIOD
, true);
633 perf_hpp__column_enable(PERF_HPP__PERIOD_BASELINE
, true);
637 int cmd_diff(int argc
, const char **argv
, const char *prefix __maybe_unused
)
639 sort_order
= diff__default_sort_order
;
640 argc
= parse_options(argc
, argv
, options
, diff_usage
, 0);
643 usage_with_options(diff_usage
, options
);
649 } else if (symbol_conf
.default_guest_vmlinux_name
||
650 symbol_conf
.default_guest_kallsyms
) {
651 input_old
= "perf.data.host";
652 input_new
= "perf.data.guest";
655 symbol_conf
.exclude_other
= false;
656 if (symbol__init() < 0)
661 setup_sorting(diff_usage
, options
);
664 sort_entry__setup_elide(&sort_dso
, symbol_conf
.dso_list
, "dso", NULL
);
665 sort_entry__setup_elide(&sort_comm
, symbol_conf
.comm_list
, "comm", NULL
);
666 sort_entry__setup_elide(&sort_sym
, symbol_conf
.sym_list
, "symbol", NULL
);