blob: c3b4b8196e005ffed8ebe45a36632655c9d1c05e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02002/*
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"
9
10#include "util/debug.h"
11#include "util/event.h"
12#include "util/hist.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020013#include "util/evsel.h"
Jiri Olsa863e4512012-09-06 17:46:55 +020014#include "util/evlist.h"
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020015#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020016#include "util/tool.h"
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020017#include "util/sort.h"
18#include "util/symbol.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Namhyung Kimd49dd152017-02-10 16:36:12 +090020#include "util/config.h"
Jin Yao48021382019-03-05 21:05:41 +080021#include "util/time-utils.h"
Jin Yao99150a12019-06-28 17:23:01 +080022#include "util/annotate.h"
Jin Yaob10c78c2019-06-28 17:23:03 +080023#include "util/map.h"
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030024#include <linux/zalloc.h>
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020025
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030026#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030027#include <inttypes.h>
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020028#include <stdlib.h>
Jiri Olsa345dc0b2013-02-03 20:08:34 +010029#include <math.h>
30
Jin Yao48021382019-03-05 21:05:41 +080031struct perf_diff {
32 struct perf_tool tool;
33 const char *time_str;
34 struct perf_time_interval *ptime_range;
35 int range_size;
36 int range_num;
Jin Yao30d81552019-06-28 17:23:00 +080037 bool has_br_stack;
Jin Yao48021382019-03-05 21:05:41 +080038};
39
Jiri Olsa345dc0b2013-02-03 20:08:34 +010040/* Diff command specific HPP columns. */
41enum {
42 PERF_HPP_DIFF__BASELINE,
43 PERF_HPP_DIFF__PERIOD,
44 PERF_HPP_DIFF__PERIOD_BASELINE,
45 PERF_HPP_DIFF__DELTA,
46 PERF_HPP_DIFF__RATIO,
47 PERF_HPP_DIFF__WEIGHTED_DIFF,
48 PERF_HPP_DIFF__FORMULA,
Namhyung Kima1668c22017-02-10 16:36:11 +090049 PERF_HPP_DIFF__DELTA_ABS,
Jin Yaob10c78c2019-06-28 17:23:03 +080050 PERF_HPP_DIFF__CYCLES,
Jiri Olsa345dc0b2013-02-03 20:08:34 +010051
52 PERF_HPP_DIFF__MAX_INDEX
53};
54
55struct diff_hpp_fmt {
56 struct perf_hpp_fmt fmt;
57 int idx;
58 char *header;
59 int header_width;
60};
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020061
Jiri Olsaec308422013-03-25 00:02:01 +010062struct data__file {
63 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010064 struct perf_data data;
Jiri Olsaec308422013-03-25 00:02:01 +010065 int idx;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +010066 struct hists *hists;
Jiri Olsac818b492012-12-01 21:57:04 +010067 struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX];
Jiri Olsaec308422013-03-25 00:02:01 +010068};
69
70static struct data__file *data__files;
71static int data__files_cnt;
72
73#define data__for_each_file_start(i, d, s) \
74 for (i = s, d = &data__files[s]; \
75 i < data__files_cnt; \
76 i++, d = &data__files[i])
77
78#define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
Jiri Olsa22aeb7f2012-12-01 22:00:00 +010079#define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
Jiri Olsaec308422013-03-25 00:02:01 +010080
Jiri Olsaec308422013-03-25 00:02:01 +010081static bool force;
Jiri Olsa61949b22012-10-05 16:44:44 +020082static bool show_period;
Jiri Olsaed279da2012-10-05 16:44:45 +020083static bool show_formula;
Jiri Olsaa06d1432012-10-05 16:44:40 +020084static bool show_baseline_only;
Namhyung Kimbe57b3f2017-02-11 01:18:56 +090085static unsigned int sort_compute = 1;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020086
Jiri Olsa81d5f952012-10-05 16:44:43 +020087static s64 compute_wdiff_w1;
88static s64 compute_wdiff_w2;
89
Jin Yaodaca23b2019-03-05 21:05:42 +080090static const char *cpu_list;
91static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
92
Jin Yao99150a12019-06-28 17:23:01 +080093static struct addr_location dummy_al;
94
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020095enum {
96 COMPUTE_DELTA,
97 COMPUTE_RATIO,
Jiri Olsa81d5f952012-10-05 16:44:43 +020098 COMPUTE_WEIGHTED_DIFF,
Namhyung Kima1668c22017-02-10 16:36:11 +090099 COMPUTE_DELTA_ABS,
Jin Yao99150a12019-06-28 17:23:01 +0800100 COMPUTE_CYCLES,
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200101 COMPUTE_MAX,
102};
103
104const char *compute_names[COMPUTE_MAX] = {
105 [COMPUTE_DELTA] = "delta",
Namhyung Kima1668c22017-02-10 16:36:11 +0900106 [COMPUTE_DELTA_ABS] = "delta-abs",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200107 [COMPUTE_RATIO] = "ratio",
Jiri Olsa81d5f952012-10-05 16:44:43 +0200108 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
Jin Yao99150a12019-06-28 17:23:01 +0800109 [COMPUTE_CYCLES] = "cycles",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200110};
111
Namhyung Kimbe57b3f2017-02-11 01:18:56 +0900112static int compute = COMPUTE_DELTA_ABS;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200113
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100114static int compute_2_hpp[COMPUTE_MAX] = {
115 [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA,
Namhyung Kima1668c22017-02-10 16:36:11 +0900116 [COMPUTE_DELTA_ABS] = PERF_HPP_DIFF__DELTA_ABS,
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100117 [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO,
118 [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF,
Jin Yaob10c78c2019-06-28 17:23:03 +0800119 [COMPUTE_CYCLES] = PERF_HPP_DIFF__CYCLES,
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100120};
121
122#define MAX_COL_WIDTH 70
123
124static struct header_column {
125 const char *name;
126 int width;
127} columns[PERF_HPP_DIFF__MAX_INDEX] = {
128 [PERF_HPP_DIFF__BASELINE] = {
129 .name = "Baseline",
130 },
131 [PERF_HPP_DIFF__PERIOD] = {
132 .name = "Period",
133 .width = 14,
134 },
135 [PERF_HPP_DIFF__PERIOD_BASELINE] = {
136 .name = "Base period",
137 .width = 14,
138 },
139 [PERF_HPP_DIFF__DELTA] = {
140 .name = "Delta",
141 .width = 7,
142 },
Namhyung Kima1668c22017-02-10 16:36:11 +0900143 [PERF_HPP_DIFF__DELTA_ABS] = {
144 .name = "Delta Abs",
145 .width = 7,
146 },
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100147 [PERF_HPP_DIFF__RATIO] = {
148 .name = "Ratio",
149 .width = 14,
150 },
151 [PERF_HPP_DIFF__WEIGHTED_DIFF] = {
152 .name = "Weighted diff",
153 .width = 14,
154 },
155 [PERF_HPP_DIFF__FORMULA] = {
156 .name = "Formula",
157 .width = MAX_COL_WIDTH,
Jin Yaob10c78c2019-06-28 17:23:03 +0800158 },
159 [PERF_HPP_DIFF__CYCLES] = {
160 .name = "[Program Block Range] Cycles Diff",
161 .width = 70,
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100162 }
163};
164
Jiri Olsa81d5f952012-10-05 16:44:43 +0200165static int setup_compute_opt_wdiff(char *opt)
166{
167 char *w1_str = opt;
168 char *w2_str;
169
170 int ret = -EINVAL;
171
172 if (!opt)
173 goto out;
174
175 w2_str = strchr(opt, ',');
176 if (!w2_str)
177 goto out;
178
179 *w2_str++ = 0x0;
180 if (!*w2_str)
181 goto out;
182
183 compute_wdiff_w1 = strtol(w1_str, NULL, 10);
184 compute_wdiff_w2 = strtol(w2_str, NULL, 10);
185
186 if (!compute_wdiff_w1 || !compute_wdiff_w2)
187 goto out;
188
189 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
190 compute_wdiff_w1, compute_wdiff_w2);
191
192 ret = 0;
193
194 out:
195 if (ret)
196 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
197
198 return ret;
199}
200
201static int setup_compute_opt(char *opt)
202{
203 if (compute == COMPUTE_WEIGHTED_DIFF)
204 return setup_compute_opt_wdiff(opt);
205
206 if (opt) {
207 pr_err("Failed: extra option specified '%s'", opt);
208 return -EINVAL;
209 }
210
211 return 0;
212}
213
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200214static int setup_compute(const struct option *opt, const char *str,
215 int unset __maybe_unused)
216{
217 int *cp = (int *) opt->value;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200218 char *cstr = (char *) str;
219 char buf[50];
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200220 unsigned i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200221 char *option;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200222
223 if (!str) {
224 *cp = COMPUTE_DELTA;
225 return 0;
226 }
227
Jiri Olsa81d5f952012-10-05 16:44:43 +0200228 option = strchr(str, ':');
229 if (option) {
230 unsigned len = option++ - str;
231
232 /*
233 * The str data are not writeable, so we need
234 * to use another buffer.
235 */
236
237 /* No option value is longer. */
238 if (len >= sizeof(buf))
239 return -EINVAL;
240
241 strncpy(buf, str, len);
242 buf[len] = 0x0;
243 cstr = buf;
244 }
245
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200246 for (i = 0; i < COMPUTE_MAX; i++)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200247 if (!strcmp(cstr, compute_names[i])) {
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200248 *cp = i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200249 return setup_compute_opt(option);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200250 }
251
252 pr_err("Failed: '%s' is not computation method "
Jiri Olsa81d5f952012-10-05 16:44:43 +0200253 "(use 'delta','ratio' or 'wdiff')\n", str);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200254 return -EINVAL;
255}
256
Jiri Olsaef358e62012-10-21 23:31:51 +0200257static double period_percent(struct hist_entry *he, u64 period)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200258{
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900259 u64 total = hists__total_period(he->hists);
260
Jiri Olsa96c47f12012-10-05 16:44:42 +0200261 return (period * 100.0) / total;
262}
263
Jiri Olsaef358e62012-10-21 23:31:51 +0200264static double compute_delta(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200265{
Jiri Olsaef358e62012-10-21 23:31:51 +0200266 double old_percent = period_percent(he, he->stat.period);
267 double new_percent = period_percent(pair, pair->stat.period);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200268
Jiri Olsa9af303e2012-12-01 21:15:40 +0100269 pair->diff.period_ratio_delta = new_percent - old_percent;
270 pair->diff.computed = true;
271 return pair->diff.period_ratio_delta;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200272}
273
Jiri Olsaef358e62012-10-21 23:31:51 +0200274static double compute_ratio(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200275{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100276 double old_period = he->stat.period ?: 1;
277 double new_period = pair->stat.period;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200278
Jiri Olsa9af303e2012-12-01 21:15:40 +0100279 pair->diff.computed = true;
280 pair->diff.period_ratio = new_period / old_period;
281 return pair->diff.period_ratio;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200282}
283
Jiri Olsaef358e62012-10-21 23:31:51 +0200284static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200285{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100286 u64 old_period = he->stat.period;
287 u64 new_period = pair->stat.period;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200288
Jiri Olsa9af303e2012-12-01 21:15:40 +0100289 pair->diff.computed = true;
290 pair->diff.wdiff = new_period * compute_wdiff_w2 -
291 old_period * compute_wdiff_w1;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200292
Jiri Olsa9af303e2012-12-01 21:15:40 +0100293 return pair->diff.wdiff;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200294}
295
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100296static int formula_delta(struct hist_entry *he, struct hist_entry *pair,
297 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200298{
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900299 u64 he_total = he->hists->stats.total_period;
300 u64 pair_total = pair->hists->stats.total_period;
301
302 if (symbol_conf.filter_relative) {
303 he_total = he->hists->stats.total_non_filtered_period;
304 pair_total = pair->hists->stats.total_non_filtered_period;
305 }
Jiri Olsaed279da2012-10-05 16:44:45 +0200306 return scnprintf(buf, size,
307 "(%" PRIu64 " * 100 / %" PRIu64 ") - "
308 "(%" PRIu64 " * 100 / %" PRIu64 ")",
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900309 pair->stat.period, pair_total,
310 he->stat.period, he_total);
Jiri Olsaed279da2012-10-05 16:44:45 +0200311}
312
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100313static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,
314 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200315{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100316 double old_period = he->stat.period;
317 double new_period = pair->stat.period;
Jiri Olsaed279da2012-10-05 16:44:45 +0200318
319 return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
320}
321
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100322static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,
323 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200324{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100325 u64 old_period = he->stat.period;
326 u64 new_period = pair->stat.period;
Jiri Olsaed279da2012-10-05 16:44:45 +0200327
328 return scnprintf(buf, size,
329 "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
330 new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
331}
332
Jiri Olsaef358e62012-10-21 23:31:51 +0200333static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
334 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200335{
336 switch (compute) {
337 case COMPUTE_DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +0900338 case COMPUTE_DELTA_ABS:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100339 return formula_delta(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200340 case COMPUTE_RATIO:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100341 return formula_ratio(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200342 case COMPUTE_WEIGHTED_DIFF:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100343 return formula_wdiff(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200344 default:
345 BUG_ON(1);
346 }
347
348 return -1;
349}
350
Jin Yao99150a12019-06-28 17:23:01 +0800351static void *block_hist_zalloc(size_t size)
352{
353 struct block_hist *bh;
354
355 bh = zalloc(size + sizeof(*bh));
356 if (!bh)
357 return NULL;
358
359 return &bh->he;
360}
361
362static void block_hist_free(void *he)
363{
364 struct block_hist *bh;
365
366 bh = container_of(he, struct block_hist, he);
367 hists__delete_entries(&bh->block_hists);
368 free(bh);
369}
370
371struct hist_entry_ops block_hist_ops = {
372 .new = block_hist_zalloc,
373 .free = block_hist_free,
374};
375
Jin Yao48021382019-03-05 21:05:41 +0800376static int diff__process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200377 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200378 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200379 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200380 struct machine *machine)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200381{
Jin Yao48021382019-03-05 21:05:41 +0800382 struct perf_diff *pdiff = container_of(tool, struct perf_diff, tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200383 struct addr_location al;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300384 struct hists *hists = evsel__hists(evsel);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300385 int ret = -1;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200386
Jin Yao48021382019-03-05 21:05:41 +0800387 if (perf_time__ranges_skip_sample(pdiff->ptime_range, pdiff->range_num,
388 sample->time)) {
389 return 0;
390 }
391
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300392 if (machine__resolve(machine, &al, sample) < 0) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200393 pr_warning("problem processing %d event, skipping it.\n",
394 event->header.type);
395 return -1;
396 }
397
Jin Yaodaca23b2019-03-05 21:05:42 +0800398 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) {
399 ret = 0;
400 goto out_put;
401 }
402
Jin Yao99150a12019-06-28 17:23:01 +0800403 if (compute != COMPUTE_CYCLES) {
404 if (!hists__add_entry(hists, &al, NULL, NULL, NULL, sample,
405 true)) {
406 pr_warning("problem incrementing symbol period, "
407 "skipping event\n");
408 goto out_put;
409 }
410 } else {
411 if (!hists__add_entry_ops(hists, &block_hist_ops, &al, NULL,
412 NULL, NULL, sample, true)) {
413 pr_warning("problem incrementing symbol period, "
414 "skipping event\n");
415 goto out_put;
416 }
417
418 hist__account_cycles(sample->branch_stack, &al, sample, false);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200419 }
420
Namhyung Kim820bc812014-04-22 11:44:21 +0900421 /*
422 * The total_period is updated here before going to the output
423 * tree since normally only the baseline hists will call
424 * hists__output_resort() and precompute needs the total
425 * period in order to sort entries by percentage delta.
426 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300427 hists->stats.total_period += sample->period;
Namhyung Kim820bc812014-04-22 11:44:21 +0900428 if (!al.filtered)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300429 hists->stats.total_non_filtered_period += sample->period;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300430 ret = 0;
431out_put:
432 addr_location__put(&al);
433 return ret;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200434}
435
Jin Yao48021382019-03-05 21:05:41 +0800436static struct perf_diff pdiff = {
437 .tool = {
438 .sample = diff__process_sample_event,
439 .mmap = perf_event__process_mmap,
440 .mmap2 = perf_event__process_mmap2,
441 .comm = perf_event__process_comm,
442 .exit = perf_event__process_exit,
443 .fork = perf_event__process_fork,
444 .lost = perf_event__process_lost,
445 .namespaces = perf_event__process_namespaces,
446 .ordered_events = true,
447 .ordering_requires_timestamps = true,
448 },
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200449};
450
Jiri Olsa32dcd022019-07-21 13:23:51 +0200451static struct evsel *evsel_match(struct evsel *evsel,
Jiri Olsa863e4512012-09-06 17:46:55 +0200452 struct perf_evlist *evlist)
453{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200454 struct evsel *e;
Jiri Olsa863e4512012-09-06 17:46:55 +0200455
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300456 evlist__for_each_entry(evlist, e) {
Jiri Olsa863e4512012-09-06 17:46:55 +0200457 if (perf_evsel__match2(evsel, e))
458 return e;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300459 }
Jiri Olsa863e4512012-09-06 17:46:55 +0200460
461 return NULL;
462}
463
Namhyung Kimce74f602012-12-10 17:29:55 +0900464static void perf_evlist__collapse_resort(struct perf_evlist *evlist)
Jiri Olsadd464342012-10-04 21:49:36 +0900465{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200466 struct evsel *evsel;
Jiri Olsadd464342012-10-04 21:49:36 +0900467
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300468 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300469 struct hists *hists = evsel__hists(evsel);
Jiri Olsadd464342012-10-04 21:49:36 +0900470
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900471 hists__collapse_resort(hists, NULL);
Jiri Olsadd464342012-10-04 21:49:36 +0900472 }
473}
474
Namhyung Kimff21cef2015-01-08 09:45:45 +0900475static struct data__file *fmt_to_data_file(struct perf_hpp_fmt *fmt)
476{
477 struct diff_hpp_fmt *dfmt = container_of(fmt, struct diff_hpp_fmt, fmt);
478 void *ptr = dfmt - dfmt->idx;
479 struct data__file *d = container_of(ptr, struct data__file, fmt);
480
481 return d;
482}
483
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100484static struct hist_entry*
485get_pair_data(struct hist_entry *he, struct data__file *d)
486{
487 if (hist_entry__has_pairs(he)) {
488 struct hist_entry *pair;
489
490 list_for_each_entry(pair, &he->pairs.head, pairs.node)
491 if (pair->hists == d->hists)
492 return pair;
493 }
494
495 return NULL;
496}
497
498static struct hist_entry*
499get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
500{
Namhyung Kimff21cef2015-01-08 09:45:45 +0900501 struct data__file *d = fmt_to_data_file(&dfmt->fmt);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100502
503 return get_pair_data(he, d);
504}
505
Jiri Olsaa06d1432012-10-05 16:44:40 +0200506static void hists__baseline_only(struct hists *hists)
507{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800508 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +0900509 struct rb_node *next;
Jiri Olsaa06d1432012-10-05 16:44:40 +0200510
Jiri Olsa52225032016-05-03 13:54:42 +0200511 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +0900512 root = &hists->entries_collapsed;
513 else
514 root = hists->entries_in;
515
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800516 next = rb_first_cached(root);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200517 while (next != NULL) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900518 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200519
Namhyung Kimce74f602012-12-10 17:29:55 +0900520 next = rb_next(&he->rb_node_in);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200521 if (!hist_entry__next_pair(he)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800522 rb_erase_cached(&he->rb_node_in, root);
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300523 hist_entry__delete(he);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200524 }
525 }
526}
527
Jin Yao99150a12019-06-28 17:23:01 +0800528static int64_t block_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
529 struct hist_entry *left, struct hist_entry *right)
530{
531 struct block_info *bi_l = left->block_info;
532 struct block_info *bi_r = right->block_info;
533 int cmp;
534
535 if (!bi_l->sym || !bi_r->sym) {
536 if (!bi_l->sym && !bi_r->sym)
537 return 0;
538 else if (!bi_l->sym)
539 return -1;
540 else
541 return 1;
542 }
543
544 if (bi_l->sym == bi_r->sym) {
545 if (bi_l->start == bi_r->start) {
546 if (bi_l->end == bi_r->end)
547 return 0;
548 else
549 return (int64_t)(bi_r->end - bi_l->end);
550 } else
551 return (int64_t)(bi_r->start - bi_l->start);
552 } else {
553 cmp = strcmp(bi_l->sym->name, bi_r->sym->name);
554 return cmp;
555 }
556
557 if (bi_l->sym->start != bi_r->sym->start)
558 return (int64_t)(bi_r->sym->start - bi_l->sym->start);
559
560 return (int64_t)(bi_r->sym->end - bi_l->sym->end);
561}
562
563static int64_t block_cycles_diff_cmp(struct hist_entry *left,
564 struct hist_entry *right)
565{
566 bool pairs_left = hist_entry__has_pairs(left);
567 bool pairs_right = hist_entry__has_pairs(right);
568 s64 l, r;
569
570 if (!pairs_left && !pairs_right)
571 return 0;
572
573 l = labs(left->diff.cycles);
574 r = labs(right->diff.cycles);
575 return r - l;
576}
577
578static int64_t block_sort(struct perf_hpp_fmt *fmt __maybe_unused,
579 struct hist_entry *left, struct hist_entry *right)
580{
581 return block_cycles_diff_cmp(right, left);
582}
583
584static void init_block_hist(struct block_hist *bh)
585{
586 __hists__init(&bh->block_hists, &bh->block_list);
587 perf_hpp_list__init(&bh->block_list);
588
589 INIT_LIST_HEAD(&bh->block_fmt.list);
590 INIT_LIST_HEAD(&bh->block_fmt.sort_list);
591 bh->block_fmt.cmp = block_cmp;
592 bh->block_fmt.sort = block_sort;
593 perf_hpp_list__register_sort_field(&bh->block_list,
594 &bh->block_fmt);
595 bh->valid = true;
596}
597
598static void init_block_info(struct block_info *bi, struct symbol *sym,
599 struct cyc_hist *ch, int offset)
600{
601 bi->sym = sym;
602 bi->start = ch->start;
603 bi->end = offset;
604 bi->cycles = ch->cycles;
605 bi->cycles_aggr = ch->cycles_aggr;
606 bi->num = ch->num;
607 bi->num_aggr = ch->num_aggr;
608}
609
610static int process_block_per_sym(struct hist_entry *he)
611{
612 struct annotation *notes;
613 struct cyc_hist *ch;
614 struct block_hist *bh;
615
616 if (!he->ms.map || !he->ms.sym)
617 return 0;
618
619 notes = symbol__annotation(he->ms.sym);
620 if (!notes || !notes->src || !notes->src->cycles_hist)
621 return 0;
622
623 bh = container_of(he, struct block_hist, he);
624 init_block_hist(bh);
625
626 ch = notes->src->cycles_hist;
627 for (unsigned int i = 0; i < symbol__size(he->ms.sym); i++) {
628 if (ch[i].num_aggr) {
629 struct block_info *bi;
630 struct hist_entry *he_block;
631
632 bi = block_info__new();
633 if (!bi)
634 return -1;
635
636 init_block_info(bi, he->ms.sym, &ch[i], i);
637 he_block = hists__add_entry_block(&bh->block_hists,
638 &dummy_al, bi);
639 if (!he_block) {
640 block_info__put(bi);
641 return -1;
642 }
643 }
644 }
645
646 return 0;
647}
648
Jin Yaof38108172019-06-28 17:23:02 +0800649static int block_pair_cmp(struct hist_entry *a, struct hist_entry *b)
650{
651 struct block_info *bi_a = a->block_info;
652 struct block_info *bi_b = b->block_info;
653 int cmp;
654
655 if (!bi_a->sym || !bi_b->sym)
656 return -1;
657
658 cmp = strcmp(bi_a->sym->name, bi_b->sym->name);
659
660 if ((!cmp) && (bi_a->start == bi_b->start) && (bi_a->end == bi_b->end))
661 return 0;
662
663 return -1;
664}
665
666static struct hist_entry *get_block_pair(struct hist_entry *he,
667 struct hists *hists_pair)
668{
669 struct rb_root_cached *root = hists_pair->entries_in;
670 struct rb_node *next = rb_first_cached(root);
671 int cmp;
672
673 while (next != NULL) {
674 struct hist_entry *he_pair = rb_entry(next, struct hist_entry,
675 rb_node_in);
676
677 next = rb_next(&he_pair->rb_node_in);
678
679 cmp = block_pair_cmp(he_pair, he);
680 if (!cmp)
681 return he_pair;
682 }
683
684 return NULL;
685}
686
687static void compute_cycles_diff(struct hist_entry *he,
688 struct hist_entry *pair)
689{
690 pair->diff.computed = true;
691 if (pair->block_info->num && he->block_info->num) {
692 pair->diff.cycles =
693 pair->block_info->cycles_aggr / pair->block_info->num_aggr -
694 he->block_info->cycles_aggr / he->block_info->num_aggr;
695 }
696}
697
698static void block_hists_match(struct hists *hists_base,
699 struct hists *hists_pair)
700{
701 struct rb_root_cached *root = hists_base->entries_in;
702 struct rb_node *next = rb_first_cached(root);
703
704 while (next != NULL) {
705 struct hist_entry *he = rb_entry(next, struct hist_entry,
706 rb_node_in);
707 struct hist_entry *pair = get_block_pair(he, hists_pair);
708
709 next = rb_next(&he->rb_node_in);
710
711 if (pair) {
712 hist_entry__add_pair(pair, he);
713 compute_cycles_diff(he, pair);
714 }
715 }
716}
717
718static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
719{
720 /* Skip the calculation of column length in output_resort */
721 he->filtered = true;
722 return 0;
723}
724
Jiri Olsa96c47f12012-10-05 16:44:42 +0200725static void hists__precompute(struct hists *hists)
726{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800727 struct rb_root_cached *root;
Jiri Olsa367c53c2012-12-13 14:08:59 +0100728 struct rb_node *next;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200729
Jiri Olsa52225032016-05-03 13:54:42 +0200730 if (hists__has(hists, need_collapse))
Jiri Olsa367c53c2012-12-13 14:08:59 +0100731 root = &hists->entries_collapsed;
732 else
733 root = hists->entries_in;
734
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800735 next = rb_first_cached(root);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200736 while (next != NULL) {
Jin Yaof38108172019-06-28 17:23:02 +0800737 struct block_hist *bh, *pair_bh;
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100738 struct hist_entry *he, *pair;
Namhyung Kim56495a82015-01-08 09:45:47 +0900739 struct data__file *d;
740 int i;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200741
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100742 he = rb_entry(next, struct hist_entry, rb_node_in);
Jiri Olsa367c53c2012-12-13 14:08:59 +0100743 next = rb_next(&he->rb_node_in);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100744
Jin Yao99150a12019-06-28 17:23:01 +0800745 if (compute == COMPUTE_CYCLES)
746 process_block_per_sym(he);
747
Namhyung Kim56495a82015-01-08 09:45:47 +0900748 data__for_each_file_new(i, d) {
749 pair = get_pair_data(he, d);
750 if (!pair)
751 continue;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200752
Namhyung Kim56495a82015-01-08 09:45:47 +0900753 switch (compute) {
754 case COMPUTE_DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +0900755 case COMPUTE_DELTA_ABS:
Namhyung Kim56495a82015-01-08 09:45:47 +0900756 compute_delta(he, pair);
757 break;
758 case COMPUTE_RATIO:
759 compute_ratio(he, pair);
760 break;
761 case COMPUTE_WEIGHTED_DIFF:
762 compute_wdiff(he, pair);
763 break;
Jin Yao99150a12019-06-28 17:23:01 +0800764 case COMPUTE_CYCLES:
765 process_block_per_sym(pair);
Jin Yaof38108172019-06-28 17:23:02 +0800766 bh = container_of(he, struct block_hist, he);
767 pair_bh = container_of(pair, struct block_hist,
768 he);
769
770 if (bh->valid && pair_bh->valid) {
771 block_hists_match(&bh->block_hists,
772 &pair_bh->block_hists);
773 hists__output_resort_cb(&pair_bh->block_hists,
774 NULL, filter_cb);
775 }
Jin Yao99150a12019-06-28 17:23:01 +0800776 break;
Namhyung Kim56495a82015-01-08 09:45:47 +0900777 default:
778 BUG_ON(1);
779 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200780 }
781 }
782}
783
784static int64_t cmp_doubles(double l, double r)
785{
786 if (l > r)
787 return -1;
788 else if (l < r)
789 return 1;
790 else
791 return 0;
792}
793
794static int64_t
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100795__hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
Jiri Olsa96c47f12012-10-05 16:44:42 +0200796 int c)
797{
798 switch (c) {
799 case COMPUTE_DELTA:
800 {
801 double l = left->diff.period_ratio_delta;
802 double r = right->diff.period_ratio_delta;
803
804 return cmp_doubles(l, r);
805 }
Namhyung Kima1668c22017-02-10 16:36:11 +0900806 case COMPUTE_DELTA_ABS:
807 {
808 double l = fabs(left->diff.period_ratio_delta);
809 double r = fabs(right->diff.period_ratio_delta);
810
811 return cmp_doubles(l, r);
812 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200813 case COMPUTE_RATIO:
814 {
815 double l = left->diff.period_ratio;
816 double r = right->diff.period_ratio;
817
818 return cmp_doubles(l, r);
819 }
Jiri Olsa81d5f952012-10-05 16:44:43 +0200820 case COMPUTE_WEIGHTED_DIFF:
821 {
822 s64 l = left->diff.wdiff;
823 s64 r = right->diff.wdiff;
824
825 return r - l;
826 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200827 default:
828 BUG_ON(1);
829 }
830
831 return 0;
832}
833
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100834static int64_t
835hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
Namhyung Kim56495a82015-01-08 09:45:47 +0900836 int c, int sort_idx)
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100837{
838 bool pairs_left = hist_entry__has_pairs(left);
839 bool pairs_right = hist_entry__has_pairs(right);
840 struct hist_entry *p_right, *p_left;
841
842 if (!pairs_left && !pairs_right)
843 return 0;
844
845 if (!pairs_left || !pairs_right)
846 return pairs_left ? -1 : 1;
847
Namhyung Kim56495a82015-01-08 09:45:47 +0900848 p_left = get_pair_data(left, &data__files[sort_idx]);
849 p_right = get_pair_data(right, &data__files[sort_idx]);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100850
851 if (!p_left && !p_right)
852 return 0;
853
854 if (!p_left || !p_right)
855 return p_left ? -1 : 1;
856
857 /*
858 * We have 2 entries of same kind, let's
859 * make the data comparison.
860 */
861 return __hist_entry__cmp_compute(p_left, p_right, c);
862}
863
Namhyung Kime7024fc2014-12-27 14:06:29 +0900864static int64_t
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900865hist_entry__cmp_compute_idx(struct hist_entry *left, struct hist_entry *right,
866 int c, int sort_idx)
867{
868 struct hist_entry *p_right, *p_left;
869
870 p_left = get_pair_data(left, &data__files[sort_idx]);
871 p_right = get_pair_data(right, &data__files[sort_idx]);
872
873 if (!p_left && !p_right)
874 return 0;
875
876 if (!p_left || !p_right)
877 return p_left ? -1 : 1;
878
Namhyung Kima1668c22017-02-10 16:36:11 +0900879 if (c != COMPUTE_DELTA && c != COMPUTE_DELTA_ABS) {
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900880 /*
881 * The delta can be computed without the baseline, but
882 * others are not. Put those entries which have no
883 * values below.
884 */
885 if (left->dummy && right->dummy)
886 return 0;
887
888 if (left->dummy || right->dummy)
889 return left->dummy ? 1 : -1;
890 }
891
892 return __hist_entry__cmp_compute(p_left, p_right, c);
893}
894
895static int64_t
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900896hist_entry__cmp_nop(struct perf_hpp_fmt *fmt __maybe_unused,
897 struct hist_entry *left __maybe_unused,
Namhyung Kime7024fc2014-12-27 14:06:29 +0900898 struct hist_entry *right __maybe_unused)
899{
900 return 0;
901}
902
903static int64_t
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900904hist_entry__cmp_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
905 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900906{
Namhyung Kime7024fc2014-12-27 14:06:29 +0900907 if (left->stat.period == right->stat.period)
908 return 0;
909 return left->stat.period > right->stat.period ? 1 : -1;
910}
911
912static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900913hist_entry__cmp_delta(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900914 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900915{
Namhyung Kim56495a82015-01-08 09:45:47 +0900916 struct data__file *d = fmt_to_data_file(fmt);
917
918 return hist_entry__cmp_compute(right, left, COMPUTE_DELTA, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900919}
920
921static int64_t
Namhyung Kima1668c22017-02-10 16:36:11 +0900922hist_entry__cmp_delta_abs(struct perf_hpp_fmt *fmt,
923 struct hist_entry *left, struct hist_entry *right)
924{
925 struct data__file *d = fmt_to_data_file(fmt);
926
927 return hist_entry__cmp_compute(right, left, COMPUTE_DELTA_ABS, d->idx);
928}
929
930static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900931hist_entry__cmp_ratio(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900932 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900933{
Namhyung Kim56495a82015-01-08 09:45:47 +0900934 struct data__file *d = fmt_to_data_file(fmt);
935
936 return hist_entry__cmp_compute(right, left, COMPUTE_RATIO, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900937}
938
939static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900940hist_entry__cmp_wdiff(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900941 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900942{
Namhyung Kim56495a82015-01-08 09:45:47 +0900943 struct data__file *d = fmt_to_data_file(fmt);
944
945 return hist_entry__cmp_compute(right, left, COMPUTE_WEIGHTED_DIFF, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900946}
947
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900948static int64_t
949hist_entry__cmp_delta_idx(struct perf_hpp_fmt *fmt __maybe_unused,
950 struct hist_entry *left, struct hist_entry *right)
951{
952 return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA,
953 sort_compute);
954}
955
956static int64_t
Namhyung Kima1668c22017-02-10 16:36:11 +0900957hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt *fmt __maybe_unused,
958 struct hist_entry *left, struct hist_entry *right)
959{
960 return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA_ABS,
961 sort_compute);
962}
963
964static int64_t
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900965hist_entry__cmp_ratio_idx(struct perf_hpp_fmt *fmt __maybe_unused,
966 struct hist_entry *left, struct hist_entry *right)
967{
968 return hist_entry__cmp_compute_idx(right, left, COMPUTE_RATIO,
969 sort_compute);
970}
971
972static int64_t
973hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt *fmt __maybe_unused,
974 struct hist_entry *left, struct hist_entry *right)
975{
976 return hist_entry__cmp_compute_idx(right, left, COMPUTE_WEIGHTED_DIFF,
977 sort_compute);
978}
979
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100980static void hists__process(struct hists *hists)
Jiri Olsaa06d1432012-10-05 16:44:40 +0200981{
Jiri Olsaa06d1432012-10-05 16:44:40 +0200982 if (show_baseline_only)
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100983 hists__baseline_only(hists);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200984
Namhyung Kim56495a82015-01-08 09:45:47 +0900985 hists__precompute(hists);
Namhyung Kim38259a12014-12-27 14:06:30 +0900986 hists__output_resort(hists, NULL);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200987
Jin Yaob10c78c2019-06-28 17:23:03 +0800988 if (compute == COMPUTE_CYCLES)
989 symbol_conf.report_block = true;
990
Namhyung Kim63b42fc2017-02-17 17:17:40 +0900991 hists__fprintf(hists, !quiet, 0, 0, 0, stdout,
Arnaldo Carvalho de Meloe9de7e22018-06-20 15:58:20 -0300992 !symbol_conf.use_callchain);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200993}
994
Jiri Olsa1d81c7f2012-12-01 21:56:03 +0100995static void data__fprintf(void)
996{
997 struct data__file *d;
998 int i;
999
1000 fprintf(stdout, "# Data files:\n");
1001
1002 data__for_each_file(i, d)
1003 fprintf(stdout, "# [%d] %s %s\n",
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001004 d->idx, d->data.path,
Jiri Olsa1d81c7f2012-12-01 21:56:03 +01001005 !d->idx ? "(Baseline)" : "");
1006
1007 fprintf(stdout, "#\n");
1008}
1009
Jiri Olsaec308422013-03-25 00:02:01 +01001010static void data_process(void)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001011{
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001012 struct perf_evlist *evlist_base = data__files[0].session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001013 struct evsel *evsel_base;
Jiri Olsa863e4512012-09-06 17:46:55 +02001014 bool first = true;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001015
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001016 evlist__for_each_entry(evlist_base, evsel_base) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001017 struct hists *hists_base = evsel__hists(evsel_base);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001018 struct data__file *d;
1019 int i;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001020
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001021 data__for_each_file_new(i, d) {
1022 struct perf_evlist *evlist = d->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001023 struct evsel *evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001024 struct hists *hists;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001025
1026 evsel = evsel_match(evsel_base, evlist);
1027 if (!evsel)
1028 continue;
1029
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001030 hists = evsel__hists(evsel);
1031 d->hists = hists;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001032
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001033 hists__match(hists_base, hists);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001034
1035 if (!show_baseline_only)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001036 hists__link(hists_base, hists);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001037 }
Jiri Olsa863e4512012-09-06 17:46:55 +02001038
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001039 if (!quiet) {
1040 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
1041 perf_evsel__name(evsel_base));
1042 }
Jiri Olsa863e4512012-09-06 17:46:55 +02001043
1044 first = false;
1045
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001046 if (verbose > 0 || ((data__files_cnt > 2) && !quiet))
Jiri Olsa1d81c7f2012-12-01 21:56:03 +01001047 data__fprintf();
1048
Kan Liangf9db0d02015-08-11 06:30:48 -04001049 /* Don't sort callchain for perf diff */
1050 perf_evsel__reset_sample_bit(evsel_base, CALLCHAIN);
1051
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001052 hists__process(hists_base);
Jiri Olsaec308422013-03-25 00:02:01 +01001053 }
1054}
1055
Jiri Olsac818b492012-12-01 21:57:04 +01001056static void data__free(struct data__file *d)
1057{
1058 int col;
1059
1060 for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
1061 struct diff_hpp_fmt *fmt = &d->fmt[col];
1062
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001063 zfree(&fmt->header);
Jiri Olsac818b492012-12-01 21:57:04 +01001064 }
1065}
1066
Jin Yao48021382019-03-05 21:05:41 +08001067static int abstime_str_dup(char **pstr)
1068{
1069 char *str = NULL;
1070
1071 if (pdiff.time_str && strchr(pdiff.time_str, ':')) {
1072 str = strdup(pdiff.time_str);
1073 if (!str)
1074 return -ENOMEM;
1075 }
1076
1077 *pstr = str;
1078 return 0;
1079}
1080
1081static int parse_absolute_time(struct data__file *d, char **pstr)
1082{
1083 char *p = *pstr;
1084 int ret;
1085
1086 /*
1087 * Absolute timestamp for one file has the format: a.b,c.d
1088 * For multiple files, the format is: a.b,c.d:a.b,c.d
1089 */
1090 p = strchr(*pstr, ':');
1091 if (p) {
1092 if (p == *pstr) {
1093 pr_err("Invalid time string\n");
1094 return -EINVAL;
1095 }
1096
1097 *p = 0;
1098 p++;
1099 if (*p == 0) {
1100 pr_err("Invalid time string\n");
1101 return -EINVAL;
1102 }
1103 }
1104
1105 ret = perf_time__parse_for_ranges(*pstr, d->session,
1106 &pdiff.ptime_range,
1107 &pdiff.range_size,
1108 &pdiff.range_num);
1109 if (ret < 0)
1110 return ret;
1111
1112 if (!p || *p == 0)
1113 *pstr = NULL;
1114 else
1115 *pstr = p;
1116
1117 return ret;
1118}
1119
1120static int parse_percent_time(struct data__file *d)
1121{
1122 int ret;
1123
1124 ret = perf_time__parse_for_ranges(pdiff.time_str, d->session,
1125 &pdiff.ptime_range,
1126 &pdiff.range_size,
1127 &pdiff.range_num);
1128 return ret;
1129}
1130
1131static int parse_time_str(struct data__file *d, char *abstime_ostr,
1132 char **pabstime_tmp)
1133{
1134 int ret = 0;
1135
1136 if (abstime_ostr)
1137 ret = parse_absolute_time(d, pabstime_tmp);
1138 else if (pdiff.time_str)
1139 ret = parse_percent_time(d);
1140
1141 return ret;
1142}
1143
Jin Yao30d81552019-06-28 17:23:00 +08001144static int check_file_brstack(void)
1145{
1146 struct data__file *d;
1147 bool has_br_stack;
1148 int i;
1149
1150 data__for_each_file(i, d) {
1151 d->session = perf_session__new(&d->data, false, &pdiff.tool);
1152 if (!d->session) {
1153 pr_err("Failed to open %s\n", d->data.path);
1154 return -1;
1155 }
1156
1157 has_br_stack = perf_header__has_feat(&d->session->header,
1158 HEADER_BRANCH_STACK);
1159 perf_session__delete(d->session);
1160 if (!has_br_stack)
1161 return 0;
1162 }
1163
1164 /* Set only all files having branch stacks */
1165 pdiff.has_br_stack = true;
1166 return 0;
1167}
1168
Jiri Olsaec308422013-03-25 00:02:01 +01001169static int __cmd_diff(void)
1170{
1171 struct data__file *d;
Jin Yao48021382019-03-05 21:05:41 +08001172 int ret, i;
1173 char *abstime_ostr, *abstime_tmp;
1174
1175 ret = abstime_str_dup(&abstime_ostr);
1176 if (ret)
1177 return ret;
1178
1179 abstime_tmp = abstime_ostr;
1180 ret = -EINVAL;
Jiri Olsaec308422013-03-25 00:02:01 +01001181
1182 data__for_each_file(i, d) {
Jin Yao48021382019-03-05 21:05:41 +08001183 d->session = perf_session__new(&d->data, false, &pdiff.tool);
Jiri Olsaec308422013-03-25 00:02:01 +01001184 if (!d->session) {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001185 pr_err("Failed to open %s\n", d->data.path);
Taeung Song52e028342014-09-24 10:33:37 +09001186 ret = -1;
Jiri Olsaec308422013-03-25 00:02:01 +01001187 goto out_delete;
1188 }
1189
Jin Yao48021382019-03-05 21:05:41 +08001190 if (pdiff.time_str) {
1191 ret = parse_time_str(d, abstime_ostr, &abstime_tmp);
1192 if (ret < 0)
1193 goto out_delete;
1194 }
1195
Jin Yaodaca23b2019-03-05 21:05:42 +08001196 if (cpu_list) {
1197 ret = perf_session__cpu_bitmap(d->session, cpu_list,
1198 cpu_bitmap);
1199 if (ret < 0)
1200 goto out_delete;
1201 }
1202
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001203 ret = perf_session__process_events(d->session);
Jiri Olsaec308422013-03-25 00:02:01 +01001204 if (ret) {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001205 pr_err("Failed to process %s\n", d->data.path);
Jiri Olsaec308422013-03-25 00:02:01 +01001206 goto out_delete;
1207 }
1208
1209 perf_evlist__collapse_resort(d->session->evlist);
Jin Yao48021382019-03-05 21:05:41 +08001210
1211 if (pdiff.ptime_range)
1212 zfree(&pdiff.ptime_range);
Jiri Olsa863e4512012-09-06 17:46:55 +02001213 }
1214
Jiri Olsaec308422013-03-25 00:02:01 +01001215 data_process();
1216
1217 out_delete:
1218 data__for_each_file(i, d) {
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001219 perf_session__delete(d->session);
Jiri Olsac818b492012-12-01 21:57:04 +01001220 data__free(d);
Jiri Olsaec308422013-03-25 00:02:01 +01001221 }
1222
1223 free(data__files);
Jin Yao48021382019-03-05 21:05:41 +08001224
1225 if (pdiff.ptime_range)
1226 zfree(&pdiff.ptime_range);
1227
1228 if (abstime_ostr)
1229 free(abstime_ostr);
1230
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001231 return ret;
1232}
1233
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -02001234static const char * const diff_usage[] = {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001235 "perf diff [<options>] [old_file] [new_file]",
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -02001236 NULL,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001237};
1238
1239static const struct option options[] = {
Ian Munsiec0555642010-04-13 18:37:33 +10001240 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001241 "be more verbose (show symbol address, etc)"),
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001242 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
Jiri Olsaa06d1432012-10-05 16:44:40 +02001243 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
1244 "Show only items with match in baseline"),
Jiri Olsa81d5f952012-10-05 16:44:43 +02001245 OPT_CALLBACK('c', "compute", &compute,
Jin Yaob10c78c2019-06-28 17:23:03 +08001246 "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs),cycles",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001247 "Entries differential computation selection",
1248 setup_compute),
Jiri Olsa61949b22012-10-05 16:44:44 +02001249 OPT_BOOLEAN('p', "period", &show_period,
1250 "Show period values."),
Jiri Olsaed279da2012-10-05 16:44:45 +02001251 OPT_BOOLEAN('F', "formula", &show_formula,
1252 "Show formula."),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001253 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1254 "dump raw trace in ASCII"),
1255 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
David Ahern6b1f3422015-03-24 09:51:57 -06001256 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1257 "file", "kallsyms pathname"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001258 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
1259 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001260 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1261 "only consider symbols in these dsos"),
1262 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1263 "only consider symbols in these comms"),
1264 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1265 "only consider these symbols"),
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001266 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Namhyung Kima2ce0672014-03-04 09:06:42 +09001267 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
1268 " Please refer the man page for the complete list."),
Wang Nan8b8ca6e2015-03-20 02:57:52 +00001269 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001270 "separator for columns, no spaces will be added between "
1271 "columns '.' is reserved."),
He Kuanga7066702016-05-19 11:47:37 +00001272 OPT_CALLBACK(0, "symfs", NULL, "directory",
1273 "Look for files with symbols relative to this directory",
1274 symbol__config_symfs),
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001275 OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
Namhyung Kim8810f6c2014-02-07 12:06:07 +09001276 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1277 "How to display percentage of filtered entries", parse_filter_percentage),
Jin Yao48021382019-03-05 21:05:41 +08001278 OPT_STRING(0, "time", &pdiff.time_str, "str",
1279 "Time span (time percent or absolute timestamp)"),
Jin Yaodaca23b2019-03-05 21:05:42 +08001280 OPT_STRING(0, "cpu", &cpu_list, "cpu", "list of cpus to profile"),
Jin Yaoc1d3e632019-03-05 21:05:43 +08001281 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1282 "only consider symbols in these pids"),
1283 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1284 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001285 OPT_END()
1286};
1287
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001288static double baseline_percent(struct hist_entry *he)
Jiri Olsa1d778222012-10-04 21:49:39 +09001289{
Namhyung Kim8810f6c2014-02-07 12:06:07 +09001290 u64 total = hists__total_period(he->hists);
1291
1292 return 100.0 * he->stat.period / total;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001293}
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001294
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001295static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
1296 struct perf_hpp *hpp, struct hist_entry *he)
1297{
1298 struct diff_hpp_fmt *dfmt =
1299 container_of(fmt, struct diff_hpp_fmt, fmt);
1300 double percent = baseline_percent(he);
1301 char pfmt[20] = " ";
1302
1303 if (!he->dummy) {
1304 scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
1305 return percent_color_snprintf(hpp->buf, hpp->size,
1306 pfmt, percent);
1307 } else
1308 return scnprintf(hpp->buf, hpp->size, "%*s",
1309 dfmt->header_width, pfmt);
1310}
1311
1312static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
1313{
1314 double percent = baseline_percent(he);
1315 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
1316 int ret = 0;
1317
1318 if (!he->dummy)
1319 ret = scnprintf(buf, size, fmt, percent);
1320
1321 return ret;
1322}
1323
Jin Yaob10c78c2019-06-28 17:23:03 +08001324static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
1325 struct perf_hpp *hpp, int width)
1326{
1327 struct block_hist *bh = container_of(he, struct block_hist, he);
1328 struct block_hist *bh_pair = container_of(pair, struct block_hist, he);
1329 struct hist_entry *block_he;
1330 struct block_info *bi;
1331 char buf[128];
1332 char *start_line, *end_line;
1333
1334 block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx);
1335 if (!block_he) {
1336 hpp->skip = true;
1337 return 0;
1338 }
1339
1340 /*
1341 * Avoid printing the warning "addr2line_init failed for ..."
1342 */
1343 symbol_conf.disable_add2line_warn = true;
1344
1345 bi = block_he->block_info;
1346
1347 start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
1348 he->ms.sym);
1349
1350 end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
1351 he->ms.sym);
1352
1353 if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
1354 scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld",
1355 start_line, end_line, block_he->diff.cycles);
1356 } else {
1357 scnprintf(buf, sizeof(buf), "[%7lx -> %7lx] %4ld",
1358 bi->start, bi->end, block_he->diff.cycles);
1359 }
1360
1361 free_srcline(start_line);
1362 free_srcline(end_line);
1363
1364 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1365}
1366
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301367static int __hpp__color_compare(struct perf_hpp_fmt *fmt,
1368 struct perf_hpp *hpp, struct hist_entry *he,
1369 int comparison_method)
1370{
1371 struct diff_hpp_fmt *dfmt =
1372 container_of(fmt, struct diff_hpp_fmt, fmt);
1373 struct hist_entry *pair = get_pair_fmt(he, dfmt);
1374 double diff;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301375 s64 wdiff;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301376 char pfmt[20] = " ";
1377
Jin Yaob10c78c2019-06-28 17:23:03 +08001378 if (!pair) {
1379 if (comparison_method == COMPUTE_CYCLES) {
1380 struct block_hist *bh;
1381
1382 bh = container_of(he, struct block_hist, he);
1383 if (bh->block_idx)
1384 hpp->skip = true;
1385 }
1386
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001387 goto no_print;
Jin Yaob10c78c2019-06-28 17:23:03 +08001388 }
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301389
1390 switch (comparison_method) {
1391 case COMPUTE_DELTA:
1392 if (pair->diff.computed)
1393 diff = pair->diff.period_ratio_delta;
1394 else
1395 diff = compute_delta(he, pair);
1396
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301397 scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1);
1398 return percent_color_snprintf(hpp->buf, hpp->size,
1399 pfmt, diff);
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301400 case COMPUTE_RATIO:
1401 if (he->dummy)
1402 goto dummy_print;
1403 if (pair->diff.computed)
1404 diff = pair->diff.period_ratio;
1405 else
1406 diff = compute_ratio(he, pair);
1407
1408 scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width);
1409 return value_color_snprintf(hpp->buf, hpp->size,
1410 pfmt, diff);
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301411 case COMPUTE_WEIGHTED_DIFF:
1412 if (he->dummy)
1413 goto dummy_print;
1414 if (pair->diff.computed)
1415 wdiff = pair->diff.wdiff;
1416 else
1417 wdiff = compute_wdiff(he, pair);
1418
1419 scnprintf(pfmt, 20, "%%14ld", dfmt->header_width);
1420 return color_snprintf(hpp->buf, hpp->size,
1421 get_percent_color(wdiff),
1422 pfmt, wdiff);
Jin Yaob10c78c2019-06-28 17:23:03 +08001423 case COMPUTE_CYCLES:
1424 return cycles_printf(he, pair, hpp, dfmt->header_width);
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301425 default:
1426 BUG_ON(1);
1427 }
1428dummy_print:
1429 return scnprintf(hpp->buf, hpp->size, "%*s",
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001430 dfmt->header_width, "N/A");
1431no_print:
1432 return scnprintf(hpp->buf, hpp->size, "%*s",
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301433 dfmt->header_width, pfmt);
1434}
1435
1436static int hpp__color_delta(struct perf_hpp_fmt *fmt,
1437 struct perf_hpp *hpp, struct hist_entry *he)
1438{
1439 return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA);
1440}
1441
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301442static int hpp__color_ratio(struct perf_hpp_fmt *fmt,
1443 struct perf_hpp *hpp, struct hist_entry *he)
1444{
1445 return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO);
1446}
1447
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301448static int hpp__color_wdiff(struct perf_hpp_fmt *fmt,
1449 struct perf_hpp *hpp, struct hist_entry *he)
1450{
1451 return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF);
1452}
1453
Jin Yaob10c78c2019-06-28 17:23:03 +08001454static int hpp__color_cycles(struct perf_hpp_fmt *fmt,
1455 struct perf_hpp *hpp, struct hist_entry *he)
1456{
1457 return __hpp__color_compare(fmt, hpp, he, COMPUTE_CYCLES);
1458}
1459
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001460static void
1461hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
1462{
1463 switch (idx) {
1464 case PERF_HPP_DIFF__PERIOD_BASELINE:
1465 scnprintf(buf, size, "%" PRIu64, he->stat.period);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001466 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001467
1468 default:
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001469 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001470 }
1471}
1472
1473static void
1474hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
1475 int idx, char *buf, size_t size)
1476{
1477 double diff;
1478 double ratio;
1479 s64 wdiff;
1480
1481 switch (idx) {
1482 case PERF_HPP_DIFF__DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +09001483 case PERF_HPP_DIFF__DELTA_ABS:
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001484 if (pair->diff.computed)
1485 diff = pair->diff.period_ratio_delta;
1486 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001487 diff = compute_delta(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001488
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001489 scnprintf(buf, size, "%+4.2F%%", diff);
Jiri Olsa81d5f952012-10-05 16:44:43 +02001490 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001491
1492 case PERF_HPP_DIFF__RATIO:
1493 /* No point for ratio number if we are dummy.. */
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001494 if (he->dummy) {
1495 scnprintf(buf, size, "N/A");
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001496 break;
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001497 }
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001498
1499 if (pair->diff.computed)
1500 ratio = pair->diff.period_ratio;
1501 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001502 ratio = compute_ratio(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001503
1504 if (ratio > 0.0)
1505 scnprintf(buf, size, "%14.6F", ratio);
1506 break;
1507
1508 case PERF_HPP_DIFF__WEIGHTED_DIFF:
1509 /* No point for wdiff number if we are dummy.. */
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001510 if (he->dummy) {
1511 scnprintf(buf, size, "N/A");
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001512 break;
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001513 }
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001514
1515 if (pair->diff.computed)
1516 wdiff = pair->diff.wdiff;
1517 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001518 wdiff = compute_wdiff(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001519
1520 if (wdiff != 0)
1521 scnprintf(buf, size, "%14ld", wdiff);
1522 break;
1523
1524 case PERF_HPP_DIFF__FORMULA:
Jiri Olsaef358e62012-10-21 23:31:51 +02001525 formula_fprintf(he, pair, buf, size);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001526 break;
1527
1528 case PERF_HPP_DIFF__PERIOD:
1529 scnprintf(buf, size, "%" PRIu64, pair->stat.period);
1530 break;
1531
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001532 default:
1533 BUG_ON(1);
1534 };
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001535}
1536
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001537static void
1538__hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
1539 char *buf, size_t size)
1540{
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001541 struct hist_entry *pair = get_pair_fmt(he, dfmt);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001542 int idx = dfmt->idx;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001543
1544 /* baseline is special */
1545 if (idx == PERF_HPP_DIFF__BASELINE)
1546 hpp__entry_baseline(he, buf, size);
1547 else {
1548 if (pair)
1549 hpp__entry_pair(he, pair, idx, buf, size);
1550 else
1551 hpp__entry_unpair(he, idx, buf, size);
1552 }
1553}
1554
1555static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
1556 struct hist_entry *he)
1557{
1558 struct diff_hpp_fmt *dfmt =
1559 container_of(_fmt, struct diff_hpp_fmt, fmt);
1560 char buf[MAX_COL_WIDTH] = " ";
1561
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001562 __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001563
1564 if (symbol_conf.field_sep)
1565 return scnprintf(hpp->buf, hpp->size, "%s", buf);
1566 else
1567 return scnprintf(hpp->buf, hpp->size, "%*s",
1568 dfmt->header_width, buf);
1569}
1570
Namhyung Kim94a07932014-03-10 16:43:52 +09001571static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsa74bb43f2016-08-07 17:28:27 +02001572 struct hists *hists __maybe_unused,
Jiri Olsa29659ab2016-08-07 17:28:30 +02001573 int line __maybe_unused,
1574 int *span __maybe_unused)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001575{
1576 struct diff_hpp_fmt *dfmt =
1577 container_of(fmt, struct diff_hpp_fmt, fmt);
1578
1579 BUG_ON(!dfmt->header);
1580 return scnprintf(hpp->buf, hpp->size, dfmt->header);
1581}
1582
1583static int hpp__width(struct perf_hpp_fmt *fmt,
Namhyung Kim94a07932014-03-10 16:43:52 +09001584 struct perf_hpp *hpp __maybe_unused,
Jiri Olsada1b0402016-06-14 20:19:20 +02001585 struct hists *hists __maybe_unused)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001586{
1587 struct diff_hpp_fmt *dfmt =
1588 container_of(fmt, struct diff_hpp_fmt, fmt);
1589
1590 BUG_ON(dfmt->header_width <= 0);
1591 return dfmt->header_width;
1592}
1593
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001594static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001595{
1596#define MAX_HEADER_NAME 100
1597 char buf_indent[MAX_HEADER_NAME];
1598 char buf[MAX_HEADER_NAME];
1599 const char *header = NULL;
1600 int width = 0;
1601
1602 BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
1603 header = columns[dfmt->idx].name;
1604 width = columns[dfmt->idx].width;
1605
1606 /* Only our defined HPP fmts should appear here. */
1607 BUG_ON(!header);
1608
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001609 if (data__files_cnt > 2)
1610 scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
1611
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001612#define NAME (data__files_cnt > 2 ? buf : header)
1613 dfmt->header_width = width;
1614 width = (int) strlen(NAME);
1615 if (dfmt->header_width < width)
1616 dfmt->header_width = width;
1617
1618 scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
1619 dfmt->header_width, NAME);
1620
1621 dfmt->header = strdup(buf_indent);
1622#undef MAX_HEADER_NAME
1623#undef NAME
1624}
1625
Jiri Olsac818b492012-12-01 21:57:04 +01001626static void data__hpp_register(struct data__file *d, int idx)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001627{
Jiri Olsac818b492012-12-01 21:57:04 +01001628 struct diff_hpp_fmt *dfmt = &d->fmt[idx];
1629 struct perf_hpp_fmt *fmt = &dfmt->fmt;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001630
Jiri Olsac818b492012-12-01 21:57:04 +01001631 dfmt->idx = idx;
1632
1633 fmt->header = hpp__header;
1634 fmt->width = hpp__width;
1635 fmt->entry = hpp__entry_global;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001636 fmt->cmp = hist_entry__cmp_nop;
1637 fmt->collapse = hist_entry__cmp_nop;
Jiri Olsac818b492012-12-01 21:57:04 +01001638
1639 /* TODO more colors */
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301640 switch (idx) {
1641 case PERF_HPP_DIFF__BASELINE:
Jiri Olsac818b492012-12-01 21:57:04 +01001642 fmt->color = hpp__color_baseline;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001643 fmt->sort = hist_entry__cmp_baseline;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301644 break;
1645 case PERF_HPP_DIFF__DELTA:
1646 fmt->color = hpp__color_delta;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001647 fmt->sort = hist_entry__cmp_delta;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301648 break;
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301649 case PERF_HPP_DIFF__RATIO:
1650 fmt->color = hpp__color_ratio;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001651 fmt->sort = hist_entry__cmp_ratio;
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301652 break;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301653 case PERF_HPP_DIFF__WEIGHTED_DIFF:
1654 fmt->color = hpp__color_wdiff;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001655 fmt->sort = hist_entry__cmp_wdiff;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301656 break;
Namhyung Kima1668c22017-02-10 16:36:11 +09001657 case PERF_HPP_DIFF__DELTA_ABS:
1658 fmt->color = hpp__color_delta;
1659 fmt->sort = hist_entry__cmp_delta_abs;
1660 break;
Jin Yaob10c78c2019-06-28 17:23:03 +08001661 case PERF_HPP_DIFF__CYCLES:
1662 fmt->color = hpp__color_cycles;
1663 fmt->sort = hist_entry__cmp_nop;
1664 break;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301665 default:
Namhyung Kime7024fc2014-12-27 14:06:29 +09001666 fmt->sort = hist_entry__cmp_nop;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301667 break;
1668 }
Jiri Olsac818b492012-12-01 21:57:04 +01001669
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001670 init_header(d, dfmt);
Jiri Olsac818b492012-12-01 21:57:04 +01001671 perf_hpp__column_register(fmt);
Namhyung Kime7024fc2014-12-27 14:06:29 +09001672 perf_hpp__register_sort_field(fmt);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001673}
1674
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001675static int ui_init(void)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001676{
Jiri Olsac818b492012-12-01 21:57:04 +01001677 struct data__file *d;
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001678 struct perf_hpp_fmt *fmt;
Jiri Olsac818b492012-12-01 21:57:04 +01001679 int i;
Jiri Olsa1d778222012-10-04 21:49:39 +09001680
Jiri Olsac818b492012-12-01 21:57:04 +01001681 data__for_each_file(i, d) {
Jiri Olsaed279da2012-10-05 16:44:45 +02001682
Jiri Olsac818b492012-12-01 21:57:04 +01001683 /*
1684 * Baseline or compute realted columns:
1685 *
1686 * PERF_HPP_DIFF__BASELINE
1687 * PERF_HPP_DIFF__DELTA
1688 * PERF_HPP_DIFF__RATIO
1689 * PERF_HPP_DIFF__WEIGHTED_DIFF
1690 */
1691 data__hpp_register(d, i ? compute_2_hpp[compute] :
1692 PERF_HPP_DIFF__BASELINE);
1693
1694 /*
1695 * And the rest:
1696 *
1697 * PERF_HPP_DIFF__FORMULA
1698 * PERF_HPP_DIFF__PERIOD
1699 * PERF_HPP_DIFF__PERIOD_BASELINE
1700 */
1701 if (show_formula && i)
1702 data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
1703
1704 if (show_period)
1705 data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
1706 PERF_HPP_DIFF__PERIOD_BASELINE);
Jiri Olsa61949b22012-10-05 16:44:44 +02001707 }
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001708
1709 if (!sort_compute)
1710 return 0;
1711
1712 /*
1713 * Prepend an fmt to sort on columns at 'sort_compute' first.
1714 * This fmt is added only to the sort list but not to the
1715 * output fields list.
1716 *
1717 * Note that this column (data) can be compared twice - one
1718 * for this 'sort_compute' fmt and another for the normal
1719 * diff_hpp_fmt. But it shouldn't a problem as most entries
1720 * will be sorted out by first try or baseline and comparing
1721 * is not a costly operation.
1722 */
1723 fmt = zalloc(sizeof(*fmt));
1724 if (fmt == NULL) {
1725 pr_err("Memory allocation failed\n");
1726 return -1;
1727 }
1728
1729 fmt->cmp = hist_entry__cmp_nop;
1730 fmt->collapse = hist_entry__cmp_nop;
1731
1732 switch (compute) {
1733 case COMPUTE_DELTA:
1734 fmt->sort = hist_entry__cmp_delta_idx;
1735 break;
1736 case COMPUTE_RATIO:
1737 fmt->sort = hist_entry__cmp_ratio_idx;
1738 break;
1739 case COMPUTE_WEIGHTED_DIFF:
1740 fmt->sort = hist_entry__cmp_wdiff_idx;
1741 break;
Namhyung Kima1668c22017-02-10 16:36:11 +09001742 case COMPUTE_DELTA_ABS:
1743 fmt->sort = hist_entry__cmp_delta_abs_idx;
1744 break;
Jin Yao99150a12019-06-28 17:23:01 +08001745 case COMPUTE_CYCLES:
1746 /*
1747 * Should set since 'fmt->sort' is called without
1748 * checking valid during sorting
1749 */
1750 fmt->sort = hist_entry__cmp_nop;
1751 break;
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001752 default:
1753 BUG_ON(1);
1754 }
1755
Namhyung Kima1c9f972017-01-18 14:14:57 +09001756 perf_hpp__prepend_sort_field(fmt);
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001757 return 0;
Jiri Olsa1d778222012-10-04 21:49:39 +09001758}
1759
Jiri Olsaec308422013-03-25 00:02:01 +01001760static int data_init(int argc, const char **argv)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001761{
Jiri Olsaec308422013-03-25 00:02:01 +01001762 struct data__file *d;
1763 static const char *defaults[] = {
1764 "perf.data.old",
1765 "perf.data",
1766 };
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001767 bool use_default = true;
Jiri Olsaec308422013-03-25 00:02:01 +01001768 int i;
1769
1770 data__files_cnt = 2;
1771
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001772 if (argc) {
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001773 if (argc == 1)
Jiri Olsaec308422013-03-25 00:02:01 +01001774 defaults[1] = argv[0];
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001775 else {
1776 data__files_cnt = argc;
1777 use_default = false;
1778 }
Dongsheng Yangd8d96082013-12-06 17:25:52 -05001779 } else if (perf_guest) {
Jiri Olsaec308422013-03-25 00:02:01 +01001780 defaults[0] = "perf.data.host";
1781 defaults[1] = "perf.data.guest";
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001782 }
1783
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001784 if (sort_compute >= (unsigned int) data__files_cnt) {
1785 pr_err("Order option out of limit.\n");
1786 return -EINVAL;
1787 }
1788
Jiri Olsaec308422013-03-25 00:02:01 +01001789 data__files = zalloc(sizeof(*data__files) * data__files_cnt);
1790 if (!data__files)
1791 return -ENOMEM;
1792
1793 data__for_each_file(i, d) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001794 struct perf_data *data = &d->data;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001795
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001796 data->path = use_default ? defaults[i] : argv[i];
1797 data->mode = PERF_DATA_MODE_READ,
1798 data->force = force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001799
Jiri Olsaec308422013-03-25 00:02:01 +01001800 d->idx = i;
1801 }
1802
1803 return 0;
1804}
1805
Namhyung Kimd49dd152017-02-10 16:36:12 +09001806static int diff__config(const char *var, const char *value,
1807 void *cb __maybe_unused)
1808{
1809 if (!strcmp(var, "diff.order")) {
Arnaldo Carvalho de Melo25ce4bb2017-06-27 11:44:58 -03001810 int ret;
1811 if (perf_config_int(&ret, var, value) < 0)
1812 return -1;
1813 sort_compute = ret;
Namhyung Kimd49dd152017-02-10 16:36:12 +09001814 return 0;
1815 }
Namhyung Kim4b359942017-02-10 16:36:13 +09001816 if (!strcmp(var, "diff.compute")) {
1817 if (!strcmp(value, "delta")) {
1818 compute = COMPUTE_DELTA;
1819 } else if (!strcmp(value, "delta-abs")) {
1820 compute = COMPUTE_DELTA_ABS;
1821 } else if (!strcmp(value, "ratio")) {
1822 compute = COMPUTE_RATIO;
1823 } else if (!strcmp(value, "wdiff")) {
1824 compute = COMPUTE_WEIGHTED_DIFF;
1825 } else {
1826 pr_err("Invalid compute method: %s\n", value);
1827 return -1;
1828 }
1829 }
Namhyung Kimd49dd152017-02-10 16:36:12 +09001830
1831 return 0;
1832}
1833
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001834int cmd_diff(int argc, const char **argv)
Jiri Olsaec308422013-03-25 00:02:01 +01001835{
Kan Liang9ab1f502014-10-22 15:02:41 -04001836 int ret = hists__init();
1837
1838 if (ret < 0)
1839 return ret;
1840
Namhyung Kimd49dd152017-02-10 16:36:12 +09001841 perf_config(diff__config, NULL);
1842
Jiri Olsaec308422013-03-25 00:02:01 +01001843 argc = parse_options(argc, argv, options, diff_usage, 0);
1844
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001845 if (quiet)
1846 perf_quiet_option();
1847
Jin Yao99150a12019-06-28 17:23:01 +08001848 symbol__annotation_init();
1849
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001850 if (symbol__init(NULL) < 0)
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001851 return -1;
1852
Jiri Olsaec308422013-03-25 00:02:01 +01001853 if (data_init(argc, argv) < 0)
1854 return -1;
1855
Jin Yao30d81552019-06-28 17:23:00 +08001856 if (check_file_brstack() < 0)
1857 return -1;
1858
Jin Yao99150a12019-06-28 17:23:01 +08001859 if (compute == COMPUTE_CYCLES && !pdiff.has_br_stack)
1860 return -1;
1861
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001862 if (ui_init() < 0)
1863 return -1;
Jiri Olsa1d778222012-10-04 21:49:39 +09001864
Namhyung Kim512ae1b2014-03-18 11:31:39 +09001865 sort__mode = SORT_MODE__DIFF;
1866
Namhyung Kim40184c42015-12-23 02:07:01 +09001867 if (setup_sorting(NULL) < 0)
Namhyung Kim55309982013-02-06 14:57:16 +09001868 usage_with_options(diff_usage, options);
1869
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001870 setup_pager();
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001871
Namhyung Kim08e71542013-04-03 21:26:19 +09001872 sort__setup_elide(NULL);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001873
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001874 return __cmd_diff();
1875}