perf diff: Change compute methods to work with pair directly
[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;
61949b21 27static bool show_period;
ed279da2 28static bool show_formula;
a06d143e 29static bool show_baseline_only;
96c47f19 30static bool sort_compute;
86a9eee0 31
81d5f958
JO
32static s64 compute_wdiff_w1;
33static s64 compute_wdiff_w2;
34
7aaf6b35
JO
35enum {
36 COMPUTE_DELTA,
37 COMPUTE_RATIO,
81d5f958 38 COMPUTE_WEIGHTED_DIFF,
7aaf6b35
JO
39 COMPUTE_MAX,
40};
41
42const char *compute_names[COMPUTE_MAX] = {
43 [COMPUTE_DELTA] = "delta",
44 [COMPUTE_RATIO] = "ratio",
81d5f958 45 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
7aaf6b35
JO
46};
47
48static int compute;
49
81d5f958
JO
50static 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
7aaf6b35
JO
99static int setup_compute(const struct option *opt, const char *str,
100 int unset __maybe_unused)
101{
102 int *cp = (int *) opt->value;
81d5f958
JO
103 char *cstr = (char *) str;
104 char buf[50];
7aaf6b35 105 unsigned i;
81d5f958 106 char *option;
7aaf6b35
JO
107
108 if (!str) {
109 *cp = COMPUTE_DELTA;
110 return 0;
111 }
112
96c47f19
JO
113 if (*str == '+') {
114 sort_compute = true;
81d5f958 115 cstr = (char *) ++str;
96c47f19
JO
116 if (!*str)
117 return 0;
118 }
119
81d5f958
JO
120 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
7aaf6b35 138 for (i = 0; i < COMPUTE_MAX; i++)
81d5f958 139 if (!strcmp(cstr, compute_names[i])) {
7aaf6b35 140 *cp = i;
81d5f958 141 return setup_compute_opt(option);
7aaf6b35
JO
142 }
143
144 pr_err("Failed: '%s' is not computation method "
81d5f958 145 "(use 'delta','ratio' or 'wdiff')\n", str);
7aaf6b35
JO
146 return -EINVAL;
147}
148
05472daa 149double perf_diff__period_percent(struct hist_entry *he, u64 period)
96c47f19
JO
150{
151 u64 total = he->hists->stats.total_period;
152 return (period * 100.0) / total;
153}
154
05472daa 155double perf_diff__compute_delta(struct hist_entry *he, struct hist_entry *pair)
96c47f19 156{
05472daa
JO
157 double new_percent = perf_diff__period_percent(he, he->stat.period);
158 double old_percent = perf_diff__period_percent(pair, pair->stat.period);
96c47f19
JO
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
05472daa 165double perf_diff__compute_ratio(struct hist_entry *he, struct hist_entry *pair)
96c47f19 166{
96c47f19 167 double new_period = he->stat.period;
05472daa 168 double old_period = pair->stat.period;
96c47f19
JO
169
170 he->diff.computed = true;
05472daa 171 he->diff.period_ratio = new_period / old_period;
96c47f19
JO
172 return he->diff.period_ratio;
173}
174
05472daa 175s64 perf_diff__compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
81d5f958 176{
81d5f958 177 u64 new_period = he->stat.period;
05472daa 178 u64 old_period = pair->stat.period;
81d5f958
JO
179
180 he->diff.computed = true;
05472daa
JO
181 he->diff.wdiff = new_period * compute_wdiff_w2 -
182 old_period * compute_wdiff_w1;
81d5f958
JO
183
184 return he->diff.wdiff;
185}
186
ed279da2
JO
187static int formula_delta(struct hist_entry *he, char *buf, size_t size)
188{
b821c732 189 struct hist_entry *pair = hist_entry__next_pair(he);
ed279da2
JO
190
191 if (!pair)
192 return -1;
193
194 return scnprintf(buf, size,
195 "(%" PRIu64 " * 100 / %" PRIu64 ") - "
196 "(%" PRIu64 " * 100 / %" PRIu64 ")",
197 he->stat.period, he->hists->stats.total_period,
198 pair->stat.period, pair->hists->stats.total_period);
199}
200
201static int formula_ratio(struct hist_entry *he, char *buf, size_t size)
202{
b821c732 203 struct hist_entry *pair = hist_entry__next_pair(he);
ed279da2
JO
204 double new_period = he->stat.period;
205 double old_period = pair ? pair->stat.period : 0;
206
207 if (!pair)
208 return -1;
209
210 return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
211}
212
213static int formula_wdiff(struct hist_entry *he, char *buf, size_t size)
214{
b821c732 215 struct hist_entry *pair = hist_entry__next_pair(he);
ed279da2
JO
216 u64 new_period = he->stat.period;
217 u64 old_period = pair ? pair->stat.period : 0;
218
219 if (!pair)
220 return -1;
221
222 return scnprintf(buf, size,
223 "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
224 new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
225}
226
227int perf_diff__formula(char *buf, size_t size, struct hist_entry *he)
228{
229 switch (compute) {
230 case COMPUTE_DELTA:
231 return formula_delta(he, buf, size);
232 case COMPUTE_RATIO:
233 return formula_ratio(he, buf, size);
234 case COMPUTE_WEIGHTED_DIFF:
235 return formula_wdiff(he, buf, size);
236 default:
237 BUG_ON(1);
238 }
239
240 return -1;
241}
242
1c02c4d2 243static int hists__add_entry(struct hists *self,
c82ee828 244 struct addr_location *al, u64 period)
86a9eee0 245{
c82ee828 246 if (__hists__add_entry(self, al, NULL, period) != NULL)
28e2a106
ACM
247 return 0;
248 return -ENOMEM;
86a9eee0
ACM
249}
250
1d037ca1 251static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
d20deb64 252 union perf_event *event,
8d50e5b4 253 struct perf_sample *sample,
863e451f 254 struct perf_evsel *evsel,
743eb868 255 struct machine *machine)
86a9eee0
ACM
256{
257 struct addr_location al;
86a9eee0 258
743eb868 259 if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
86a9eee0
ACM
260 pr_warning("problem processing %d event, skipping it.\n",
261 event->header.type);
262 return -1;
263 }
264
d88c48f9 265 if (al.filtered)
c410a338
ACM
266 return 0;
267
863e451f 268 if (hists__add_entry(&evsel->hists, &al, sample->period)) {
c82ee828 269 pr_warning("problem incrementing symbol period, skipping event\n");
86a9eee0
ACM
270 return -1;
271 }
272
863e451f 273 evsel->hists.stats.total_period += sample->period;
86a9eee0
ACM
274 return 0;
275}
276
863e451f
JO
277static struct perf_tool tool = {
278 .sample = diff__process_sample_event,
279 .mmap = perf_event__process_mmap,
280 .comm = perf_event__process_comm,
f62d3f0f
ACM
281 .exit = perf_event__process_exit,
282 .fork = perf_event__process_fork,
863e451f
JO
283 .lost = perf_event__process_lost,
284 .ordered_samples = true,
285 .ordering_requires_timestamps = true,
86a9eee0
ACM
286};
287
dd464345
JO
288static void insert_hist_entry_by_name(struct rb_root *root,
289 struct hist_entry *he)
86a9eee0
ACM
290{
291 struct rb_node **p = &root->rb_node;
292 struct rb_node *parent = NULL;
293 struct hist_entry *iter;
294
295 while (*p != NULL) {
86a9eee0
ACM
296 parent = *p;
297 iter = rb_entry(parent, struct hist_entry, rb_node);
9c443dfd 298 if (hist_entry__cmp(he, iter) < 0)
86a9eee0 299 p = &(*p)->rb_left;
9c443dfd 300 else
86a9eee0 301 p = &(*p)->rb_right;
86a9eee0
ACM
302 }
303
304 rb_link_node(&he->rb_node, parent, p);
305 rb_insert_color(&he->rb_node, root);
306}
307
dd464345 308static void hists__name_resort(struct hists *self, bool sort)
86a9eee0
ACM
309{
310 unsigned long position = 1;
311 struct rb_root tmp = RB_ROOT;
1c02c4d2 312 struct rb_node *next = rb_first(&self->entries);
86a9eee0
ACM
313
314 while (next != NULL) {
315 struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
316
317 next = rb_next(&n->rb_node);
86a9eee0 318 n->position = position++;
dd464345
JO
319
320 if (sort) {
321 rb_erase(&n->rb_node, &self->entries);
322 insert_hist_entry_by_name(&tmp, n);
323 }
86a9eee0
ACM
324 }
325
dd464345
JO
326 if (sort)
327 self->entries = tmp;
86a9eee0
ACM
328}
329
863e451f
JO
330static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
331 struct perf_evlist *evlist)
332{
333 struct perf_evsel *e;
334
335 list_for_each_entry(e, &evlist->entries, node)
336 if (perf_evsel__match2(evsel, e))
337 return e;
338
339 return NULL;
340}
341
dd464345
JO
342static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
343{
344 struct perf_evsel *evsel;
345
346 list_for_each_entry(evsel, &evlist->entries, node) {
347 struct hists *hists = &evsel->hists;
348
349 hists__output_resort(hists);
350
351 /*
352 * The hists__name_resort only sets possition
353 * if name is false.
354 */
355 if (name || ((!name) && show_displacement))
356 hists__name_resort(hists, name);
357 }
358}
359
a06d143e
JO
360static void hists__baseline_only(struct hists *hists)
361{
362 struct rb_node *next = rb_first(&hists->entries);
363
364 while (next != NULL) {
365 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
366
367 next = rb_next(&he->rb_node);
b821c732 368 if (!hist_entry__next_pair(he)) {
a06d143e
JO
369 rb_erase(&he->rb_node, &hists->entries);
370 hist_entry__free(he);
371 }
372 }
373}
374
96c47f19
JO
375static void hists__precompute(struct hists *hists)
376{
377 struct rb_node *next = rb_first(&hists->entries);
378
379 while (next != NULL) {
380 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
05472daa 381 struct hist_entry *pair = hist_entry__next_pair(he);
96c47f19
JO
382
383 next = rb_next(&he->rb_node);
05472daa
JO
384 if (!pair)
385 continue;
96c47f19
JO
386
387 switch (compute) {
388 case COMPUTE_DELTA:
05472daa 389 perf_diff__compute_delta(he, pair);
96c47f19
JO
390 break;
391 case COMPUTE_RATIO:
05472daa 392 perf_diff__compute_ratio(he, pair);
96c47f19 393 break;
81d5f958 394 case COMPUTE_WEIGHTED_DIFF:
05472daa 395 perf_diff__compute_wdiff(he, pair);
81d5f958 396 break;
96c47f19
JO
397 default:
398 BUG_ON(1);
399 }
400 }
401}
402
403static int64_t cmp_doubles(double l, double r)
404{
405 if (l > r)
406 return -1;
407 else if (l < r)
408 return 1;
409 else
410 return 0;
411}
412
413static int64_t
414hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
415 int c)
416{
417 switch (c) {
418 case COMPUTE_DELTA:
419 {
420 double l = left->diff.period_ratio_delta;
421 double r = right->diff.period_ratio_delta;
422
423 return cmp_doubles(l, r);
424 }
425 case COMPUTE_RATIO:
426 {
427 double l = left->diff.period_ratio;
428 double r = right->diff.period_ratio;
429
430 return cmp_doubles(l, r);
431 }
81d5f958
JO
432 case COMPUTE_WEIGHTED_DIFF:
433 {
434 s64 l = left->diff.wdiff;
435 s64 r = right->diff.wdiff;
436
437 return r - l;
438 }
96c47f19
JO
439 default:
440 BUG_ON(1);
441 }
442
443 return 0;
444}
445
446static void insert_hist_entry_by_compute(struct rb_root *root,
447 struct hist_entry *he,
448 int c)
449{
450 struct rb_node **p = &root->rb_node;
451 struct rb_node *parent = NULL;
452 struct hist_entry *iter;
453
454 while (*p != NULL) {
455 parent = *p;
456 iter = rb_entry(parent, struct hist_entry, rb_node);
457 if (hist_entry__cmp_compute(he, iter, c) < 0)
458 p = &(*p)->rb_left;
459 else
460 p = &(*p)->rb_right;
461 }
462
463 rb_link_node(&he->rb_node, parent, p);
464 rb_insert_color(&he->rb_node, root);
465}
466
467static void hists__compute_resort(struct hists *hists)
468{
469 struct rb_root tmp = RB_ROOT;
470 struct rb_node *next = rb_first(&hists->entries);
471
472 while (next != NULL) {
473 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
474
475 next = rb_next(&he->rb_node);
476
477 rb_erase(&he->rb_node, &hists->entries);
478 insert_hist_entry_by_compute(&tmp, he, compute);
479 }
480
481 hists->entries = tmp;
482}
483
a06d143e
JO
484static void hists__process(struct hists *old, struct hists *new)
485{
95529be4 486 hists__match(new, old);
a06d143e
JO
487
488 if (show_baseline_only)
489 hists__baseline_only(new);
bfaef4b4
ACM
490 else
491 hists__link(new, old);
a06d143e 492
96c47f19
JO
493 if (sort_compute) {
494 hists__precompute(new);
495 hists__compute_resort(new);
496 }
497
a06d143e
JO
498 hists__fprintf(new, true, 0, 0, stdout);
499}
500
86a9eee0
ACM
501static int __cmd_diff(void)
502{
503 int ret, i;
4bf9ce1b
JO
504#define older (session[0])
505#define newer (session[1])
86a9eee0 506 struct perf_session *session[2];
863e451f
JO
507 struct perf_evlist *evlist_new, *evlist_old;
508 struct perf_evsel *evsel;
509 bool first = true;
86a9eee0 510
4bf9ce1b 511 older = perf_session__new(input_old, O_RDONLY, force, false,
863e451f 512 &tool);
4bf9ce1b 513 newer = perf_session__new(input_new, O_RDONLY, force, false,
863e451f 514 &tool);
86a9eee0
ACM
515 if (session[0] == NULL || session[1] == NULL)
516 return -ENOMEM;
517
518 for (i = 0; i < 2; ++i) {
863e451f 519 ret = perf_session__process_events(session[i], &tool);
86a9eee0
ACM
520 if (ret)
521 goto out_delete;
86a9eee0
ACM
522 }
523
863e451f
JO
524 evlist_old = older->evlist;
525 evlist_new = newer->evlist;
526
dd464345
JO
527 perf_evlist__resort_hists(evlist_old, true);
528 perf_evlist__resort_hists(evlist_new, false);
863e451f
JO
529
530 list_for_each_entry(evsel, &evlist_new->entries, node) {
531 struct perf_evsel *evsel_old;
532
533 evsel_old = evsel_match(evsel, evlist_old);
534 if (!evsel_old)
535 continue;
536
537 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
538 perf_evsel__name(evsel));
539
540 first = false;
541
a06d143e 542 hists__process(&evsel_old->hists, &evsel->hists);
863e451f 543 }
9c443dfd 544
86a9eee0
ACM
545out_delete:
546 for (i = 0; i < 2; ++i)
547 perf_session__delete(session[i]);
548 return ret;
4bf9ce1b
JO
549#undef older
550#undef newer
86a9eee0
ACM
551}
552
0422a4fc 553static const char * const diff_usage[] = {
86a9eee0 554 "perf diff [<options>] [old_file] [new_file]",
0422a4fc 555 NULL,
86a9eee0
ACM
556};
557
558static const struct option options[] = {
c0555642 559 OPT_INCR('v', "verbose", &verbose,
86a9eee0 560 "be more verbose (show symbol address, etc)"),
34295559 561 OPT_BOOLEAN('M', "displacement", &show_displacement,
c351c281 562 "Show position displacement relative to baseline"),
a06d143e
JO
563 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
564 "Show only items with match in baseline"),
81d5f958
JO
565 OPT_CALLBACK('c', "compute", &compute,
566 "delta,ratio,wdiff:w1,w2 (default delta)",
7aaf6b35
JO
567 "Entries differential computation selection",
568 setup_compute),
61949b21
JO
569 OPT_BOOLEAN('p', "period", &show_period,
570 "Show period values."),
ed279da2
JO
571 OPT_BOOLEAN('F', "formula", &show_formula,
572 "Show formula."),
86a9eee0
ACM
573 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
574 "dump raw trace in ASCII"),
575 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
576 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
577 "load module symbols - WARNING: use only with -k and LIVE kernel"),
c410a338
ACM
578 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
579 "only consider symbols in these dsos"),
580 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
581 "only consider symbols in these comms"),
582 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
583 "only consider these symbols"),
c351c281
ACM
584 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
585 "sort by key(s): pid, comm, dso, symbol, parent"),
586 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
587 "separator for columns, no spaces will be added between "
588 "columns '.' is reserved."),
ec5761ea
DA
589 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
590 "Look for files with symbols relative to this directory"),
86a9eee0
ACM
591 OPT_END()
592};
593
1d77822e
JO
594static void ui_init(void)
595{
ed279da2
JO
596 /*
597 * Display baseline/delta/ratio/displacement/
598 * formula/periods columns.
599 */
1240005e 600 perf_hpp__column_enable(PERF_HPP__BASELINE);
7aaf6b35
JO
601
602 switch (compute) {
603 case COMPUTE_DELTA:
1240005e 604 perf_hpp__column_enable(PERF_HPP__DELTA);
7aaf6b35
JO
605 break;
606 case COMPUTE_RATIO:
1240005e 607 perf_hpp__column_enable(PERF_HPP__RATIO);
81d5f958
JO
608 break;
609 case COMPUTE_WEIGHTED_DIFF:
1240005e 610 perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF);
7aaf6b35
JO
611 break;
612 default:
613 BUG_ON(1);
614 };
1d77822e
JO
615
616 if (show_displacement)
1240005e 617 perf_hpp__column_enable(PERF_HPP__DISPL);
61949b21 618
ed279da2 619 if (show_formula)
1240005e 620 perf_hpp__column_enable(PERF_HPP__FORMULA);
ed279da2 621
61949b21 622 if (show_period) {
1240005e
JO
623 perf_hpp__column_enable(PERF_HPP__PERIOD);
624 perf_hpp__column_enable(PERF_HPP__PERIOD_BASELINE);
61949b21 625 }
1d77822e
JO
626}
627
1d037ca1 628int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
86a9eee0 629{
604c5c92 630 sort_order = diff__default_sort_order;
86a9eee0
ACM
631 argc = parse_options(argc, argv, options, diff_usage, 0);
632 if (argc) {
633 if (argc > 2)
634 usage_with_options(diff_usage, options);
635 if (argc == 2) {
636 input_old = argv[0];
637 input_new = argv[1];
638 } else
639 input_new = argv[0];
a1645ce1
ZY
640 } else if (symbol_conf.default_guest_vmlinux_name ||
641 symbol_conf.default_guest_kallsyms) {
642 input_old = "perf.data.host";
643 input_new = "perf.data.guest";
86a9eee0
ACM
644 }
645
c351c281 646 symbol_conf.exclude_other = false;
655000e7
ACM
647 if (symbol__init() < 0)
648 return -1;
649
1d77822e
JO
650 ui_init();
651
655000e7 652 setup_sorting(diff_usage, options);
86a9eee0 653 setup_pager();
c351c281
ACM
654
655 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
656 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
657 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
658
86a9eee0
ACM
659 return __cmd_diff();
660}
This page took 0.205541 seconds and 5 git commands to generate.