]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/builtin-diff.c
perf diff: Add -p option to display period values for hist entries
[mirror_ubuntu-artful-kernel.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;
a06d143e 28static bool show_baseline_only;
96c47f19 29static bool sort_compute;
86a9eee0 30
81d5f958
JO
31static s64 compute_wdiff_w1;
32static s64 compute_wdiff_w2;
33
7aaf6b35
JO
34enum {
35 COMPUTE_DELTA,
36 COMPUTE_RATIO,
81d5f958 37 COMPUTE_WEIGHTED_DIFF,
7aaf6b35
JO
38 COMPUTE_MAX,
39};
40
41const char *compute_names[COMPUTE_MAX] = {
42 [COMPUTE_DELTA] = "delta",
43 [COMPUTE_RATIO] = "ratio",
81d5f958 44 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
7aaf6b35
JO
45};
46
47static int compute;
48
81d5f958
JO
49static 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
7aaf6b35
JO
98static int setup_compute(const struct option *opt, const char *str,
99 int unset __maybe_unused)
100{
101 int *cp = (int *) opt->value;
81d5f958
JO
102 char *cstr = (char *) str;
103 char buf[50];
7aaf6b35 104 unsigned i;
81d5f958 105 char *option;
7aaf6b35
JO
106
107 if (!str) {
108 *cp = COMPUTE_DELTA;
109 return 0;
110 }
111
96c47f19
JO
112 if (*str == '+') {
113 sort_compute = true;
81d5f958 114 cstr = (char *) ++str;
96c47f19
JO
115 if (!*str)
116 return 0;
117 }
118
81d5f958
JO
119 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
7aaf6b35 137 for (i = 0; i < COMPUTE_MAX; i++)
81d5f958 138 if (!strcmp(cstr, compute_names[i])) {
7aaf6b35 139 *cp = i;
81d5f958 140 return setup_compute_opt(option);
7aaf6b35
JO
141 }
142
143 pr_err("Failed: '%s' is not computation method "
81d5f958 144 "(use 'delta','ratio' or 'wdiff')\n", str);
7aaf6b35
JO
145 return -EINVAL;
146}
147
96c47f19
JO
148static 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
81d5f958
JO
176s64 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
1c02c4d2 193static int hists__add_entry(struct hists *self,
c82ee828 194 struct addr_location *al, u64 period)
86a9eee0 195{
c82ee828 196 if (__hists__add_entry(self, al, NULL, period) != NULL)
28e2a106
ACM
197 return 0;
198 return -ENOMEM;
86a9eee0
ACM
199}
200
1d037ca1 201static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
d20deb64 202 union perf_event *event,
8d50e5b4 203 struct perf_sample *sample,
863e451f 204 struct perf_evsel *evsel,
743eb868 205 struct machine *machine)
86a9eee0
ACM
206{
207 struct addr_location al;
86a9eee0 208
743eb868 209 if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
86a9eee0
ACM
210 pr_warning("problem processing %d event, skipping it.\n",
211 event->header.type);
212 return -1;
213 }
214
cdbae314 215 if (al.filtered || al.sym == NULL)
c410a338
ACM
216 return 0;
217
863e451f 218 if (hists__add_entry(&evsel->hists, &al, sample->period)) {
c82ee828 219 pr_warning("problem incrementing symbol period, skipping event\n");
86a9eee0
ACM
220 return -1;
221 }
222
863e451f 223 evsel->hists.stats.total_period += sample->period;
86a9eee0
ACM
224 return 0;
225}
226
863e451f
JO
227static 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,
86a9eee0
ACM
236};
237
dd464345
JO
238static void insert_hist_entry_by_name(struct rb_root *root,
239 struct hist_entry *he)
86a9eee0
ACM
240{
241 struct rb_node **p = &root->rb_node;
242 struct rb_node *parent = NULL;
243 struct hist_entry *iter;
244
245 while (*p != NULL) {
86a9eee0
ACM
246 parent = *p;
247 iter = rb_entry(parent, struct hist_entry, rb_node);
9c443dfd 248 if (hist_entry__cmp(he, iter) < 0)
86a9eee0 249 p = &(*p)->rb_left;
9c443dfd 250 else
86a9eee0 251 p = &(*p)->rb_right;
86a9eee0
ACM
252 }
253
254 rb_link_node(&he->rb_node, parent, p);
255 rb_insert_color(&he->rb_node, root);
256}
257
dd464345 258static void hists__name_resort(struct hists *self, bool sort)
86a9eee0
ACM
259{
260 unsigned long position = 1;
261 struct rb_root tmp = RB_ROOT;
1c02c4d2 262 struct rb_node *next = rb_first(&self->entries);
86a9eee0
ACM
263
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);
86a9eee0 268 n->position = position++;
dd464345
JO
269
270 if (sort) {
271 rb_erase(&n->rb_node, &self->entries);
272 insert_hist_entry_by_name(&tmp, n);
273 }
86a9eee0
ACM
274 }
275
dd464345
JO
276 if (sort)
277 self->entries = tmp;
86a9eee0
ACM
278}
279
1c02c4d2
ACM
280static struct hist_entry *hists__find_entry(struct hists *self,
281 struct hist_entry *he)
86a9eee0 282{
1c02c4d2 283 struct rb_node *n = self->entries.rb_node;
86a9eee0
ACM
284
285 while (n) {
286 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
9c443dfd 287 int64_t cmp = hist_entry__cmp(he, iter);
86a9eee0 288
9c443dfd 289 if (cmp < 0)
86a9eee0 290 n = n->rb_left;
9c443dfd 291 else if (cmp > 0)
86a9eee0 292 n = n->rb_right;
dd464345 293 else
9c443dfd 294 return iter;
86a9eee0
ACM
295 }
296
297 return NULL;
298}
299
1c02c4d2 300static void hists__match(struct hists *older, struct hists *newer)
86a9eee0
ACM
301{
302 struct rb_node *nd;
303
1c02c4d2 304 for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) {
86a9eee0 305 struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
1c02c4d2 306 pos->pair = hists__find_entry(older, pos);
86a9eee0
ACM
307 }
308}
309
863e451f
JO
310static 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
dd464345
JO
322static 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
a06d143e
JO
340static 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
96c47f19
JO
355static 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;
81d5f958
JO
371 case COMPUTE_WEIGHTED_DIFF:
372 perf_diff__compute_wdiff(he);
373 break;
96c47f19
JO
374 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 }
81d5f958
JO
409 case COMPUTE_WEIGHTED_DIFF:
410 {
411 s64 l = left->diff.wdiff;
412 s64 r = right->diff.wdiff;
413
414 return r - l;
415 }
96c47f19
JO
416 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
a06d143e
JO
461static 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
96c47f19
JO
468 if (sort_compute) {
469 hists__precompute(new);
470 hists__compute_resort(new);
471 }
472
a06d143e
JO
473 hists__fprintf(new, true, 0, 0, stdout);
474}
475
86a9eee0
ACM
476static int __cmd_diff(void)
477{
478 int ret, i;
4bf9ce1b
JO
479#define older (session[0])
480#define newer (session[1])
86a9eee0 481 struct perf_session *session[2];
863e451f
JO
482 struct perf_evlist *evlist_new, *evlist_old;
483 struct perf_evsel *evsel;
484 bool first = true;
86a9eee0 485
4bf9ce1b 486 older = perf_session__new(input_old, O_RDONLY, force, false,
863e451f 487 &tool);
4bf9ce1b 488 newer = perf_session__new(input_new, O_RDONLY, force, false,
863e451f 489 &tool);
86a9eee0
ACM
490 if (session[0] == NULL || session[1] == NULL)
491 return -ENOMEM;
492
493 for (i = 0; i < 2; ++i) {
863e451f 494 ret = perf_session__process_events(session[i], &tool);
86a9eee0
ACM
495 if (ret)
496 goto out_delete;
86a9eee0
ACM
497 }
498
863e451f
JO
499 evlist_old = older->evlist;
500 evlist_new = newer->evlist;
501
dd464345
JO
502 perf_evlist__resort_hists(evlist_old, true);
503 perf_evlist__resort_hists(evlist_new, false);
863e451f
JO
504
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
a06d143e 517 hists__process(&evsel_old->hists, &evsel->hists);
863e451f 518 }
9c443dfd 519
86a9eee0
ACM
520out_delete:
521 for (i = 0; i < 2; ++i)
522 perf_session__delete(session[i]);
523 return ret;
4bf9ce1b
JO
524#undef older
525#undef newer
86a9eee0
ACM
526}
527
0422a4fc 528static const char * const diff_usage[] = {
86a9eee0 529 "perf diff [<options>] [old_file] [new_file]",
0422a4fc 530 NULL,
86a9eee0
ACM
531};
532
533static const struct option options[] = {
c0555642 534 OPT_INCR('v', "verbose", &verbose,
86a9eee0 535 "be more verbose (show symbol address, etc)"),
34295559 536 OPT_BOOLEAN('M', "displacement", &show_displacement,
c351c281 537 "Show position displacement relative to baseline"),
a06d143e
JO
538 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
539 "Show only items with match in baseline"),
81d5f958
JO
540 OPT_CALLBACK('c', "compute", &compute,
541 "delta,ratio,wdiff:w1,w2 (default delta)",
7aaf6b35
JO
542 "Entries differential computation selection",
543 setup_compute),
61949b21
JO
544 OPT_BOOLEAN('p', "period", &show_period,
545 "Show period values."),
86a9eee0
ACM
546 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"),
c410a338
ACM
551 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"),
c351c281
ACM
557 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."),
ec5761ea
DA
562 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
563 "Look for files with symbols relative to this directory"),
86a9eee0
ACM
564 OPT_END()
565};
566
1d77822e
JO
567static void ui_init(void)
568{
569 perf_hpp__init();
570
571 /* No overhead column. */
572 perf_hpp__column_enable(PERF_HPP__OVERHEAD, false);
573
61949b21 574 /* Display baseline/delta/ratio/displacement/periods columns. */
1d77822e 575 perf_hpp__column_enable(PERF_HPP__BASELINE, true);
7aaf6b35
JO
576
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);
81d5f958
JO
583 break;
584 case COMPUTE_WEIGHTED_DIFF:
585 perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF, true);
7aaf6b35
JO
586 break;
587 default:
588 BUG_ON(1);
589 };
1d77822e
JO
590
591 if (show_displacement)
592 perf_hpp__column_enable(PERF_HPP__DISPL, true);
61949b21
JO
593
594 if (show_period) {
595 perf_hpp__column_enable(PERF_HPP__PERIOD, true);
596 perf_hpp__column_enable(PERF_HPP__PERIOD_BASELINE, true);
597 }
1d77822e
JO
598}
599
1d037ca1 600int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
86a9eee0 601{
604c5c92 602 sort_order = diff__default_sort_order;
86a9eee0
ACM
603 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];
a1645ce1
ZY
612 } 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";
86a9eee0
ACM
616 }
617
c351c281 618 symbol_conf.exclude_other = false;
655000e7
ACM
619 if (symbol__init() < 0)
620 return -1;
621
1d77822e
JO
622 ui_init();
623
655000e7 624 setup_sorting(diff_usage, options);
86a9eee0 625 setup_pager();
c351c281
ACM
626
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
86a9eee0
ACM
631 return __cmd_diff();
632}