blob: 2411dd18c556733122c0e586ff145f0aacb65477 [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 Olsaa06d1432012-10-05 16:44:40 +020028static bool show_baseline_only;
Jiri Olsa96c47f12012-10-05 16:44:42 +020029static bool sort_compute;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020030
Jiri Olsa81d5f952012-10-05 16:44:43 +020031static s64 compute_wdiff_w1;
32static s64 compute_wdiff_w2;
33
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020034enum {
35 COMPUTE_DELTA,
36 COMPUTE_RATIO,
Jiri Olsa81d5f952012-10-05 16:44:43 +020037 COMPUTE_WEIGHTED_DIFF,
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020038 COMPUTE_MAX,
39};
40
41const char *compute_names[COMPUTE_MAX] = {
42 [COMPUTE_DELTA] = "delta",
43 [COMPUTE_RATIO] = "ratio",
Jiri Olsa81d5f952012-10-05 16:44:43 +020044 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020045};
46
47static int compute;
48
Jiri Olsa81d5f952012-10-05 16:44:43 +020049static int setup_compute_opt_wdiff(char *opt)
50{
51 char *w1_str = opt;
52 char *w2_str;
53
54 int ret = -EINVAL;
55
56 if (!opt)
57 goto out;
58
59 w2_str = strchr(opt, ',');
60 if (!w2_str)
61 goto out;
62
63 *w2_str++ = 0x0;
64 if (!*w2_str)
65 goto out;
66
67 compute_wdiff_w1 = strtol(w1_str, NULL, 10);
68 compute_wdiff_w2 = strtol(w2_str, NULL, 10);
69
70 if (!compute_wdiff_w1 || !compute_wdiff_w2)
71 goto out;
72
73 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
74 compute_wdiff_w1, compute_wdiff_w2);
75
76 ret = 0;
77
78 out:
79 if (ret)
80 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
81
82 return ret;
83}
84
85static int setup_compute_opt(char *opt)
86{
87 if (compute == COMPUTE_WEIGHTED_DIFF)
88 return setup_compute_opt_wdiff(opt);
89
90 if (opt) {
91 pr_err("Failed: extra option specified '%s'", opt);
92 return -EINVAL;
93 }
94
95 return 0;
96}
97
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020098static int setup_compute(const struct option *opt, const char *str,
99 int unset __maybe_unused)
100{
101 int *cp = (int *) opt->value;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200102 char *cstr = (char *) str;
103 char buf[50];
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200104 unsigned i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200105 char *option;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200106
107 if (!str) {
108 *cp = COMPUTE_DELTA;
109 return 0;
110 }
111
Jiri Olsa96c47f12012-10-05 16:44:42 +0200112 if (*str == '+') {
113 sort_compute = true;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200114 cstr = (char *) ++str;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200115 if (!*str)
116 return 0;
117 }
118
Jiri Olsa81d5f952012-10-05 16:44:43 +0200119 option = strchr(str, ':');
120 if (option) {
121 unsigned len = option++ - str;
122
123 /*
124 * The str data are not writeable, so we need
125 * to use another buffer.
126 */
127
128 /* No option value is longer. */
129 if (len >= sizeof(buf))
130 return -EINVAL;
131
132 strncpy(buf, str, len);
133 buf[len] = 0x0;
134 cstr = buf;
135 }
136
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200137 for (i = 0; i < COMPUTE_MAX; i++)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200138 if (!strcmp(cstr, compute_names[i])) {
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200139 *cp = i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200140 return setup_compute_opt(option);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200141 }
142
143 pr_err("Failed: '%s' is not computation method "
Jiri Olsa81d5f952012-10-05 16:44:43 +0200144 "(use 'delta','ratio' or 'wdiff')\n", str);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200145 return -EINVAL;
146}
147
Jiri Olsa96c47f12012-10-05 16:44:42 +0200148static double get_period_percent(struct hist_entry *he, u64 period)
149{
150 u64 total = he->hists->stats.total_period;
151 return (period * 100.0) / total;
152}
153
154double perf_diff__compute_delta(struct hist_entry *he)
155{
156 struct hist_entry *pair = he->pair;
157 double new_percent = get_period_percent(he, he->stat.period);
158 double old_percent = pair ? get_period_percent(pair, pair->stat.period) : 0.0;
159
160 he->diff.period_ratio_delta = new_percent - old_percent;
161 he->diff.computed = true;
162 return he->diff.period_ratio_delta;
163}
164
165double perf_diff__compute_ratio(struct hist_entry *he)
166{
167 struct hist_entry *pair = he->pair;
168 double new_period = he->stat.period;
169 double old_period = pair ? pair->stat.period : 0;
170
171 he->diff.computed = true;
172 he->diff.period_ratio = pair ? (new_period / old_period) : 0;
173 return he->diff.period_ratio;
174}
175
Jiri Olsa81d5f952012-10-05 16:44:43 +0200176s64 perf_diff__compute_wdiff(struct hist_entry *he)
177{
178 struct hist_entry *pair = he->pair;
179 u64 new_period = he->stat.period;
180 u64 old_period = pair ? pair->stat.period : 0;
181
182 he->diff.computed = true;
183
184 if (!pair)
185 he->diff.wdiff = 0;
186 else
187 he->diff.wdiff = new_period * compute_wdiff_w2 -
188 old_period * compute_wdiff_w1;
189
190 return he->diff.wdiff;
191}
192
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300193static int hists__add_entry(struct hists *self,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300194 struct addr_location *al, u64 period)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200195{
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300196 if (__hists__add_entry(self, al, NULL, period) != NULL)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300197 return 0;
198 return -ENOMEM;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200199}
200
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300201static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200202 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200203 struct perf_sample *sample,
Jiri Olsa863e4512012-09-06 17:46:55 +0200204 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200205 struct machine *machine)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200206{
207 struct addr_location al;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200208
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200209 if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200210 pr_warning("problem processing %d event, skipping it.\n",
211 event->header.type);
212 return -1;
213 }
214
Arnaldo Carvalho de Melocdbae312009-12-28 22:48:35 -0200215 if (al.filtered || al.sym == NULL)
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200216 return 0;
217
Jiri Olsa863e4512012-09-06 17:46:55 +0200218 if (hists__add_entry(&evsel->hists, &al, sample->period)) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300219 pr_warning("problem incrementing symbol period, skipping event\n");
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200220 return -1;
221 }
222
Jiri Olsa863e4512012-09-06 17:46:55 +0200223 evsel->hists.stats.total_period += sample->period;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200224 return 0;
225}
226
Jiri Olsa863e4512012-09-06 17:46:55 +0200227static struct perf_tool tool = {
228 .sample = diff__process_sample_event,
229 .mmap = perf_event__process_mmap,
230 .comm = perf_event__process_comm,
231 .exit = perf_event__process_task,
232 .fork = perf_event__process_task,
233 .lost = perf_event__process_lost,
234 .ordered_samples = true,
235 .ordering_requires_timestamps = true,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200236};
237
Jiri Olsadd464342012-10-04 21:49:36 +0900238static void insert_hist_entry_by_name(struct rb_root *root,
239 struct hist_entry *he)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200240{
241 struct rb_node **p = &root->rb_node;
242 struct rb_node *parent = NULL;
243 struct hist_entry *iter;
244
245 while (*p != NULL) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200246 parent = *p;
247 iter = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200248 if (hist_entry__cmp(he, iter) < 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200249 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200250 else
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200251 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200252 }
253
254 rb_link_node(&he->rb_node, parent, p);
255 rb_insert_color(&he->rb_node, root);
256}
257
Jiri Olsadd464342012-10-04 21:49:36 +0900258static void hists__name_resort(struct hists *self, bool sort)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200259{
260 unsigned long position = 1;
261 struct rb_root tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300262 struct rb_node *next = rb_first(&self->entries);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200263
264 while (next != NULL) {
265 struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
266
267 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200268 n->position = position++;
Jiri Olsadd464342012-10-04 21:49:36 +0900269
270 if (sort) {
271 rb_erase(&n->rb_node, &self->entries);
272 insert_hist_entry_by_name(&tmp, n);
273 }
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200274 }
275
Jiri Olsadd464342012-10-04 21:49:36 +0900276 if (sort)
277 self->entries = tmp;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200278}
279
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300280static struct hist_entry *hists__find_entry(struct hists *self,
281 struct hist_entry *he)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200282{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300283 struct rb_node *n = self->entries.rb_node;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200284
285 while (n) {
286 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200287 int64_t cmp = hist_entry__cmp(he, iter);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200288
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200289 if (cmp < 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200290 n = n->rb_left;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200291 else if (cmp > 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200292 n = n->rb_right;
Jiri Olsadd464342012-10-04 21:49:36 +0900293 else
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200294 return iter;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200295 }
296
297 return NULL;
298}
299
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300300static void hists__match(struct hists *older, struct hists *newer)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200301{
302 struct rb_node *nd;
303
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300304 for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200305 struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300306 pos->pair = hists__find_entry(older, pos);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200307 }
308}
309
Jiri Olsa863e4512012-09-06 17:46:55 +0200310static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
311 struct perf_evlist *evlist)
312{
313 struct perf_evsel *e;
314
315 list_for_each_entry(e, &evlist->entries, node)
316 if (perf_evsel__match2(evsel, e))
317 return e;
318
319 return NULL;
320}
321
Jiri Olsadd464342012-10-04 21:49:36 +0900322static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
323{
324 struct perf_evsel *evsel;
325
326 list_for_each_entry(evsel, &evlist->entries, node) {
327 struct hists *hists = &evsel->hists;
328
329 hists__output_resort(hists);
330
331 /*
332 * The hists__name_resort only sets possition
333 * if name is false.
334 */
335 if (name || ((!name) && show_displacement))
336 hists__name_resort(hists, name);
337 }
338}
339
Jiri Olsaa06d1432012-10-05 16:44:40 +0200340static void hists__baseline_only(struct hists *hists)
341{
342 struct rb_node *next = rb_first(&hists->entries);
343
344 while (next != NULL) {
345 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
346
347 next = rb_next(&he->rb_node);
348 if (!he->pair) {
349 rb_erase(&he->rb_node, &hists->entries);
350 hist_entry__free(he);
351 }
352 }
353}
354
Jiri Olsa96c47f12012-10-05 16:44:42 +0200355static void hists__precompute(struct hists *hists)
356{
357 struct rb_node *next = rb_first(&hists->entries);
358
359 while (next != NULL) {
360 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
361
362 next = rb_next(&he->rb_node);
363
364 switch (compute) {
365 case COMPUTE_DELTA:
366 perf_diff__compute_delta(he);
367 break;
368 case COMPUTE_RATIO:
369 perf_diff__compute_ratio(he);
370 break;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200371 case COMPUTE_WEIGHTED_DIFF:
372 perf_diff__compute_wdiff(he);
373 break;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200374 default:
375 BUG_ON(1);
376 }
377 }
378}
379
380static int64_t cmp_doubles(double l, double r)
381{
382 if (l > r)
383 return -1;
384 else if (l < r)
385 return 1;
386 else
387 return 0;
388}
389
390static int64_t
391hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
392 int c)
393{
394 switch (c) {
395 case COMPUTE_DELTA:
396 {
397 double l = left->diff.period_ratio_delta;
398 double r = right->diff.period_ratio_delta;
399
400 return cmp_doubles(l, r);
401 }
402 case COMPUTE_RATIO:
403 {
404 double l = left->diff.period_ratio;
405 double r = right->diff.period_ratio;
406
407 return cmp_doubles(l, r);
408 }
Jiri Olsa81d5f952012-10-05 16:44:43 +0200409 case COMPUTE_WEIGHTED_DIFF:
410 {
411 s64 l = left->diff.wdiff;
412 s64 r = right->diff.wdiff;
413
414 return r - l;
415 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200416 default:
417 BUG_ON(1);
418 }
419
420 return 0;
421}
422
423static void insert_hist_entry_by_compute(struct rb_root *root,
424 struct hist_entry *he,
425 int c)
426{
427 struct rb_node **p = &root->rb_node;
428 struct rb_node *parent = NULL;
429 struct hist_entry *iter;
430
431 while (*p != NULL) {
432 parent = *p;
433 iter = rb_entry(parent, struct hist_entry, rb_node);
434 if (hist_entry__cmp_compute(he, iter, c) < 0)
435 p = &(*p)->rb_left;
436 else
437 p = &(*p)->rb_right;
438 }
439
440 rb_link_node(&he->rb_node, parent, p);
441 rb_insert_color(&he->rb_node, root);
442}
443
444static void hists__compute_resort(struct hists *hists)
445{
446 struct rb_root tmp = RB_ROOT;
447 struct rb_node *next = rb_first(&hists->entries);
448
449 while (next != NULL) {
450 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
451
452 next = rb_next(&he->rb_node);
453
454 rb_erase(&he->rb_node, &hists->entries);
455 insert_hist_entry_by_compute(&tmp, he, compute);
456 }
457
458 hists->entries = tmp;
459}
460
Jiri Olsaa06d1432012-10-05 16:44:40 +0200461static void hists__process(struct hists *old, struct hists *new)
462{
463 hists__match(old, new);
464
465 if (show_baseline_only)
466 hists__baseline_only(new);
467
Jiri Olsa96c47f12012-10-05 16:44:42 +0200468 if (sort_compute) {
469 hists__precompute(new);
470 hists__compute_resort(new);
471 }
472
Jiri Olsaa06d1432012-10-05 16:44:40 +0200473 hists__fprintf(new, true, 0, 0, stdout);
474}
475
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200476static int __cmd_diff(void)
477{
478 int ret, i;
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100479#define older (session[0])
480#define newer (session[1])
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200481 struct perf_session *session[2];
Jiri Olsa863e4512012-09-06 17:46:55 +0200482 struct perf_evlist *evlist_new, *evlist_old;
483 struct perf_evsel *evsel;
484 bool first = true;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200485
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100486 older = perf_session__new(input_old, O_RDONLY, force, false,
Jiri Olsa863e4512012-09-06 17:46:55 +0200487 &tool);
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100488 newer = perf_session__new(input_new, O_RDONLY, force, false,
Jiri Olsa863e4512012-09-06 17:46:55 +0200489 &tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200490 if (session[0] == NULL || session[1] == NULL)
491 return -ENOMEM;
492
493 for (i = 0; i < 2; ++i) {
Jiri Olsa863e4512012-09-06 17:46:55 +0200494 ret = perf_session__process_events(session[i], &tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200495 if (ret)
496 goto out_delete;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200497 }
498
Jiri Olsa863e4512012-09-06 17:46:55 +0200499 evlist_old = older->evlist;
500 evlist_new = newer->evlist;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200501
Jiri Olsadd464342012-10-04 21:49:36 +0900502 perf_evlist__resort_hists(evlist_old, true);
503 perf_evlist__resort_hists(evlist_new, false);
Jiri Olsa863e4512012-09-06 17:46:55 +0200504
505 list_for_each_entry(evsel, &evlist_new->entries, node) {
506 struct perf_evsel *evsel_old;
507
508 evsel_old = evsel_match(evsel, evlist_old);
509 if (!evsel_old)
510 continue;
511
512 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
513 perf_evsel__name(evsel));
514
515 first = false;
516
Jiri Olsaa06d1432012-10-05 16:44:40 +0200517 hists__process(&evsel_old->hists, &evsel->hists);
Jiri Olsa863e4512012-09-06 17:46:55 +0200518 }
519
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200520out_delete:
521 for (i = 0; i < 2; ++i)
522 perf_session__delete(session[i]);
523 return ret;
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100524#undef older
525#undef newer
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200526}
527
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200528static const char * const diff_usage[] = {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200529 "perf diff [<options>] [old_file] [new_file]",
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200530 NULL,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200531};
532
533static const struct option options[] = {
Ian Munsiec0555642010-04-13 18:37:33 +1000534 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200535 "be more verbose (show symbol address, etc)"),
Shawn Bohrer34295552010-11-30 19:57:11 -0600536 OPT_BOOLEAN('M', "displacement", &show_displacement,
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200537 "Show position displacement relative to baseline"),
Jiri Olsaa06d1432012-10-05 16:44:40 +0200538 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
539 "Show only items with match in baseline"),
Jiri Olsa81d5f952012-10-05 16:44:43 +0200540 OPT_CALLBACK('c', "compute", &compute,
541 "delta,ratio,wdiff:w1,w2 (default delta)",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200542 "Entries differential computation selection",
543 setup_compute),
Jiri Olsa61949b22012-10-05 16:44:44 +0200544 OPT_BOOLEAN('p', "period", &show_period,
545 "Show period values."),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200546 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
547 "dump raw trace in ASCII"),
548 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
549 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
550 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200551 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
552 "only consider symbols in these dsos"),
553 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
554 "only consider symbols in these comms"),
555 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
556 "only consider these symbols"),
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200557 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
558 "sort by key(s): pid, comm, dso, symbol, parent"),
559 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
560 "separator for columns, no spaces will be added between "
561 "columns '.' is reserved."),
David Ahernec5761e2010-12-09 13:27:07 -0700562 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
563 "Look for files with symbols relative to this directory"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200564 OPT_END()
565};
566
Jiri Olsa1d778222012-10-04 21:49:39 +0900567static void ui_init(void)
568{
569 perf_hpp__init();
570
571 /* No overhead column. */
572 perf_hpp__column_enable(PERF_HPP__OVERHEAD, false);
573
Jiri Olsa61949b22012-10-05 16:44:44 +0200574 /* Display baseline/delta/ratio/displacement/periods columns. */
Jiri Olsa1d778222012-10-04 21:49:39 +0900575 perf_hpp__column_enable(PERF_HPP__BASELINE, true);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200576
577 switch (compute) {
578 case COMPUTE_DELTA:
579 perf_hpp__column_enable(PERF_HPP__DELTA, true);
580 break;
581 case COMPUTE_RATIO:
582 perf_hpp__column_enable(PERF_HPP__RATIO, true);
583 break;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200584 case COMPUTE_WEIGHTED_DIFF:
585 perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF, true);
586 break;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200587 default:
588 BUG_ON(1);
589 };
Jiri Olsa1d778222012-10-04 21:49:39 +0900590
591 if (show_displacement)
592 perf_hpp__column_enable(PERF_HPP__DISPL, true);
Jiri Olsa61949b22012-10-05 16:44:44 +0200593
594 if (show_period) {
595 perf_hpp__column_enable(PERF_HPP__PERIOD, true);
596 perf_hpp__column_enable(PERF_HPP__PERIOD_BASELINE, true);
597 }
Jiri Olsa1d778222012-10-04 21:49:39 +0900598}
599
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300600int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200601{
Arnaldo Carvalho de Melo604c5c92009-12-16 14:09:53 -0200602 sort_order = diff__default_sort_order;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200603 argc = parse_options(argc, argv, options, diff_usage, 0);
604 if (argc) {
605 if (argc > 2)
606 usage_with_options(diff_usage, options);
607 if (argc == 2) {
608 input_old = argv[0];
609 input_new = argv[1];
610 } else
611 input_new = argv[0];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800612 } else if (symbol_conf.default_guest_vmlinux_name ||
613 symbol_conf.default_guest_kallsyms) {
614 input_old = "perf.data.host";
615 input_new = "perf.data.guest";
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200616 }
617
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200618 symbol_conf.exclude_other = false;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200619 if (symbol__init() < 0)
620 return -1;
621
Jiri Olsa1d778222012-10-04 21:49:39 +0900622 ui_init();
623
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200624 setup_sorting(diff_usage, options);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200625 setup_pager();
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200626
627 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
628 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
629 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
630
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200631 return __cmd_diff();
632}