perf diff: Add option to sort entries based on diff computation
[deliverable/linux.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
c351c281
ACM
22static char const *input_old = "perf.data.old",
23 *input_new = "perf.data";
604c5c92 24static char diff__default_sort_order[] = "dso,symbol";
c0555642 25static bool force;
c351c281 26static bool show_displacement;
a06d143e 27static bool show_baseline_only;
96c47f19 28static bool sort_compute;
86a9eee0 29
7aaf6b35
JO
30enum {
31 COMPUTE_DELTA,
32 COMPUTE_RATIO,
33 COMPUTE_MAX,
34};
35
36const char *compute_names[COMPUTE_MAX] = {
37 [COMPUTE_DELTA] = "delta",
38 [COMPUTE_RATIO] = "ratio",
39};
40
41static int compute;
42
43static int setup_compute(const struct option *opt, const char *str,
44 int unset __maybe_unused)
45{
46 int *cp = (int *) opt->value;
47 unsigned i;
48
49 if (!str) {
50 *cp = COMPUTE_DELTA;
51 return 0;
52 }
53
96c47f19
JO
54 if (*str == '+') {
55 sort_compute = true;
56 str++;
57 if (!*str)
58 return 0;
59 }
60
7aaf6b35
JO
61 for (i = 0; i < COMPUTE_MAX; i++)
62 if (!strcmp(str, compute_names[i])) {
63 *cp = i;
64 return 0;
65 }
66
67 pr_err("Failed: '%s' is not computation method "
68 "(use 'delta' or 'ratio').\n", str);
69 return -EINVAL;
70}
71
96c47f19
JO
72static double get_period_percent(struct hist_entry *he, u64 period)
73{
74 u64 total = he->hists->stats.total_period;
75 return (period * 100.0) / total;
76}
77
78double perf_diff__compute_delta(struct hist_entry *he)
79{
80 struct hist_entry *pair = he->pair;
81 double new_percent = get_period_percent(he, he->stat.period);
82 double old_percent = pair ? get_period_percent(pair, pair->stat.period) : 0.0;
83
84 he->diff.period_ratio_delta = new_percent - old_percent;
85 he->diff.computed = true;
86 return he->diff.period_ratio_delta;
87}
88
89double perf_diff__compute_ratio(struct hist_entry *he)
90{
91 struct hist_entry *pair = he->pair;
92 double new_period = he->stat.period;
93 double old_period = pair ? pair->stat.period : 0;
94
95 he->diff.computed = true;
96 he->diff.period_ratio = pair ? (new_period / old_period) : 0;
97 return he->diff.period_ratio;
98}
99
1c02c4d2 100static int hists__add_entry(struct hists *self,
c82ee828 101 struct addr_location *al, u64 period)
86a9eee0 102{
c82ee828 103 if (__hists__add_entry(self, al, NULL, period) != NULL)
28e2a106
ACM
104 return 0;
105 return -ENOMEM;
86a9eee0
ACM
106}
107
1d037ca1 108static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
d20deb64 109 union perf_event *event,
8d50e5b4 110 struct perf_sample *sample,
863e451f 111 struct perf_evsel *evsel,
743eb868 112 struct machine *machine)
86a9eee0
ACM
113{
114 struct addr_location al;
86a9eee0 115
743eb868 116 if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
86a9eee0
ACM
117 pr_warning("problem processing %d event, skipping it.\n",
118 event->header.type);
119 return -1;
120 }
121
cdbae314 122 if (al.filtered || al.sym == NULL)
c410a338
ACM
123 return 0;
124
863e451f 125 if (hists__add_entry(&evsel->hists, &al, sample->period)) {
c82ee828 126 pr_warning("problem incrementing symbol period, skipping event\n");
86a9eee0
ACM
127 return -1;
128 }
129
863e451f 130 evsel->hists.stats.total_period += sample->period;
86a9eee0
ACM
131 return 0;
132}
133
863e451f
JO
134static struct perf_tool tool = {
135 .sample = diff__process_sample_event,
136 .mmap = perf_event__process_mmap,
137 .comm = perf_event__process_comm,
138 .exit = perf_event__process_task,
139 .fork = perf_event__process_task,
140 .lost = perf_event__process_lost,
141 .ordered_samples = true,
142 .ordering_requires_timestamps = true,
86a9eee0
ACM
143};
144
dd464345
JO
145static void insert_hist_entry_by_name(struct rb_root *root,
146 struct hist_entry *he)
86a9eee0
ACM
147{
148 struct rb_node **p = &root->rb_node;
149 struct rb_node *parent = NULL;
150 struct hist_entry *iter;
151
152 while (*p != NULL) {
86a9eee0
ACM
153 parent = *p;
154 iter = rb_entry(parent, struct hist_entry, rb_node);
9c443dfd 155 if (hist_entry__cmp(he, iter) < 0)
86a9eee0 156 p = &(*p)->rb_left;
9c443dfd 157 else
86a9eee0 158 p = &(*p)->rb_right;
86a9eee0
ACM
159 }
160
161 rb_link_node(&he->rb_node, parent, p);
162 rb_insert_color(&he->rb_node, root);
163}
164
dd464345 165static void hists__name_resort(struct hists *self, bool sort)
86a9eee0
ACM
166{
167 unsigned long position = 1;
168 struct rb_root tmp = RB_ROOT;
1c02c4d2 169 struct rb_node *next = rb_first(&self->entries);
86a9eee0
ACM
170
171 while (next != NULL) {
172 struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
173
174 next = rb_next(&n->rb_node);
86a9eee0 175 n->position = position++;
dd464345
JO
176
177 if (sort) {
178 rb_erase(&n->rb_node, &self->entries);
179 insert_hist_entry_by_name(&tmp, n);
180 }
86a9eee0
ACM
181 }
182
dd464345
JO
183 if (sort)
184 self->entries = tmp;
86a9eee0
ACM
185}
186
1c02c4d2
ACM
187static struct hist_entry *hists__find_entry(struct hists *self,
188 struct hist_entry *he)
86a9eee0 189{
1c02c4d2 190 struct rb_node *n = self->entries.rb_node;
86a9eee0
ACM
191
192 while (n) {
193 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
9c443dfd 194 int64_t cmp = hist_entry__cmp(he, iter);
86a9eee0 195
9c443dfd 196 if (cmp < 0)
86a9eee0 197 n = n->rb_left;
9c443dfd 198 else if (cmp > 0)
86a9eee0 199 n = n->rb_right;
dd464345 200 else
9c443dfd 201 return iter;
86a9eee0
ACM
202 }
203
204 return NULL;
205}
206
1c02c4d2 207static void hists__match(struct hists *older, struct hists *newer)
86a9eee0
ACM
208{
209 struct rb_node *nd;
210
1c02c4d2 211 for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) {
86a9eee0 212 struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
1c02c4d2 213 pos->pair = hists__find_entry(older, pos);
86a9eee0
ACM
214 }
215}
216
863e451f
JO
217static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
218 struct perf_evlist *evlist)
219{
220 struct perf_evsel *e;
221
222 list_for_each_entry(e, &evlist->entries, node)
223 if (perf_evsel__match2(evsel, e))
224 return e;
225
226 return NULL;
227}
228
dd464345
JO
229static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
230{
231 struct perf_evsel *evsel;
232
233 list_for_each_entry(evsel, &evlist->entries, node) {
234 struct hists *hists = &evsel->hists;
235
236 hists__output_resort(hists);
237
238 /*
239 * The hists__name_resort only sets possition
240 * if name is false.
241 */
242 if (name || ((!name) && show_displacement))
243 hists__name_resort(hists, name);
244 }
245}
246
a06d143e
JO
247static void hists__baseline_only(struct hists *hists)
248{
249 struct rb_node *next = rb_first(&hists->entries);
250
251 while (next != NULL) {
252 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
253
254 next = rb_next(&he->rb_node);
255 if (!he->pair) {
256 rb_erase(&he->rb_node, &hists->entries);
257 hist_entry__free(he);
258 }
259 }
260}
261
96c47f19
JO
262static void hists__precompute(struct hists *hists)
263{
264 struct rb_node *next = rb_first(&hists->entries);
265
266 while (next != NULL) {
267 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
268
269 next = rb_next(&he->rb_node);
270
271 switch (compute) {
272 case COMPUTE_DELTA:
273 perf_diff__compute_delta(he);
274 break;
275 case COMPUTE_RATIO:
276 perf_diff__compute_ratio(he);
277 break;
278 default:
279 BUG_ON(1);
280 }
281 }
282}
283
284static int64_t cmp_doubles(double l, double r)
285{
286 if (l > r)
287 return -1;
288 else if (l < r)
289 return 1;
290 else
291 return 0;
292}
293
294static int64_t
295hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
296 int c)
297{
298 switch (c) {
299 case COMPUTE_DELTA:
300 {
301 double l = left->diff.period_ratio_delta;
302 double r = right->diff.period_ratio_delta;
303
304 return cmp_doubles(l, r);
305 }
306 case COMPUTE_RATIO:
307 {
308 double l = left->diff.period_ratio;
309 double r = right->diff.period_ratio;
310
311 return cmp_doubles(l, r);
312 }
313 default:
314 BUG_ON(1);
315 }
316
317 return 0;
318}
319
320static void insert_hist_entry_by_compute(struct rb_root *root,
321 struct hist_entry *he,
322 int c)
323{
324 struct rb_node **p = &root->rb_node;
325 struct rb_node *parent = NULL;
326 struct hist_entry *iter;
327
328 while (*p != NULL) {
329 parent = *p;
330 iter = rb_entry(parent, struct hist_entry, rb_node);
331 if (hist_entry__cmp_compute(he, iter, c) < 0)
332 p = &(*p)->rb_left;
333 else
334 p = &(*p)->rb_right;
335 }
336
337 rb_link_node(&he->rb_node, parent, p);
338 rb_insert_color(&he->rb_node, root);
339}
340
341static void hists__compute_resort(struct hists *hists)
342{
343 struct rb_root tmp = RB_ROOT;
344 struct rb_node *next = rb_first(&hists->entries);
345
346 while (next != NULL) {
347 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
348
349 next = rb_next(&he->rb_node);
350
351 rb_erase(&he->rb_node, &hists->entries);
352 insert_hist_entry_by_compute(&tmp, he, compute);
353 }
354
355 hists->entries = tmp;
356}
357
a06d143e
JO
358static void hists__process(struct hists *old, struct hists *new)
359{
360 hists__match(old, new);
361
362 if (show_baseline_only)
363 hists__baseline_only(new);
364
96c47f19
JO
365 if (sort_compute) {
366 hists__precompute(new);
367 hists__compute_resort(new);
368 }
369
a06d143e
JO
370 hists__fprintf(new, true, 0, 0, stdout);
371}
372
86a9eee0
ACM
373static int __cmd_diff(void)
374{
375 int ret, i;
4bf9ce1b
JO
376#define older (session[0])
377#define newer (session[1])
86a9eee0 378 struct perf_session *session[2];
863e451f
JO
379 struct perf_evlist *evlist_new, *evlist_old;
380 struct perf_evsel *evsel;
381 bool first = true;
86a9eee0 382
4bf9ce1b 383 older = perf_session__new(input_old, O_RDONLY, force, false,
863e451f 384 &tool);
4bf9ce1b 385 newer = perf_session__new(input_new, O_RDONLY, force, false,
863e451f 386 &tool);
86a9eee0
ACM
387 if (session[0] == NULL || session[1] == NULL)
388 return -ENOMEM;
389
390 for (i = 0; i < 2; ++i) {
863e451f 391 ret = perf_session__process_events(session[i], &tool);
86a9eee0
ACM
392 if (ret)
393 goto out_delete;
86a9eee0
ACM
394 }
395
863e451f
JO
396 evlist_old = older->evlist;
397 evlist_new = newer->evlist;
398
dd464345
JO
399 perf_evlist__resort_hists(evlist_old, true);
400 perf_evlist__resort_hists(evlist_new, false);
863e451f
JO
401
402 list_for_each_entry(evsel, &evlist_new->entries, node) {
403 struct perf_evsel *evsel_old;
404
405 evsel_old = evsel_match(evsel, evlist_old);
406 if (!evsel_old)
407 continue;
408
409 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
410 perf_evsel__name(evsel));
411
412 first = false;
413
a06d143e 414 hists__process(&evsel_old->hists, &evsel->hists);
863e451f 415 }
9c443dfd 416
86a9eee0
ACM
417out_delete:
418 for (i = 0; i < 2; ++i)
419 perf_session__delete(session[i]);
420 return ret;
4bf9ce1b
JO
421#undef older
422#undef newer
86a9eee0
ACM
423}
424
0422a4fc 425static const char * const diff_usage[] = {
86a9eee0 426 "perf diff [<options>] [old_file] [new_file]",
0422a4fc 427 NULL,
86a9eee0
ACM
428};
429
430static const struct option options[] = {
c0555642 431 OPT_INCR('v', "verbose", &verbose,
86a9eee0 432 "be more verbose (show symbol address, etc)"),
34295559 433 OPT_BOOLEAN('M', "displacement", &show_displacement,
c351c281 434 "Show position displacement relative to baseline"),
a06d143e
JO
435 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
436 "Show only items with match in baseline"),
7aaf6b35
JO
437 OPT_CALLBACK('c', "compute", &compute, "delta,ratio (default delta)",
438 "Entries differential computation selection",
439 setup_compute),
86a9eee0
ACM
440 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
441 "dump raw trace in ASCII"),
442 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
443 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
444 "load module symbols - WARNING: use only with -k and LIVE kernel"),
c410a338
ACM
445 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
446 "only consider symbols in these dsos"),
447 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
448 "only consider symbols in these comms"),
449 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
450 "only consider these symbols"),
c351c281
ACM
451 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
452 "sort by key(s): pid, comm, dso, symbol, parent"),
453 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
454 "separator for columns, no spaces will be added between "
455 "columns '.' is reserved."),
ec5761ea
DA
456 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
457 "Look for files with symbols relative to this directory"),
86a9eee0
ACM
458 OPT_END()
459};
460
1d77822e
JO
461static void ui_init(void)
462{
463 perf_hpp__init();
464
465 /* No overhead column. */
466 perf_hpp__column_enable(PERF_HPP__OVERHEAD, false);
467
7aaf6b35 468 /* Display baseline/delta/ratio/displacement columns. */
1d77822e 469 perf_hpp__column_enable(PERF_HPP__BASELINE, true);
7aaf6b35
JO
470
471 switch (compute) {
472 case COMPUTE_DELTA:
473 perf_hpp__column_enable(PERF_HPP__DELTA, true);
474 break;
475 case COMPUTE_RATIO:
476 perf_hpp__column_enable(PERF_HPP__RATIO, true);
477 break;
478 default:
479 BUG_ON(1);
480 };
1d77822e
JO
481
482 if (show_displacement)
483 perf_hpp__column_enable(PERF_HPP__DISPL, true);
484}
485
1d037ca1 486int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
86a9eee0 487{
604c5c92 488 sort_order = diff__default_sort_order;
86a9eee0
ACM
489 argc = parse_options(argc, argv, options, diff_usage, 0);
490 if (argc) {
491 if (argc > 2)
492 usage_with_options(diff_usage, options);
493 if (argc == 2) {
494 input_old = argv[0];
495 input_new = argv[1];
496 } else
497 input_new = argv[0];
a1645ce1
ZY
498 } else if (symbol_conf.default_guest_vmlinux_name ||
499 symbol_conf.default_guest_kallsyms) {
500 input_old = "perf.data.host";
501 input_new = "perf.data.guest";
86a9eee0
ACM
502 }
503
c351c281 504 symbol_conf.exclude_other = false;
655000e7
ACM
505 if (symbol__init() < 0)
506 return -1;
507
1d77822e
JO
508 ui_init();
509
655000e7 510 setup_sorting(diff_usage, options);
86a9eee0 511 setup_pager();
c351c281
ACM
512
513 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
514 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
515 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
516
86a9eee0
ACM
517 return __cmd_diff();
518}
This page took 0.143973 seconds and 5 git commands to generate.