]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/builtin-diff.c
perf diff: Introducing diff_data object to hold files
[mirror_ubuntu-artful-kernel.git] / tools / perf / builtin-diff.c
CommitLineData
86a9eee0
ACM
1/*
2 * builtin-diff.c
3 *
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.
6 */
7#include "builtin.h"
8
9#include "util/debug.h"
10#include "util/event.h"
11#include "util/hist.h"
743eb868 12#include "util/evsel.h"
863e451f 13#include "util/evlist.h"
86a9eee0 14#include "util/session.h"
45694aa7 15#include "util/tool.h"
86a9eee0
ACM
16#include "util/sort.h"
17#include "util/symbol.h"
18#include "util/util.h"
19
20#include <stdlib.h>
21
ec308426
JO
22struct data__file {
23 struct perf_session *session;
24 const char *file;
25 int idx;
26};
27
28static struct data__file *data__files;
29static int data__files_cnt;
30
31#define data__for_each_file_start(i, d, s) \
32 for (i = s, d = &data__files[s]; \
33 i < data__files_cnt; \
34 i++, d = &data__files[i])
35
36#define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
37
38static char diff__default_sort_order[] = "dso,symbol";
39static bool force;
61949b21 40static bool show_period;
ed279da2 41static bool show_formula;
a06d143e 42static bool show_baseline_only;
96c47f19 43static bool sort_compute;
86a9eee0 44
81d5f958
JO
45static s64 compute_wdiff_w1;
46static s64 compute_wdiff_w2;
47
7aaf6b35
JO
48enum {
49 COMPUTE_DELTA,
50 COMPUTE_RATIO,
81d5f958 51 COMPUTE_WEIGHTED_DIFF,
7aaf6b35
JO
52 COMPUTE_MAX,
53};
54
55const char *compute_names[COMPUTE_MAX] = {
56 [COMPUTE_DELTA] = "delta",
57 [COMPUTE_RATIO] = "ratio",
81d5f958 58 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
7aaf6b35
JO
59};
60
61static int compute;
62
81d5f958
JO
63static int setup_compute_opt_wdiff(char *opt)
64{
65 char *w1_str = opt;
66 char *w2_str;
67
68 int ret = -EINVAL;
69
70 if (!opt)
71 goto out;
72
73 w2_str = strchr(opt, ',');
74 if (!w2_str)
75 goto out;
76
77 *w2_str++ = 0x0;
78 if (!*w2_str)
79 goto out;
80
81 compute_wdiff_w1 = strtol(w1_str, NULL, 10);
82 compute_wdiff_w2 = strtol(w2_str, NULL, 10);
83
84 if (!compute_wdiff_w1 || !compute_wdiff_w2)
85 goto out;
86
87 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
88 compute_wdiff_w1, compute_wdiff_w2);
89
90 ret = 0;
91
92 out:
93 if (ret)
94 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
95
96 return ret;
97}
98
99static int setup_compute_opt(char *opt)
100{
101 if (compute == COMPUTE_WEIGHTED_DIFF)
102 return setup_compute_opt_wdiff(opt);
103
104 if (opt) {
105 pr_err("Failed: extra option specified '%s'", opt);
106 return -EINVAL;
107 }
108
109 return 0;
110}
111
7aaf6b35
JO
112static int setup_compute(const struct option *opt, const char *str,
113 int unset __maybe_unused)
114{
115 int *cp = (int *) opt->value;
81d5f958
JO
116 char *cstr = (char *) str;
117 char buf[50];
7aaf6b35 118 unsigned i;
81d5f958 119 char *option;
7aaf6b35
JO
120
121 if (!str) {
122 *cp = COMPUTE_DELTA;
123 return 0;
124 }
125
96c47f19
JO
126 if (*str == '+') {
127 sort_compute = true;
81d5f958 128 cstr = (char *) ++str;
96c47f19
JO
129 if (!*str)
130 return 0;
131 }
132
81d5f958
JO
133 option = strchr(str, ':');
134 if (option) {
135 unsigned len = option++ - str;
136
137 /*
138 * The str data are not writeable, so we need
139 * to use another buffer.
140 */
141
142 /* No option value is longer. */
143 if (len >= sizeof(buf))
144 return -EINVAL;
145
146 strncpy(buf, str, len);
147 buf[len] = 0x0;
148 cstr = buf;
149 }
150
7aaf6b35 151 for (i = 0; i < COMPUTE_MAX; i++)
81d5f958 152 if (!strcmp(cstr, compute_names[i])) {
7aaf6b35 153 *cp = i;
81d5f958 154 return setup_compute_opt(option);
7aaf6b35
JO
155 }
156
157 pr_err("Failed: '%s' is not computation method "
81d5f958 158 "(use 'delta','ratio' or 'wdiff')\n", str);
7aaf6b35
JO
159 return -EINVAL;
160}
161
05472daa 162double perf_diff__period_percent(struct hist_entry *he, u64 period)
96c47f19
JO
163{
164 u64 total = he->hists->stats.total_period;
165 return (period * 100.0) / total;
166}
167
05472daa 168double perf_diff__compute_delta(struct hist_entry *he, struct hist_entry *pair)
96c47f19 169{
05472daa
JO
170 double new_percent = perf_diff__period_percent(he, he->stat.period);
171 double old_percent = perf_diff__period_percent(pair, pair->stat.period);
96c47f19
JO
172
173 he->diff.period_ratio_delta = new_percent - old_percent;
174 he->diff.computed = true;
175 return he->diff.period_ratio_delta;
176}
177
05472daa 178double perf_diff__compute_ratio(struct hist_entry *he, struct hist_entry *pair)
96c47f19 179{
96c47f19 180 double new_period = he->stat.period;
05472daa 181 double old_period = pair->stat.period;
96c47f19
JO
182
183 he->diff.computed = true;
05472daa 184 he->diff.period_ratio = new_period / old_period;
96c47f19
JO
185 return he->diff.period_ratio;
186}
187
05472daa 188s64 perf_diff__compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
81d5f958 189{
81d5f958 190 u64 new_period = he->stat.period;
05472daa 191 u64 old_period = pair->stat.period;
81d5f958
JO
192
193 he->diff.computed = true;
05472daa
JO
194 he->diff.wdiff = new_period * compute_wdiff_w2 -
195 old_period * compute_wdiff_w1;
81d5f958
JO
196
197 return he->diff.wdiff;
198}
199
f4c8bae1
JO
200static int formula_delta(struct hist_entry *he, struct hist_entry *pair,
201 char *buf, size_t size)
ed279da2 202{
ed279da2
JO
203 return scnprintf(buf, size,
204 "(%" PRIu64 " * 100 / %" PRIu64 ") - "
205 "(%" PRIu64 " * 100 / %" PRIu64 ")",
206 he->stat.period, he->hists->stats.total_period,
207 pair->stat.period, pair->hists->stats.total_period);
208}
209
f4c8bae1
JO
210static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,
211 char *buf, size_t size)
ed279da2 212{
ed279da2 213 double new_period = he->stat.period;
f4c8bae1 214 double old_period = pair->stat.period;
ed279da2
JO
215
216 return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
217}
218
f4c8bae1
JO
219static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,
220 char *buf, size_t size)
ed279da2 221{
ed279da2 222 u64 new_period = he->stat.period;
f4c8bae1 223 u64 old_period = pair->stat.period;
ed279da2
JO
224
225 return scnprintf(buf, size,
226 "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
227 new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
228}
229
f4c8bae1
JO
230int perf_diff__formula(struct hist_entry *he, struct hist_entry *pair,
231 char *buf, size_t size)
ed279da2
JO
232{
233 switch (compute) {
234 case COMPUTE_DELTA:
f4c8bae1 235 return formula_delta(he, pair, buf, size);
ed279da2 236 case COMPUTE_RATIO:
f4c8bae1 237 return formula_ratio(he, pair, buf, size);
ed279da2 238 case COMPUTE_WEIGHTED_DIFF:
f4c8bae1 239 return formula_wdiff(he, pair, buf, size);
ed279da2
JO
240 default:
241 BUG_ON(1);
242 }
243
244 return -1;
245}
246
1c02c4d2 247static int hists__add_entry(struct hists *self,
05484298
AK
248 struct addr_location *al, u64 period,
249 u64 weight)
86a9eee0 250{
05484298 251 if (__hists__add_entry(self, al, NULL, period, weight) != NULL)
28e2a106
ACM
252 return 0;
253 return -ENOMEM;
86a9eee0
ACM
254}
255
1d037ca1 256static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
d20deb64 257 union perf_event *event,
8d50e5b4 258 struct perf_sample *sample,
863e451f 259 struct perf_evsel *evsel,
743eb868 260 struct machine *machine)
86a9eee0
ACM
261{
262 struct addr_location al;
86a9eee0 263
743eb868 264 if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
86a9eee0
ACM
265 pr_warning("problem processing %d event, skipping it.\n",
266 event->header.type);
267 return -1;
268 }
269
d88c48f9 270 if (al.filtered)
c410a338
ACM
271 return 0;
272
05484298 273 if (hists__add_entry(&evsel->hists, &al, sample->period, sample->weight)) {
c82ee828 274 pr_warning("problem incrementing symbol period, skipping event\n");
86a9eee0
ACM
275 return -1;
276 }
277
863e451f 278 evsel->hists.stats.total_period += sample->period;
86a9eee0
ACM
279 return 0;
280}
281
863e451f
JO
282static struct perf_tool tool = {
283 .sample = diff__process_sample_event,
284 .mmap = perf_event__process_mmap,
285 .comm = perf_event__process_comm,
f62d3f0f
ACM
286 .exit = perf_event__process_exit,
287 .fork = perf_event__process_fork,
863e451f
JO
288 .lost = perf_event__process_lost,
289 .ordered_samples = true,
290 .ordering_requires_timestamps = true,
86a9eee0
ACM
291};
292
863e451f
JO
293static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
294 struct perf_evlist *evlist)
295{
296 struct perf_evsel *e;
297
298 list_for_each_entry(e, &evlist->entries, node)
299 if (perf_evsel__match2(evsel, e))
300 return e;
301
302 return NULL;
303}
304
ce74f60e 305static void perf_evlist__collapse_resort(struct perf_evlist *evlist)
dd464345
JO
306{
307 struct perf_evsel *evsel;
308
309 list_for_each_entry(evsel, &evlist->entries, node) {
310 struct hists *hists = &evsel->hists;
311
ce74f60e 312 hists__collapse_resort(hists);
dd464345
JO
313 }
314}
315
a06d143e
JO
316static void hists__baseline_only(struct hists *hists)
317{
ce74f60e
NK
318 struct rb_root *root;
319 struct rb_node *next;
320
321 if (sort__need_collapse)
322 root = &hists->entries_collapsed;
323 else
324 root = hists->entries_in;
a06d143e 325
ce74f60e 326 next = rb_first(root);
a06d143e 327 while (next != NULL) {
ce74f60e 328 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
a06d143e 329
ce74f60e 330 next = rb_next(&he->rb_node_in);
b821c732 331 if (!hist_entry__next_pair(he)) {
ce74f60e 332 rb_erase(&he->rb_node_in, root);
a06d143e
JO
333 hist_entry__free(he);
334 }
335 }
336}
337
96c47f19
JO
338static void hists__precompute(struct hists *hists)
339{
367c53c0
JO
340 struct rb_root *root;
341 struct rb_node *next;
342
343 if (sort__need_collapse)
344 root = &hists->entries_collapsed;
345 else
346 root = hists->entries_in;
96c47f19 347
367c53c0 348 next = rb_first(root);
96c47f19 349 while (next != NULL) {
367c53c0 350 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
05472daa 351 struct hist_entry *pair = hist_entry__next_pair(he);
96c47f19 352
367c53c0 353 next = rb_next(&he->rb_node_in);
05472daa
JO
354 if (!pair)
355 continue;
96c47f19
JO
356
357 switch (compute) {
358 case COMPUTE_DELTA:
05472daa 359 perf_diff__compute_delta(he, pair);
96c47f19
JO
360 break;
361 case COMPUTE_RATIO:
05472daa 362 perf_diff__compute_ratio(he, pair);
96c47f19 363 break;
81d5f958 364 case COMPUTE_WEIGHTED_DIFF:
05472daa 365 perf_diff__compute_wdiff(he, pair);
81d5f958 366 break;
96c47f19
JO
367 default:
368 BUG_ON(1);
369 }
370 }
371}
372
373static int64_t cmp_doubles(double l, double r)
374{
375 if (l > r)
376 return -1;
377 else if (l < r)
378 return 1;
379 else
380 return 0;
381}
382
383static int64_t
384hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
385 int c)
386{
387 switch (c) {
388 case COMPUTE_DELTA:
389 {
390 double l = left->diff.period_ratio_delta;
391 double r = right->diff.period_ratio_delta;
392
393 return cmp_doubles(l, r);
394 }
395 case COMPUTE_RATIO:
396 {
397 double l = left->diff.period_ratio;
398 double r = right->diff.period_ratio;
399
400 return cmp_doubles(l, r);
401 }
81d5f958
JO
402 case COMPUTE_WEIGHTED_DIFF:
403 {
404 s64 l = left->diff.wdiff;
405 s64 r = right->diff.wdiff;
406
407 return r - l;
408 }
96c47f19
JO
409 default:
410 BUG_ON(1);
411 }
412
413 return 0;
414}
415
416static void insert_hist_entry_by_compute(struct rb_root *root,
417 struct hist_entry *he,
418 int c)
419{
420 struct rb_node **p = &root->rb_node;
421 struct rb_node *parent = NULL;
422 struct hist_entry *iter;
423
424 while (*p != NULL) {
425 parent = *p;
426 iter = rb_entry(parent, struct hist_entry, rb_node);
427 if (hist_entry__cmp_compute(he, iter, c) < 0)
428 p = &(*p)->rb_left;
429 else
430 p = &(*p)->rb_right;
431 }
432
433 rb_link_node(&he->rb_node, parent, p);
434 rb_insert_color(&he->rb_node, root);
435}
436
437static void hists__compute_resort(struct hists *hists)
438{
66f97ed3
NK
439 struct rb_root *root;
440 struct rb_node *next;
441
442 if (sort__need_collapse)
443 root = &hists->entries_collapsed;
444 else
445 root = hists->entries_in;
446
447 hists->entries = RB_ROOT;
448 next = rb_first(root);
449
450 hists->nr_entries = 0;
451 hists->stats.total_period = 0;
452 hists__reset_col_len(hists);
96c47f19
JO
453
454 while (next != NULL) {
66f97ed3 455 struct hist_entry *he;
96c47f19 456
66f97ed3
NK
457 he = rb_entry(next, struct hist_entry, rb_node_in);
458 next = rb_next(&he->rb_node_in);
96c47f19 459
66f97ed3
NK
460 insert_hist_entry_by_compute(&hists->entries, he, compute);
461 hists__inc_nr_entries(hists, he);
96c47f19 462 }
96c47f19
JO
463}
464
a06d143e
JO
465static void hists__process(struct hists *old, struct hists *new)
466{
95529be4 467 hists__match(new, old);
a06d143e
JO
468
469 if (show_baseline_only)
470 hists__baseline_only(new);
bfaef4b4
ACM
471 else
472 hists__link(new, old);
a06d143e 473
96c47f19
JO
474 if (sort_compute) {
475 hists__precompute(new);
476 hists__compute_resort(new);
66f97ed3
NK
477 } else {
478 hists__output_resort(new);
96c47f19
JO
479 }
480
064f1981 481 hists__fprintf(new, true, 0, 0, 0, stdout);
a06d143e
JO
482}
483
ec308426 484static void data_process(void)
86a9eee0 485{
ec308426
JO
486 struct perf_evlist *evlist_old = data__files[0].session->evlist;
487 struct perf_evlist *evlist_new = data__files[1].session->evlist;
488 struct perf_evsel *evsel_old;
863e451f 489 bool first = true;
86a9eee0 490
ec308426
JO
491 list_for_each_entry(evsel_old, &evlist_old->entries, node) {
492 struct perf_evsel *evsel_new;
86a9eee0 493
ec308426
JO
494 evsel_new = evsel_match(evsel_old, evlist_new);
495 if (!evsel_new)
496 continue;
86a9eee0 497
ec308426
JO
498 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
499 perf_evsel__name(evsel_old));
863e451f 500
ec308426 501 first = false;
863e451f 502
ec308426
JO
503 hists__process(&evsel_old->hists, &evsel_new->hists);
504 }
505}
863e451f 506
ec308426
JO
507static int __cmd_diff(void)
508{
509 struct data__file *d;
510 int ret = -EINVAL, i;
511
512 data__for_each_file(i, d) {
513 d->session = perf_session__new(d->file, O_RDONLY, force,
514 false, &tool);
515 if (!d->session) {
516 pr_err("Failed to open %s\n", d->file);
517 ret = -ENOMEM;
518 goto out_delete;
519 }
863e451f 520
ec308426
JO
521 ret = perf_session__process_events(d->session, &tool);
522 if (ret) {
523 pr_err("Failed to process %s\n", d->file);
524 goto out_delete;
525 }
863e451f 526
ec308426
JO
527 perf_evlist__collapse_resort(d->session->evlist);
528 }
529
530 data_process();
863e451f 531
ec308426
JO
532 out_delete:
533 data__for_each_file(i, d) {
534 if (d->session)
535 perf_session__delete(d->session);
863e451f 536 }
9c443dfd 537
ec308426 538 free(data__files);
86a9eee0
ACM
539 return ret;
540}
541
0422a4fc 542static const char * const diff_usage[] = {
86a9eee0 543 "perf diff [<options>] [old_file] [new_file]",
0422a4fc 544 NULL,
86a9eee0
ACM
545};
546
547static const struct option options[] = {
c0555642 548 OPT_INCR('v', "verbose", &verbose,
86a9eee0 549 "be more verbose (show symbol address, etc)"),
a06d143e
JO
550 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
551 "Show only items with match in baseline"),
81d5f958
JO
552 OPT_CALLBACK('c', "compute", &compute,
553 "delta,ratio,wdiff:w1,w2 (default delta)",
7aaf6b35
JO
554 "Entries differential computation selection",
555 setup_compute),
61949b21
JO
556 OPT_BOOLEAN('p', "period", &show_period,
557 "Show period values."),
ed279da2
JO
558 OPT_BOOLEAN('F', "formula", &show_formula,
559 "Show formula."),
86a9eee0
ACM
560 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
561 "dump raw trace in ASCII"),
562 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
563 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
564 "load module symbols - WARNING: use only with -k and LIVE kernel"),
c410a338
ACM
565 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
566 "only consider symbols in these dsos"),
567 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
568 "only consider symbols in these comms"),
569 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
570 "only consider these symbols"),
c351c281
ACM
571 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
572 "sort by key(s): pid, comm, dso, symbol, parent"),
573 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
574 "separator for columns, no spaces will be added between "
575 "columns '.' is reserved."),
ec5761ea
DA
576 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
577 "Look for files with symbols relative to this directory"),
86a9eee0
ACM
578 OPT_END()
579};
580
1d77822e
JO
581static void ui_init(void)
582{
ed279da2 583 /*
db6d0bb8 584 * Display baseline/delta/ratio
ed279da2
JO
585 * formula/periods columns.
586 */
1240005e 587 perf_hpp__column_enable(PERF_HPP__BASELINE);
7aaf6b35
JO
588
589 switch (compute) {
590 case COMPUTE_DELTA:
1240005e 591 perf_hpp__column_enable(PERF_HPP__DELTA);
7aaf6b35
JO
592 break;
593 case COMPUTE_RATIO:
1240005e 594 perf_hpp__column_enable(PERF_HPP__RATIO);
81d5f958
JO
595 break;
596 case COMPUTE_WEIGHTED_DIFF:
1240005e 597 perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF);
7aaf6b35
JO
598 break;
599 default:
600 BUG_ON(1);
601 };
1d77822e 602
ed279da2 603 if (show_formula)
1240005e 604 perf_hpp__column_enable(PERF_HPP__FORMULA);
ed279da2 605
61949b21 606 if (show_period) {
1240005e
JO
607 perf_hpp__column_enable(PERF_HPP__PERIOD);
608 perf_hpp__column_enable(PERF_HPP__PERIOD_BASELINE);
61949b21 609 }
1d77822e
JO
610}
611
ec308426 612static int data_init(int argc, const char **argv)
86a9eee0 613{
ec308426
JO
614 struct data__file *d;
615 static const char *defaults[] = {
616 "perf.data.old",
617 "perf.data",
618 };
619 int i;
620
621 data__files_cnt = 2;
622
86a9eee0
ACM
623 if (argc) {
624 if (argc > 2)
625 usage_with_options(diff_usage, options);
626 if (argc == 2) {
ec308426
JO
627 defaults[0] = argv[0];
628 defaults[1] = argv[1];
86a9eee0 629 } else
ec308426 630 defaults[1] = argv[0];
a1645ce1
ZY
631 } else if (symbol_conf.default_guest_vmlinux_name ||
632 symbol_conf.default_guest_kallsyms) {
ec308426
JO
633 defaults[0] = "perf.data.host";
634 defaults[1] = "perf.data.guest";
86a9eee0
ACM
635 }
636
ec308426
JO
637 data__files = zalloc(sizeof(*data__files) * data__files_cnt);
638 if (!data__files)
639 return -ENOMEM;
640
641 data__for_each_file(i, d) {
642 d->file = defaults[i];
643 d->idx = i;
644 }
645
646 return 0;
647}
648
649int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
650{
651 sort_order = diff__default_sort_order;
652 argc = parse_options(argc, argv, options, diff_usage, 0);
653
655000e7
ACM
654 if (symbol__init() < 0)
655 return -1;
656
ec308426
JO
657 if (data_init(argc, argv) < 0)
658 return -1;
659
1d77822e
JO
660 ui_init();
661
55309985
NK
662 if (setup_sorting() < 0)
663 usage_with_options(diff_usage, options);
664
86a9eee0 665 setup_pager();
c351c281 666
08e71542 667 sort__setup_elide(NULL);
c351c281 668
86a9eee0
ACM
669 return __cmd_diff();
670}