blob: 1063c31e507c52243ee6681d3442a85eb012e940 [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;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020028
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030029static int hists__add_entry(struct hists *self,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030030 struct addr_location *al, u64 period)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020031{
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030032 if (__hists__add_entry(self, al, NULL, period) != NULL)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030033 return 0;
34 return -ENOMEM;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020035}
36
Irina Tirdea1d037ca2012-09-11 01:15:03 +030037static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020038 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -020039 struct perf_sample *sample,
Jiri Olsa863e4512012-09-06 17:46:55 +020040 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020041 struct machine *machine)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020042{
43 struct addr_location al;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020044
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020045 if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020046 pr_warning("problem processing %d event, skipping it.\n",
47 event->header.type);
48 return -1;
49 }
50
Arnaldo Carvalho de Melocdbae312009-12-28 22:48:35 -020051 if (al.filtered || al.sym == NULL)
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -020052 return 0;
53
Jiri Olsa863e4512012-09-06 17:46:55 +020054 if (hists__add_entry(&evsel->hists, &al, sample->period)) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030055 pr_warning("problem incrementing symbol period, skipping event\n");
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020056 return -1;
57 }
58
Jiri Olsa863e4512012-09-06 17:46:55 +020059 evsel->hists.stats.total_period += sample->period;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020060 return 0;
61}
62
Jiri Olsa863e4512012-09-06 17:46:55 +020063static struct perf_tool tool = {
64 .sample = diff__process_sample_event,
65 .mmap = perf_event__process_mmap,
66 .comm = perf_event__process_comm,
67 .exit = perf_event__process_task,
68 .fork = perf_event__process_task,
69 .lost = perf_event__process_lost,
70 .ordered_samples = true,
71 .ordering_requires_timestamps = true,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020072};
73
Jiri Olsadd464342012-10-04 21:49:36 +090074static void insert_hist_entry_by_name(struct rb_root *root,
75 struct hist_entry *he)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020076{
77 struct rb_node **p = &root->rb_node;
78 struct rb_node *parent = NULL;
79 struct hist_entry *iter;
80
81 while (*p != NULL) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020082 parent = *p;
83 iter = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -020084 if (hist_entry__cmp(he, iter) < 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020085 p = &(*p)->rb_left;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -020086 else
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020087 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020088 }
89
90 rb_link_node(&he->rb_node, parent, p);
91 rb_insert_color(&he->rb_node, root);
92}
93
Jiri Olsadd464342012-10-04 21:49:36 +090094static void hists__name_resort(struct hists *self, bool sort)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020095{
96 unsigned long position = 1;
97 struct rb_root tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -030098 struct rb_node *next = rb_first(&self->entries);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020099
100 while (next != NULL) {
101 struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
102
103 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200104 n->position = position++;
Jiri Olsadd464342012-10-04 21:49:36 +0900105
106 if (sort) {
107 rb_erase(&n->rb_node, &self->entries);
108 insert_hist_entry_by_name(&tmp, n);
109 }
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200110 }
111
Jiri Olsadd464342012-10-04 21:49:36 +0900112 if (sort)
113 self->entries = tmp;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200114}
115
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300116static struct hist_entry *hists__find_entry(struct hists *self,
117 struct hist_entry *he)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200118{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300119 struct rb_node *n = self->entries.rb_node;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200120
121 while (n) {
122 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200123 int64_t cmp = hist_entry__cmp(he, iter);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200124
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200125 if (cmp < 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200126 n = n->rb_left;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200127 else if (cmp > 0)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200128 n = n->rb_right;
Jiri Olsadd464342012-10-04 21:49:36 +0900129 else
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200130 return iter;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200131 }
132
133 return NULL;
134}
135
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300136static void hists__match(struct hists *older, struct hists *newer)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200137{
138 struct rb_node *nd;
139
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300140 for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200141 struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300142 pos->pair = hists__find_entry(older, pos);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200143 }
144}
145
Jiri Olsa863e4512012-09-06 17:46:55 +0200146static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
147 struct perf_evlist *evlist)
148{
149 struct perf_evsel *e;
150
151 list_for_each_entry(e, &evlist->entries, node)
152 if (perf_evsel__match2(evsel, e))
153 return e;
154
155 return NULL;
156}
157
Jiri Olsadd464342012-10-04 21:49:36 +0900158static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
159{
160 struct perf_evsel *evsel;
161
162 list_for_each_entry(evsel, &evlist->entries, node) {
163 struct hists *hists = &evsel->hists;
164
165 hists__output_resort(hists);
166
167 /*
168 * The hists__name_resort only sets possition
169 * if name is false.
170 */
171 if (name || ((!name) && show_displacement))
172 hists__name_resort(hists, name);
173 }
174}
175
Jiri Olsaa06d1432012-10-05 16:44:40 +0200176static void hists__baseline_only(struct hists *hists)
177{
178 struct rb_node *next = rb_first(&hists->entries);
179
180 while (next != NULL) {
181 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
182
183 next = rb_next(&he->rb_node);
184 if (!he->pair) {
185 rb_erase(&he->rb_node, &hists->entries);
186 hist_entry__free(he);
187 }
188 }
189}
190
191static void hists__process(struct hists *old, struct hists *new)
192{
193 hists__match(old, new);
194
195 if (show_baseline_only)
196 hists__baseline_only(new);
197
198 hists__fprintf(new, true, 0, 0, stdout);
199}
200
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200201static int __cmd_diff(void)
202{
203 int ret, i;
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100204#define older (session[0])
205#define newer (session[1])
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200206 struct perf_session *session[2];
Jiri Olsa863e4512012-09-06 17:46:55 +0200207 struct perf_evlist *evlist_new, *evlist_old;
208 struct perf_evsel *evsel;
209 bool first = true;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200210
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100211 older = perf_session__new(input_old, O_RDONLY, force, false,
Jiri Olsa863e4512012-09-06 17:46:55 +0200212 &tool);
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100213 newer = perf_session__new(input_new, O_RDONLY, force, false,
Jiri Olsa863e4512012-09-06 17:46:55 +0200214 &tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200215 if (session[0] == NULL || session[1] == NULL)
216 return -ENOMEM;
217
218 for (i = 0; i < 2; ++i) {
Jiri Olsa863e4512012-09-06 17:46:55 +0200219 ret = perf_session__process_events(session[i], &tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200220 if (ret)
221 goto out_delete;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200222 }
223
Jiri Olsa863e4512012-09-06 17:46:55 +0200224 evlist_old = older->evlist;
225 evlist_new = newer->evlist;
Arnaldo Carvalho de Melo9c443df2009-12-28 22:48:36 -0200226
Jiri Olsadd464342012-10-04 21:49:36 +0900227 perf_evlist__resort_hists(evlist_old, true);
228 perf_evlist__resort_hists(evlist_new, false);
Jiri Olsa863e4512012-09-06 17:46:55 +0200229
230 list_for_each_entry(evsel, &evlist_new->entries, node) {
231 struct perf_evsel *evsel_old;
232
233 evsel_old = evsel_match(evsel, evlist_old);
234 if (!evsel_old)
235 continue;
236
237 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
238 perf_evsel__name(evsel));
239
240 first = false;
241
Jiri Olsaa06d1432012-10-05 16:44:40 +0200242 hists__process(&evsel_old->hists, &evsel->hists);
Jiri Olsa863e4512012-09-06 17:46:55 +0200243 }
244
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200245out_delete:
246 for (i = 0; i < 2; ++i)
247 perf_session__delete(session[i]);
248 return ret;
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100249#undef older
250#undef newer
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200251}
252
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200253static const char * const diff_usage[] = {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200254 "perf diff [<options>] [old_file] [new_file]",
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200255 NULL,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200256};
257
258static const struct option options[] = {
Ian Munsiec0555642010-04-13 18:37:33 +1000259 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200260 "be more verbose (show symbol address, etc)"),
Shawn Bohrer34295552010-11-30 19:57:11 -0600261 OPT_BOOLEAN('M', "displacement", &show_displacement,
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200262 "Show position displacement relative to baseline"),
Jiri Olsaa06d1432012-10-05 16:44:40 +0200263 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
264 "Show only items with match in baseline"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200265 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
266 "dump raw trace in ASCII"),
267 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
268 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
269 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -0200270 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
271 "only consider symbols in these dsos"),
272 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
273 "only consider symbols in these comms"),
274 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
275 "only consider these symbols"),
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200276 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
277 "sort by key(s): pid, comm, dso, symbol, parent"),
278 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
279 "separator for columns, no spaces will be added between "
280 "columns '.' is reserved."),
David Ahernec5761e2010-12-09 13:27:07 -0700281 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
282 "Look for files with symbols relative to this directory"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200283 OPT_END()
284};
285
Jiri Olsa1d778222012-10-04 21:49:39 +0900286static void ui_init(void)
287{
288 perf_hpp__init();
289
290 /* No overhead column. */
291 perf_hpp__column_enable(PERF_HPP__OVERHEAD, false);
292
293 /* Display baseline/delta/displacement columns. */
294 perf_hpp__column_enable(PERF_HPP__BASELINE, true);
295 perf_hpp__column_enable(PERF_HPP__DELTA, true);
296
297 if (show_displacement)
298 perf_hpp__column_enable(PERF_HPP__DISPL, true);
299}
300
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300301int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200302{
Arnaldo Carvalho de Melo604c5c92009-12-16 14:09:53 -0200303 sort_order = diff__default_sort_order;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200304 argc = parse_options(argc, argv, options, diff_usage, 0);
305 if (argc) {
306 if (argc > 2)
307 usage_with_options(diff_usage, options);
308 if (argc == 2) {
309 input_old = argv[0];
310 input_new = argv[1];
311 } else
312 input_new = argv[0];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800313 } else if (symbol_conf.default_guest_vmlinux_name ||
314 symbol_conf.default_guest_kallsyms) {
315 input_old = "perf.data.host";
316 input_new = "perf.data.guest";
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200317 }
318
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200319 symbol_conf.exclude_other = false;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200320 if (symbol__init() < 0)
321 return -1;
322
Jiri Olsa1d778222012-10-04 21:49:39 +0900323 ui_init();
324
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200325 setup_sorting(diff_usage, options);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200326 setup_pager();
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200327
328 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
329 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
330 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
331
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200332 return __cmd_diff();
333}