blob: d78e8386e1a96c7ae8f22096867ad1832845f7ad [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 Olsaa06d1432012-10-05 16:44:40 +020027static bool show_baseline_only;
Jiri Olsa96c47f12012-10-05 16:44:42 +020028static bool sort_compute;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020029
Jiri Olsa81d5f952012-10-05 16:44:43 +020030static s64 compute_wdiff_w1;
31static s64 compute_wdiff_w2;
32
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020033enum {
34 COMPUTE_DELTA,
35 COMPUTE_RATIO,
Jiri Olsa81d5f952012-10-05 16:44:43 +020036 COMPUTE_WEIGHTED_DIFF,
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020037 COMPUTE_MAX,
38};
39
40const char *compute_names[COMPUTE_MAX] = {
41 [COMPUTE_DELTA] = "delta",
42 [COMPUTE_RATIO] = "ratio",
Jiri Olsa81d5f952012-10-05 16:44:43 +020043 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020044};
45
46static int compute;
47
Jiri Olsa81d5f952012-10-05 16:44:43 +020048static int setup_compute_opt_wdiff(char *opt)
49{
50 char *w1_str = opt;
51 char *w2_str;
52
53 int ret = -EINVAL;
54
55 if (!opt)
56 goto out;
57
58 w2_str = strchr(opt, ',');
59 if (!w2_str)
60 goto out;
61
62 *w2_str++ = 0x0;
63 if (!*w2_str)
64 goto out;
65
66 compute_wdiff_w1 = strtol(w1_str, NULL, 10);
67 compute_wdiff_w2 = strtol(w2_str, NULL, 10);
68
69 if (!compute_wdiff_w1 || !compute_wdiff_w2)
70 goto out;
71
72 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
73 compute_wdiff_w1, compute_wdiff_w2);
74
75 ret = 0;
76
77 out:
78 if (ret)
79 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
80
81 return ret;
82}
83
84static int setup_compute_opt(char *opt)
85{
86 if (compute == COMPUTE_WEIGHTED_DIFF)
87 return setup_compute_opt_wdiff(opt);
88
89 if (opt) {
90 pr_err("Failed: extra option specified '%s'", opt);
91 return -EINVAL;
92 }
93
94 return 0;
95}
96
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020097static int setup_compute(const struct option *opt, const char *str,
98 int unset __maybe_unused)
99{
100 int *cp = (int *) opt->value;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200101 char *cstr = (char *) str;
102 char buf[50];
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200103 unsigned i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200104 char *option;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200105
106 if (!str) {
107 *cp = COMPUTE_DELTA;
108 return 0;
109 }
110
Jiri Olsa96c47f12012-10-05 16:44:42 +0200111 if (*str == '+') {
112 sort_compute = true;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200113 cstr = (char *) ++str;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200114 if (!*str)
115 return 0;
116 }
117
Jiri Olsa81d5f952012-10-05 16:44:43 +0200118 option = strchr(str, ':');
119 if (option) {
120 unsigned len = option++ - str;
121
122 /*
123 * The str data are not writeable, so we need
124 * to use another buffer.
125 */
126
127 /* No option value is longer. */
128 if (len >= sizeof(buf))
129 return -EINVAL;
130
131 strncpy(buf, str, len);
132 buf[len] = 0x0;
133 cstr = buf;
134 }
135
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200136 for (i = 0; i < COMPUTE_MAX; i++)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200137 if (!strcmp(cstr, compute_names[i])) {
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200138 *cp = i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200139 return setup_compute_opt(option);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200140 }
141
142 pr_err("Failed: '%s' is not computation method "
Jiri Olsa81d5f952012-10-05 16:44:43 +0200143 "(use 'delta','ratio' or 'wdiff')\n", str);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200144 return -EINVAL;
145}
146
Jiri Olsa96c47f12012-10-05 16:44:42 +0200147static double get_period_percent(struct hist_entry *he, u64 period)
148{
149 u64 total = he->hists->stats.total_period;
150 return (period * 100.0) / total;
151}
152
153double perf_diff__compute_delta(struct hist_entry *he)
154{
155 struct hist_entry *pair = he->pair;
156 double new_percent = get_period_percent(he, he->stat.period);
157 double old_percent = pair ? get_period_percent(pair, pair->stat.period) : 0.0;
158
159 he->diff.period_ratio_delta = new_percent - old_percent;
160 he->diff.computed = true;
161 return he->diff.period_ratio_delta;
162}
163
164double perf_diff__compute_ratio(struct hist_entry *he)
165{
166 struct hist_entry *pair = he->pair;
167 double new_period = he->stat.period;
168 double old_period = pair ? pair->stat.period : 0;
169
170 he->diff.computed = true;
171 he->diff.period_ratio = pair ? (new_period / old_period) : 0;
172 return he->diff.period_ratio;
173}
174
Jiri Olsa81d5f952012-10-05 16:44:43 +0200175s64 perf_diff__compute_wdiff(struct hist_entry *he)
176{
177 struct hist_entry *pair = he->pair;
178 u64 new_period = he->stat.period;
179 u64 old_period = pair ? pair->stat.period : 0;
180
181 he->diff.computed = true;
182
183 if (!pair)
184 he->diff.wdiff = 0;
185 else
186 he->diff.wdiff = new_period * compute_wdiff_w2 -
187 old_period * compute_wdiff_w1;
188
189 return he->diff.wdiff;
190}
191
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300192static int hists__add_entry(struct hists *self,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300193 struct addr_location *al, u64 period)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200194{
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300195 if (__hists__add_entry(self, al, NULL, period) != NULL)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300196 return 0;
197 return -ENOMEM;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200198}
199
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300200static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200201 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200202 struct perf_sample *sample,
Jiri Olsa863e4512012-09-06 17:46:55 +0200203 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200204 struct machine *machine)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200205{
206 struct addr_location al;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200207
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200208 if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200209 pr_warning("problem processing %d event, skipping it.\n",
210 event->header.type);
211 return -1;
212 }
213
Arnaldo Carvalho de Melocdbae312009-12-28 22:48:35 -0200214 if (al.filtered || al.sym == NULL)
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200215 return 0;
216
Jiri Olsa863e4512012-09-06 17:46:55 +0200217 if (hists__add_entry(&evsel->hists, &al, sample->period)) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300218 pr_warning("problem incrementing symbol period, skipping event\n");
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200219 return -1;
220 }
221
Jiri Olsa863e4512012-09-06 17:46:55 +0200222 evsel->hists.stats.total_period += sample->period;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200223 return 0;
224}
225
Jiri Olsa863e4512012-09-06 17:46:55 +0200226static struct perf_tool tool = {
227 .sample = diff__process_sample_event,
228 .mmap = perf_event__process_mmap,
229 .comm = perf_event__process_comm,
230 .exit = perf_event__process_task,
231 .fork = perf_event__process_task,
232 .lost = perf_event__process_lost,
233 .ordered_samples = true,
234 .ordering_requires_timestamps = true,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200235};
236
Jiri Olsadd464342012-10-04 21:49:36 +0900237static void insert_hist_entry_by_name(struct rb_root *root,
238 struct hist_entry *he)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200239{
240 struct rb_node **p = &root->rb_node;
241 struct rb_node *parent = NULL;
242 struct hist_entry *iter;
243
244 while (*p != NULL) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200245 parent = *p;
246 iter = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200247 if (hist_entry__cmp(he, iter) < 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200248 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200249 else
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200250 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200251 }
252
253 rb_link_node(&he->rb_node, parent, p);
254 rb_insert_color(&he->rb_node, root);
255}
256
Jiri Olsadd464342012-10-04 21:49:36 +0900257static void hists__name_resort(struct hists *self, bool sort)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200258{
259 unsigned long position = 1;
260 struct rb_root tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300261 struct rb_node *next = rb_first(&self->entries);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200262
263 while (next != NULL) {
264 struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
265
266 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200267 n->position = position++;
Jiri Olsadd464342012-10-04 21:49:36 +0900268
269 if (sort) {
270 rb_erase(&n->rb_node, &self->entries);
271 insert_hist_entry_by_name(&tmp, n);
272 }
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200273 }
274
Jiri Olsadd464342012-10-04 21:49:36 +0900275 if (sort)
276 self->entries = tmp;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200277}
278
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300279static struct hist_entry *hists__find_entry(struct hists *self,
280 struct hist_entry *he)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200281{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300282 struct rb_node *n = self->entries.rb_node;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200283
284 while (n) {
285 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200286 int64_t cmp = hist_entry__cmp(he, iter);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200287
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200288 if (cmp < 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200289 n = n->rb_left;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200290 else if (cmp > 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200291 n = n->rb_right;
Jiri Olsadd464342012-10-04 21:49:36 +0900292 else
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200293 return iter;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200294 }
295
296 return NULL;
297}
298
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300299static void hists__match(struct hists *older, struct hists *newer)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200300{
301 struct rb_node *nd;
302
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300303 for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200304 struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300305 pos->pair = hists__find_entry(older, pos);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200306 }
307}
308
Jiri Olsa863e4512012-09-06 17:46:55 +0200309static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
310 struct perf_evlist *evlist)
311{
312 struct perf_evsel *e;
313
314 list_for_each_entry(e, &evlist->entries, node)
315 if (perf_evsel__match2(evsel, e))
316 return e;
317
318 return NULL;
319}
320
Jiri Olsadd464342012-10-04 21:49:36 +0900321static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
322{
323 struct perf_evsel *evsel;
324
325 list_for_each_entry(evsel, &evlist->entries, node) {
326 struct hists *hists = &evsel->hists;
327
328 hists__output_resort(hists);
329
330 /*
331 * The hists__name_resort only sets possition
332 * if name is false.
333 */
334 if (name || ((!name) && show_displacement))
335 hists__name_resort(hists, name);
336 }
337}
338
Jiri Olsaa06d1432012-10-05 16:44:40 +0200339static void hists__baseline_only(struct hists *hists)
340{
341 struct rb_node *next = rb_first(&hists->entries);
342
343 while (next != NULL) {
344 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
345
346 next = rb_next(&he->rb_node);
347 if (!he->pair) {
348 rb_erase(&he->rb_node, &hists->entries);
349 hist_entry__free(he);
350 }
351 }
352}
353
Jiri Olsa96c47f12012-10-05 16:44:42 +0200354static void hists__precompute(struct hists *hists)
355{
356 struct rb_node *next = rb_first(&hists->entries);
357
358 while (next != NULL) {
359 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
360
361 next = rb_next(&he->rb_node);
362
363 switch (compute) {
364 case COMPUTE_DELTA:
365 perf_diff__compute_delta(he);
366 break;
367 case COMPUTE_RATIO:
368 perf_diff__compute_ratio(he);
369 break;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200370 case COMPUTE_WEIGHTED_DIFF:
371 perf_diff__compute_wdiff(he);
372 break;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200373 default:
374 BUG_ON(1);
375 }
376 }
377}
378
379static int64_t cmp_doubles(double l, double r)
380{
381 if (l > r)
382 return -1;
383 else if (l < r)
384 return 1;
385 else
386 return 0;
387}
388
389static int64_t
390hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
391 int c)
392{
393 switch (c) {
394 case COMPUTE_DELTA:
395 {
396 double l = left->diff.period_ratio_delta;
397 double r = right->diff.period_ratio_delta;
398
399 return cmp_doubles(l, r);
400 }
401 case COMPUTE_RATIO:
402 {
403 double l = left->diff.period_ratio;
404 double r = right->diff.period_ratio;
405
406 return cmp_doubles(l, r);
407 }
Jiri Olsa81d5f952012-10-05 16:44:43 +0200408 case COMPUTE_WEIGHTED_DIFF:
409 {
410 s64 l = left->diff.wdiff;
411 s64 r = right->diff.wdiff;
412
413 return r - l;
414 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200415 default:
416 BUG_ON(1);
417 }
418
419 return 0;
420}
421
422static void insert_hist_entry_by_compute(struct rb_root *root,
423 struct hist_entry *he,
424 int c)
425{
426 struct rb_node **p = &root->rb_node;
427 struct rb_node *parent = NULL;
428 struct hist_entry *iter;
429
430 while (*p != NULL) {
431 parent = *p;
432 iter = rb_entry(parent, struct hist_entry, rb_node);
433 if (hist_entry__cmp_compute(he, iter, c) < 0)
434 p = &(*p)->rb_left;
435 else
436 p = &(*p)->rb_right;
437 }
438
439 rb_link_node(&he->rb_node, parent, p);
440 rb_insert_color(&he->rb_node, root);
441}
442
443static void hists__compute_resort(struct hists *hists)
444{
445 struct rb_root tmp = RB_ROOT;
446 struct rb_node *next = rb_first(&hists->entries);
447
448 while (next != NULL) {
449 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
450
451 next = rb_next(&he->rb_node);
452
453 rb_erase(&he->rb_node, &hists->entries);
454 insert_hist_entry_by_compute(&tmp, he, compute);
455 }
456
457 hists->entries = tmp;
458}
459
Jiri Olsaa06d1432012-10-05 16:44:40 +0200460static void hists__process(struct hists *old, struct hists *new)
461{
462 hists__match(old, new);
463
464 if (show_baseline_only)
465 hists__baseline_only(new);
466
Jiri Olsa96c47f12012-10-05 16:44:42 +0200467 if (sort_compute) {
468 hists__precompute(new);
469 hists__compute_resort(new);
470 }
471
Jiri Olsaa06d1432012-10-05 16:44:40 +0200472 hists__fprintf(new, true, 0, 0, stdout);
473}
474
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200475static int __cmd_diff(void)
476{
477 int ret, i;
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100478#define older (session[0])
479#define newer (session[1])
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200480 struct perf_session *session[2];
Jiri Olsa863e4512012-09-06 17:46:55 +0200481 struct perf_evlist *evlist_new, *evlist_old;
482 struct perf_evsel *evsel;
483 bool first = true;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200484
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100485 older = perf_session__new(input_old, O_RDONLY, force, false,
Jiri Olsa863e4512012-09-06 17:46:55 +0200486 &tool);
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100487 newer = perf_session__new(input_new, O_RDONLY, force, false,
Jiri Olsa863e4512012-09-06 17:46:55 +0200488 &tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200489 if (session[0] == NULL || session[1] == NULL)
490 return -ENOMEM;
491
492 for (i = 0; i < 2; ++i) {
Jiri Olsa863e4512012-09-06 17:46:55 +0200493 ret = perf_session__process_events(session[i], &tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200494 if (ret)
495 goto out_delete;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200496 }
497
Jiri Olsa863e4512012-09-06 17:46:55 +0200498 evlist_old = older->evlist;
499 evlist_new = newer->evlist;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200500
Jiri Olsadd464342012-10-04 21:49:36 +0900501 perf_evlist__resort_hists(evlist_old, true);
502 perf_evlist__resort_hists(evlist_new, false);
Jiri Olsa863e4512012-09-06 17:46:55 +0200503
504 list_for_each_entry(evsel, &evlist_new->entries, node) {
505 struct perf_evsel *evsel_old;
506
507 evsel_old = evsel_match(evsel, evlist_old);
508 if (!evsel_old)
509 continue;
510
511 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
512 perf_evsel__name(evsel));
513
514 first = false;
515
Jiri Olsaa06d1432012-10-05 16:44:40 +0200516 hists__process(&evsel_old->hists, &evsel->hists);
Jiri Olsa863e4512012-09-06 17:46:55 +0200517 }
518
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200519out_delete:
520 for (i = 0; i < 2; ++i)
521 perf_session__delete(session[i]);
522 return ret;
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100523#undef older
524#undef newer
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200525}
526
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200527static const char * const diff_usage[] = {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200528 "perf diff [<options>] [old_file] [new_file]",
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200529 NULL,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200530};
531
532static const struct option options[] = {
Ian Munsiec0555642010-04-13 18:37:33 +1000533 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200534 "be more verbose (show symbol address, etc)"),
Shawn Bohrer34295552010-11-30 19:57:11 -0600535 OPT_BOOLEAN('M', "displacement", &show_displacement,
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200536 "Show position displacement relative to baseline"),
Jiri Olsaa06d1432012-10-05 16:44:40 +0200537 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
538 "Show only items with match in baseline"),
Jiri Olsa81d5f952012-10-05 16:44:43 +0200539 OPT_CALLBACK('c', "compute", &compute,
540 "delta,ratio,wdiff:w1,w2 (default delta)",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200541 "Entries differential computation selection",
542 setup_compute),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200543 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
544 "dump raw trace in ASCII"),
545 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
546 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
547 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200548 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
549 "only consider symbols in these dsos"),
550 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
551 "only consider symbols in these comms"),
552 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
553 "only consider these symbols"),
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200554 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
555 "sort by key(s): pid, comm, dso, symbol, parent"),
556 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
557 "separator for columns, no spaces will be added between "
558 "columns '.' is reserved."),
David Ahernec5761e2010-12-09 13:27:07 -0700559 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
560 "Look for files with symbols relative to this directory"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200561 OPT_END()
562};
563
Jiri Olsa1d778222012-10-04 21:49:39 +0900564static void ui_init(void)
565{
566 perf_hpp__init();
567
568 /* No overhead column. */
569 perf_hpp__column_enable(PERF_HPP__OVERHEAD, false);
570
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200571 /* Display baseline/delta/ratio/displacement columns. */
Jiri Olsa1d778222012-10-04 21:49:39 +0900572 perf_hpp__column_enable(PERF_HPP__BASELINE, true);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200573
574 switch (compute) {
575 case COMPUTE_DELTA:
576 perf_hpp__column_enable(PERF_HPP__DELTA, true);
577 break;
578 case COMPUTE_RATIO:
579 perf_hpp__column_enable(PERF_HPP__RATIO, true);
580 break;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200581 case COMPUTE_WEIGHTED_DIFF:
582 perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF, true);
583 break;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200584 default:
585 BUG_ON(1);
586 };
Jiri Olsa1d778222012-10-04 21:49:39 +0900587
588 if (show_displacement)
589 perf_hpp__column_enable(PERF_HPP__DISPL, true);
590}
591
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300592int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200593{
Arnaldo Carvalho de Melo604c5c92009-12-16 14:09:53 -0200594 sort_order = diff__default_sort_order;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200595 argc = parse_options(argc, argv, options, diff_usage, 0);
596 if (argc) {
597 if (argc > 2)
598 usage_with_options(diff_usage, options);
599 if (argc == 2) {
600 input_old = argv[0];
601 input_new = argv[1];
602 } else
603 input_new = argv[0];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800604 } else if (symbol_conf.default_guest_vmlinux_name ||
605 symbol_conf.default_guest_kallsyms) {
606 input_old = "perf.data.host";
607 input_new = "perf.data.guest";
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200608 }
609
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200610 symbol_conf.exclude_other = false;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200611 if (symbol__init() < 0)
612 return -1;
613
Jiri Olsa1d778222012-10-04 21:49:39 +0900614 ui_init();
615
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200616 setup_sorting(diff_usage, options);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200617 setup_pager();
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200618
619 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
620 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
621 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
622
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200623 return __cmd_diff();
624}