Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
86a9eee0 ACM |
2 | /* |
3 | * builtin-diff.c | |
4 | * | |
5 | * Builtin diff command: Analyze two perf.data input files, look up and read | |
6 | * DSOs and symbol information, sort them and produce a diff. | |
7 | */ | |
8 | #include "builtin.h" | |
02b5ed8a | 9 | #include "perf.h" |
86a9eee0 ACM |
10 | |
11 | #include "util/debug.h" | |
12 | #include "util/event.h" | |
13 | #include "util/hist.h" | |
743eb868 | 14 | #include "util/evsel.h" |
863e451f | 15 | #include "util/evlist.h" |
86a9eee0 | 16 | #include "util/session.h" |
45694aa7 | 17 | #include "util/tool.h" |
86a9eee0 | 18 | #include "util/sort.h" |
97b9d866 | 19 | #include "util/srcline.h" |
86a9eee0 | 20 | #include "util/symbol.h" |
f5fc1412 | 21 | #include "util/data.h" |
d49dd15d | 22 | #include "util/config.h" |
4802138d | 23 | #include "util/time-utils.h" |
99150a1f | 24 | #include "util/annotate.h" |
b10c78c5 | 25 | #include "util/map.h" |
cebf7d51 | 26 | #include "util/spark.h" |
60414418 | 27 | #include "util/block-info.h" |
2a09a84c | 28 | #include "util/stream.h" |
ea0c5239 | 29 | #include "util/util.h" |
6ef81c55 | 30 | #include <linux/err.h> |
7f7c536f | 31 | #include <linux/zalloc.h> |
8520a98d | 32 | #include <subcmd/pager.h> |
97b9d866 | 33 | #include <subcmd/parse-options.h> |
86a9eee0 | 34 | |
a43783ae | 35 | #include <errno.h> |
fd20e811 | 36 | #include <inttypes.h> |
86a9eee0 | 37 | #include <stdlib.h> |
345dc0b4 JO |
38 | #include <math.h> |
39 | ||
4802138d JY |
40 | struct perf_diff { |
41 | struct perf_tool tool; | |
42 | const char *time_str; | |
43 | struct perf_time_interval *ptime_range; | |
44 | int range_size; | |
45 | int range_num; | |
30d81553 | 46 | bool has_br_stack; |
2a09a84c | 47 | bool stream; |
4802138d JY |
48 | }; |
49 | ||
345dc0b4 JO |
50 | /* Diff command specific HPP columns. */ |
51 | enum { | |
52 | PERF_HPP_DIFF__BASELINE, | |
53 | PERF_HPP_DIFF__PERIOD, | |
54 | PERF_HPP_DIFF__PERIOD_BASELINE, | |
55 | PERF_HPP_DIFF__DELTA, | |
56 | PERF_HPP_DIFF__RATIO, | |
57 | PERF_HPP_DIFF__WEIGHTED_DIFF, | |
58 | PERF_HPP_DIFF__FORMULA, | |
a1668c25 | 59 | PERF_HPP_DIFF__DELTA_ABS, |
b10c78c5 | 60 | PERF_HPP_DIFF__CYCLES, |
cebf7d51 | 61 | PERF_HPP_DIFF__CYCLES_HIST, |
345dc0b4 JO |
62 | |
63 | PERF_HPP_DIFF__MAX_INDEX | |
64 | }; | |
65 | ||
66 | struct diff_hpp_fmt { | |
67 | struct perf_hpp_fmt fmt; | |
68 | int idx; | |
69 | char *header; | |
70 | int header_width; | |
71 | }; | |
86a9eee0 | 72 | |
ec308426 JO |
73 | struct data__file { |
74 | struct perf_session *session; | |
8ceb41d7 | 75 | struct perf_data data; |
ec308426 | 76 | int idx; |
22aeb7f5 | 77 | struct hists *hists; |
2a09a84c | 78 | struct evlist_streams *evlist_streams; |
c818b498 | 79 | struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX]; |
ec308426 JO |
80 | }; |
81 | ||
82 | static struct data__file *data__files; | |
83 | static int data__files_cnt; | |
84 | ||
85 | #define data__for_each_file_start(i, d, s) \ | |
86 | for (i = s, d = &data__files[s]; \ | |
87 | i < data__files_cnt; \ | |
88 | i++, d = &data__files[i]) | |
89 | ||
90 | #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0) | |
22aeb7f5 | 91 | #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1) |
ec308426 | 92 | |
ec308426 | 93 | static bool force; |
61949b21 | 94 | static bool show_period; |
ed279da2 | 95 | static bool show_formula; |
a06d143e | 96 | static bool show_baseline_only; |
cebf7d51 | 97 | static bool cycles_hist; |
be57b3fd | 98 | static unsigned int sort_compute = 1; |
86a9eee0 | 99 | |
81d5f958 JO |
100 | static s64 compute_wdiff_w1; |
101 | static s64 compute_wdiff_w2; | |
102 | ||
daca23b2 JY |
103 | static const char *cpu_list; |
104 | static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); | |
105 | ||
7aaf6b35 JO |
106 | enum { |
107 | COMPUTE_DELTA, | |
108 | COMPUTE_RATIO, | |
81d5f958 | 109 | COMPUTE_WEIGHTED_DIFF, |
a1668c25 | 110 | COMPUTE_DELTA_ABS, |
99150a1f | 111 | COMPUTE_CYCLES, |
7aaf6b35 | 112 | COMPUTE_MAX, |
2a09a84c | 113 | COMPUTE_STREAM, /* After COMPUTE_MAX to avoid use current compute arrays */ |
7aaf6b35 JO |
114 | }; |
115 | ||
116 | const char *compute_names[COMPUTE_MAX] = { | |
117 | [COMPUTE_DELTA] = "delta", | |
a1668c25 | 118 | [COMPUTE_DELTA_ABS] = "delta-abs", |
7aaf6b35 | 119 | [COMPUTE_RATIO] = "ratio", |
81d5f958 | 120 | [COMPUTE_WEIGHTED_DIFF] = "wdiff", |
99150a1f | 121 | [COMPUTE_CYCLES] = "cycles", |
7aaf6b35 JO |
122 | }; |
123 | ||
be57b3fd | 124 | static int compute = COMPUTE_DELTA_ABS; |
7aaf6b35 | 125 | |
345dc0b4 JO |
126 | static int compute_2_hpp[COMPUTE_MAX] = { |
127 | [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA, | |
a1668c25 | 128 | [COMPUTE_DELTA_ABS] = PERF_HPP_DIFF__DELTA_ABS, |
345dc0b4 JO |
129 | [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO, |
130 | [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF, | |
b10c78c5 | 131 | [COMPUTE_CYCLES] = PERF_HPP_DIFF__CYCLES, |
345dc0b4 JO |
132 | }; |
133 | ||
134 | #define MAX_COL_WIDTH 70 | |
135 | ||
136 | static struct header_column { | |
137 | const char *name; | |
138 | int width; | |
139 | } columns[PERF_HPP_DIFF__MAX_INDEX] = { | |
140 | [PERF_HPP_DIFF__BASELINE] = { | |
141 | .name = "Baseline", | |
142 | }, | |
143 | [PERF_HPP_DIFF__PERIOD] = { | |
144 | .name = "Period", | |
145 | .width = 14, | |
146 | }, | |
147 | [PERF_HPP_DIFF__PERIOD_BASELINE] = { | |
148 | .name = "Base period", | |
149 | .width = 14, | |
150 | }, | |
151 | [PERF_HPP_DIFF__DELTA] = { | |
152 | .name = "Delta", | |
153 | .width = 7, | |
154 | }, | |
a1668c25 NK |
155 | [PERF_HPP_DIFF__DELTA_ABS] = { |
156 | .name = "Delta Abs", | |
157 | .width = 7, | |
158 | }, | |
345dc0b4 JO |
159 | [PERF_HPP_DIFF__RATIO] = { |
160 | .name = "Ratio", | |
161 | .width = 14, | |
162 | }, | |
163 | [PERF_HPP_DIFF__WEIGHTED_DIFF] = { | |
164 | .name = "Weighted diff", | |
165 | .width = 14, | |
166 | }, | |
167 | [PERF_HPP_DIFF__FORMULA] = { | |
168 | .name = "Formula", | |
169 | .width = MAX_COL_WIDTH, | |
b10c78c5 JY |
170 | }, |
171 | [PERF_HPP_DIFF__CYCLES] = { | |
172 | .name = "[Program Block Range] Cycles Diff", | |
173 | .width = 70, | |
cebf7d51 JY |
174 | }, |
175 | [PERF_HPP_DIFF__CYCLES_HIST] = { | |
176 | .name = "stddev/Hist", | |
177 | .width = NUM_SPARKS + 9, | |
345dc0b4 JO |
178 | } |
179 | }; | |
180 | ||
81d5f958 JO |
181 | static int setup_compute_opt_wdiff(char *opt) |
182 | { | |
183 | char *w1_str = opt; | |
184 | char *w2_str; | |
185 | ||
186 | int ret = -EINVAL; | |
187 | ||
188 | if (!opt) | |
189 | goto out; | |
190 | ||
191 | w2_str = strchr(opt, ','); | |
192 | if (!w2_str) | |
193 | goto out; | |
194 | ||
195 | *w2_str++ = 0x0; | |
196 | if (!*w2_str) | |
197 | goto out; | |
198 | ||
199 | compute_wdiff_w1 = strtol(w1_str, NULL, 10); | |
200 | compute_wdiff_w2 = strtol(w2_str, NULL, 10); | |
201 | ||
202 | if (!compute_wdiff_w1 || !compute_wdiff_w2) | |
203 | goto out; | |
204 | ||
205 | pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n", | |
206 | compute_wdiff_w1, compute_wdiff_w2); | |
207 | ||
208 | ret = 0; | |
209 | ||
210 | out: | |
211 | if (ret) | |
212 | pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n"); | |
213 | ||
214 | return ret; | |
215 | } | |
216 | ||
217 | static int setup_compute_opt(char *opt) | |
218 | { | |
219 | if (compute == COMPUTE_WEIGHTED_DIFF) | |
220 | return setup_compute_opt_wdiff(opt); | |
221 | ||
222 | if (opt) { | |
223 | pr_err("Failed: extra option specified '%s'", opt); | |
224 | return -EINVAL; | |
225 | } | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
7aaf6b35 JO |
230 | static int setup_compute(const struct option *opt, const char *str, |
231 | int unset __maybe_unused) | |
232 | { | |
233 | int *cp = (int *) opt->value; | |
81d5f958 JO |
234 | char *cstr = (char *) str; |
235 | char buf[50]; | |
7aaf6b35 | 236 | unsigned i; |
81d5f958 | 237 | char *option; |
7aaf6b35 JO |
238 | |
239 | if (!str) { | |
240 | *cp = COMPUTE_DELTA; | |
241 | return 0; | |
242 | } | |
243 | ||
81d5f958 JO |
244 | option = strchr(str, ':'); |
245 | if (option) { | |
246 | unsigned len = option++ - str; | |
247 | ||
248 | /* | |
249 | * The str data are not writeable, so we need | |
250 | * to use another buffer. | |
251 | */ | |
252 | ||
253 | /* No option value is longer. */ | |
254 | if (len >= sizeof(buf)) | |
255 | return -EINVAL; | |
256 | ||
257 | strncpy(buf, str, len); | |
258 | buf[len] = 0x0; | |
259 | cstr = buf; | |
260 | } | |
261 | ||
7aaf6b35 | 262 | for (i = 0; i < COMPUTE_MAX; i++) |
81d5f958 | 263 | if (!strcmp(cstr, compute_names[i])) { |
7aaf6b35 | 264 | *cp = i; |
81d5f958 | 265 | return setup_compute_opt(option); |
7aaf6b35 JO |
266 | } |
267 | ||
268 | pr_err("Failed: '%s' is not computation method " | |
81d5f958 | 269 | "(use 'delta','ratio' or 'wdiff')\n", str); |
7aaf6b35 JO |
270 | return -EINVAL; |
271 | } | |
272 | ||
ef358e6d | 273 | static double period_percent(struct hist_entry *he, u64 period) |
96c47f19 | 274 | { |
8810f6ce NK |
275 | u64 total = hists__total_period(he->hists); |
276 | ||
96c47f19 JO |
277 | return (period * 100.0) / total; |
278 | } | |
279 | ||
ef358e6d | 280 | static double compute_delta(struct hist_entry *he, struct hist_entry *pair) |
96c47f19 | 281 | { |
ef358e6d JO |
282 | double old_percent = period_percent(he, he->stat.period); |
283 | double new_percent = period_percent(pair, pair->stat.period); | |
96c47f19 | 284 | |
9af303e2 JO |
285 | pair->diff.period_ratio_delta = new_percent - old_percent; |
286 | pair->diff.computed = true; | |
287 | return pair->diff.period_ratio_delta; | |
96c47f19 JO |
288 | } |
289 | ||
ef358e6d | 290 | static double compute_ratio(struct hist_entry *he, struct hist_entry *pair) |
96c47f19 | 291 | { |
9af303e2 JO |
292 | double old_period = he->stat.period ?: 1; |
293 | double new_period = pair->stat.period; | |
96c47f19 | 294 | |
9af303e2 JO |
295 | pair->diff.computed = true; |
296 | pair->diff.period_ratio = new_period / old_period; | |
297 | return pair->diff.period_ratio; | |
96c47f19 JO |
298 | } |
299 | ||
ef358e6d | 300 | static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair) |
81d5f958 | 301 | { |
9af303e2 JO |
302 | u64 old_period = he->stat.period; |
303 | u64 new_period = pair->stat.period; | |
81d5f958 | 304 | |
9af303e2 JO |
305 | pair->diff.computed = true; |
306 | pair->diff.wdiff = new_period * compute_wdiff_w2 - | |
307 | old_period * compute_wdiff_w1; | |
81d5f958 | 308 | |
9af303e2 | 309 | return pair->diff.wdiff; |
81d5f958 JO |
310 | } |
311 | ||
f4c8bae1 JO |
312 | static int formula_delta(struct hist_entry *he, struct hist_entry *pair, |
313 | char *buf, size_t size) | |
ed279da2 | 314 | { |
8810f6ce NK |
315 | u64 he_total = he->hists->stats.total_period; |
316 | u64 pair_total = pair->hists->stats.total_period; | |
317 | ||
318 | if (symbol_conf.filter_relative) { | |
319 | he_total = he->hists->stats.total_non_filtered_period; | |
320 | pair_total = pair->hists->stats.total_non_filtered_period; | |
321 | } | |
ed279da2 JO |
322 | return scnprintf(buf, size, |
323 | "(%" PRIu64 " * 100 / %" PRIu64 ") - " | |
324 | "(%" PRIu64 " * 100 / %" PRIu64 ")", | |
8810f6ce NK |
325 | pair->stat.period, pair_total, |
326 | he->stat.period, he_total); | |
ed279da2 JO |
327 | } |
328 | ||
f4c8bae1 JO |
329 | static int formula_ratio(struct hist_entry *he, struct hist_entry *pair, |
330 | char *buf, size_t size) | |
ed279da2 | 331 | { |
9af303e2 JO |
332 | double old_period = he->stat.period; |
333 | double new_period = pair->stat.period; | |
ed279da2 JO |
334 | |
335 | return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period); | |
336 | } | |
337 | ||
f4c8bae1 JO |
338 | static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair, |
339 | char *buf, size_t size) | |
ed279da2 | 340 | { |
9af303e2 JO |
341 | u64 old_period = he->stat.period; |
342 | u64 new_period = pair->stat.period; | |
ed279da2 JO |
343 | |
344 | return scnprintf(buf, size, | |
345 | "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")", | |
346 | new_period, compute_wdiff_w2, old_period, compute_wdiff_w1); | |
347 | } | |
348 | ||
ef358e6d JO |
349 | static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair, |
350 | char *buf, size_t size) | |
ed279da2 JO |
351 | { |
352 | switch (compute) { | |
353 | case COMPUTE_DELTA: | |
a1668c25 | 354 | case COMPUTE_DELTA_ABS: |
f4c8bae1 | 355 | return formula_delta(he, pair, buf, size); |
ed279da2 | 356 | case COMPUTE_RATIO: |
f4c8bae1 | 357 | return formula_ratio(he, pair, buf, size); |
ed279da2 | 358 | case COMPUTE_WEIGHTED_DIFF: |
f4c8bae1 | 359 | return formula_wdiff(he, pair, buf, size); |
ed279da2 JO |
360 | default: |
361 | BUG_ON(1); | |
362 | } | |
363 | ||
364 | return -1; | |
365 | } | |
366 | ||
99150a1f JY |
367 | static void *block_hist_zalloc(size_t size) |
368 | { | |
369 | struct block_hist *bh; | |
370 | ||
371 | bh = zalloc(size + sizeof(*bh)); | |
372 | if (!bh) | |
373 | return NULL; | |
374 | ||
375 | return &bh->he; | |
376 | } | |
377 | ||
378 | static void block_hist_free(void *he) | |
379 | { | |
380 | struct block_hist *bh; | |
381 | ||
382 | bh = container_of(he, struct block_hist, he); | |
383 | hists__delete_entries(&bh->block_hists); | |
384 | free(bh); | |
385 | } | |
386 | ||
387 | struct hist_entry_ops block_hist_ops = { | |
388 | .new = block_hist_zalloc, | |
389 | .free = block_hist_free, | |
390 | }; | |
391 | ||
30f29bae | 392 | static int diff__process_sample_event(const struct perf_tool *tool, |
d20deb64 | 393 | union perf_event *event, |
8d50e5b4 | 394 | struct perf_sample *sample, |
32dcd021 | 395 | struct evsel *evsel, |
743eb868 | 396 | struct machine *machine) |
86a9eee0 | 397 | { |
4802138d | 398 | struct perf_diff *pdiff = container_of(tool, struct perf_diff, tool); |
86a9eee0 | 399 | struct addr_location al; |
4ea062ed | 400 | struct hists *hists = evsel__hists(evsel); |
2a09a84c JY |
401 | struct hist_entry_iter iter = { |
402 | .evsel = evsel, | |
403 | .sample = sample, | |
404 | .ops = &hist_iter_normal, | |
405 | }; | |
b91fc39f | 406 | int ret = -1; |
86a9eee0 | 407 | |
4802138d JY |
408 | if (perf_time__ranges_skip_sample(pdiff->ptime_range, pdiff->range_num, |
409 | sample->time)) { | |
410 | return 0; | |
411 | } | |
412 | ||
0dd5041c | 413 | addr_location__init(&al); |
bb3eb566 | 414 | if (machine__resolve(machine, &al, sample) < 0) { |
86a9eee0 ACM |
415 | pr_warning("problem processing %d event, skipping it.\n", |
416 | event->header.type); | |
0dd5041c IR |
417 | ret = -1; |
418 | goto out; | |
86a9eee0 ACM |
419 | } |
420 | ||
daca23b2 JY |
421 | if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) { |
422 | ret = 0; | |
0dd5041c | 423 | goto out; |
daca23b2 JY |
424 | } |
425 | ||
2a09a84c JY |
426 | switch (compute) { |
427 | case COMPUTE_CYCLES: | |
99150a1f | 428 | if (!hists__add_entry_ops(hists, &block_hist_ops, &al, NULL, |
ebf39d29 | 429 | NULL, NULL, NULL, sample, true)) { |
99150a1f JY |
430 | pr_warning("problem incrementing symbol period, " |
431 | "skipping event\n"); | |
0dd5041c | 432 | goto out; |
99150a1f JY |
433 | } |
434 | ||
1f2b7fbb KL |
435 | hist__account_cycles(sample->branch_stack, &al, sample, |
436 | false, NULL, evsel); | |
2a09a84c JY |
437 | break; |
438 | ||
439 | case COMPUTE_STREAM: | |
440 | if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH, | |
441 | NULL)) { | |
442 | pr_debug("problem adding hist entry, skipping event\n"); | |
0dd5041c | 443 | goto out; |
2a09a84c JY |
444 | } |
445 | break; | |
446 | ||
447 | default: | |
ebf39d29 | 448 | if (!hists__add_entry(hists, &al, NULL, NULL, NULL, NULL, sample, |
2a09a84c JY |
449 | true)) { |
450 | pr_warning("problem incrementing symbol period, " | |
451 | "skipping event\n"); | |
0dd5041c | 452 | goto out; |
2a09a84c | 453 | } |
86a9eee0 ACM |
454 | } |
455 | ||
820bc81f NK |
456 | /* |
457 | * The total_period is updated here before going to the output | |
458 | * tree since normally only the baseline hists will call | |
459 | * hists__output_resort() and precompute needs the total | |
460 | * period in order to sort entries by percentage delta. | |
461 | */ | |
4ea062ed | 462 | hists->stats.total_period += sample->period; |
820bc81f | 463 | if (!al.filtered) |
4ea062ed | 464 | hists->stats.total_non_filtered_period += sample->period; |
b91fc39f | 465 | ret = 0; |
0dd5041c IR |
466 | out: |
467 | addr_location__exit(&al); | |
b91fc39f | 468 | return ret; |
86a9eee0 ACM |
469 | } |
470 | ||
1e1ec8f2 | 471 | static struct perf_diff pdiff; |
86a9eee0 | 472 | |
22a4db3c | 473 | static struct evsel *evsel_match(struct evsel *evsel, struct evlist *evlist) |
863e451f | 474 | { |
32dcd021 | 475 | struct evsel *e; |
863e451f | 476 | |
e5cadb93 | 477 | evlist__for_each_entry(evlist, e) { |
22a4db3c IR |
478 | if ((evsel->core.attr.type == e->core.attr.type) && |
479 | (evsel->core.attr.config == e->core.attr.config)) | |
863e451f | 480 | return e; |
0050f7aa | 481 | } |
863e451f JO |
482 | |
483 | return NULL; | |
484 | } | |
485 | ||
b979a2f1 | 486 | static void evlist__collapse_resort(struct evlist *evlist) |
dd464345 | 487 | { |
32dcd021 | 488 | struct evsel *evsel; |
dd464345 | 489 | |
e5cadb93 | 490 | evlist__for_each_entry(evlist, evsel) { |
4ea062ed | 491 | struct hists *hists = evsel__hists(evsel); |
dd464345 | 492 | |
c1fb5651 | 493 | hists__collapse_resort(hists, NULL); |
dd464345 JO |
494 | } |
495 | } | |
496 | ||
ff21cef6 NK |
497 | static struct data__file *fmt_to_data_file(struct perf_hpp_fmt *fmt) |
498 | { | |
499 | struct diff_hpp_fmt *dfmt = container_of(fmt, struct diff_hpp_fmt, fmt); | |
500 | void *ptr = dfmt - dfmt->idx; | |
501 | struct data__file *d = container_of(ptr, struct data__file, fmt); | |
502 | ||
503 | return d; | |
504 | } | |
505 | ||
5f3f8d3b JO |
506 | static struct hist_entry* |
507 | get_pair_data(struct hist_entry *he, struct data__file *d) | |
508 | { | |
509 | if (hist_entry__has_pairs(he)) { | |
510 | struct hist_entry *pair; | |
511 | ||
512 | list_for_each_entry(pair, &he->pairs.head, pairs.node) | |
513 | if (pair->hists == d->hists) | |
514 | return pair; | |
515 | } | |
516 | ||
517 | return NULL; | |
518 | } | |
519 | ||
520 | static struct hist_entry* | |
521 | get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt) | |
522 | { | |
ff21cef6 | 523 | struct data__file *d = fmt_to_data_file(&dfmt->fmt); |
5f3f8d3b JO |
524 | |
525 | return get_pair_data(he, d); | |
526 | } | |
527 | ||
a06d143e JO |
528 | static void hists__baseline_only(struct hists *hists) |
529 | { | |
2eb3d689 | 530 | struct rb_root_cached *root; |
ce74f60e NK |
531 | struct rb_node *next; |
532 | ||
52225036 | 533 | if (hists__has(hists, need_collapse)) |
ce74f60e NK |
534 | root = &hists->entries_collapsed; |
535 | else | |
536 | root = hists->entries_in; | |
a06d143e | 537 | |
2eb3d689 | 538 | next = rb_first_cached(root); |
a06d143e | 539 | while (next != NULL) { |
ce74f60e | 540 | struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in); |
a06d143e | 541 | |
ce74f60e | 542 | next = rb_next(&he->rb_node_in); |
b821c732 | 543 | if (!hist_entry__next_pair(he)) { |
2eb3d689 | 544 | rb_erase_cached(&he->rb_node_in, root); |
6733d1bf | 545 | hist_entry__delete(he); |
a06d143e JO |
546 | } |
547 | } | |
548 | } | |
549 | ||
99150a1f JY |
550 | static int64_t block_cycles_diff_cmp(struct hist_entry *left, |
551 | struct hist_entry *right) | |
552 | { | |
553 | bool pairs_left = hist_entry__has_pairs(left); | |
554 | bool pairs_right = hist_entry__has_pairs(right); | |
555 | s64 l, r; | |
556 | ||
557 | if (!pairs_left && !pairs_right) | |
558 | return 0; | |
559 | ||
98e93245 ACM |
560 | l = llabs(left->diff.cycles); |
561 | r = llabs(right->diff.cycles); | |
99150a1f JY |
562 | return r - l; |
563 | } | |
564 | ||
565 | static int64_t block_sort(struct perf_hpp_fmt *fmt __maybe_unused, | |
566 | struct hist_entry *left, struct hist_entry *right) | |
567 | { | |
568 | return block_cycles_diff_cmp(right, left); | |
569 | } | |
570 | ||
571 | static void init_block_hist(struct block_hist *bh) | |
572 | { | |
573 | __hists__init(&bh->block_hists, &bh->block_list); | |
574 | perf_hpp_list__init(&bh->block_list); | |
575 | ||
576 | INIT_LIST_HEAD(&bh->block_fmt.list); | |
577 | INIT_LIST_HEAD(&bh->block_fmt.sort_list); | |
60414418 | 578 | bh->block_fmt.cmp = block_info__cmp; |
99150a1f JY |
579 | bh->block_fmt.sort = block_sort; |
580 | perf_hpp_list__register_sort_field(&bh->block_list, | |
581 | &bh->block_fmt); | |
582 | bh->valid = true; | |
583 | } | |
584 | ||
f3810817 JY |
585 | static struct hist_entry *get_block_pair(struct hist_entry *he, |
586 | struct hists *hists_pair) | |
587 | { | |
588 | struct rb_root_cached *root = hists_pair->entries_in; | |
589 | struct rb_node *next = rb_first_cached(root); | |
a8a9f6dc | 590 | int64_t cmp; |
f3810817 JY |
591 | |
592 | while (next != NULL) { | |
593 | struct hist_entry *he_pair = rb_entry(next, struct hist_entry, | |
594 | rb_node_in); | |
595 | ||
596 | next = rb_next(&he_pair->rb_node_in); | |
597 | ||
a8a9f6dc | 598 | cmp = __block_info__cmp(he_pair, he); |
f3810817 JY |
599 | if (!cmp) |
600 | return he_pair; | |
601 | } | |
602 | ||
603 | return NULL; | |
604 | } | |
605 | ||
cebf7d51 JY |
606 | static void init_spark_values(unsigned long *svals, int num) |
607 | { | |
608 | for (int i = 0; i < num; i++) | |
609 | svals[i] = 0; | |
610 | } | |
611 | ||
612 | static void update_spark_value(unsigned long *svals, int num, | |
613 | struct stats *stats, u64 val) | |
614 | { | |
615 | int n = stats->n; | |
616 | ||
617 | if (n < num) | |
618 | svals[n] = val; | |
619 | } | |
620 | ||
f3810817 JY |
621 | static void compute_cycles_diff(struct hist_entry *he, |
622 | struct hist_entry *pair) | |
623 | { | |
624 | pair->diff.computed = true; | |
625 | if (pair->block_info->num && he->block_info->num) { | |
626 | pair->diff.cycles = | |
627 | pair->block_info->cycles_aggr / pair->block_info->num_aggr - | |
628 | he->block_info->cycles_aggr / he->block_info->num_aggr; | |
cebf7d51 JY |
629 | |
630 | if (!cycles_hist) | |
631 | return; | |
632 | ||
633 | init_stats(&pair->diff.stats); | |
634 | init_spark_values(pair->diff.svals, NUM_SPARKS); | |
635 | ||
636 | for (int i = 0; i < pair->block_info->num; i++) { | |
637 | u64 val; | |
638 | ||
639 | if (i >= he->block_info->num || i >= NUM_SPARKS) | |
640 | break; | |
641 | ||
2b1ac640 | 642 | val = llabs(pair->block_info->cycles_spark[i] - |
cebf7d51 JY |
643 | he->block_info->cycles_spark[i]); |
644 | ||
645 | update_spark_value(pair->diff.svals, NUM_SPARKS, | |
646 | &pair->diff.stats, val); | |
647 | update_stats(&pair->diff.stats, val); | |
648 | } | |
f3810817 JY |
649 | } |
650 | } | |
651 | ||
652 | static void block_hists_match(struct hists *hists_base, | |
653 | struct hists *hists_pair) | |
654 | { | |
655 | struct rb_root_cached *root = hists_base->entries_in; | |
656 | struct rb_node *next = rb_first_cached(root); | |
657 | ||
658 | while (next != NULL) { | |
659 | struct hist_entry *he = rb_entry(next, struct hist_entry, | |
660 | rb_node_in); | |
661 | struct hist_entry *pair = get_block_pair(he, hists_pair); | |
662 | ||
663 | next = rb_next(&he->rb_node_in); | |
664 | ||
665 | if (pair) { | |
666 | hist_entry__add_pair(pair, he); | |
667 | compute_cycles_diff(he, pair); | |
668 | } | |
669 | } | |
670 | } | |
671 | ||
96c47f19 JO |
672 | static void hists__precompute(struct hists *hists) |
673 | { | |
2eb3d689 | 674 | struct rb_root_cached *root; |
367c53c0 JO |
675 | struct rb_node *next; |
676 | ||
52225036 | 677 | if (hists__has(hists, need_collapse)) |
367c53c0 JO |
678 | root = &hists->entries_collapsed; |
679 | else | |
680 | root = hists->entries_in; | |
96c47f19 | 681 | |
2eb3d689 | 682 | next = rb_first_cached(root); |
96c47f19 | 683 | while (next != NULL) { |
f3810817 | 684 | struct block_hist *bh, *pair_bh; |
5f3f8d3b | 685 | struct hist_entry *he, *pair; |
56495a8a NK |
686 | struct data__file *d; |
687 | int i; | |
96c47f19 | 688 | |
5f3f8d3b | 689 | he = rb_entry(next, struct hist_entry, rb_node_in); |
367c53c0 | 690 | next = rb_next(&he->rb_node_in); |
5f3f8d3b | 691 | |
60414418 JY |
692 | if (compute == COMPUTE_CYCLES) { |
693 | bh = container_of(he, struct block_hist, he); | |
694 | init_block_hist(bh); | |
20d6f555 | 695 | block_info__process_sym(he, bh, NULL, 0, 0); |
60414418 | 696 | } |
99150a1f | 697 | |
56495a8a NK |
698 | data__for_each_file_new(i, d) { |
699 | pair = get_pair_data(he, d); | |
700 | if (!pair) | |
701 | continue; | |
702 | ||
703 | switch (compute) { | |
704 | case COMPUTE_DELTA: | |
a1668c25 | 705 | case COMPUTE_DELTA_ABS: |
56495a8a NK |
706 | compute_delta(he, pair); |
707 | break; | |
708 | case COMPUTE_RATIO: | |
709 | compute_ratio(he, pair); | |
710 | break; | |
711 | case COMPUTE_WEIGHTED_DIFF: | |
712 | compute_wdiff(he, pair); | |
713 | break; | |
99150a1f | 714 | case COMPUTE_CYCLES: |
f3810817 JY |
715 | pair_bh = container_of(pair, struct block_hist, |
716 | he); | |
60414418 | 717 | init_block_hist(pair_bh); |
20d6f555 | 718 | block_info__process_sym(pair, pair_bh, NULL, 0, 0); |
60414418 JY |
719 | |
720 | bh = container_of(he, struct block_hist, he); | |
f3810817 JY |
721 | |
722 | if (bh->valid && pair_bh->valid) { | |
723 | block_hists_match(&bh->block_hists, | |
724 | &pair_bh->block_hists); | |
0bdf181f JY |
725 | hists__output_resort(&pair_bh->block_hists, |
726 | NULL); | |
f3810817 | 727 | } |
99150a1f | 728 | break; |
56495a8a NK |
729 | default: |
730 | BUG_ON(1); | |
731 | } | |
96c47f19 JO |
732 | } |
733 | } | |
734 | } | |
735 | ||
736 | static int64_t cmp_doubles(double l, double r) | |
737 | { | |
738 | if (l > r) | |
739 | return -1; | |
740 | else if (l < r) | |
741 | return 1; | |
742 | else | |
743 | return 0; | |
744 | } | |
745 | ||
746 | static int64_t | |
5f3f8d3b | 747 | __hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right, |
96c47f19 JO |
748 | int c) |
749 | { | |
750 | switch (c) { | |
751 | case COMPUTE_DELTA: | |
752 | { | |
753 | double l = left->diff.period_ratio_delta; | |
754 | double r = right->diff.period_ratio_delta; | |
755 | ||
756 | return cmp_doubles(l, r); | |
757 | } | |
a1668c25 NK |
758 | case COMPUTE_DELTA_ABS: |
759 | { | |
760 | double l = fabs(left->diff.period_ratio_delta); | |
761 | double r = fabs(right->diff.period_ratio_delta); | |
762 | ||
763 | return cmp_doubles(l, r); | |
764 | } | |
96c47f19 JO |
765 | case COMPUTE_RATIO: |
766 | { | |
767 | double l = left->diff.period_ratio; | |
768 | double r = right->diff.period_ratio; | |
769 | ||
770 | return cmp_doubles(l, r); | |
771 | } | |
81d5f958 JO |
772 | case COMPUTE_WEIGHTED_DIFF: |
773 | { | |
774 | s64 l = left->diff.wdiff; | |
775 | s64 r = right->diff.wdiff; | |
776 | ||
777 | return r - l; | |
778 | } | |
96c47f19 JO |
779 | default: |
780 | BUG_ON(1); | |
781 | } | |
782 | ||
783 | return 0; | |
784 | } | |
785 | ||
5f3f8d3b JO |
786 | static int64_t |
787 | hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right, | |
56495a8a | 788 | int c, int sort_idx) |
5f3f8d3b JO |
789 | { |
790 | bool pairs_left = hist_entry__has_pairs(left); | |
791 | bool pairs_right = hist_entry__has_pairs(right); | |
792 | struct hist_entry *p_right, *p_left; | |
793 | ||
794 | if (!pairs_left && !pairs_right) | |
795 | return 0; | |
796 | ||
797 | if (!pairs_left || !pairs_right) | |
798 | return pairs_left ? -1 : 1; | |
799 | ||
56495a8a NK |
800 | p_left = get_pair_data(left, &data__files[sort_idx]); |
801 | p_right = get_pair_data(right, &data__files[sort_idx]); | |
5f3f8d3b JO |
802 | |
803 | if (!p_left && !p_right) | |
804 | return 0; | |
805 | ||
806 | if (!p_left || !p_right) | |
807 | return p_left ? -1 : 1; | |
808 | ||
809 | /* | |
810 | * We have 2 entries of same kind, let's | |
811 | * make the data comparison. | |
812 | */ | |
813 | return __hist_entry__cmp_compute(p_left, p_right, c); | |
814 | } | |
815 | ||
566b5cfb NK |
816 | static int64_t |
817 | hist_entry__cmp_compute_idx(struct hist_entry *left, struct hist_entry *right, | |
818 | int c, int sort_idx) | |
819 | { | |
820 | struct hist_entry *p_right, *p_left; | |
821 | ||
822 | p_left = get_pair_data(left, &data__files[sort_idx]); | |
823 | p_right = get_pair_data(right, &data__files[sort_idx]); | |
824 | ||
825 | if (!p_left && !p_right) | |
826 | return 0; | |
827 | ||
828 | if (!p_left || !p_right) | |
829 | return p_left ? -1 : 1; | |
830 | ||
a1668c25 | 831 | if (c != COMPUTE_DELTA && c != COMPUTE_DELTA_ABS) { |
566b5cfb NK |
832 | /* |
833 | * The delta can be computed without the baseline, but | |
834 | * others are not. Put those entries which have no | |
835 | * values below. | |
836 | */ | |
837 | if (left->dummy && right->dummy) | |
838 | return 0; | |
839 | ||
840 | if (left->dummy || right->dummy) | |
841 | return left->dummy ? 1 : -1; | |
842 | } | |
843 | ||
844 | return __hist_entry__cmp_compute(p_left, p_right, c); | |
845 | } | |
846 | ||
e7024fc3 | 847 | static int64_t |
87bbdf76 NK |
848 | hist_entry__cmp_nop(struct perf_hpp_fmt *fmt __maybe_unused, |
849 | struct hist_entry *left __maybe_unused, | |
e7024fc3 NK |
850 | struct hist_entry *right __maybe_unused) |
851 | { | |
852 | return 0; | |
853 | } | |
854 | ||
855 | static int64_t | |
87bbdf76 NK |
856 | hist_entry__cmp_baseline(struct perf_hpp_fmt *fmt __maybe_unused, |
857 | struct hist_entry *left, struct hist_entry *right) | |
e7024fc3 | 858 | { |
e7024fc3 NK |
859 | if (left->stat.period == right->stat.period) |
860 | return 0; | |
861 | return left->stat.period > right->stat.period ? 1 : -1; | |
862 | } | |
863 | ||
864 | static int64_t | |
56495a8a | 865 | hist_entry__cmp_delta(struct perf_hpp_fmt *fmt, |
87bbdf76 | 866 | struct hist_entry *left, struct hist_entry *right) |
e7024fc3 | 867 | { |
56495a8a NK |
868 | struct data__file *d = fmt_to_data_file(fmt); |
869 | ||
870 | return hist_entry__cmp_compute(right, left, COMPUTE_DELTA, d->idx); | |
e7024fc3 NK |
871 | } |
872 | ||
a1668c25 NK |
873 | static int64_t |
874 | hist_entry__cmp_delta_abs(struct perf_hpp_fmt *fmt, | |
875 | struct hist_entry *left, struct hist_entry *right) | |
876 | { | |
877 | struct data__file *d = fmt_to_data_file(fmt); | |
878 | ||
879 | return hist_entry__cmp_compute(right, left, COMPUTE_DELTA_ABS, d->idx); | |
880 | } | |
881 | ||
e7024fc3 | 882 | static int64_t |
56495a8a | 883 | hist_entry__cmp_ratio(struct perf_hpp_fmt *fmt, |
87bbdf76 | 884 | struct hist_entry *left, struct hist_entry *right) |
e7024fc3 | 885 | { |
56495a8a NK |
886 | struct data__file *d = fmt_to_data_file(fmt); |
887 | ||
888 | return hist_entry__cmp_compute(right, left, COMPUTE_RATIO, d->idx); | |
e7024fc3 NK |
889 | } |
890 | ||
891 | static int64_t | |
56495a8a | 892 | hist_entry__cmp_wdiff(struct perf_hpp_fmt *fmt, |
87bbdf76 | 893 | struct hist_entry *left, struct hist_entry *right) |
e7024fc3 | 894 | { |
56495a8a NK |
895 | struct data__file *d = fmt_to_data_file(fmt); |
896 | ||
897 | return hist_entry__cmp_compute(right, left, COMPUTE_WEIGHTED_DIFF, d->idx); | |
e7024fc3 NK |
898 | } |
899 | ||
566b5cfb NK |
900 | static int64_t |
901 | hist_entry__cmp_delta_idx(struct perf_hpp_fmt *fmt __maybe_unused, | |
902 | struct hist_entry *left, struct hist_entry *right) | |
903 | { | |
904 | return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA, | |
905 | sort_compute); | |
906 | } | |
907 | ||
a1668c25 NK |
908 | static int64_t |
909 | hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt *fmt __maybe_unused, | |
910 | struct hist_entry *left, struct hist_entry *right) | |
911 | { | |
912 | return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA_ABS, | |
913 | sort_compute); | |
914 | } | |
915 | ||
566b5cfb NK |
916 | static int64_t |
917 | hist_entry__cmp_ratio_idx(struct perf_hpp_fmt *fmt __maybe_unused, | |
918 | struct hist_entry *left, struct hist_entry *right) | |
919 | { | |
920 | return hist_entry__cmp_compute_idx(right, left, COMPUTE_RATIO, | |
921 | sort_compute); | |
922 | } | |
923 | ||
924 | static int64_t | |
925 | hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt *fmt __maybe_unused, | |
926 | struct hist_entry *left, struct hist_entry *right) | |
927 | { | |
928 | return hist_entry__cmp_compute_idx(right, left, COMPUTE_WEIGHTED_DIFF, | |
929 | sort_compute); | |
930 | } | |
931 | ||
22aeb7f5 | 932 | static void hists__process(struct hists *hists) |
a06d143e | 933 | { |
a06d143e | 934 | if (show_baseline_only) |
22aeb7f5 | 935 | hists__baseline_only(hists); |
a06d143e | 936 | |
56495a8a | 937 | hists__precompute(hists); |
38259a17 | 938 | hists__output_resort(hists, NULL); |
96c47f19 | 939 | |
b10c78c5 JY |
940 | if (compute == COMPUTE_CYCLES) |
941 | symbol_conf.report_block = true; | |
942 | ||
63b42fce | 943 | hists__fprintf(hists, !quiet, 0, 0, 0, stdout, |
e9de7e2f | 944 | !symbol_conf.use_callchain); |
a06d143e JO |
945 | } |
946 | ||
1d81c7fc JO |
947 | static void data__fprintf(void) |
948 | { | |
949 | struct data__file *d; | |
950 | int i; | |
951 | ||
952 | fprintf(stdout, "# Data files:\n"); | |
953 | ||
954 | data__for_each_file(i, d) | |
955 | fprintf(stdout, "# [%d] %s %s\n", | |
2d4f2799 | 956 | d->idx, d->data.path, |
1d81c7fc JO |
957 | !d->idx ? "(Baseline)" : ""); |
958 | ||
959 | fprintf(stdout, "#\n"); | |
960 | } | |
961 | ||
ec308426 | 962 | static void data_process(void) |
86a9eee0 | 963 | { |
63503dba | 964 | struct evlist *evlist_base = data__files[0].session->evlist; |
32dcd021 | 965 | struct evsel *evsel_base; |
863e451f | 966 | bool first = true; |
86a9eee0 | 967 | |
e5cadb93 | 968 | evlist__for_each_entry(evlist_base, evsel_base) { |
4ea062ed | 969 | struct hists *hists_base = evsel__hists(evsel_base); |
22aeb7f5 JO |
970 | struct data__file *d; |
971 | int i; | |
86a9eee0 | 972 | |
22aeb7f5 | 973 | data__for_each_file_new(i, d) { |
63503dba | 974 | struct evlist *evlist = d->session->evlist; |
32dcd021 | 975 | struct evsel *evsel; |
4ea062ed | 976 | struct hists *hists; |
22aeb7f5 JO |
977 | |
978 | evsel = evsel_match(evsel_base, evlist); | |
979 | if (!evsel) | |
980 | continue; | |
981 | ||
4ea062ed ACM |
982 | hists = evsel__hists(evsel); |
983 | d->hists = hists; | |
22aeb7f5 | 984 | |
4ea062ed | 985 | hists__match(hists_base, hists); |
22aeb7f5 JO |
986 | |
987 | if (!show_baseline_only) | |
4ea062ed | 988 | hists__link(hists_base, hists); |
22aeb7f5 | 989 | } |
86a9eee0 | 990 | |
63b42fce NK |
991 | if (!quiet) { |
992 | fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n", | |
8ab2e96d | 993 | evsel__name(evsel_base)); |
63b42fce | 994 | } |
863e451f | 995 | |
ec308426 | 996 | first = false; |
863e451f | 997 | |
63b42fce | 998 | if (verbose > 0 || ((data__files_cnt > 2) && !quiet)) |
1d81c7fc JO |
999 | data__fprintf(); |
1000 | ||
f9db0d0f | 1001 | /* Don't sort callchain for perf diff */ |
862b2f8f | 1002 | evsel__reset_sample_bit(evsel_base, CALLCHAIN); |
f9db0d0f | 1003 | |
4ea062ed | 1004 | hists__process(hists_base); |
ec308426 JO |
1005 | } |
1006 | } | |
863e451f | 1007 | |
2a09a84c JY |
1008 | static int process_base_stream(struct data__file *data_base, |
1009 | struct data__file *data_pair, | |
1010 | const char *title __maybe_unused) | |
1011 | { | |
1012 | struct evlist *evlist_base = data_base->session->evlist; | |
1013 | struct evlist *evlist_pair = data_pair->session->evlist; | |
1014 | struct evsel *evsel_base, *evsel_pair; | |
1015 | struct evsel_streams *es_base, *es_pair; | |
1016 | ||
1017 | evlist__for_each_entry(evlist_base, evsel_base) { | |
1018 | evsel_pair = evsel_match(evsel_base, evlist_pair); | |
1019 | if (!evsel_pair) | |
1020 | continue; | |
1021 | ||
1022 | es_base = evsel_streams__entry(data_base->evlist_streams, | |
2f0539fa | 1023 | evsel_base); |
2a09a84c JY |
1024 | if (!es_base) |
1025 | return -1; | |
1026 | ||
1027 | es_pair = evsel_streams__entry(data_pair->evlist_streams, | |
2f0539fa | 1028 | evsel_pair); |
2a09a84c JY |
1029 | if (!es_pair) |
1030 | return -1; | |
1031 | ||
1032 | evsel_streams__match(es_base, es_pair); | |
1033 | evsel_streams__report(es_base, es_pair); | |
1034 | } | |
1035 | ||
1036 | return 0; | |
1037 | } | |
1038 | ||
1039 | static void stream_process(void) | |
1040 | { | |
1041 | /* | |
1042 | * Stream comparison only supports two data files. | |
1043 | * perf.data.old and perf.data. data__files[0] is perf.data.old, | |
1044 | * data__files[1] is perf.data. | |
1045 | */ | |
1046 | process_base_stream(&data__files[0], &data__files[1], | |
1047 | "# Output based on old perf data:\n#\n"); | |
1048 | } | |
1049 | ||
c818b498 JO |
1050 | static void data__free(struct data__file *d) |
1051 | { | |
1052 | int col; | |
1053 | ||
2a09a84c JY |
1054 | if (d->evlist_streams) |
1055 | evlist_streams__delete(d->evlist_streams); | |
1056 | ||
c818b498 JO |
1057 | for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) { |
1058 | struct diff_hpp_fmt *fmt = &d->fmt[col]; | |
1059 | ||
74cf249d | 1060 | zfree(&fmt->header); |
c818b498 JO |
1061 | } |
1062 | } | |
1063 | ||
4802138d JY |
1064 | static int abstime_str_dup(char **pstr) |
1065 | { | |
1066 | char *str = NULL; | |
1067 | ||
1068 | if (pdiff.time_str && strchr(pdiff.time_str, ':')) { | |
1069 | str = strdup(pdiff.time_str); | |
1070 | if (!str) | |
1071 | return -ENOMEM; | |
1072 | } | |
1073 | ||
1074 | *pstr = str; | |
1075 | return 0; | |
1076 | } | |
1077 | ||
1078 | static int parse_absolute_time(struct data__file *d, char **pstr) | |
1079 | { | |
1080 | char *p = *pstr; | |
1081 | int ret; | |
1082 | ||
1083 | /* | |
1084 | * Absolute timestamp for one file has the format: a.b,c.d | |
1085 | * For multiple files, the format is: a.b,c.d:a.b,c.d | |
1086 | */ | |
1087 | p = strchr(*pstr, ':'); | |
1088 | if (p) { | |
1089 | if (p == *pstr) { | |
1090 | pr_err("Invalid time string\n"); | |
1091 | return -EINVAL; | |
1092 | } | |
1093 | ||
1094 | *p = 0; | |
1095 | p++; | |
1096 | if (*p == 0) { | |
1097 | pr_err("Invalid time string\n"); | |
1098 | return -EINVAL; | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | ret = perf_time__parse_for_ranges(*pstr, d->session, | |
1103 | &pdiff.ptime_range, | |
1104 | &pdiff.range_size, | |
1105 | &pdiff.range_num); | |
1106 | if (ret < 0) | |
1107 | return ret; | |
1108 | ||
1109 | if (!p || *p == 0) | |
1110 | *pstr = NULL; | |
1111 | else | |
1112 | *pstr = p; | |
1113 | ||
1114 | return ret; | |
1115 | } | |
1116 | ||
1117 | static int parse_percent_time(struct data__file *d) | |
1118 | { | |
1119 | int ret; | |
1120 | ||
1121 | ret = perf_time__parse_for_ranges(pdiff.time_str, d->session, | |
1122 | &pdiff.ptime_range, | |
1123 | &pdiff.range_size, | |
1124 | &pdiff.range_num); | |
1125 | return ret; | |
1126 | } | |
1127 | ||
1128 | static int parse_time_str(struct data__file *d, char *abstime_ostr, | |
1129 | char **pabstime_tmp) | |
1130 | { | |
1131 | int ret = 0; | |
1132 | ||
1133 | if (abstime_ostr) | |
1134 | ret = parse_absolute_time(d, pabstime_tmp); | |
1135 | else if (pdiff.time_str) | |
1136 | ret = parse_percent_time(d); | |
1137 | ||
1138 | return ret; | |
1139 | } | |
1140 | ||
30d81553 JY |
1141 | static int check_file_brstack(void) |
1142 | { | |
1143 | struct data__file *d; | |
1144 | bool has_br_stack; | |
1145 | int i; | |
1146 | ||
1147 | data__for_each_file(i, d) { | |
2681bd85 | 1148 | d->session = perf_session__new(&d->data, &pdiff.tool); |
6ef81c55 | 1149 | if (IS_ERR(d->session)) { |
30d81553 | 1150 | pr_err("Failed to open %s\n", d->data.path); |
6ef81c55 | 1151 | return PTR_ERR(d->session); |
30d81553 JY |
1152 | } |
1153 | ||
1154 | has_br_stack = perf_header__has_feat(&d->session->header, | |
1155 | HEADER_BRANCH_STACK); | |
1156 | perf_session__delete(d->session); | |
1157 | if (!has_br_stack) | |
1158 | return 0; | |
1159 | } | |
1160 | ||
1161 | /* Set only all files having branch stacks */ | |
1162 | pdiff.has_br_stack = true; | |
1163 | return 0; | |
1164 | } | |
1165 | ||
ec308426 JO |
1166 | static int __cmd_diff(void) |
1167 | { | |
1168 | struct data__file *d; | |
4802138d JY |
1169 | int ret, i; |
1170 | char *abstime_ostr, *abstime_tmp; | |
1171 | ||
1172 | ret = abstime_str_dup(&abstime_ostr); | |
1173 | if (ret) | |
1174 | return ret; | |
1175 | ||
1176 | abstime_tmp = abstime_ostr; | |
1177 | ret = -EINVAL; | |
ec308426 JO |
1178 | |
1179 | data__for_each_file(i, d) { | |
2681bd85 | 1180 | d->session = perf_session__new(&d->data, &pdiff.tool); |
6ef81c55 MI |
1181 | if (IS_ERR(d->session)) { |
1182 | ret = PTR_ERR(d->session); | |
2d4f2799 | 1183 | pr_err("Failed to open %s\n", d->data.path); |
ec308426 JO |
1184 | goto out_delete; |
1185 | } | |
863e451f | 1186 | |
4802138d JY |
1187 | if (pdiff.time_str) { |
1188 | ret = parse_time_str(d, abstime_ostr, &abstime_tmp); | |
1189 | if (ret < 0) | |
1190 | goto out_delete; | |
1191 | } | |
1192 | ||
daca23b2 JY |
1193 | if (cpu_list) { |
1194 | ret = perf_session__cpu_bitmap(d->session, cpu_list, | |
1195 | cpu_bitmap); | |
1196 | if (ret < 0) | |
1197 | goto out_delete; | |
1198 | } | |
1199 | ||
b7b61cbe | 1200 | ret = perf_session__process_events(d->session); |
ec308426 | 1201 | if (ret) { |
2d4f2799 | 1202 | pr_err("Failed to process %s\n", d->data.path); |
ec308426 JO |
1203 | goto out_delete; |
1204 | } | |
863e451f | 1205 | |
b979a2f1 | 1206 | evlist__collapse_resort(d->session->evlist); |
4802138d JY |
1207 | |
1208 | if (pdiff.ptime_range) | |
1209 | zfree(&pdiff.ptime_range); | |
2a09a84c JY |
1210 | |
1211 | if (compute == COMPUTE_STREAM) { | |
1212 | d->evlist_streams = evlist__create_streams( | |
1213 | d->session->evlist, 5); | |
97130700 ZL |
1214 | if (!d->evlist_streams) { |
1215 | ret = -ENOMEM; | |
2a09a84c | 1216 | goto out_delete; |
97130700 | 1217 | } |
2a09a84c | 1218 | } |
ec308426 JO |
1219 | } |
1220 | ||
2a09a84c JY |
1221 | if (compute == COMPUTE_STREAM) |
1222 | stream_process(); | |
1223 | else | |
1224 | data_process(); | |
863e451f | 1225 | |
ec308426 JO |
1226 | out_delete: |
1227 | data__for_each_file(i, d) { | |
ffc52b7a DS |
1228 | if (!IS_ERR(d->session)) |
1229 | perf_session__delete(d->session); | |
c818b498 | 1230 | data__free(d); |
863e451f | 1231 | } |
9c443dfd | 1232 | |
ec308426 | 1233 | free(data__files); |
4802138d JY |
1234 | |
1235 | if (pdiff.ptime_range) | |
1236 | zfree(&pdiff.ptime_range); | |
1237 | ||
1238 | if (abstime_ostr) | |
1239 | free(abstime_ostr); | |
1240 | ||
86a9eee0 ACM |
1241 | return ret; |
1242 | } | |
1243 | ||
0422a4fc | 1244 | static const char * const diff_usage[] = { |
86a9eee0 | 1245 | "perf diff [<options>] [old_file] [new_file]", |
0422a4fc | 1246 | NULL, |
86a9eee0 ACM |
1247 | }; |
1248 | ||
1249 | static const struct option options[] = { | |
c0555642 | 1250 | OPT_INCR('v', "verbose", &verbose, |
86a9eee0 | 1251 | "be more verbose (show symbol address, etc)"), |
a527c2c1 | 1252 | OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"), |
a06d143e JO |
1253 | OPT_BOOLEAN('b', "baseline-only", &show_baseline_only, |
1254 | "Show only items with match in baseline"), | |
81d5f958 | 1255 | OPT_CALLBACK('c', "compute", &compute, |
b10c78c5 | 1256 | "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs),cycles", |
7aaf6b35 JO |
1257 | "Entries differential computation selection", |
1258 | setup_compute), | |
61949b21 JO |
1259 | OPT_BOOLEAN('p', "period", &show_period, |
1260 | "Show period values."), | |
ed279da2 JO |
1261 | OPT_BOOLEAN('F', "formula", &show_formula, |
1262 | "Show formula."), | |
cebf7d51 JY |
1263 | OPT_BOOLEAN(0, "cycles-hist", &cycles_hist, |
1264 | "Show cycles histogram and standard deviation " | |
1265 | "- WARNING: use only with -c cycles."), | |
86a9eee0 ACM |
1266 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
1267 | "dump raw trace in ASCII"), | |
1268 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), | |
6b1f3423 DA |
1269 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, |
1270 | "file", "kallsyms pathname"), | |
86a9eee0 ACM |
1271 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, |
1272 | "load module symbols - WARNING: use only with -k and LIVE kernel"), | |
c410a338 ACM |
1273 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
1274 | "only consider symbols in these dsos"), | |
1275 | OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", | |
1276 | "only consider symbols in these comms"), | |
1277 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", | |
1278 | "only consider these symbols"), | |
c351c281 | 1279 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
a2ce067e NK |
1280 | "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..." |
1281 | " Please refer the man page for the complete list."), | |
8b8ca6e1 | 1282 | OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator", |
c351c281 ACM |
1283 | "separator for columns, no spaces will be added between " |
1284 | "columns '.' is reserved."), | |
a7066709 HK |
1285 | OPT_CALLBACK(0, "symfs", NULL, "directory", |
1286 | "Look for files with symbols relative to this directory", | |
1287 | symbol__config_symfs), | |
5f3f8d3b | 1288 | OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."), |
8810f6ce NK |
1289 | OPT_CALLBACK(0, "percentage", NULL, "relative|absolute", |
1290 | "How to display percentage of filtered entries", parse_filter_percentage), | |
4802138d JY |
1291 | OPT_STRING(0, "time", &pdiff.time_str, "str", |
1292 | "Time span (time percent or absolute timestamp)"), | |
daca23b2 | 1293 | OPT_STRING(0, "cpu", &cpu_list, "cpu", "list of cpus to profile"), |
c1d3e633 JY |
1294 | OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]", |
1295 | "only consider symbols in these pids"), | |
1296 | OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]", | |
1297 | "only consider symbols in these tids"), | |
2a09a84c JY |
1298 | OPT_BOOLEAN(0, "stream", &pdiff.stream, |
1299 | "Enable hot streams comparison."), | |
86a9eee0 ACM |
1300 | OPT_END() |
1301 | }; | |
1302 | ||
345dc0b4 | 1303 | static double baseline_percent(struct hist_entry *he) |
1d77822e | 1304 | { |
8810f6ce NK |
1305 | u64 total = hists__total_period(he->hists); |
1306 | ||
1307 | return 100.0 * he->stat.period / total; | |
345dc0b4 | 1308 | } |
7aaf6b35 | 1309 | |
345dc0b4 JO |
1310 | static int hpp__color_baseline(struct perf_hpp_fmt *fmt, |
1311 | struct perf_hpp *hpp, struct hist_entry *he) | |
1312 | { | |
1313 | struct diff_hpp_fmt *dfmt = | |
1314 | container_of(fmt, struct diff_hpp_fmt, fmt); | |
1315 | double percent = baseline_percent(he); | |
1316 | char pfmt[20] = " "; | |
1317 | ||
1318 | if (!he->dummy) { | |
1319 | scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1); | |
1320 | return percent_color_snprintf(hpp->buf, hpp->size, | |
1321 | pfmt, percent); | |
1322 | } else | |
1323 | return scnprintf(hpp->buf, hpp->size, "%*s", | |
1324 | dfmt->header_width, pfmt); | |
1325 | } | |
1326 | ||
1327 | static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size) | |
1328 | { | |
1329 | double percent = baseline_percent(he); | |
1330 | const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%"; | |
1331 | int ret = 0; | |
1332 | ||
1333 | if (!he->dummy) | |
1334 | ret = scnprintf(buf, size, fmt, percent); | |
1335 | ||
1336 | return ret; | |
1337 | } | |
1338 | ||
b10c78c5 JY |
1339 | static int cycles_printf(struct hist_entry *he, struct hist_entry *pair, |
1340 | struct perf_hpp *hpp, int width) | |
1341 | { | |
1342 | struct block_hist *bh = container_of(he, struct block_hist, he); | |
1343 | struct block_hist *bh_pair = container_of(pair, struct block_hist, he); | |
1344 | struct hist_entry *block_he; | |
1345 | struct block_info *bi; | |
1346 | char buf[128]; | |
1347 | char *start_line, *end_line; | |
1348 | ||
1349 | block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx); | |
1350 | if (!block_he) { | |
1351 | hpp->skip = true; | |
1352 | return 0; | |
1353 | } | |
1354 | ||
1355 | /* | |
1356 | * Avoid printing the warning "addr2line_init failed for ..." | |
1357 | */ | |
1358 | symbol_conf.disable_add2line_warn = true; | |
1359 | ||
1360 | bi = block_he->block_info; | |
1361 | ||
1362 | start_line = map__srcline(he->ms.map, bi->sym->start + bi->start, | |
1363 | he->ms.sym); | |
1364 | ||
1365 | end_line = map__srcline(he->ms.map, bi->sym->start + bi->end, | |
1366 | he->ms.sym); | |
1367 | ||
922db21d ACM |
1368 | if (start_line != SRCLINE_UNKNOWN && |
1369 | end_line != SRCLINE_UNKNOWN) { | |
b10c78c5 JY |
1370 | scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld", |
1371 | start_line, end_line, block_he->diff.cycles); | |
1372 | } else { | |
1373 | scnprintf(buf, sizeof(buf), "[%7lx -> %7lx] %4ld", | |
1374 | bi->start, bi->end, block_he->diff.cycles); | |
1375 | } | |
1376 | ||
625db36e IR |
1377 | zfree_srcline(&start_line); |
1378 | zfree_srcline(&end_line); | |
b10c78c5 JY |
1379 | |
1380 | return scnprintf(hpp->buf, hpp->size, "%*s", width, buf); | |
1381 | } | |
1382 | ||
01f10bc8 RR |
1383 | static int __hpp__color_compare(struct perf_hpp_fmt *fmt, |
1384 | struct perf_hpp *hpp, struct hist_entry *he, | |
1385 | int comparison_method) | |
1386 | { | |
1387 | struct diff_hpp_fmt *dfmt = | |
1388 | container_of(fmt, struct diff_hpp_fmt, fmt); | |
1389 | struct hist_entry *pair = get_pair_fmt(he, dfmt); | |
1390 | double diff; | |
a5846e21 | 1391 | s64 wdiff; |
01f10bc8 RR |
1392 | char pfmt[20] = " "; |
1393 | ||
b10c78c5 JY |
1394 | if (!pair) { |
1395 | if (comparison_method == COMPUTE_CYCLES) { | |
1396 | struct block_hist *bh; | |
1397 | ||
1398 | bh = container_of(he, struct block_hist, he); | |
1399 | if (bh->block_idx) | |
1400 | hpp->skip = true; | |
1401 | } | |
1402 | ||
ec3d07cb | 1403 | goto no_print; |
b10c78c5 | 1404 | } |
01f10bc8 RR |
1405 | |
1406 | switch (comparison_method) { | |
1407 | case COMPUTE_DELTA: | |
1408 | if (pair->diff.computed) | |
1409 | diff = pair->diff.period_ratio_delta; | |
1410 | else | |
1411 | diff = compute_delta(he, pair); | |
1412 | ||
01f10bc8 RR |
1413 | scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1); |
1414 | return percent_color_snprintf(hpp->buf, hpp->size, | |
1415 | pfmt, diff); | |
1f513b2c RR |
1416 | case COMPUTE_RATIO: |
1417 | if (he->dummy) | |
1418 | goto dummy_print; | |
1419 | if (pair->diff.computed) | |
1420 | diff = pair->diff.period_ratio; | |
1421 | else | |
1422 | diff = compute_ratio(he, pair); | |
1423 | ||
1424 | scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width); | |
1425 | return value_color_snprintf(hpp->buf, hpp->size, | |
1426 | pfmt, diff); | |
a5846e21 RR |
1427 | case COMPUTE_WEIGHTED_DIFF: |
1428 | if (he->dummy) | |
1429 | goto dummy_print; | |
1430 | if (pair->diff.computed) | |
1431 | wdiff = pair->diff.wdiff; | |
1432 | else | |
1433 | wdiff = compute_wdiff(he, pair); | |
1434 | ||
1435 | scnprintf(pfmt, 20, "%%14ld", dfmt->header_width); | |
1436 | return color_snprintf(hpp->buf, hpp->size, | |
1437 | get_percent_color(wdiff), | |
1438 | pfmt, wdiff); | |
b10c78c5 JY |
1439 | case COMPUTE_CYCLES: |
1440 | return cycles_printf(he, pair, hpp, dfmt->header_width); | |
01f10bc8 RR |
1441 | default: |
1442 | BUG_ON(1); | |
1443 | } | |
1444 | dummy_print: | |
ec3d07cb NK |
1445 | return scnprintf(hpp->buf, hpp->size, "%*s", |
1446 | dfmt->header_width, "N/A"); | |
1447 | no_print: | |
01f10bc8 RR |
1448 | return scnprintf(hpp->buf, hpp->size, "%*s", |
1449 | dfmt->header_width, pfmt); | |
1450 | } | |
1451 | ||
1452 | static int hpp__color_delta(struct perf_hpp_fmt *fmt, | |
1453 | struct perf_hpp *hpp, struct hist_entry *he) | |
1454 | { | |
1455 | return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA); | |
1456 | } | |
1457 | ||
1f513b2c RR |
1458 | static int hpp__color_ratio(struct perf_hpp_fmt *fmt, |
1459 | struct perf_hpp *hpp, struct hist_entry *he) | |
1460 | { | |
1461 | return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO); | |
1462 | } | |
1463 | ||
a5846e21 RR |
1464 | static int hpp__color_wdiff(struct perf_hpp_fmt *fmt, |
1465 | struct perf_hpp *hpp, struct hist_entry *he) | |
1466 | { | |
1467 | return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF); | |
1468 | } | |
1469 | ||
b10c78c5 JY |
1470 | static int hpp__color_cycles(struct perf_hpp_fmt *fmt, |
1471 | struct perf_hpp *hpp, struct hist_entry *he) | |
1472 | { | |
1473 | return __hpp__color_compare(fmt, hpp, he, COMPUTE_CYCLES); | |
1474 | } | |
1475 | ||
cebf7d51 JY |
1476 | static int all_zero(unsigned long *vals, int len) |
1477 | { | |
1478 | int i; | |
1479 | ||
1480 | for (i = 0; i < len; i++) | |
1481 | if (vals[i] != 0) | |
1482 | return 0; | |
1483 | return 1; | |
1484 | } | |
1485 | ||
1486 | static int print_cycles_spark(char *bf, int size, unsigned long *svals, u64 n) | |
1487 | { | |
1488 | int printed; | |
1489 | ||
1490 | if (n <= 1) | |
1491 | return 0; | |
1492 | ||
1493 | if (n > NUM_SPARKS) | |
1494 | n = NUM_SPARKS; | |
1495 | if (all_zero(svals, n)) | |
1496 | return 0; | |
1497 | ||
1498 | printed = print_spark(bf, size, svals, n); | |
1499 | printed += scnprintf(bf + printed, size - printed, " "); | |
1500 | return printed; | |
1501 | } | |
1502 | ||
1503 | static int hpp__color_cycles_hist(struct perf_hpp_fmt *fmt, | |
1504 | struct perf_hpp *hpp, struct hist_entry *he) | |
1505 | { | |
1506 | struct diff_hpp_fmt *dfmt = | |
1507 | container_of(fmt, struct diff_hpp_fmt, fmt); | |
1508 | struct hist_entry *pair = get_pair_fmt(he, dfmt); | |
1509 | struct block_hist *bh = container_of(he, struct block_hist, he); | |
1510 | struct block_hist *bh_pair; | |
1511 | struct hist_entry *block_he; | |
1512 | char spark[32], buf[128]; | |
1513 | double r; | |
1514 | int ret, pad; | |
1515 | ||
1516 | if (!pair) { | |
1517 | if (bh->block_idx) | |
1518 | hpp->skip = true; | |
1519 | ||
1520 | goto no_print; | |
1521 | } | |
1522 | ||
1523 | bh_pair = container_of(pair, struct block_hist, he); | |
1524 | ||
1525 | block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx); | |
1526 | if (!block_he) { | |
1527 | hpp->skip = true; | |
1528 | goto no_print; | |
1529 | } | |
1530 | ||
1531 | ret = print_cycles_spark(spark, sizeof(spark), block_he->diff.svals, | |
1532 | block_he->diff.stats.n); | |
1533 | ||
1534 | r = rel_stddev_stats(stddev_stats(&block_he->diff.stats), | |
1535 | avg_stats(&block_he->diff.stats)); | |
1536 | ||
1537 | if (ret) { | |
1538 | /* | |
1539 | * Padding spaces if number of sparks less than NUM_SPARKS | |
1540 | * otherwise the output is not aligned. | |
1541 | */ | |
1542 | pad = NUM_SPARKS - ((ret - 1) / 3); | |
1543 | scnprintf(buf, sizeof(buf), "%s%5.1f%% %s", "\u00B1", r, spark); | |
1544 | ret = scnprintf(hpp->buf, hpp->size, "%*s", | |
1545 | dfmt->header_width, buf); | |
1546 | ||
1547 | if (pad) { | |
1548 | ret += scnprintf(hpp->buf + ret, hpp->size - ret, | |
1549 | "%-*s", pad, " "); | |
1550 | } | |
1551 | ||
1552 | return ret; | |
1553 | } | |
1554 | ||
1555 | no_print: | |
1556 | return scnprintf(hpp->buf, hpp->size, "%*s", | |
1557 | dfmt->header_width, " "); | |
1558 | } | |
1559 | ||
345dc0b4 JO |
1560 | static void |
1561 | hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size) | |
1562 | { | |
1563 | switch (idx) { | |
1564 | case PERF_HPP_DIFF__PERIOD_BASELINE: | |
1565 | scnprintf(buf, size, "%" PRIu64, he->stat.period); | |
7aaf6b35 | 1566 | break; |
345dc0b4 JO |
1567 | |
1568 | default: | |
81d5f958 | 1569 | break; |
345dc0b4 JO |
1570 | } |
1571 | } | |
1572 | ||
1573 | static void | |
1574 | hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair, | |
1575 | int idx, char *buf, size_t size) | |
1576 | { | |
1577 | double diff; | |
1578 | double ratio; | |
1579 | s64 wdiff; | |
1580 | ||
1581 | switch (idx) { | |
1582 | case PERF_HPP_DIFF__DELTA: | |
a1668c25 | 1583 | case PERF_HPP_DIFF__DELTA_ABS: |
345dc0b4 JO |
1584 | if (pair->diff.computed) |
1585 | diff = pair->diff.period_ratio_delta; | |
1586 | else | |
ef358e6d | 1587 | diff = compute_delta(he, pair); |
345dc0b4 | 1588 | |
ec3d07cb | 1589 | scnprintf(buf, size, "%+4.2F%%", diff); |
345dc0b4 JO |
1590 | break; |
1591 | ||
1592 | case PERF_HPP_DIFF__RATIO: | |
1593 | /* No point for ratio number if we are dummy.. */ | |
ec3d07cb NK |
1594 | if (he->dummy) { |
1595 | scnprintf(buf, size, "N/A"); | |
345dc0b4 | 1596 | break; |
ec3d07cb | 1597 | } |
345dc0b4 JO |
1598 | |
1599 | if (pair->diff.computed) | |
1600 | ratio = pair->diff.period_ratio; | |
1601 | else | |
ef358e6d | 1602 | ratio = compute_ratio(he, pair); |
345dc0b4 JO |
1603 | |
1604 | if (ratio > 0.0) | |
1605 | scnprintf(buf, size, "%14.6F", ratio); | |
1606 | break; | |
1607 | ||
1608 | case PERF_HPP_DIFF__WEIGHTED_DIFF: | |
1609 | /* No point for wdiff number if we are dummy.. */ | |
ec3d07cb NK |
1610 | if (he->dummy) { |
1611 | scnprintf(buf, size, "N/A"); | |
345dc0b4 | 1612 | break; |
ec3d07cb | 1613 | } |
345dc0b4 JO |
1614 | |
1615 | if (pair->diff.computed) | |
1616 | wdiff = pair->diff.wdiff; | |
1617 | else | |
ef358e6d | 1618 | wdiff = compute_wdiff(he, pair); |
345dc0b4 JO |
1619 | |
1620 | if (wdiff != 0) | |
1621 | scnprintf(buf, size, "%14ld", wdiff); | |
1622 | break; | |
1623 | ||
1624 | case PERF_HPP_DIFF__FORMULA: | |
ef358e6d | 1625 | formula_fprintf(he, pair, buf, size); |
7aaf6b35 | 1626 | break; |
345dc0b4 JO |
1627 | |
1628 | case PERF_HPP_DIFF__PERIOD: | |
1629 | scnprintf(buf, size, "%" PRIu64, pair->stat.period); | |
1630 | break; | |
1631 | ||
7aaf6b35 JO |
1632 | default: |
1633 | BUG_ON(1); | |
8284bbea | 1634 | } |
345dc0b4 JO |
1635 | } |
1636 | ||
1637 | static void | |
22aeb7f5 JO |
1638 | __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt, |
1639 | char *buf, size_t size) | |
345dc0b4 | 1640 | { |
5f3f8d3b | 1641 | struct hist_entry *pair = get_pair_fmt(he, dfmt); |
22aeb7f5 | 1642 | int idx = dfmt->idx; |
345dc0b4 JO |
1643 | |
1644 | /* baseline is special */ | |
1645 | if (idx == PERF_HPP_DIFF__BASELINE) | |
1646 | hpp__entry_baseline(he, buf, size); | |
1647 | else { | |
1648 | if (pair) | |
1649 | hpp__entry_pair(he, pair, idx, buf, size); | |
1650 | else | |
1651 | hpp__entry_unpair(he, idx, buf, size); | |
1652 | } | |
1653 | } | |
1654 | ||
1655 | static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp, | |
1656 | struct hist_entry *he) | |
1657 | { | |
1658 | struct diff_hpp_fmt *dfmt = | |
1659 | container_of(_fmt, struct diff_hpp_fmt, fmt); | |
1660 | char buf[MAX_COL_WIDTH] = " "; | |
1661 | ||
22aeb7f5 | 1662 | __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH); |
345dc0b4 JO |
1663 | |
1664 | if (symbol_conf.field_sep) | |
1665 | return scnprintf(hpp->buf, hpp->size, "%s", buf); | |
1666 | else | |
1667 | return scnprintf(hpp->buf, hpp->size, "%*s", | |
1668 | dfmt->header_width, buf); | |
1669 | } | |
1670 | ||
94a0793d | 1671 | static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, |
74bb43f2 | 1672 | struct hists *hists __maybe_unused, |
29659ab4 JO |
1673 | int line __maybe_unused, |
1674 | int *span __maybe_unused) | |
345dc0b4 JO |
1675 | { |
1676 | struct diff_hpp_fmt *dfmt = | |
1677 | container_of(fmt, struct diff_hpp_fmt, fmt); | |
1678 | ||
1679 | BUG_ON(!dfmt->header); | |
1680 | return scnprintf(hpp->buf, hpp->size, dfmt->header); | |
1681 | } | |
1682 | ||
1683 | static int hpp__width(struct perf_hpp_fmt *fmt, | |
94a0793d | 1684 | struct perf_hpp *hpp __maybe_unused, |
da1b0407 | 1685 | struct hists *hists __maybe_unused) |
345dc0b4 JO |
1686 | { |
1687 | struct diff_hpp_fmt *dfmt = | |
1688 | container_of(fmt, struct diff_hpp_fmt, fmt); | |
1689 | ||
1690 | BUG_ON(dfmt->header_width <= 0); | |
1691 | return dfmt->header_width; | |
1692 | } | |
1693 | ||
22aeb7f5 | 1694 | static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt) |
345dc0b4 JO |
1695 | { |
1696 | #define MAX_HEADER_NAME 100 | |
1697 | char buf_indent[MAX_HEADER_NAME]; | |
1698 | char buf[MAX_HEADER_NAME]; | |
1699 | const char *header = NULL; | |
1700 | int width = 0; | |
1701 | ||
1702 | BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX); | |
1703 | header = columns[dfmt->idx].name; | |
1704 | width = columns[dfmt->idx].width; | |
1705 | ||
1706 | /* Only our defined HPP fmts should appear here. */ | |
1707 | BUG_ON(!header); | |
1708 | ||
22aeb7f5 JO |
1709 | if (data__files_cnt > 2) |
1710 | scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx); | |
1711 | ||
345dc0b4 JO |
1712 | #define NAME (data__files_cnt > 2 ? buf : header) |
1713 | dfmt->header_width = width; | |
1714 | width = (int) strlen(NAME); | |
1715 | if (dfmt->header_width < width) | |
1716 | dfmt->header_width = width; | |
1717 | ||
1718 | scnprintf(buf_indent, MAX_HEADER_NAME, "%*s", | |
1719 | dfmt->header_width, NAME); | |
1720 | ||
1721 | dfmt->header = strdup(buf_indent); | |
1722 | #undef MAX_HEADER_NAME | |
1723 | #undef NAME | |
1724 | } | |
1725 | ||
c818b498 | 1726 | static void data__hpp_register(struct data__file *d, int idx) |
345dc0b4 | 1727 | { |
c818b498 JO |
1728 | struct diff_hpp_fmt *dfmt = &d->fmt[idx]; |
1729 | struct perf_hpp_fmt *fmt = &dfmt->fmt; | |
1730 | ||
1731 | dfmt->idx = idx; | |
1732 | ||
1733 | fmt->header = hpp__header; | |
1734 | fmt->width = hpp__width; | |
1735 | fmt->entry = hpp__entry_global; | |
e7024fc3 NK |
1736 | fmt->cmp = hist_entry__cmp_nop; |
1737 | fmt->collapse = hist_entry__cmp_nop; | |
c818b498 JO |
1738 | |
1739 | /* TODO more colors */ | |
01f10bc8 RR |
1740 | switch (idx) { |
1741 | case PERF_HPP_DIFF__BASELINE: | |
c818b498 | 1742 | fmt->color = hpp__color_baseline; |
e7024fc3 | 1743 | fmt->sort = hist_entry__cmp_baseline; |
01f10bc8 RR |
1744 | break; |
1745 | case PERF_HPP_DIFF__DELTA: | |
1746 | fmt->color = hpp__color_delta; | |
e7024fc3 | 1747 | fmt->sort = hist_entry__cmp_delta; |
01f10bc8 | 1748 | break; |
1f513b2c RR |
1749 | case PERF_HPP_DIFF__RATIO: |
1750 | fmt->color = hpp__color_ratio; | |
e7024fc3 | 1751 | fmt->sort = hist_entry__cmp_ratio; |
1f513b2c | 1752 | break; |
a5846e21 RR |
1753 | case PERF_HPP_DIFF__WEIGHTED_DIFF: |
1754 | fmt->color = hpp__color_wdiff; | |
e7024fc3 | 1755 | fmt->sort = hist_entry__cmp_wdiff; |
a5846e21 | 1756 | break; |
a1668c25 NK |
1757 | case PERF_HPP_DIFF__DELTA_ABS: |
1758 | fmt->color = hpp__color_delta; | |
1759 | fmt->sort = hist_entry__cmp_delta_abs; | |
1760 | break; | |
b10c78c5 JY |
1761 | case PERF_HPP_DIFF__CYCLES: |
1762 | fmt->color = hpp__color_cycles; | |
1763 | fmt->sort = hist_entry__cmp_nop; | |
1764 | break; | |
cebf7d51 JY |
1765 | case PERF_HPP_DIFF__CYCLES_HIST: |
1766 | fmt->color = hpp__color_cycles_hist; | |
1767 | fmt->sort = hist_entry__cmp_nop; | |
1768 | break; | |
01f10bc8 | 1769 | default: |
e7024fc3 | 1770 | fmt->sort = hist_entry__cmp_nop; |
01f10bc8 RR |
1771 | break; |
1772 | } | |
345dc0b4 | 1773 | |
22aeb7f5 | 1774 | init_header(d, dfmt); |
c818b498 | 1775 | perf_hpp__column_register(fmt); |
e7024fc3 | 1776 | perf_hpp__register_sort_field(fmt); |
345dc0b4 JO |
1777 | } |
1778 | ||
566b5cfb | 1779 | static int ui_init(void) |
345dc0b4 | 1780 | { |
c818b498 | 1781 | struct data__file *d; |
566b5cfb | 1782 | struct perf_hpp_fmt *fmt; |
c818b498 JO |
1783 | int i; |
1784 | ||
1785 | data__for_each_file(i, d) { | |
1786 | ||
1787 | /* | |
4d39c89f | 1788 | * Baseline or compute related columns: |
c818b498 JO |
1789 | * |
1790 | * PERF_HPP_DIFF__BASELINE | |
1791 | * PERF_HPP_DIFF__DELTA | |
1792 | * PERF_HPP_DIFF__RATIO | |
1793 | * PERF_HPP_DIFF__WEIGHTED_DIFF | |
cebf7d51 | 1794 | * PERF_HPP_DIFF__CYCLES |
c818b498 JO |
1795 | */ |
1796 | data__hpp_register(d, i ? compute_2_hpp[compute] : | |
1797 | PERF_HPP_DIFF__BASELINE); | |
1d77822e | 1798 | |
cebf7d51 JY |
1799 | if (cycles_hist && i) |
1800 | data__hpp_register(d, PERF_HPP_DIFF__CYCLES_HIST); | |
1801 | ||
c818b498 JO |
1802 | /* |
1803 | * And the rest: | |
1804 | * | |
1805 | * PERF_HPP_DIFF__FORMULA | |
1806 | * PERF_HPP_DIFF__PERIOD | |
1807 | * PERF_HPP_DIFF__PERIOD_BASELINE | |
1808 | */ | |
1809 | if (show_formula && i) | |
1810 | data__hpp_register(d, PERF_HPP_DIFF__FORMULA); | |
ed279da2 | 1811 | |
c818b498 JO |
1812 | if (show_period) |
1813 | data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD : | |
1814 | PERF_HPP_DIFF__PERIOD_BASELINE); | |
61949b21 | 1815 | } |
566b5cfb NK |
1816 | |
1817 | if (!sort_compute) | |
1818 | return 0; | |
1819 | ||
1820 | /* | |
1821 | * Prepend an fmt to sort on columns at 'sort_compute' first. | |
1822 | * This fmt is added only to the sort list but not to the | |
1823 | * output fields list. | |
1824 | * | |
1825 | * Note that this column (data) can be compared twice - one | |
1826 | * for this 'sort_compute' fmt and another for the normal | |
1827 | * diff_hpp_fmt. But it shouldn't a problem as most entries | |
1828 | * will be sorted out by first try or baseline and comparing | |
1829 | * is not a costly operation. | |
1830 | */ | |
1831 | fmt = zalloc(sizeof(*fmt)); | |
1832 | if (fmt == NULL) { | |
1833 | pr_err("Memory allocation failed\n"); | |
1834 | return -1; | |
1835 | } | |
1836 | ||
1837 | fmt->cmp = hist_entry__cmp_nop; | |
1838 | fmt->collapse = hist_entry__cmp_nop; | |
1839 | ||
1840 | switch (compute) { | |
1841 | case COMPUTE_DELTA: | |
1842 | fmt->sort = hist_entry__cmp_delta_idx; | |
1843 | break; | |
1844 | case COMPUTE_RATIO: | |
1845 | fmt->sort = hist_entry__cmp_ratio_idx; | |
1846 | break; | |
1847 | case COMPUTE_WEIGHTED_DIFF: | |
1848 | fmt->sort = hist_entry__cmp_wdiff_idx; | |
1849 | break; | |
a1668c25 NK |
1850 | case COMPUTE_DELTA_ABS: |
1851 | fmt->sort = hist_entry__cmp_delta_abs_idx; | |
1852 | break; | |
99150a1f JY |
1853 | case COMPUTE_CYCLES: |
1854 | /* | |
1855 | * Should set since 'fmt->sort' is called without | |
1856 | * checking valid during sorting | |
1857 | */ | |
1858 | fmt->sort = hist_entry__cmp_nop; | |
1859 | break; | |
566b5cfb NK |
1860 | default: |
1861 | BUG_ON(1); | |
1862 | } | |
1863 | ||
a1c9f97f | 1864 | perf_hpp__prepend_sort_field(fmt); |
566b5cfb | 1865 | return 0; |
1d77822e JO |
1866 | } |
1867 | ||
ec308426 | 1868 | static int data_init(int argc, const char **argv) |
86a9eee0 | 1869 | { |
ec308426 JO |
1870 | struct data__file *d; |
1871 | static const char *defaults[] = { | |
1872 | "perf.data.old", | |
1873 | "perf.data", | |
1874 | }; | |
22aeb7f5 | 1875 | bool use_default = true; |
ec308426 JO |
1876 | int i; |
1877 | ||
1878 | data__files_cnt = 2; | |
1879 | ||
86a9eee0 | 1880 | if (argc) { |
22aeb7f5 | 1881 | if (argc == 1) |
ec308426 | 1882 | defaults[1] = argv[0]; |
22aeb7f5 JO |
1883 | else { |
1884 | data__files_cnt = argc; | |
1885 | use_default = false; | |
1886 | } | |
d8d9608f | 1887 | } else if (perf_guest) { |
ec308426 JO |
1888 | defaults[0] = "perf.data.host"; |
1889 | defaults[1] = "perf.data.guest"; | |
86a9eee0 ACM |
1890 | } |
1891 | ||
5f3f8d3b JO |
1892 | if (sort_compute >= (unsigned int) data__files_cnt) { |
1893 | pr_err("Order option out of limit.\n"); | |
1894 | return -EINVAL; | |
1895 | } | |
1896 | ||
ec308426 JO |
1897 | data__files = zalloc(sizeof(*data__files) * data__files_cnt); |
1898 | if (!data__files) | |
1899 | return -ENOMEM; | |
1900 | ||
1901 | data__for_each_file(i, d) { | |
8ceb41d7 | 1902 | struct perf_data *data = &d->data; |
f5fc1412 | 1903 | |
2d4f2799 | 1904 | data->path = use_default ? defaults[i] : argv[i]; |
681f34d5 LH |
1905 | data->mode = PERF_DATA_MODE_READ; |
1906 | data->force = force; | |
f5fc1412 | 1907 | |
ec308426 JO |
1908 | d->idx = i; |
1909 | } | |
1910 | ||
1911 | return 0; | |
1912 | } | |
1913 | ||
d49dd15d NK |
1914 | static int diff__config(const char *var, const char *value, |
1915 | void *cb __maybe_unused) | |
1916 | { | |
1917 | if (!strcmp(var, "diff.order")) { | |
25ce4bb8 ACM |
1918 | int ret; |
1919 | if (perf_config_int(&ret, var, value) < 0) | |
1920 | return -1; | |
1921 | sort_compute = ret; | |
d49dd15d NK |
1922 | return 0; |
1923 | } | |
4b35994a NK |
1924 | if (!strcmp(var, "diff.compute")) { |
1925 | if (!strcmp(value, "delta")) { | |
1926 | compute = COMPUTE_DELTA; | |
1927 | } else if (!strcmp(value, "delta-abs")) { | |
1928 | compute = COMPUTE_DELTA_ABS; | |
1929 | } else if (!strcmp(value, "ratio")) { | |
1930 | compute = COMPUTE_RATIO; | |
1931 | } else if (!strcmp(value, "wdiff")) { | |
1932 | compute = COMPUTE_WEIGHTED_DIFF; | |
1933 | } else { | |
1934 | pr_err("Invalid compute method: %s\n", value); | |
1935 | return -1; | |
1936 | } | |
1937 | } | |
d49dd15d NK |
1938 | |
1939 | return 0; | |
1940 | } | |
1941 | ||
b0ad8ea6 | 1942 | int cmd_diff(int argc, const char **argv) |
ec308426 | 1943 | { |
9ab1f508 KL |
1944 | int ret = hists__init(); |
1945 | ||
1946 | if (ret < 0) | |
1947 | return ret; | |
1948 | ||
1e1ec8f2 IR |
1949 | perf_tool__init(&pdiff.tool, /*ordered_events=*/true); |
1950 | pdiff.tool.sample = diff__process_sample_event; | |
1951 | pdiff.tool.mmap = perf_event__process_mmap; | |
1952 | pdiff.tool.mmap2 = perf_event__process_mmap2; | |
1953 | pdiff.tool.comm = perf_event__process_comm; | |
1954 | pdiff.tool.exit = perf_event__process_exit; | |
1955 | pdiff.tool.fork = perf_event__process_fork; | |
1956 | pdiff.tool.lost = perf_event__process_lost; | |
1957 | pdiff.tool.namespaces = perf_event__process_namespaces; | |
1958 | pdiff.tool.cgroup = perf_event__process_cgroup; | |
1959 | pdiff.tool.ordering_requires_timestamps = true; | |
1960 | ||
d49dd15d NK |
1961 | perf_config(diff__config, NULL); |
1962 | ||
ec308426 JO |
1963 | argc = parse_options(argc, argv, options, diff_usage, 0); |
1964 | ||
63b42fce NK |
1965 | if (quiet) |
1966 | perf_quiet_option(); | |
1967 | ||
cebf7d51 JY |
1968 | if (cycles_hist && (compute != COMPUTE_CYCLES)) |
1969 | usage_with_options(diff_usage, options); | |
1970 | ||
2a09a84c JY |
1971 | if (pdiff.stream) |
1972 | compute = COMPUTE_STREAM; | |
1973 | ||
99150a1f JY |
1974 | symbol__annotation_init(); |
1975 | ||
0a7e6d1b | 1976 | if (symbol__init(NULL) < 0) |
655000e7 ACM |
1977 | return -1; |
1978 | ||
ec308426 JO |
1979 | if (data_init(argc, argv) < 0) |
1980 | return -1; | |
1981 | ||
30d81553 JY |
1982 | if (check_file_brstack() < 0) |
1983 | return -1; | |
1984 | ||
2a09a84c JY |
1985 | if ((compute == COMPUTE_CYCLES || compute == COMPUTE_STREAM) |
1986 | && !pdiff.has_br_stack) { | |
99150a1f | 1987 | return -1; |
2a09a84c | 1988 | } |
99150a1f | 1989 | |
2a09a84c JY |
1990 | if (compute == COMPUTE_STREAM) { |
1991 | symbol_conf.show_branchflag_count = true; | |
1992 | symbol_conf.disable_add2line_warn = true; | |
1993 | callchain_param.mode = CHAIN_FLAT; | |
1994 | callchain_param.key = CCKEY_SRCLINE; | |
1995 | callchain_param.branch_callstack = 1; | |
1996 | symbol_conf.use_callchain = true; | |
1997 | callchain_register_param(&callchain_param); | |
1998 | sort_order = "srcline,symbol,dso"; | |
1999 | } else { | |
2000 | if (ui_init() < 0) | |
2001 | return -1; | |
1d77822e | 2002 | |
2a09a84c JY |
2003 | sort__mode = SORT_MODE__DIFF; |
2004 | } | |
512ae1bd | 2005 | |
40184c46 | 2006 | if (setup_sorting(NULL) < 0) |
55309985 NK |
2007 | usage_with_options(diff_usage, options); |
2008 | ||
86a9eee0 | 2009 | setup_pager(); |
c351c281 | 2010 | |
08e71542 | 2011 | sort__setup_elide(NULL); |
c351c281 | 2012 | |
86a9eee0 ACM |
2013 | return __cmd_diff(); |
2014 | } |