blob: 9fbbc01c5ad70bd3444b093446a4c6f876c3bb12 [file] [log] [blame]
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001/*
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"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020012#include "util/evsel.h"
Jiri Olsa863e4512012-09-06 17:46:55 +020013#include "util/evlist.h"
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020014#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020015#include "util/tool.h"
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020016#include "util/sort.h"
17#include "util/symbol.h"
18#include "util/util.h"
19
20#include <stdlib.h>
21
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -020022static char const *input_old = "perf.data.old",
23 *input_new = "perf.data";
Arnaldo Carvalho de Melo604c5c92009-12-16 14:09:53 -020024static char diff__default_sort_order[] = "dso,symbol";
Ian Munsiec0555642010-04-13 18:37:33 +100025static bool force;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -020026static bool show_displacement;
Jiri Olsa61949b22012-10-05 16:44:44 +020027static bool show_period;
Jiri Olsaed279da2012-10-05 16:44:45 +020028static bool show_formula;
Jiri Olsaa06d1432012-10-05 16:44:40 +020029static bool show_baseline_only;
Jiri Olsa96c47f12012-10-05 16:44:42 +020030static bool sort_compute;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020031
Jiri Olsa81d5f952012-10-05 16:44:43 +020032static s64 compute_wdiff_w1;
33static s64 compute_wdiff_w2;
34
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020035enum {
36 COMPUTE_DELTA,
37 COMPUTE_RATIO,
Jiri Olsa81d5f952012-10-05 16:44:43 +020038 COMPUTE_WEIGHTED_DIFF,
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020039 COMPUTE_MAX,
40};
41
42const char *compute_names[COMPUTE_MAX] = {
43 [COMPUTE_DELTA] = "delta",
44 [COMPUTE_RATIO] = "ratio",
Jiri Olsa81d5f952012-10-05 16:44:43 +020045 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020046};
47
48static int compute;
49
Jiri Olsa81d5f952012-10-05 16:44:43 +020050static int setup_compute_opt_wdiff(char *opt)
51{
52 char *w1_str = opt;
53 char *w2_str;
54
55 int ret = -EINVAL;
56
57 if (!opt)
58 goto out;
59
60 w2_str = strchr(opt, ',');
61 if (!w2_str)
62 goto out;
63
64 *w2_str++ = 0x0;
65 if (!*w2_str)
66 goto out;
67
68 compute_wdiff_w1 = strtol(w1_str, NULL, 10);
69 compute_wdiff_w2 = strtol(w2_str, NULL, 10);
70
71 if (!compute_wdiff_w1 || !compute_wdiff_w2)
72 goto out;
73
74 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
75 compute_wdiff_w1, compute_wdiff_w2);
76
77 ret = 0;
78
79 out:
80 if (ret)
81 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
82
83 return ret;
84}
85
86static int setup_compute_opt(char *opt)
87{
88 if (compute == COMPUTE_WEIGHTED_DIFF)
89 return setup_compute_opt_wdiff(opt);
90
91 if (opt) {
92 pr_err("Failed: extra option specified '%s'", opt);
93 return -EINVAL;
94 }
95
96 return 0;
97}
98
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020099static int setup_compute(const struct option *opt, const char *str,
100 int unset __maybe_unused)
101{
102 int *cp = (int *) opt->value;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200103 char *cstr = (char *) str;
104 char buf[50];
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200105 unsigned i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200106 char *option;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200107
108 if (!str) {
109 *cp = COMPUTE_DELTA;
110 return 0;
111 }
112
Jiri Olsa96c47f12012-10-05 16:44:42 +0200113 if (*str == '+') {
114 sort_compute = true;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200115 cstr = (char *) ++str;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200116 if (!*str)
117 return 0;
118 }
119
Jiri Olsa81d5f952012-10-05 16:44:43 +0200120 option = strchr(str, ':');
121 if (option) {
122 unsigned len = option++ - str;
123
124 /*
125 * The str data are not writeable, so we need
126 * to use another buffer.
127 */
128
129 /* No option value is longer. */
130 if (len >= sizeof(buf))
131 return -EINVAL;
132
133 strncpy(buf, str, len);
134 buf[len] = 0x0;
135 cstr = buf;
136 }
137
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200138 for (i = 0; i < COMPUTE_MAX; i++)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200139 if (!strcmp(cstr, compute_names[i])) {
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200140 *cp = i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200141 return setup_compute_opt(option);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200142 }
143
144 pr_err("Failed: '%s' is not computation method "
Jiri Olsa81d5f952012-10-05 16:44:43 +0200145 "(use 'delta','ratio' or 'wdiff')\n", str);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200146 return -EINVAL;
147}
148
Jiri Olsa96c47f12012-10-05 16:44:42 +0200149static double get_period_percent(struct hist_entry *he, u64 period)
150{
151 u64 total = he->hists->stats.total_period;
152 return (period * 100.0) / total;
153}
154
155double perf_diff__compute_delta(struct hist_entry *he)
156{
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200157 struct hist_entry *pair = hist_entry__next_pair(he);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200158 double new_percent = get_period_percent(he, he->stat.period);
159 double old_percent = pair ? get_period_percent(pair, pair->stat.period) : 0.0;
160
161 he->diff.period_ratio_delta = new_percent - old_percent;
162 he->diff.computed = true;
163 return he->diff.period_ratio_delta;
164}
165
166double perf_diff__compute_ratio(struct hist_entry *he)
167{
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200168 struct hist_entry *pair = hist_entry__next_pair(he);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200169 double new_period = he->stat.period;
170 double old_period = pair ? pair->stat.period : 0;
171
172 he->diff.computed = true;
173 he->diff.period_ratio = pair ? (new_period / old_period) : 0;
174 return he->diff.period_ratio;
175}
176
Jiri Olsa81d5f952012-10-05 16:44:43 +0200177s64 perf_diff__compute_wdiff(struct hist_entry *he)
178{
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200179 struct hist_entry *pair = hist_entry__next_pair(he);
Jiri Olsa81d5f952012-10-05 16:44:43 +0200180 u64 new_period = he->stat.period;
181 u64 old_period = pair ? pair->stat.period : 0;
182
183 he->diff.computed = true;
184
185 if (!pair)
186 he->diff.wdiff = 0;
187 else
188 he->diff.wdiff = new_period * compute_wdiff_w2 -
189 old_period * compute_wdiff_w1;
190
191 return he->diff.wdiff;
192}
193
Jiri Olsaed279da2012-10-05 16:44:45 +0200194static int formula_delta(struct hist_entry *he, char *buf, size_t size)
195{
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200196 struct hist_entry *pair = hist_entry__next_pair(he);
Jiri Olsaed279da2012-10-05 16:44:45 +0200197
198 if (!pair)
199 return -1;
200
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);
206}
207
208static int formula_ratio(struct hist_entry *he, char *buf, size_t size)
209{
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200210 struct hist_entry *pair = hist_entry__next_pair(he);
Jiri Olsaed279da2012-10-05 16:44:45 +0200211 double new_period = he->stat.period;
212 double old_period = pair ? pair->stat.period : 0;
213
214 if (!pair)
215 return -1;
216
217 return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
218}
219
220static int formula_wdiff(struct hist_entry *he, char *buf, size_t size)
221{
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200222 struct hist_entry *pair = hist_entry__next_pair(he);
Jiri Olsaed279da2012-10-05 16:44:45 +0200223 u64 new_period = he->stat.period;
224 u64 old_period = pair ? pair->stat.period : 0;
225
226 if (!pair)
227 return -1;
228
229 return scnprintf(buf, size,
230 "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
231 new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
232}
233
234int perf_diff__formula(char *buf, size_t size, struct hist_entry *he)
235{
236 switch (compute) {
237 case COMPUTE_DELTA:
238 return formula_delta(he, buf, size);
239 case COMPUTE_RATIO:
240 return formula_ratio(he, buf, size);
241 case COMPUTE_WEIGHTED_DIFF:
242 return formula_wdiff(he, buf, size);
243 default:
244 BUG_ON(1);
245 }
246
247 return -1;
248}
249
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300250static int hists__add_entry(struct hists *self,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300251 struct addr_location *al, u64 period)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200252{
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300253 if (__hists__add_entry(self, al, NULL, period) != NULL)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300254 return 0;
255 return -ENOMEM;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200256}
257
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300258static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200259 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200260 struct perf_sample *sample,
Jiri Olsa863e4512012-09-06 17:46:55 +0200261 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200262 struct machine *machine)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200263{
264 struct addr_location al;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200265
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200266 if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200267 pr_warning("problem processing %d event, skipping it.\n",
268 event->header.type);
269 return -1;
270 }
271
Jiri Olsad88c48f2012-10-05 16:44:46 +0200272 if (al.filtered)
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200273 return 0;
274
Jiri Olsa863e4512012-09-06 17:46:55 +0200275 if (hists__add_entry(&evsel->hists, &al, sample->period)) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300276 pr_warning("problem incrementing symbol period, skipping event\n");
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200277 return -1;
278 }
279
Jiri Olsa863e4512012-09-06 17:46:55 +0200280 evsel->hists.stats.total_period += sample->period;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200281 return 0;
282}
283
Jiri Olsa863e4512012-09-06 17:46:55 +0200284static struct perf_tool tool = {
285 .sample = diff__process_sample_event,
286 .mmap = perf_event__process_mmap,
287 .comm = perf_event__process_comm,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300288 .exit = perf_event__process_exit,
289 .fork = perf_event__process_fork,
Jiri Olsa863e4512012-09-06 17:46:55 +0200290 .lost = perf_event__process_lost,
291 .ordered_samples = true,
292 .ordering_requires_timestamps = true,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200293};
294
Jiri Olsadd464342012-10-04 21:49:36 +0900295static void insert_hist_entry_by_name(struct rb_root *root,
296 struct hist_entry *he)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200297{
298 struct rb_node **p = &root->rb_node;
299 struct rb_node *parent = NULL;
300 struct hist_entry *iter;
301
302 while (*p != NULL) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200303 parent = *p;
304 iter = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200305 if (hist_entry__cmp(he, iter) < 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200306 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200307 else
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200308 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200309 }
310
311 rb_link_node(&he->rb_node, parent, p);
312 rb_insert_color(&he->rb_node, root);
313}
314
Jiri Olsadd464342012-10-04 21:49:36 +0900315static void hists__name_resort(struct hists *self, bool sort)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200316{
317 unsigned long position = 1;
318 struct rb_root tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300319 struct rb_node *next = rb_first(&self->entries);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200320
321 while (next != NULL) {
322 struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
323
324 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200325 n->position = position++;
Jiri Olsadd464342012-10-04 21:49:36 +0900326
327 if (sort) {
328 rb_erase(&n->rb_node, &self->entries);
329 insert_hist_entry_by_name(&tmp, n);
330 }
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200331 }
332
Jiri Olsadd464342012-10-04 21:49:36 +0900333 if (sort)
334 self->entries = tmp;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200335}
336
Jiri Olsa863e4512012-09-06 17:46:55 +0200337static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
338 struct perf_evlist *evlist)
339{
340 struct perf_evsel *e;
341
342 list_for_each_entry(e, &evlist->entries, node)
343 if (perf_evsel__match2(evsel, e))
344 return e;
345
346 return NULL;
347}
348
Jiri Olsadd464342012-10-04 21:49:36 +0900349static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
350{
351 struct perf_evsel *evsel;
352
353 list_for_each_entry(evsel, &evlist->entries, node) {
354 struct hists *hists = &evsel->hists;
355
356 hists__output_resort(hists);
357
358 /*
359 * The hists__name_resort only sets possition
360 * if name is false.
361 */
362 if (name || ((!name) && show_displacement))
363 hists__name_resort(hists, name);
364 }
365}
366
Jiri Olsaa06d1432012-10-05 16:44:40 +0200367static void hists__baseline_only(struct hists *hists)
368{
369 struct rb_node *next = rb_first(&hists->entries);
370
371 while (next != NULL) {
372 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
373
374 next = rb_next(&he->rb_node);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200375 if (!hist_entry__next_pair(he)) {
Jiri Olsaa06d1432012-10-05 16:44:40 +0200376 rb_erase(&he->rb_node, &hists->entries);
377 hist_entry__free(he);
378 }
379 }
380}
381
Jiri Olsa96c47f12012-10-05 16:44:42 +0200382static void hists__precompute(struct hists *hists)
383{
384 struct rb_node *next = rb_first(&hists->entries);
385
386 while (next != NULL) {
387 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
388
389 next = rb_next(&he->rb_node);
390
391 switch (compute) {
392 case COMPUTE_DELTA:
393 perf_diff__compute_delta(he);
394 break;
395 case COMPUTE_RATIO:
396 perf_diff__compute_ratio(he);
397 break;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200398 case COMPUTE_WEIGHTED_DIFF:
399 perf_diff__compute_wdiff(he);
400 break;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200401 default:
402 BUG_ON(1);
403 }
404 }
405}
406
407static int64_t cmp_doubles(double l, double r)
408{
409 if (l > r)
410 return -1;
411 else if (l < r)
412 return 1;
413 else
414 return 0;
415}
416
417static int64_t
418hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
419 int c)
420{
421 switch (c) {
422 case COMPUTE_DELTA:
423 {
424 double l = left->diff.period_ratio_delta;
425 double r = right->diff.period_ratio_delta;
426
427 return cmp_doubles(l, r);
428 }
429 case COMPUTE_RATIO:
430 {
431 double l = left->diff.period_ratio;
432 double r = right->diff.period_ratio;
433
434 return cmp_doubles(l, r);
435 }
Jiri Olsa81d5f952012-10-05 16:44:43 +0200436 case COMPUTE_WEIGHTED_DIFF:
437 {
438 s64 l = left->diff.wdiff;
439 s64 r = right->diff.wdiff;
440
441 return r - l;
442 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200443 default:
444 BUG_ON(1);
445 }
446
447 return 0;
448}
449
450static void insert_hist_entry_by_compute(struct rb_root *root,
451 struct hist_entry *he,
452 int c)
453{
454 struct rb_node **p = &root->rb_node;
455 struct rb_node *parent = NULL;
456 struct hist_entry *iter;
457
458 while (*p != NULL) {
459 parent = *p;
460 iter = rb_entry(parent, struct hist_entry, rb_node);
461 if (hist_entry__cmp_compute(he, iter, c) < 0)
462 p = &(*p)->rb_left;
463 else
464 p = &(*p)->rb_right;
465 }
466
467 rb_link_node(&he->rb_node, parent, p);
468 rb_insert_color(&he->rb_node, root);
469}
470
471static void hists__compute_resort(struct hists *hists)
472{
473 struct rb_root tmp = RB_ROOT;
474 struct rb_node *next = rb_first(&hists->entries);
475
476 while (next != NULL) {
477 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
478
479 next = rb_next(&he->rb_node);
480
481 rb_erase(&he->rb_node, &hists->entries);
482 insert_hist_entry_by_compute(&tmp, he, compute);
483 }
484
485 hists->entries = tmp;
486}
487
Jiri Olsaa06d1432012-10-05 16:44:40 +0200488static void hists__process(struct hists *old, struct hists *new)
489{
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300490 hists__match(new, old);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200491
492 if (show_baseline_only)
493 hists__baseline_only(new);
Arnaldo Carvalho de Melobfaef4b2012-11-08 18:08:26 -0300494 else
495 hists__link(new, old);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200496
Jiri Olsa96c47f12012-10-05 16:44:42 +0200497 if (sort_compute) {
498 hists__precompute(new);
499 hists__compute_resort(new);
500 }
501
Jiri Olsaa06d1432012-10-05 16:44:40 +0200502 hists__fprintf(new, true, 0, 0, stdout);
503}
504
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200505static int __cmd_diff(void)
506{
507 int ret, i;
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100508#define older (session[0])
509#define newer (session[1])
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200510 struct perf_session *session[2];
Jiri Olsa863e4512012-09-06 17:46:55 +0200511 struct perf_evlist *evlist_new, *evlist_old;
512 struct perf_evsel *evsel;
513 bool first = true;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200514
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100515 older = perf_session__new(input_old, O_RDONLY, force, false,
Jiri Olsa863e4512012-09-06 17:46:55 +0200516 &tool);
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100517 newer = perf_session__new(input_new, O_RDONLY, force, false,
Jiri Olsa863e4512012-09-06 17:46:55 +0200518 &tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200519 if (session[0] == NULL || session[1] == NULL)
520 return -ENOMEM;
521
522 for (i = 0; i < 2; ++i) {
Jiri Olsa863e4512012-09-06 17:46:55 +0200523 ret = perf_session__process_events(session[i], &tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200524 if (ret)
525 goto out_delete;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200526 }
527
Jiri Olsa863e4512012-09-06 17:46:55 +0200528 evlist_old = older->evlist;
529 evlist_new = newer->evlist;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200530
Jiri Olsadd464342012-10-04 21:49:36 +0900531 perf_evlist__resort_hists(evlist_old, true);
532 perf_evlist__resort_hists(evlist_new, false);
Jiri Olsa863e4512012-09-06 17:46:55 +0200533
534 list_for_each_entry(evsel, &evlist_new->entries, node) {
535 struct perf_evsel *evsel_old;
536
537 evsel_old = evsel_match(evsel, evlist_old);
538 if (!evsel_old)
539 continue;
540
541 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
542 perf_evsel__name(evsel));
543
544 first = false;
545
Jiri Olsaa06d1432012-10-05 16:44:40 +0200546 hists__process(&evsel_old->hists, &evsel->hists);
Jiri Olsa863e4512012-09-06 17:46:55 +0200547 }
548
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200549out_delete:
550 for (i = 0; i < 2; ++i)
551 perf_session__delete(session[i]);
552 return ret;
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100553#undef older
554#undef newer
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200555}
556
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200557static const char * const diff_usage[] = {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200558 "perf diff [<options>] [old_file] [new_file]",
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200559 NULL,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200560};
561
562static const struct option options[] = {
Ian Munsiec0555642010-04-13 18:37:33 +1000563 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200564 "be more verbose (show symbol address, etc)"),
Shawn Bohrer34295552010-11-30 19:57:11 -0600565 OPT_BOOLEAN('M', "displacement", &show_displacement,
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200566 "Show position displacement relative to baseline"),
Jiri Olsaa06d1432012-10-05 16:44:40 +0200567 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
568 "Show only items with match in baseline"),
Jiri Olsa81d5f952012-10-05 16:44:43 +0200569 OPT_CALLBACK('c', "compute", &compute,
570 "delta,ratio,wdiff:w1,w2 (default delta)",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200571 "Entries differential computation selection",
572 setup_compute),
Jiri Olsa61949b22012-10-05 16:44:44 +0200573 OPT_BOOLEAN('p', "period", &show_period,
574 "Show period values."),
Jiri Olsaed279da2012-10-05 16:44:45 +0200575 OPT_BOOLEAN('F', "formula", &show_formula,
576 "Show formula."),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200577 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"),
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200582 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"),
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200588 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."),
David Ahernec5761e2010-12-09 13:27:07 -0700593 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
594 "Look for files with symbols relative to this directory"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200595 OPT_END()
596};
597
Jiri Olsa1d778222012-10-04 21:49:39 +0900598static void ui_init(void)
599{
Jiri Olsaed279da2012-10-05 16:44:45 +0200600 /*
601 * Display baseline/delta/ratio/displacement/
602 * formula/periods columns.
603 */
Jiri Olsa12400052012-10-13 00:06:16 +0200604 perf_hpp__column_enable(PERF_HPP__BASELINE);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200605
606 switch (compute) {
607 case COMPUTE_DELTA:
Jiri Olsa12400052012-10-13 00:06:16 +0200608 perf_hpp__column_enable(PERF_HPP__DELTA);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200609 break;
610 case COMPUTE_RATIO:
Jiri Olsa12400052012-10-13 00:06:16 +0200611 perf_hpp__column_enable(PERF_HPP__RATIO);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200612 break;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200613 case COMPUTE_WEIGHTED_DIFF:
Jiri Olsa12400052012-10-13 00:06:16 +0200614 perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF);
Jiri Olsa81d5f952012-10-05 16:44:43 +0200615 break;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200616 default:
617 BUG_ON(1);
618 };
Jiri Olsa1d778222012-10-04 21:49:39 +0900619
620 if (show_displacement)
Jiri Olsa12400052012-10-13 00:06:16 +0200621 perf_hpp__column_enable(PERF_HPP__DISPL);
Jiri Olsa61949b22012-10-05 16:44:44 +0200622
Jiri Olsaed279da2012-10-05 16:44:45 +0200623 if (show_formula)
Jiri Olsa12400052012-10-13 00:06:16 +0200624 perf_hpp__column_enable(PERF_HPP__FORMULA);
Jiri Olsaed279da2012-10-05 16:44:45 +0200625
Jiri Olsa61949b22012-10-05 16:44:44 +0200626 if (show_period) {
Jiri Olsa12400052012-10-13 00:06:16 +0200627 perf_hpp__column_enable(PERF_HPP__PERIOD);
628 perf_hpp__column_enable(PERF_HPP__PERIOD_BASELINE);
Jiri Olsa61949b22012-10-05 16:44:44 +0200629 }
Jiri Olsa1d778222012-10-04 21:49:39 +0900630}
631
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300632int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200633{
Arnaldo Carvalho de Melo604c5c92009-12-16 14:09:53 -0200634 sort_order = diff__default_sort_order;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200635 argc = parse_options(argc, argv, options, diff_usage, 0);
636 if (argc) {
637 if (argc > 2)
638 usage_with_options(diff_usage, options);
639 if (argc == 2) {
640 input_old = argv[0];
641 input_new = argv[1];
642 } else
643 input_new = argv[0];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800644 } else if (symbol_conf.default_guest_vmlinux_name ||
645 symbol_conf.default_guest_kallsyms) {
646 input_old = "perf.data.host";
647 input_new = "perf.data.guest";
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200648 }
649
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200650 symbol_conf.exclude_other = false;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200651 if (symbol__init() < 0)
652 return -1;
653
Jiri Olsa1d778222012-10-04 21:49:39 +0900654 ui_init();
655
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200656 setup_sorting(diff_usage, options);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200657 setup_pager();
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200658
659 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
660 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
661 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
662
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200663 return __cmd_diff();
664}