]>
Commit | Line | Data |
---|---|---|
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> | |
345dc0b4 JO |
21 | #include <math.h> |
22 | ||
23 | /* Diff command specific HPP columns. */ | |
24 | enum { | |
25 | PERF_HPP_DIFF__BASELINE, | |
26 | PERF_HPP_DIFF__PERIOD, | |
27 | PERF_HPP_DIFF__PERIOD_BASELINE, | |
28 | PERF_HPP_DIFF__DELTA, | |
29 | PERF_HPP_DIFF__RATIO, | |
30 | PERF_HPP_DIFF__WEIGHTED_DIFF, | |
31 | PERF_HPP_DIFF__FORMULA, | |
32 | ||
33 | PERF_HPP_DIFF__MAX_INDEX | |
34 | }; | |
35 | ||
36 | struct diff_hpp_fmt { | |
37 | struct perf_hpp_fmt fmt; | |
38 | int idx; | |
39 | char *header; | |
40 | int header_width; | |
41 | }; | |
86a9eee0 | 42 | |
ec308426 JO |
43 | struct data__file { |
44 | struct perf_session *session; | |
45 | const char *file; | |
46 | int idx; | |
47 | }; | |
48 | ||
49 | static struct data__file *data__files; | |
50 | static int data__files_cnt; | |
51 | ||
52 | #define data__for_each_file_start(i, d, s) \ | |
53 | for (i = s, d = &data__files[s]; \ | |
54 | i < data__files_cnt; \ | |
55 | i++, d = &data__files[i]) | |
56 | ||
57 | #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0) | |
58 | ||
59 | static char diff__default_sort_order[] = "dso,symbol"; | |
60 | static bool force; | |
61949b21 | 61 | static bool show_period; |
ed279da2 | 62 | static bool show_formula; |
a06d143e | 63 | static bool show_baseline_only; |
96c47f19 | 64 | static bool sort_compute; |
86a9eee0 | 65 | |
81d5f958 JO |
66 | static s64 compute_wdiff_w1; |
67 | static s64 compute_wdiff_w2; | |
68 | ||
7aaf6b35 JO |
69 | enum { |
70 | COMPUTE_DELTA, | |
71 | COMPUTE_RATIO, | |
81d5f958 | 72 | COMPUTE_WEIGHTED_DIFF, |
7aaf6b35 JO |
73 | COMPUTE_MAX, |
74 | }; | |
75 | ||
76 | const char *compute_names[COMPUTE_MAX] = { | |
77 | [COMPUTE_DELTA] = "delta", | |
78 | [COMPUTE_RATIO] = "ratio", | |
81d5f958 | 79 | [COMPUTE_WEIGHTED_DIFF] = "wdiff", |
7aaf6b35 JO |
80 | }; |
81 | ||
82 | static int compute; | |
83 | ||
345dc0b4 JO |
84 | static int compute_2_hpp[COMPUTE_MAX] = { |
85 | [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA, | |
86 | [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO, | |
87 | [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF, | |
88 | }; | |
89 | ||
90 | #define MAX_COL_WIDTH 70 | |
91 | ||
92 | static struct header_column { | |
93 | const char *name; | |
94 | int width; | |
95 | } columns[PERF_HPP_DIFF__MAX_INDEX] = { | |
96 | [PERF_HPP_DIFF__BASELINE] = { | |
97 | .name = "Baseline", | |
98 | }, | |
99 | [PERF_HPP_DIFF__PERIOD] = { | |
100 | .name = "Period", | |
101 | .width = 14, | |
102 | }, | |
103 | [PERF_HPP_DIFF__PERIOD_BASELINE] = { | |
104 | .name = "Base period", | |
105 | .width = 14, | |
106 | }, | |
107 | [PERF_HPP_DIFF__DELTA] = { | |
108 | .name = "Delta", | |
109 | .width = 7, | |
110 | }, | |
111 | [PERF_HPP_DIFF__RATIO] = { | |
112 | .name = "Ratio", | |
113 | .width = 14, | |
114 | }, | |
115 | [PERF_HPP_DIFF__WEIGHTED_DIFF] = { | |
116 | .name = "Weighted diff", | |
117 | .width = 14, | |
118 | }, | |
119 | [PERF_HPP_DIFF__FORMULA] = { | |
120 | .name = "Formula", | |
121 | .width = MAX_COL_WIDTH, | |
122 | } | |
123 | }; | |
124 | ||
81d5f958 JO |
125 | static int setup_compute_opt_wdiff(char *opt) |
126 | { | |
127 | char *w1_str = opt; | |
128 | char *w2_str; | |
129 | ||
130 | int ret = -EINVAL; | |
131 | ||
132 | if (!opt) | |
133 | goto out; | |
134 | ||
135 | w2_str = strchr(opt, ','); | |
136 | if (!w2_str) | |
137 | goto out; | |
138 | ||
139 | *w2_str++ = 0x0; | |
140 | if (!*w2_str) | |
141 | goto out; | |
142 | ||
143 | compute_wdiff_w1 = strtol(w1_str, NULL, 10); | |
144 | compute_wdiff_w2 = strtol(w2_str, NULL, 10); | |
145 | ||
146 | if (!compute_wdiff_w1 || !compute_wdiff_w2) | |
147 | goto out; | |
148 | ||
149 | pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n", | |
150 | compute_wdiff_w1, compute_wdiff_w2); | |
151 | ||
152 | ret = 0; | |
153 | ||
154 | out: | |
155 | if (ret) | |
156 | pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n"); | |
157 | ||
158 | return ret; | |
159 | } | |
160 | ||
161 | static int setup_compute_opt(char *opt) | |
162 | { | |
163 | if (compute == COMPUTE_WEIGHTED_DIFF) | |
164 | return setup_compute_opt_wdiff(opt); | |
165 | ||
166 | if (opt) { | |
167 | pr_err("Failed: extra option specified '%s'", opt); | |
168 | return -EINVAL; | |
169 | } | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
7aaf6b35 JO |
174 | static int setup_compute(const struct option *opt, const char *str, |
175 | int unset __maybe_unused) | |
176 | { | |
177 | int *cp = (int *) opt->value; | |
81d5f958 JO |
178 | char *cstr = (char *) str; |
179 | char buf[50]; | |
7aaf6b35 | 180 | unsigned i; |
81d5f958 | 181 | char *option; |
7aaf6b35 JO |
182 | |
183 | if (!str) { | |
184 | *cp = COMPUTE_DELTA; | |
185 | return 0; | |
186 | } | |
187 | ||
96c47f19 JO |
188 | if (*str == '+') { |
189 | sort_compute = true; | |
81d5f958 | 190 | cstr = (char *) ++str; |
96c47f19 JO |
191 | if (!*str) |
192 | return 0; | |
193 | } | |
194 | ||
81d5f958 JO |
195 | option = strchr(str, ':'); |
196 | if (option) { | |
197 | unsigned len = option++ - str; | |
198 | ||
199 | /* | |
200 | * The str data are not writeable, so we need | |
201 | * to use another buffer. | |
202 | */ | |
203 | ||
204 | /* No option value is longer. */ | |
205 | if (len >= sizeof(buf)) | |
206 | return -EINVAL; | |
207 | ||
208 | strncpy(buf, str, len); | |
209 | buf[len] = 0x0; | |
210 | cstr = buf; | |
211 | } | |
212 | ||
7aaf6b35 | 213 | for (i = 0; i < COMPUTE_MAX; i++) |
81d5f958 | 214 | if (!strcmp(cstr, compute_names[i])) { |
7aaf6b35 | 215 | *cp = i; |
81d5f958 | 216 | return setup_compute_opt(option); |
7aaf6b35 JO |
217 | } |
218 | ||
219 | pr_err("Failed: '%s' is not computation method " | |
81d5f958 | 220 | "(use 'delta','ratio' or 'wdiff')\n", str); |
7aaf6b35 JO |
221 | return -EINVAL; |
222 | } | |
223 | ||
05472daa | 224 | double perf_diff__period_percent(struct hist_entry *he, u64 period) |
96c47f19 JO |
225 | { |
226 | u64 total = he->hists->stats.total_period; | |
227 | return (period * 100.0) / total; | |
228 | } | |
229 | ||
05472daa | 230 | double perf_diff__compute_delta(struct hist_entry *he, struct hist_entry *pair) |
96c47f19 | 231 | { |
9af303e2 JO |
232 | double old_percent = perf_diff__period_percent(he, he->stat.period); |
233 | double new_percent = perf_diff__period_percent(pair, pair->stat.period); | |
96c47f19 | 234 | |
9af303e2 JO |
235 | pair->diff.period_ratio_delta = new_percent - old_percent; |
236 | pair->diff.computed = true; | |
237 | return pair->diff.period_ratio_delta; | |
96c47f19 JO |
238 | } |
239 | ||
05472daa | 240 | double perf_diff__compute_ratio(struct hist_entry *he, struct hist_entry *pair) |
96c47f19 | 241 | { |
9af303e2 JO |
242 | double old_period = he->stat.period ?: 1; |
243 | double new_period = pair->stat.period; | |
96c47f19 | 244 | |
9af303e2 JO |
245 | pair->diff.computed = true; |
246 | pair->diff.period_ratio = new_period / old_period; | |
247 | return pair->diff.period_ratio; | |
96c47f19 JO |
248 | } |
249 | ||
05472daa | 250 | s64 perf_diff__compute_wdiff(struct hist_entry *he, struct hist_entry *pair) |
81d5f958 | 251 | { |
9af303e2 JO |
252 | u64 old_period = he->stat.period; |
253 | u64 new_period = pair->stat.period; | |
81d5f958 | 254 | |
9af303e2 JO |
255 | pair->diff.computed = true; |
256 | pair->diff.wdiff = new_period * compute_wdiff_w2 - | |
257 | old_period * compute_wdiff_w1; | |
81d5f958 | 258 | |
9af303e2 | 259 | return pair->diff.wdiff; |
81d5f958 JO |
260 | } |
261 | ||
f4c8bae1 JO |
262 | static int formula_delta(struct hist_entry *he, struct hist_entry *pair, |
263 | char *buf, size_t size) | |
ed279da2 | 264 | { |
ed279da2 JO |
265 | return scnprintf(buf, size, |
266 | "(%" PRIu64 " * 100 / %" PRIu64 ") - " | |
267 | "(%" PRIu64 " * 100 / %" PRIu64 ")", | |
9af303e2 JO |
268 | pair->stat.period, pair->hists->stats.total_period, |
269 | he->stat.period, he->hists->stats.total_period); | |
ed279da2 JO |
270 | } |
271 | ||
f4c8bae1 JO |
272 | static int formula_ratio(struct hist_entry *he, struct hist_entry *pair, |
273 | char *buf, size_t size) | |
ed279da2 | 274 | { |
9af303e2 JO |
275 | double old_period = he->stat.period; |
276 | double new_period = pair->stat.period; | |
ed279da2 JO |
277 | |
278 | return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period); | |
279 | } | |
280 | ||
f4c8bae1 JO |
281 | static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair, |
282 | char *buf, size_t size) | |
ed279da2 | 283 | { |
9af303e2 JO |
284 | u64 old_period = he->stat.period; |
285 | u64 new_period = pair->stat.period; | |
ed279da2 JO |
286 | |
287 | return scnprintf(buf, size, | |
288 | "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")", | |
289 | new_period, compute_wdiff_w2, old_period, compute_wdiff_w1); | |
290 | } | |
291 | ||
f4c8bae1 JO |
292 | int perf_diff__formula(struct hist_entry *he, struct hist_entry *pair, |
293 | char *buf, size_t size) | |
ed279da2 JO |
294 | { |
295 | switch (compute) { | |
296 | case COMPUTE_DELTA: | |
f4c8bae1 | 297 | return formula_delta(he, pair, buf, size); |
ed279da2 | 298 | case COMPUTE_RATIO: |
f4c8bae1 | 299 | return formula_ratio(he, pair, buf, size); |
ed279da2 | 300 | case COMPUTE_WEIGHTED_DIFF: |
f4c8bae1 | 301 | return formula_wdiff(he, pair, buf, size); |
ed279da2 JO |
302 | default: |
303 | BUG_ON(1); | |
304 | } | |
305 | ||
306 | return -1; | |
307 | } | |
308 | ||
1c02c4d2 | 309 | static int hists__add_entry(struct hists *self, |
05484298 AK |
310 | struct addr_location *al, u64 period, |
311 | u64 weight) | |
86a9eee0 | 312 | { |
05484298 | 313 | if (__hists__add_entry(self, al, NULL, period, weight) != NULL) |
28e2a106 ACM |
314 | return 0; |
315 | return -ENOMEM; | |
86a9eee0 ACM |
316 | } |
317 | ||
1d037ca1 | 318 | static int diff__process_sample_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 319 | union perf_event *event, |
8d50e5b4 | 320 | struct perf_sample *sample, |
863e451f | 321 | struct perf_evsel *evsel, |
743eb868 | 322 | struct machine *machine) |
86a9eee0 ACM |
323 | { |
324 | struct addr_location al; | |
86a9eee0 | 325 | |
743eb868 | 326 | if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) { |
86a9eee0 ACM |
327 | pr_warning("problem processing %d event, skipping it.\n", |
328 | event->header.type); | |
329 | return -1; | |
330 | } | |
331 | ||
d88c48f9 | 332 | if (al.filtered) |
c410a338 ACM |
333 | return 0; |
334 | ||
05484298 | 335 | if (hists__add_entry(&evsel->hists, &al, sample->period, sample->weight)) { |
c82ee828 | 336 | pr_warning("problem incrementing symbol period, skipping event\n"); |
86a9eee0 ACM |
337 | return -1; |
338 | } | |
339 | ||
863e451f | 340 | evsel->hists.stats.total_period += sample->period; |
86a9eee0 ACM |
341 | return 0; |
342 | } | |
343 | ||
863e451f JO |
344 | static struct perf_tool tool = { |
345 | .sample = diff__process_sample_event, | |
346 | .mmap = perf_event__process_mmap, | |
347 | .comm = perf_event__process_comm, | |
f62d3f0f ACM |
348 | .exit = perf_event__process_exit, |
349 | .fork = perf_event__process_fork, | |
863e451f JO |
350 | .lost = perf_event__process_lost, |
351 | .ordered_samples = true, | |
352 | .ordering_requires_timestamps = true, | |
86a9eee0 ACM |
353 | }; |
354 | ||
863e451f JO |
355 | static struct perf_evsel *evsel_match(struct perf_evsel *evsel, |
356 | struct perf_evlist *evlist) | |
357 | { | |
358 | struct perf_evsel *e; | |
359 | ||
360 | list_for_each_entry(e, &evlist->entries, node) | |
361 | if (perf_evsel__match2(evsel, e)) | |
362 | return e; | |
363 | ||
364 | return NULL; | |
365 | } | |
366 | ||
ce74f60e | 367 | static void perf_evlist__collapse_resort(struct perf_evlist *evlist) |
dd464345 JO |
368 | { |
369 | struct perf_evsel *evsel; | |
370 | ||
371 | list_for_each_entry(evsel, &evlist->entries, node) { | |
372 | struct hists *hists = &evsel->hists; | |
373 | ||
ce74f60e | 374 | hists__collapse_resort(hists); |
dd464345 JO |
375 | } |
376 | } | |
377 | ||
a06d143e JO |
378 | static void hists__baseline_only(struct hists *hists) |
379 | { | |
ce74f60e NK |
380 | struct rb_root *root; |
381 | struct rb_node *next; | |
382 | ||
383 | if (sort__need_collapse) | |
384 | root = &hists->entries_collapsed; | |
385 | else | |
386 | root = hists->entries_in; | |
a06d143e | 387 | |
ce74f60e | 388 | next = rb_first(root); |
a06d143e | 389 | while (next != NULL) { |
ce74f60e | 390 | struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in); |
a06d143e | 391 | |
ce74f60e | 392 | next = rb_next(&he->rb_node_in); |
b821c732 | 393 | if (!hist_entry__next_pair(he)) { |
ce74f60e | 394 | rb_erase(&he->rb_node_in, root); |
a06d143e JO |
395 | hist_entry__free(he); |
396 | } | |
397 | } | |
398 | } | |
399 | ||
96c47f19 JO |
400 | static void hists__precompute(struct hists *hists) |
401 | { | |
367c53c0 JO |
402 | struct rb_root *root; |
403 | struct rb_node *next; | |
404 | ||
405 | if (sort__need_collapse) | |
406 | root = &hists->entries_collapsed; | |
407 | else | |
408 | root = hists->entries_in; | |
96c47f19 | 409 | |
367c53c0 | 410 | next = rb_first(root); |
96c47f19 | 411 | while (next != NULL) { |
367c53c0 | 412 | struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in); |
05472daa | 413 | struct hist_entry *pair = hist_entry__next_pair(he); |
96c47f19 | 414 | |
367c53c0 | 415 | next = rb_next(&he->rb_node_in); |
05472daa JO |
416 | if (!pair) |
417 | continue; | |
96c47f19 JO |
418 | |
419 | switch (compute) { | |
420 | case COMPUTE_DELTA: | |
05472daa | 421 | perf_diff__compute_delta(he, pair); |
96c47f19 JO |
422 | break; |
423 | case COMPUTE_RATIO: | |
05472daa | 424 | perf_diff__compute_ratio(he, pair); |
96c47f19 | 425 | break; |
81d5f958 | 426 | case COMPUTE_WEIGHTED_DIFF: |
05472daa | 427 | perf_diff__compute_wdiff(he, pair); |
81d5f958 | 428 | break; |
96c47f19 JO |
429 | default: |
430 | BUG_ON(1); | |
431 | } | |
432 | } | |
433 | } | |
434 | ||
435 | static int64_t cmp_doubles(double l, double r) | |
436 | { | |
437 | if (l > r) | |
438 | return -1; | |
439 | else if (l < r) | |
440 | return 1; | |
441 | else | |
442 | return 0; | |
443 | } | |
444 | ||
445 | static int64_t | |
446 | hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right, | |
447 | int c) | |
448 | { | |
449 | switch (c) { | |
450 | case COMPUTE_DELTA: | |
451 | { | |
452 | double l = left->diff.period_ratio_delta; | |
453 | double r = right->diff.period_ratio_delta; | |
454 | ||
455 | return cmp_doubles(l, r); | |
456 | } | |
457 | case COMPUTE_RATIO: | |
458 | { | |
459 | double l = left->diff.period_ratio; | |
460 | double r = right->diff.period_ratio; | |
461 | ||
462 | return cmp_doubles(l, r); | |
463 | } | |
81d5f958 JO |
464 | case COMPUTE_WEIGHTED_DIFF: |
465 | { | |
466 | s64 l = left->diff.wdiff; | |
467 | s64 r = right->diff.wdiff; | |
468 | ||
469 | return r - l; | |
470 | } | |
96c47f19 JO |
471 | default: |
472 | BUG_ON(1); | |
473 | } | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | static void insert_hist_entry_by_compute(struct rb_root *root, | |
479 | struct hist_entry *he, | |
480 | int c) | |
481 | { | |
482 | struct rb_node **p = &root->rb_node; | |
483 | struct rb_node *parent = NULL; | |
484 | struct hist_entry *iter; | |
485 | ||
486 | while (*p != NULL) { | |
487 | parent = *p; | |
488 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
489 | if (hist_entry__cmp_compute(he, iter, c) < 0) | |
490 | p = &(*p)->rb_left; | |
491 | else | |
492 | p = &(*p)->rb_right; | |
493 | } | |
494 | ||
495 | rb_link_node(&he->rb_node, parent, p); | |
496 | rb_insert_color(&he->rb_node, root); | |
497 | } | |
498 | ||
499 | static void hists__compute_resort(struct hists *hists) | |
500 | { | |
66f97ed3 NK |
501 | struct rb_root *root; |
502 | struct rb_node *next; | |
503 | ||
504 | if (sort__need_collapse) | |
505 | root = &hists->entries_collapsed; | |
506 | else | |
507 | root = hists->entries_in; | |
508 | ||
509 | hists->entries = RB_ROOT; | |
510 | next = rb_first(root); | |
511 | ||
512 | hists->nr_entries = 0; | |
513 | hists->stats.total_period = 0; | |
514 | hists__reset_col_len(hists); | |
96c47f19 JO |
515 | |
516 | while (next != NULL) { | |
66f97ed3 | 517 | struct hist_entry *he; |
96c47f19 | 518 | |
66f97ed3 NK |
519 | he = rb_entry(next, struct hist_entry, rb_node_in); |
520 | next = rb_next(&he->rb_node_in); | |
96c47f19 | 521 | |
66f97ed3 NK |
522 | insert_hist_entry_by_compute(&hists->entries, he, compute); |
523 | hists__inc_nr_entries(hists, he); | |
96c47f19 | 524 | } |
96c47f19 JO |
525 | } |
526 | ||
9af303e2 | 527 | static void hists__process(struct hists *base, struct hists *new) |
a06d143e | 528 | { |
9af303e2 | 529 | hists__match(base, new); |
a06d143e JO |
530 | |
531 | if (show_baseline_only) | |
9af303e2 | 532 | hists__baseline_only(base); |
bfaef4b4 | 533 | else |
9af303e2 | 534 | hists__link(base, new); |
a06d143e | 535 | |
96c47f19 | 536 | if (sort_compute) { |
9af303e2 JO |
537 | hists__precompute(base); |
538 | hists__compute_resort(base); | |
66f97ed3 | 539 | } else { |
9af303e2 | 540 | hists__output_resort(base); |
96c47f19 JO |
541 | } |
542 | ||
9af303e2 | 543 | hists__fprintf(base, true, 0, 0, 0, stdout); |
a06d143e JO |
544 | } |
545 | ||
1d81c7fc JO |
546 | static void data__fprintf(void) |
547 | { | |
548 | struct data__file *d; | |
549 | int i; | |
550 | ||
551 | fprintf(stdout, "# Data files:\n"); | |
552 | ||
553 | data__for_each_file(i, d) | |
554 | fprintf(stdout, "# [%d] %s %s\n", | |
555 | d->idx, d->file, | |
556 | !d->idx ? "(Baseline)" : ""); | |
557 | ||
558 | fprintf(stdout, "#\n"); | |
559 | } | |
560 | ||
ec308426 | 561 | static void data_process(void) |
86a9eee0 | 562 | { |
ec308426 JO |
563 | struct perf_evlist *evlist_old = data__files[0].session->evlist; |
564 | struct perf_evlist *evlist_new = data__files[1].session->evlist; | |
565 | struct perf_evsel *evsel_old; | |
863e451f | 566 | bool first = true; |
86a9eee0 | 567 | |
ec308426 JO |
568 | list_for_each_entry(evsel_old, &evlist_old->entries, node) { |
569 | struct perf_evsel *evsel_new; | |
86a9eee0 | 570 | |
ec308426 JO |
571 | evsel_new = evsel_match(evsel_old, evlist_new); |
572 | if (!evsel_new) | |
573 | continue; | |
86a9eee0 | 574 | |
ec308426 JO |
575 | fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n", |
576 | perf_evsel__name(evsel_old)); | |
863e451f | 577 | |
ec308426 | 578 | first = false; |
863e451f | 579 | |
1d81c7fc JO |
580 | if (verbose) |
581 | data__fprintf(); | |
582 | ||
ec308426 JO |
583 | hists__process(&evsel_old->hists, &evsel_new->hists); |
584 | } | |
585 | } | |
863e451f | 586 | |
ec308426 JO |
587 | static int __cmd_diff(void) |
588 | { | |
589 | struct data__file *d; | |
590 | int ret = -EINVAL, i; | |
591 | ||
592 | data__for_each_file(i, d) { | |
593 | d->session = perf_session__new(d->file, O_RDONLY, force, | |
594 | false, &tool); | |
595 | if (!d->session) { | |
596 | pr_err("Failed to open %s\n", d->file); | |
597 | ret = -ENOMEM; | |
598 | goto out_delete; | |
599 | } | |
863e451f | 600 | |
ec308426 JO |
601 | ret = perf_session__process_events(d->session, &tool); |
602 | if (ret) { | |
603 | pr_err("Failed to process %s\n", d->file); | |
604 | goto out_delete; | |
605 | } | |
863e451f | 606 | |
ec308426 JO |
607 | perf_evlist__collapse_resort(d->session->evlist); |
608 | } | |
609 | ||
610 | data_process(); | |
863e451f | 611 | |
ec308426 JO |
612 | out_delete: |
613 | data__for_each_file(i, d) { | |
614 | if (d->session) | |
615 | perf_session__delete(d->session); | |
863e451f | 616 | } |
9c443dfd | 617 | |
ec308426 | 618 | free(data__files); |
86a9eee0 ACM |
619 | return ret; |
620 | } | |
621 | ||
0422a4fc | 622 | static const char * const diff_usage[] = { |
86a9eee0 | 623 | "perf diff [<options>] [old_file] [new_file]", |
0422a4fc | 624 | NULL, |
86a9eee0 ACM |
625 | }; |
626 | ||
627 | static const struct option options[] = { | |
c0555642 | 628 | OPT_INCR('v', "verbose", &verbose, |
86a9eee0 | 629 | "be more verbose (show symbol address, etc)"), |
a06d143e JO |
630 | OPT_BOOLEAN('b', "baseline-only", &show_baseline_only, |
631 | "Show only items with match in baseline"), | |
81d5f958 JO |
632 | OPT_CALLBACK('c', "compute", &compute, |
633 | "delta,ratio,wdiff:w1,w2 (default delta)", | |
7aaf6b35 JO |
634 | "Entries differential computation selection", |
635 | setup_compute), | |
61949b21 JO |
636 | OPT_BOOLEAN('p', "period", &show_period, |
637 | "Show period values."), | |
ed279da2 JO |
638 | OPT_BOOLEAN('F', "formula", &show_formula, |
639 | "Show formula."), | |
86a9eee0 ACM |
640 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
641 | "dump raw trace in ASCII"), | |
642 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), | |
643 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, | |
644 | "load module symbols - WARNING: use only with -k and LIVE kernel"), | |
c410a338 ACM |
645 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
646 | "only consider symbols in these dsos"), | |
647 | OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", | |
648 | "only consider symbols in these comms"), | |
649 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", | |
650 | "only consider these symbols"), | |
c351c281 ACM |
651 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
652 | "sort by key(s): pid, comm, dso, symbol, parent"), | |
653 | OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", | |
654 | "separator for columns, no spaces will be added between " | |
655 | "columns '.' is reserved."), | |
ec5761ea DA |
656 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
657 | "Look for files with symbols relative to this directory"), | |
86a9eee0 ACM |
658 | OPT_END() |
659 | }; | |
660 | ||
345dc0b4 | 661 | static double baseline_percent(struct hist_entry *he) |
1d77822e | 662 | { |
345dc0b4 JO |
663 | struct hists *hists = he->hists; |
664 | return 100.0 * he->stat.period / hists->stats.total_period; | |
665 | } | |
7aaf6b35 | 666 | |
345dc0b4 JO |
667 | static int hpp__color_baseline(struct perf_hpp_fmt *fmt, |
668 | struct perf_hpp *hpp, struct hist_entry *he) | |
669 | { | |
670 | struct diff_hpp_fmt *dfmt = | |
671 | container_of(fmt, struct diff_hpp_fmt, fmt); | |
672 | double percent = baseline_percent(he); | |
673 | char pfmt[20] = " "; | |
674 | ||
675 | if (!he->dummy) { | |
676 | scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1); | |
677 | return percent_color_snprintf(hpp->buf, hpp->size, | |
678 | pfmt, percent); | |
679 | } else | |
680 | return scnprintf(hpp->buf, hpp->size, "%*s", | |
681 | dfmt->header_width, pfmt); | |
682 | } | |
683 | ||
684 | static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size) | |
685 | { | |
686 | double percent = baseline_percent(he); | |
687 | const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%"; | |
688 | int ret = 0; | |
689 | ||
690 | if (!he->dummy) | |
691 | ret = scnprintf(buf, size, fmt, percent); | |
692 | ||
693 | return ret; | |
694 | } | |
695 | ||
696 | static void | |
697 | hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size) | |
698 | { | |
699 | switch (idx) { | |
700 | case PERF_HPP_DIFF__PERIOD_BASELINE: | |
701 | scnprintf(buf, size, "%" PRIu64, he->stat.period); | |
7aaf6b35 | 702 | break; |
345dc0b4 JO |
703 | |
704 | default: | |
81d5f958 | 705 | break; |
345dc0b4 JO |
706 | } |
707 | } | |
708 | ||
709 | static void | |
710 | hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair, | |
711 | int idx, char *buf, size_t size) | |
712 | { | |
713 | double diff; | |
714 | double ratio; | |
715 | s64 wdiff; | |
716 | ||
717 | switch (idx) { | |
718 | case PERF_HPP_DIFF__DELTA: | |
719 | if (pair->diff.computed) | |
720 | diff = pair->diff.period_ratio_delta; | |
721 | else | |
722 | diff = perf_diff__compute_delta(he, pair); | |
723 | ||
724 | if (fabs(diff) >= 0.01) | |
725 | scnprintf(buf, size, "%+4.2F%%", diff); | |
726 | break; | |
727 | ||
728 | case PERF_HPP_DIFF__RATIO: | |
729 | /* No point for ratio number if we are dummy.. */ | |
730 | if (he->dummy) | |
731 | break; | |
732 | ||
733 | if (pair->diff.computed) | |
734 | ratio = pair->diff.period_ratio; | |
735 | else | |
736 | ratio = perf_diff__compute_ratio(he, pair); | |
737 | ||
738 | if (ratio > 0.0) | |
739 | scnprintf(buf, size, "%14.6F", ratio); | |
740 | break; | |
741 | ||
742 | case PERF_HPP_DIFF__WEIGHTED_DIFF: | |
743 | /* No point for wdiff number if we are dummy.. */ | |
744 | if (he->dummy) | |
745 | break; | |
746 | ||
747 | if (pair->diff.computed) | |
748 | wdiff = pair->diff.wdiff; | |
749 | else | |
750 | wdiff = perf_diff__compute_wdiff(he, pair); | |
751 | ||
752 | if (wdiff != 0) | |
753 | scnprintf(buf, size, "%14ld", wdiff); | |
754 | break; | |
755 | ||
756 | case PERF_HPP_DIFF__FORMULA: | |
757 | perf_diff__formula(he, pair, buf, size); | |
7aaf6b35 | 758 | break; |
345dc0b4 JO |
759 | |
760 | case PERF_HPP_DIFF__PERIOD: | |
761 | scnprintf(buf, size, "%" PRIu64, pair->stat.period); | |
762 | break; | |
763 | ||
7aaf6b35 JO |
764 | default: |
765 | BUG_ON(1); | |
766 | }; | |
345dc0b4 JO |
767 | } |
768 | ||
769 | static void | |
770 | __hpp__entry_global(struct hist_entry *he, int idx, char *buf, size_t size) | |
771 | { | |
772 | struct hist_entry *pair = hist_entry__next_pair(he); | |
773 | ||
774 | /* baseline is special */ | |
775 | if (idx == PERF_HPP_DIFF__BASELINE) | |
776 | hpp__entry_baseline(he, buf, size); | |
777 | else { | |
778 | if (pair) | |
779 | hpp__entry_pair(he, pair, idx, buf, size); | |
780 | else | |
781 | hpp__entry_unpair(he, idx, buf, size); | |
782 | } | |
783 | } | |
784 | ||
785 | static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp, | |
786 | struct hist_entry *he) | |
787 | { | |
788 | struct diff_hpp_fmt *dfmt = | |
789 | container_of(_fmt, struct diff_hpp_fmt, fmt); | |
790 | char buf[MAX_COL_WIDTH] = " "; | |
791 | ||
792 | __hpp__entry_global(he, dfmt->idx, buf, MAX_COL_WIDTH); | |
793 | ||
794 | if (symbol_conf.field_sep) | |
795 | return scnprintf(hpp->buf, hpp->size, "%s", buf); | |
796 | else | |
797 | return scnprintf(hpp->buf, hpp->size, "%*s", | |
798 | dfmt->header_width, buf); | |
799 | } | |
800 | ||
801 | static int hpp__header(struct perf_hpp_fmt *fmt, | |
802 | struct perf_hpp *hpp) | |
803 | { | |
804 | struct diff_hpp_fmt *dfmt = | |
805 | container_of(fmt, struct diff_hpp_fmt, fmt); | |
806 | ||
807 | BUG_ON(!dfmt->header); | |
808 | return scnprintf(hpp->buf, hpp->size, dfmt->header); | |
809 | } | |
810 | ||
811 | static int hpp__width(struct perf_hpp_fmt *fmt, | |
812 | struct perf_hpp *hpp __maybe_unused) | |
813 | { | |
814 | struct diff_hpp_fmt *dfmt = | |
815 | container_of(fmt, struct diff_hpp_fmt, fmt); | |
816 | ||
817 | BUG_ON(dfmt->header_width <= 0); | |
818 | return dfmt->header_width; | |
819 | } | |
820 | ||
821 | #define hpp__color_global hpp__entry_global | |
822 | ||
823 | #define FMT(_i, _entry, _color) \ | |
824 | [_i] = { \ | |
825 | .fmt = { \ | |
826 | .header = hpp__header, \ | |
827 | .width = hpp__width, \ | |
828 | .entry = hpp__entry_ ## _entry, \ | |
829 | .color = hpp__color_ ## _color, \ | |
830 | }, \ | |
831 | .idx = _i, \ | |
832 | } | |
833 | ||
834 | #define FMT_GLOBAL(_i) FMT(_i, global, global) | |
835 | #define FMT_BASELINE(_i) FMT(_i, global, baseline) | |
836 | ||
837 | static struct diff_hpp_fmt diff_fmt[] = { | |
838 | FMT_BASELINE(PERF_HPP_DIFF__BASELINE), | |
839 | FMT_GLOBAL(PERF_HPP_DIFF__PERIOD), | |
840 | FMT_GLOBAL(PERF_HPP_DIFF__PERIOD_BASELINE), | |
841 | FMT_GLOBAL(PERF_HPP_DIFF__DELTA), | |
842 | FMT_GLOBAL(PERF_HPP_DIFF__RATIO), | |
843 | FMT_GLOBAL(PERF_HPP_DIFF__WEIGHTED_DIFF), | |
844 | FMT_GLOBAL(PERF_HPP_DIFF__FORMULA), | |
845 | }; | |
846 | ||
847 | static void init_header(struct diff_hpp_fmt *dfmt) | |
848 | { | |
849 | #define MAX_HEADER_NAME 100 | |
850 | char buf_indent[MAX_HEADER_NAME]; | |
851 | char buf[MAX_HEADER_NAME]; | |
852 | const char *header = NULL; | |
853 | int width = 0; | |
854 | ||
855 | BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX); | |
856 | header = columns[dfmt->idx].name; | |
857 | width = columns[dfmt->idx].width; | |
858 | ||
859 | /* Only our defined HPP fmts should appear here. */ | |
860 | BUG_ON(!header); | |
861 | ||
862 | #define NAME (data__files_cnt > 2 ? buf : header) | |
863 | dfmt->header_width = width; | |
864 | width = (int) strlen(NAME); | |
865 | if (dfmt->header_width < width) | |
866 | dfmt->header_width = width; | |
867 | ||
868 | scnprintf(buf_indent, MAX_HEADER_NAME, "%*s", | |
869 | dfmt->header_width, NAME); | |
870 | ||
871 | dfmt->header = strdup(buf_indent); | |
872 | #undef MAX_HEADER_NAME | |
873 | #undef NAME | |
874 | } | |
875 | ||
876 | static void column_enable(unsigned col) | |
877 | { | |
878 | struct diff_hpp_fmt *dfmt; | |
879 | ||
880 | BUG_ON(col >= PERF_HPP_DIFF__MAX_INDEX); | |
881 | dfmt = &diff_fmt[col]; | |
882 | init_header(dfmt); | |
883 | perf_hpp__column_register(&dfmt->fmt); | |
884 | } | |
885 | ||
886 | static void ui_init(void) | |
887 | { | |
888 | /* | |
889 | * Display baseline/delta/ratio/ | |
890 | * formula/periods columns. | |
891 | */ | |
892 | column_enable(PERF_HPP_DIFF__BASELINE); | |
893 | column_enable(compute_2_hpp[compute]); | |
1d77822e | 894 | |
ed279da2 | 895 | if (show_formula) |
345dc0b4 | 896 | column_enable(PERF_HPP_DIFF__FORMULA); |
ed279da2 | 897 | |
61949b21 | 898 | if (show_period) { |
345dc0b4 JO |
899 | column_enable(PERF_HPP_DIFF__PERIOD); |
900 | column_enable(PERF_HPP_DIFF__PERIOD_BASELINE); | |
61949b21 | 901 | } |
1d77822e JO |
902 | } |
903 | ||
ec308426 | 904 | static int data_init(int argc, const char **argv) |
86a9eee0 | 905 | { |
ec308426 JO |
906 | struct data__file *d; |
907 | static const char *defaults[] = { | |
908 | "perf.data.old", | |
909 | "perf.data", | |
910 | }; | |
911 | int i; | |
912 | ||
913 | data__files_cnt = 2; | |
914 | ||
86a9eee0 ACM |
915 | if (argc) { |
916 | if (argc > 2) | |
917 | usage_with_options(diff_usage, options); | |
918 | if (argc == 2) { | |
ec308426 JO |
919 | defaults[0] = argv[0]; |
920 | defaults[1] = argv[1]; | |
86a9eee0 | 921 | } else |
ec308426 | 922 | defaults[1] = argv[0]; |
a1645ce1 ZY |
923 | } else if (symbol_conf.default_guest_vmlinux_name || |
924 | symbol_conf.default_guest_kallsyms) { | |
ec308426 JO |
925 | defaults[0] = "perf.data.host"; |
926 | defaults[1] = "perf.data.guest"; | |
86a9eee0 ACM |
927 | } |
928 | ||
ec308426 JO |
929 | data__files = zalloc(sizeof(*data__files) * data__files_cnt); |
930 | if (!data__files) | |
931 | return -ENOMEM; | |
932 | ||
933 | data__for_each_file(i, d) { | |
934 | d->file = defaults[i]; | |
935 | d->idx = i; | |
936 | } | |
937 | ||
938 | return 0; | |
939 | } | |
940 | ||
941 | int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused) | |
942 | { | |
943 | sort_order = diff__default_sort_order; | |
944 | argc = parse_options(argc, argv, options, diff_usage, 0); | |
945 | ||
655000e7 ACM |
946 | if (symbol__init() < 0) |
947 | return -1; | |
948 | ||
ec308426 JO |
949 | if (data_init(argc, argv) < 0) |
950 | return -1; | |
951 | ||
1d77822e JO |
952 | ui_init(); |
953 | ||
55309985 NK |
954 | if (setup_sorting() < 0) |
955 | usage_with_options(diff_usage, options); | |
956 | ||
86a9eee0 | 957 | setup_pager(); |
c351c281 | 958 | |
08e71542 | 959 | sort__setup_elide(NULL); |
c351c281 | 960 | |
86a9eee0 ACM |
961 | return __cmd_diff(); |
962 | } |