blob: 51c37e53b3d8b5f248e0f5fa5a0b32e94f8e76d5 [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"
Arnaldo Carvalho de Melo97b9d862019-08-22 17:10:08 -030018#include "util/srcline.h"
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020019#include "util/symbol.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Namhyung Kimd49dd152017-02-10 16:36:12 +090021#include "util/config.h"
Jin Yao48021382019-03-05 21:05:41 +080022#include "util/time-utils.h"
Jin Yao99150a12019-06-28 17:23:01 +080023#include "util/annotate.h"
Jin Yaob10c78c2019-06-28 17:23:03 +080024#include "util/map.h"
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030025#include <linux/zalloc.h>
Arnaldo Carvalho de Melo97b9d862019-08-22 17:10:08 -030026#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020027
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030028#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030029#include <inttypes.h>
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020030#include <stdlib.h>
Jiri Olsa345dc0b2013-02-03 20:08:34 +010031#include <math.h>
32
Jin Yao48021382019-03-05 21:05:41 +080033struct perf_diff {
34 struct perf_tool tool;
35 const char *time_str;
36 struct perf_time_interval *ptime_range;
37 int range_size;
38 int range_num;
Jin Yao30d81552019-06-28 17:23:00 +080039 bool has_br_stack;
Jin Yao48021382019-03-05 21:05:41 +080040};
41
Jiri Olsa345dc0b2013-02-03 20:08:34 +010042/* Diff command specific HPP columns. */
43enum {
44 PERF_HPP_DIFF__BASELINE,
45 PERF_HPP_DIFF__PERIOD,
46 PERF_HPP_DIFF__PERIOD_BASELINE,
47 PERF_HPP_DIFF__DELTA,
48 PERF_HPP_DIFF__RATIO,
49 PERF_HPP_DIFF__WEIGHTED_DIFF,
50 PERF_HPP_DIFF__FORMULA,
Namhyung Kima1668c22017-02-10 16:36:11 +090051 PERF_HPP_DIFF__DELTA_ABS,
Jin Yaob10c78c2019-06-28 17:23:03 +080052 PERF_HPP_DIFF__CYCLES,
Jiri Olsa345dc0b2013-02-03 20:08:34 +010053
54 PERF_HPP_DIFF__MAX_INDEX
55};
56
57struct diff_hpp_fmt {
58 struct perf_hpp_fmt fmt;
59 int idx;
60 char *header;
61 int header_width;
62};
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020063
Jiri Olsaec308422013-03-25 00:02:01 +010064struct data__file {
65 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010066 struct perf_data data;
Jiri Olsaec308422013-03-25 00:02:01 +010067 int idx;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +010068 struct hists *hists;
Jiri Olsac818b492012-12-01 21:57:04 +010069 struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX];
Jiri Olsaec308422013-03-25 00:02:01 +010070};
71
72static struct data__file *data__files;
73static int data__files_cnt;
74
75#define data__for_each_file_start(i, d, s) \
76 for (i = s, d = &data__files[s]; \
77 i < data__files_cnt; \
78 i++, d = &data__files[i])
79
80#define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
Jiri Olsa22aeb7f2012-12-01 22:00:00 +010081#define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
Jiri Olsaec308422013-03-25 00:02:01 +010082
Jiri Olsaec308422013-03-25 00:02:01 +010083static bool force;
Jiri Olsa61949b22012-10-05 16:44:44 +020084static bool show_period;
Jiri Olsaed279da2012-10-05 16:44:45 +020085static bool show_formula;
Jiri Olsaa06d1432012-10-05 16:44:40 +020086static bool show_baseline_only;
Namhyung Kimbe57b3f2017-02-11 01:18:56 +090087static unsigned int sort_compute = 1;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020088
Jiri Olsa81d5f952012-10-05 16:44:43 +020089static s64 compute_wdiff_w1;
90static s64 compute_wdiff_w2;
91
Jin Yaodaca23b2019-03-05 21:05:42 +080092static const char *cpu_list;
93static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
94
Jin Yao99150a12019-06-28 17:23:01 +080095static struct addr_location dummy_al;
96
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020097enum {
98 COMPUTE_DELTA,
99 COMPUTE_RATIO,
Jiri Olsa81d5f952012-10-05 16:44:43 +0200100 COMPUTE_WEIGHTED_DIFF,
Namhyung Kima1668c22017-02-10 16:36:11 +0900101 COMPUTE_DELTA_ABS,
Jin Yao99150a12019-06-28 17:23:01 +0800102 COMPUTE_CYCLES,
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200103 COMPUTE_MAX,
104};
105
106const char *compute_names[COMPUTE_MAX] = {
107 [COMPUTE_DELTA] = "delta",
Namhyung Kima1668c22017-02-10 16:36:11 +0900108 [COMPUTE_DELTA_ABS] = "delta-abs",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200109 [COMPUTE_RATIO] = "ratio",
Jiri Olsa81d5f952012-10-05 16:44:43 +0200110 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
Jin Yao99150a12019-06-28 17:23:01 +0800111 [COMPUTE_CYCLES] = "cycles",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200112};
113
Namhyung Kimbe57b3f2017-02-11 01:18:56 +0900114static int compute = COMPUTE_DELTA_ABS;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200115
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100116static int compute_2_hpp[COMPUTE_MAX] = {
117 [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA,
Namhyung Kima1668c22017-02-10 16:36:11 +0900118 [COMPUTE_DELTA_ABS] = PERF_HPP_DIFF__DELTA_ABS,
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100119 [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO,
120 [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF,
Jin Yaob10c78c2019-06-28 17:23:03 +0800121 [COMPUTE_CYCLES] = PERF_HPP_DIFF__CYCLES,
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100122};
123
124#define MAX_COL_WIDTH 70
125
126static struct header_column {
127 const char *name;
128 int width;
129} columns[PERF_HPP_DIFF__MAX_INDEX] = {
130 [PERF_HPP_DIFF__BASELINE] = {
131 .name = "Baseline",
132 },
133 [PERF_HPP_DIFF__PERIOD] = {
134 .name = "Period",
135 .width = 14,
136 },
137 [PERF_HPP_DIFF__PERIOD_BASELINE] = {
138 .name = "Base period",
139 .width = 14,
140 },
141 [PERF_HPP_DIFF__DELTA] = {
142 .name = "Delta",
143 .width = 7,
144 },
Namhyung Kima1668c22017-02-10 16:36:11 +0900145 [PERF_HPP_DIFF__DELTA_ABS] = {
146 .name = "Delta Abs",
147 .width = 7,
148 },
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100149 [PERF_HPP_DIFF__RATIO] = {
150 .name = "Ratio",
151 .width = 14,
152 },
153 [PERF_HPP_DIFF__WEIGHTED_DIFF] = {
154 .name = "Weighted diff",
155 .width = 14,
156 },
157 [PERF_HPP_DIFF__FORMULA] = {
158 .name = "Formula",
159 .width = MAX_COL_WIDTH,
Jin Yaob10c78c2019-06-28 17:23:03 +0800160 },
161 [PERF_HPP_DIFF__CYCLES] = {
162 .name = "[Program Block Range] Cycles Diff",
163 .width = 70,
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100164 }
165};
166
Jiri Olsa81d5f952012-10-05 16:44:43 +0200167static int setup_compute_opt_wdiff(char *opt)
168{
169 char *w1_str = opt;
170 char *w2_str;
171
172 int ret = -EINVAL;
173
174 if (!opt)
175 goto out;
176
177 w2_str = strchr(opt, ',');
178 if (!w2_str)
179 goto out;
180
181 *w2_str++ = 0x0;
182 if (!*w2_str)
183 goto out;
184
185 compute_wdiff_w1 = strtol(w1_str, NULL, 10);
186 compute_wdiff_w2 = strtol(w2_str, NULL, 10);
187
188 if (!compute_wdiff_w1 || !compute_wdiff_w2)
189 goto out;
190
191 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
192 compute_wdiff_w1, compute_wdiff_w2);
193
194 ret = 0;
195
196 out:
197 if (ret)
198 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
199
200 return ret;
201}
202
203static int setup_compute_opt(char *opt)
204{
205 if (compute == COMPUTE_WEIGHTED_DIFF)
206 return setup_compute_opt_wdiff(opt);
207
208 if (opt) {
209 pr_err("Failed: extra option specified '%s'", opt);
210 return -EINVAL;
211 }
212
213 return 0;
214}
215
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200216static int setup_compute(const struct option *opt, const char *str,
217 int unset __maybe_unused)
218{
219 int *cp = (int *) opt->value;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200220 char *cstr = (char *) str;
221 char buf[50];
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200222 unsigned i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200223 char *option;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200224
225 if (!str) {
226 *cp = COMPUTE_DELTA;
227 return 0;
228 }
229
Jiri Olsa81d5f952012-10-05 16:44:43 +0200230 option = strchr(str, ':');
231 if (option) {
232 unsigned len = option++ - str;
233
234 /*
235 * The str data are not writeable, so we need
236 * to use another buffer.
237 */
238
239 /* No option value is longer. */
240 if (len >= sizeof(buf))
241 return -EINVAL;
242
243 strncpy(buf, str, len);
244 buf[len] = 0x0;
245 cstr = buf;
246 }
247
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200248 for (i = 0; i < COMPUTE_MAX; i++)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200249 if (!strcmp(cstr, compute_names[i])) {
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200250 *cp = i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200251 return setup_compute_opt(option);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200252 }
253
254 pr_err("Failed: '%s' is not computation method "
Jiri Olsa81d5f952012-10-05 16:44:43 +0200255 "(use 'delta','ratio' or 'wdiff')\n", str);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200256 return -EINVAL;
257}
258
Jiri Olsaef358e62012-10-21 23:31:51 +0200259static double period_percent(struct hist_entry *he, u64 period)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200260{
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900261 u64 total = hists__total_period(he->hists);
262
Jiri Olsa96c47f12012-10-05 16:44:42 +0200263 return (period * 100.0) / total;
264}
265
Jiri Olsaef358e62012-10-21 23:31:51 +0200266static double compute_delta(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200267{
Jiri Olsaef358e62012-10-21 23:31:51 +0200268 double old_percent = period_percent(he, he->stat.period);
269 double new_percent = period_percent(pair, pair->stat.period);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200270
Jiri Olsa9af303e2012-12-01 21:15:40 +0100271 pair->diff.period_ratio_delta = new_percent - old_percent;
272 pair->diff.computed = true;
273 return pair->diff.period_ratio_delta;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200274}
275
Jiri Olsaef358e62012-10-21 23:31:51 +0200276static double compute_ratio(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200277{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100278 double old_period = he->stat.period ?: 1;
279 double new_period = pair->stat.period;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200280
Jiri Olsa9af303e2012-12-01 21:15:40 +0100281 pair->diff.computed = true;
282 pair->diff.period_ratio = new_period / old_period;
283 return pair->diff.period_ratio;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200284}
285
Jiri Olsaef358e62012-10-21 23:31:51 +0200286static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200287{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100288 u64 old_period = he->stat.period;
289 u64 new_period = pair->stat.period;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200290
Jiri Olsa9af303e2012-12-01 21:15:40 +0100291 pair->diff.computed = true;
292 pair->diff.wdiff = new_period * compute_wdiff_w2 -
293 old_period * compute_wdiff_w1;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200294
Jiri Olsa9af303e2012-12-01 21:15:40 +0100295 return pair->diff.wdiff;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200296}
297
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100298static int formula_delta(struct hist_entry *he, struct hist_entry *pair,
299 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200300{
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900301 u64 he_total = he->hists->stats.total_period;
302 u64 pair_total = pair->hists->stats.total_period;
303
304 if (symbol_conf.filter_relative) {
305 he_total = he->hists->stats.total_non_filtered_period;
306 pair_total = pair->hists->stats.total_non_filtered_period;
307 }
Jiri Olsaed279da2012-10-05 16:44:45 +0200308 return scnprintf(buf, size,
309 "(%" PRIu64 " * 100 / %" PRIu64 ") - "
310 "(%" PRIu64 " * 100 / %" PRIu64 ")",
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900311 pair->stat.period, pair_total,
312 he->stat.period, he_total);
Jiri Olsaed279da2012-10-05 16:44:45 +0200313}
314
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100315static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,
316 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200317{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100318 double old_period = he->stat.period;
319 double new_period = pair->stat.period;
Jiri Olsaed279da2012-10-05 16:44:45 +0200320
321 return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
322}
323
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100324static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,
325 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200326{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100327 u64 old_period = he->stat.period;
328 u64 new_period = pair->stat.period;
Jiri Olsaed279da2012-10-05 16:44:45 +0200329
330 return scnprintf(buf, size,
331 "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
332 new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
333}
334
Jiri Olsaef358e62012-10-21 23:31:51 +0200335static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
336 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200337{
338 switch (compute) {
339 case COMPUTE_DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +0900340 case COMPUTE_DELTA_ABS:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100341 return formula_delta(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200342 case COMPUTE_RATIO:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100343 return formula_ratio(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200344 case COMPUTE_WEIGHTED_DIFF:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100345 return formula_wdiff(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200346 default:
347 BUG_ON(1);
348 }
349
350 return -1;
351}
352
Jin Yao99150a12019-06-28 17:23:01 +0800353static void *block_hist_zalloc(size_t size)
354{
355 struct block_hist *bh;
356
357 bh = zalloc(size + sizeof(*bh));
358 if (!bh)
359 return NULL;
360
361 return &bh->he;
362}
363
364static void block_hist_free(void *he)
365{
366 struct block_hist *bh;
367
368 bh = container_of(he, struct block_hist, he);
369 hists__delete_entries(&bh->block_hists);
370 free(bh);
371}
372
373struct hist_entry_ops block_hist_ops = {
374 .new = block_hist_zalloc,
375 .free = block_hist_free,
376};
377
Jin Yao48021382019-03-05 21:05:41 +0800378static int diff__process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200379 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200380 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200381 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200382 struct machine *machine)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200383{
Jin Yao48021382019-03-05 21:05:41 +0800384 struct perf_diff *pdiff = container_of(tool, struct perf_diff, tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200385 struct addr_location al;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300386 struct hists *hists = evsel__hists(evsel);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300387 int ret = -1;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200388
Jin Yao48021382019-03-05 21:05:41 +0800389 if (perf_time__ranges_skip_sample(pdiff->ptime_range, pdiff->range_num,
390 sample->time)) {
391 return 0;
392 }
393
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300394 if (machine__resolve(machine, &al, sample) < 0) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200395 pr_warning("problem processing %d event, skipping it.\n",
396 event->header.type);
397 return -1;
398 }
399
Jin Yaodaca23b2019-03-05 21:05:42 +0800400 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) {
401 ret = 0;
402 goto out_put;
403 }
404
Jin Yao99150a12019-06-28 17:23:01 +0800405 if (compute != COMPUTE_CYCLES) {
406 if (!hists__add_entry(hists, &al, NULL, NULL, NULL, sample,
407 true)) {
408 pr_warning("problem incrementing symbol period, "
409 "skipping event\n");
410 goto out_put;
411 }
412 } else {
413 if (!hists__add_entry_ops(hists, &block_hist_ops, &al, NULL,
414 NULL, NULL, sample, true)) {
415 pr_warning("problem incrementing symbol period, "
416 "skipping event\n");
417 goto out_put;
418 }
419
420 hist__account_cycles(sample->branch_stack, &al, sample, false);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200421 }
422
Namhyung Kim820bc812014-04-22 11:44:21 +0900423 /*
424 * The total_period is updated here before going to the output
425 * tree since normally only the baseline hists will call
426 * hists__output_resort() and precompute needs the total
427 * period in order to sort entries by percentage delta.
428 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300429 hists->stats.total_period += sample->period;
Namhyung Kim820bc812014-04-22 11:44:21 +0900430 if (!al.filtered)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300431 hists->stats.total_non_filtered_period += sample->period;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300432 ret = 0;
433out_put:
434 addr_location__put(&al);
435 return ret;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200436}
437
Jin Yao48021382019-03-05 21:05:41 +0800438static struct perf_diff pdiff = {
439 .tool = {
440 .sample = diff__process_sample_event,
441 .mmap = perf_event__process_mmap,
442 .mmap2 = perf_event__process_mmap2,
443 .comm = perf_event__process_comm,
444 .exit = perf_event__process_exit,
445 .fork = perf_event__process_fork,
446 .lost = perf_event__process_lost,
447 .namespaces = perf_event__process_namespaces,
448 .ordered_events = true,
449 .ordering_requires_timestamps = true,
450 },
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200451};
452
Jiri Olsa32dcd022019-07-21 13:23:51 +0200453static struct evsel *evsel_match(struct evsel *evsel,
Jiri Olsa63503db2019-07-21 13:23:52 +0200454 struct evlist *evlist)
Jiri Olsa863e4512012-09-06 17:46:55 +0200455{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200456 struct evsel *e;
Jiri Olsa863e4512012-09-06 17:46:55 +0200457
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300458 evlist__for_each_entry(evlist, e) {
Jiri Olsa863e4512012-09-06 17:46:55 +0200459 if (perf_evsel__match2(evsel, e))
460 return e;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300461 }
Jiri Olsa863e4512012-09-06 17:46:55 +0200462
463 return NULL;
464}
465
Jiri Olsa63503db2019-07-21 13:23:52 +0200466static void perf_evlist__collapse_resort(struct evlist *evlist)
Jiri Olsadd464342012-10-04 21:49:36 +0900467{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200468 struct evsel *evsel;
Jiri Olsadd464342012-10-04 21:49:36 +0900469
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300470 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300471 struct hists *hists = evsel__hists(evsel);
Jiri Olsadd464342012-10-04 21:49:36 +0900472
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900473 hists__collapse_resort(hists, NULL);
Jiri Olsadd464342012-10-04 21:49:36 +0900474 }
475}
476
Namhyung Kimff21cef2015-01-08 09:45:45 +0900477static struct data__file *fmt_to_data_file(struct perf_hpp_fmt *fmt)
478{
479 struct diff_hpp_fmt *dfmt = container_of(fmt, struct diff_hpp_fmt, fmt);
480 void *ptr = dfmt - dfmt->idx;
481 struct data__file *d = container_of(ptr, struct data__file, fmt);
482
483 return d;
484}
485
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100486static struct hist_entry*
487get_pair_data(struct hist_entry *he, struct data__file *d)
488{
489 if (hist_entry__has_pairs(he)) {
490 struct hist_entry *pair;
491
492 list_for_each_entry(pair, &he->pairs.head, pairs.node)
493 if (pair->hists == d->hists)
494 return pair;
495 }
496
497 return NULL;
498}
499
500static struct hist_entry*
501get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
502{
Namhyung Kimff21cef2015-01-08 09:45:45 +0900503 struct data__file *d = fmt_to_data_file(&dfmt->fmt);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100504
505 return get_pair_data(he, d);
506}
507
Jiri Olsaa06d1432012-10-05 16:44:40 +0200508static void hists__baseline_only(struct hists *hists)
509{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800510 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +0900511 struct rb_node *next;
Jiri Olsaa06d1432012-10-05 16:44:40 +0200512
Jiri Olsa52225032016-05-03 13:54:42 +0200513 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +0900514 root = &hists->entries_collapsed;
515 else
516 root = hists->entries_in;
517
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800518 next = rb_first_cached(root);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200519 while (next != NULL) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900520 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200521
Namhyung Kimce74f602012-12-10 17:29:55 +0900522 next = rb_next(&he->rb_node_in);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200523 if (!hist_entry__next_pair(he)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800524 rb_erase_cached(&he->rb_node_in, root);
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300525 hist_entry__delete(he);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200526 }
527 }
528}
529
Jin Yao99150a12019-06-28 17:23:01 +0800530static int64_t block_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
531 struct hist_entry *left, struct hist_entry *right)
532{
533 struct block_info *bi_l = left->block_info;
534 struct block_info *bi_r = right->block_info;
535 int cmp;
536
537 if (!bi_l->sym || !bi_r->sym) {
538 if (!bi_l->sym && !bi_r->sym)
539 return 0;
540 else if (!bi_l->sym)
541 return -1;
542 else
543 return 1;
544 }
545
546 if (bi_l->sym == bi_r->sym) {
547 if (bi_l->start == bi_r->start) {
548 if (bi_l->end == bi_r->end)
549 return 0;
550 else
551 return (int64_t)(bi_r->end - bi_l->end);
552 } else
553 return (int64_t)(bi_r->start - bi_l->start);
554 } else {
555 cmp = strcmp(bi_l->sym->name, bi_r->sym->name);
556 return cmp;
557 }
558
559 if (bi_l->sym->start != bi_r->sym->start)
560 return (int64_t)(bi_r->sym->start - bi_l->sym->start);
561
562 return (int64_t)(bi_r->sym->end - bi_l->sym->end);
563}
564
565static int64_t block_cycles_diff_cmp(struct hist_entry *left,
566 struct hist_entry *right)
567{
568 bool pairs_left = hist_entry__has_pairs(left);
569 bool pairs_right = hist_entry__has_pairs(right);
570 s64 l, r;
571
572 if (!pairs_left && !pairs_right)
573 return 0;
574
575 l = labs(left->diff.cycles);
576 r = labs(right->diff.cycles);
577 return r - l;
578}
579
580static int64_t block_sort(struct perf_hpp_fmt *fmt __maybe_unused,
581 struct hist_entry *left, struct hist_entry *right)
582{
583 return block_cycles_diff_cmp(right, left);
584}
585
586static void init_block_hist(struct block_hist *bh)
587{
588 __hists__init(&bh->block_hists, &bh->block_list);
589 perf_hpp_list__init(&bh->block_list);
590
591 INIT_LIST_HEAD(&bh->block_fmt.list);
592 INIT_LIST_HEAD(&bh->block_fmt.sort_list);
593 bh->block_fmt.cmp = block_cmp;
594 bh->block_fmt.sort = block_sort;
595 perf_hpp_list__register_sort_field(&bh->block_list,
596 &bh->block_fmt);
597 bh->valid = true;
598}
599
600static void init_block_info(struct block_info *bi, struct symbol *sym,
601 struct cyc_hist *ch, int offset)
602{
603 bi->sym = sym;
604 bi->start = ch->start;
605 bi->end = offset;
606 bi->cycles = ch->cycles;
607 bi->cycles_aggr = ch->cycles_aggr;
608 bi->num = ch->num;
609 bi->num_aggr = ch->num_aggr;
610}
611
612static int process_block_per_sym(struct hist_entry *he)
613{
614 struct annotation *notes;
615 struct cyc_hist *ch;
616 struct block_hist *bh;
617
618 if (!he->ms.map || !he->ms.sym)
619 return 0;
620
621 notes = symbol__annotation(he->ms.sym);
622 if (!notes || !notes->src || !notes->src->cycles_hist)
623 return 0;
624
625 bh = container_of(he, struct block_hist, he);
626 init_block_hist(bh);
627
628 ch = notes->src->cycles_hist;
629 for (unsigned int i = 0; i < symbol__size(he->ms.sym); i++) {
630 if (ch[i].num_aggr) {
631 struct block_info *bi;
632 struct hist_entry *he_block;
633
634 bi = block_info__new();
635 if (!bi)
636 return -1;
637
638 init_block_info(bi, he->ms.sym, &ch[i], i);
639 he_block = hists__add_entry_block(&bh->block_hists,
640 &dummy_al, bi);
641 if (!he_block) {
642 block_info__put(bi);
643 return -1;
644 }
645 }
646 }
647
648 return 0;
649}
650
Jin Yaof38108172019-06-28 17:23:02 +0800651static int block_pair_cmp(struct hist_entry *a, struct hist_entry *b)
652{
653 struct block_info *bi_a = a->block_info;
654 struct block_info *bi_b = b->block_info;
655 int cmp;
656
657 if (!bi_a->sym || !bi_b->sym)
658 return -1;
659
660 cmp = strcmp(bi_a->sym->name, bi_b->sym->name);
661
662 if ((!cmp) && (bi_a->start == bi_b->start) && (bi_a->end == bi_b->end))
663 return 0;
664
665 return -1;
666}
667
668static struct hist_entry *get_block_pair(struct hist_entry *he,
669 struct hists *hists_pair)
670{
671 struct rb_root_cached *root = hists_pair->entries_in;
672 struct rb_node *next = rb_first_cached(root);
673 int cmp;
674
675 while (next != NULL) {
676 struct hist_entry *he_pair = rb_entry(next, struct hist_entry,
677 rb_node_in);
678
679 next = rb_next(&he_pair->rb_node_in);
680
681 cmp = block_pair_cmp(he_pair, he);
682 if (!cmp)
683 return he_pair;
684 }
685
686 return NULL;
687}
688
689static void compute_cycles_diff(struct hist_entry *he,
690 struct hist_entry *pair)
691{
692 pair->diff.computed = true;
693 if (pair->block_info->num && he->block_info->num) {
694 pair->diff.cycles =
695 pair->block_info->cycles_aggr / pair->block_info->num_aggr -
696 he->block_info->cycles_aggr / he->block_info->num_aggr;
697 }
698}
699
700static void block_hists_match(struct hists *hists_base,
701 struct hists *hists_pair)
702{
703 struct rb_root_cached *root = hists_base->entries_in;
704 struct rb_node *next = rb_first_cached(root);
705
706 while (next != NULL) {
707 struct hist_entry *he = rb_entry(next, struct hist_entry,
708 rb_node_in);
709 struct hist_entry *pair = get_block_pair(he, hists_pair);
710
711 next = rb_next(&he->rb_node_in);
712
713 if (pair) {
714 hist_entry__add_pair(pair, he);
715 compute_cycles_diff(he, pair);
716 }
717 }
718}
719
720static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
721{
722 /* Skip the calculation of column length in output_resort */
723 he->filtered = true;
724 return 0;
725}
726
Jiri Olsa96c47f12012-10-05 16:44:42 +0200727static void hists__precompute(struct hists *hists)
728{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800729 struct rb_root_cached *root;
Jiri Olsa367c53c2012-12-13 14:08:59 +0100730 struct rb_node *next;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200731
Jiri Olsa52225032016-05-03 13:54:42 +0200732 if (hists__has(hists, need_collapse))
Jiri Olsa367c53c2012-12-13 14:08:59 +0100733 root = &hists->entries_collapsed;
734 else
735 root = hists->entries_in;
736
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800737 next = rb_first_cached(root);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200738 while (next != NULL) {
Jin Yaof38108172019-06-28 17:23:02 +0800739 struct block_hist *bh, *pair_bh;
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100740 struct hist_entry *he, *pair;
Namhyung Kim56495a82015-01-08 09:45:47 +0900741 struct data__file *d;
742 int i;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200743
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100744 he = rb_entry(next, struct hist_entry, rb_node_in);
Jiri Olsa367c53c2012-12-13 14:08:59 +0100745 next = rb_next(&he->rb_node_in);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100746
Jin Yao99150a12019-06-28 17:23:01 +0800747 if (compute == COMPUTE_CYCLES)
748 process_block_per_sym(he);
749
Namhyung Kim56495a82015-01-08 09:45:47 +0900750 data__for_each_file_new(i, d) {
751 pair = get_pair_data(he, d);
752 if (!pair)
753 continue;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200754
Namhyung Kim56495a82015-01-08 09:45:47 +0900755 switch (compute) {
756 case COMPUTE_DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +0900757 case COMPUTE_DELTA_ABS:
Namhyung Kim56495a82015-01-08 09:45:47 +0900758 compute_delta(he, pair);
759 break;
760 case COMPUTE_RATIO:
761 compute_ratio(he, pair);
762 break;
763 case COMPUTE_WEIGHTED_DIFF:
764 compute_wdiff(he, pair);
765 break;
Jin Yao99150a12019-06-28 17:23:01 +0800766 case COMPUTE_CYCLES:
767 process_block_per_sym(pair);
Jin Yaof38108172019-06-28 17:23:02 +0800768 bh = container_of(he, struct block_hist, he);
769 pair_bh = container_of(pair, struct block_hist,
770 he);
771
772 if (bh->valid && pair_bh->valid) {
773 block_hists_match(&bh->block_hists,
774 &pair_bh->block_hists);
775 hists__output_resort_cb(&pair_bh->block_hists,
776 NULL, filter_cb);
777 }
Jin Yao99150a12019-06-28 17:23:01 +0800778 break;
Namhyung Kim56495a82015-01-08 09:45:47 +0900779 default:
780 BUG_ON(1);
781 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200782 }
783 }
784}
785
786static int64_t cmp_doubles(double l, double r)
787{
788 if (l > r)
789 return -1;
790 else if (l < r)
791 return 1;
792 else
793 return 0;
794}
795
796static int64_t
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100797__hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
Jiri Olsa96c47f12012-10-05 16:44:42 +0200798 int c)
799{
800 switch (c) {
801 case COMPUTE_DELTA:
802 {
803 double l = left->diff.period_ratio_delta;
804 double r = right->diff.period_ratio_delta;
805
806 return cmp_doubles(l, r);
807 }
Namhyung Kima1668c22017-02-10 16:36:11 +0900808 case COMPUTE_DELTA_ABS:
809 {
810 double l = fabs(left->diff.period_ratio_delta);
811 double r = fabs(right->diff.period_ratio_delta);
812
813 return cmp_doubles(l, r);
814 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200815 case COMPUTE_RATIO:
816 {
817 double l = left->diff.period_ratio;
818 double r = right->diff.period_ratio;
819
820 return cmp_doubles(l, r);
821 }
Jiri Olsa81d5f952012-10-05 16:44:43 +0200822 case COMPUTE_WEIGHTED_DIFF:
823 {
824 s64 l = left->diff.wdiff;
825 s64 r = right->diff.wdiff;
826
827 return r - l;
828 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200829 default:
830 BUG_ON(1);
831 }
832
833 return 0;
834}
835
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100836static int64_t
837hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
Namhyung Kim56495a82015-01-08 09:45:47 +0900838 int c, int sort_idx)
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100839{
840 bool pairs_left = hist_entry__has_pairs(left);
841 bool pairs_right = hist_entry__has_pairs(right);
842 struct hist_entry *p_right, *p_left;
843
844 if (!pairs_left && !pairs_right)
845 return 0;
846
847 if (!pairs_left || !pairs_right)
848 return pairs_left ? -1 : 1;
849
Namhyung Kim56495a82015-01-08 09:45:47 +0900850 p_left = get_pair_data(left, &data__files[sort_idx]);
851 p_right = get_pair_data(right, &data__files[sort_idx]);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100852
853 if (!p_left && !p_right)
854 return 0;
855
856 if (!p_left || !p_right)
857 return p_left ? -1 : 1;
858
859 /*
860 * We have 2 entries of same kind, let's
861 * make the data comparison.
862 */
863 return __hist_entry__cmp_compute(p_left, p_right, c);
864}
865
Namhyung Kime7024fc2014-12-27 14:06:29 +0900866static int64_t
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900867hist_entry__cmp_compute_idx(struct hist_entry *left, struct hist_entry *right,
868 int c, int sort_idx)
869{
870 struct hist_entry *p_right, *p_left;
871
872 p_left = get_pair_data(left, &data__files[sort_idx]);
873 p_right = get_pair_data(right, &data__files[sort_idx]);
874
875 if (!p_left && !p_right)
876 return 0;
877
878 if (!p_left || !p_right)
879 return p_left ? -1 : 1;
880
Namhyung Kima1668c22017-02-10 16:36:11 +0900881 if (c != COMPUTE_DELTA && c != COMPUTE_DELTA_ABS) {
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900882 /*
883 * The delta can be computed without the baseline, but
884 * others are not. Put those entries which have no
885 * values below.
886 */
887 if (left->dummy && right->dummy)
888 return 0;
889
890 if (left->dummy || right->dummy)
891 return left->dummy ? 1 : -1;
892 }
893
894 return __hist_entry__cmp_compute(p_left, p_right, c);
895}
896
897static int64_t
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900898hist_entry__cmp_nop(struct perf_hpp_fmt *fmt __maybe_unused,
899 struct hist_entry *left __maybe_unused,
Namhyung Kime7024fc2014-12-27 14:06:29 +0900900 struct hist_entry *right __maybe_unused)
901{
902 return 0;
903}
904
905static int64_t
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900906hist_entry__cmp_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
907 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900908{
Namhyung Kime7024fc2014-12-27 14:06:29 +0900909 if (left->stat.period == right->stat.period)
910 return 0;
911 return left->stat.period > right->stat.period ? 1 : -1;
912}
913
914static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900915hist_entry__cmp_delta(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900916 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900917{
Namhyung Kim56495a82015-01-08 09:45:47 +0900918 struct data__file *d = fmt_to_data_file(fmt);
919
920 return hist_entry__cmp_compute(right, left, COMPUTE_DELTA, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900921}
922
923static int64_t
Namhyung Kima1668c22017-02-10 16:36:11 +0900924hist_entry__cmp_delta_abs(struct perf_hpp_fmt *fmt,
925 struct hist_entry *left, struct hist_entry *right)
926{
927 struct data__file *d = fmt_to_data_file(fmt);
928
929 return hist_entry__cmp_compute(right, left, COMPUTE_DELTA_ABS, d->idx);
930}
931
932static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900933hist_entry__cmp_ratio(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900934 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900935{
Namhyung Kim56495a82015-01-08 09:45:47 +0900936 struct data__file *d = fmt_to_data_file(fmt);
937
938 return hist_entry__cmp_compute(right, left, COMPUTE_RATIO, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900939}
940
941static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900942hist_entry__cmp_wdiff(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900943 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900944{
Namhyung Kim56495a82015-01-08 09:45:47 +0900945 struct data__file *d = fmt_to_data_file(fmt);
946
947 return hist_entry__cmp_compute(right, left, COMPUTE_WEIGHTED_DIFF, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900948}
949
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900950static int64_t
951hist_entry__cmp_delta_idx(struct perf_hpp_fmt *fmt __maybe_unused,
952 struct hist_entry *left, struct hist_entry *right)
953{
954 return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA,
955 sort_compute);
956}
957
958static int64_t
Namhyung Kima1668c22017-02-10 16:36:11 +0900959hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt *fmt __maybe_unused,
960 struct hist_entry *left, struct hist_entry *right)
961{
962 return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA_ABS,
963 sort_compute);
964}
965
966static int64_t
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900967hist_entry__cmp_ratio_idx(struct perf_hpp_fmt *fmt __maybe_unused,
968 struct hist_entry *left, struct hist_entry *right)
969{
970 return hist_entry__cmp_compute_idx(right, left, COMPUTE_RATIO,
971 sort_compute);
972}
973
974static int64_t
975hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt *fmt __maybe_unused,
976 struct hist_entry *left, struct hist_entry *right)
977{
978 return hist_entry__cmp_compute_idx(right, left, COMPUTE_WEIGHTED_DIFF,
979 sort_compute);
980}
981
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100982static void hists__process(struct hists *hists)
Jiri Olsaa06d1432012-10-05 16:44:40 +0200983{
Jiri Olsaa06d1432012-10-05 16:44:40 +0200984 if (show_baseline_only)
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100985 hists__baseline_only(hists);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200986
Namhyung Kim56495a82015-01-08 09:45:47 +0900987 hists__precompute(hists);
Namhyung Kim38259a12014-12-27 14:06:30 +0900988 hists__output_resort(hists, NULL);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200989
Jin Yaob10c78c2019-06-28 17:23:03 +0800990 if (compute == COMPUTE_CYCLES)
991 symbol_conf.report_block = true;
992
Namhyung Kim63b42fc2017-02-17 17:17:40 +0900993 hists__fprintf(hists, !quiet, 0, 0, 0, stdout,
Arnaldo Carvalho de Meloe9de7e22018-06-20 15:58:20 -0300994 !symbol_conf.use_callchain);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200995}
996
Jiri Olsa1d81c7f2012-12-01 21:56:03 +0100997static void data__fprintf(void)
998{
999 struct data__file *d;
1000 int i;
1001
1002 fprintf(stdout, "# Data files:\n");
1003
1004 data__for_each_file(i, d)
1005 fprintf(stdout, "# [%d] %s %s\n",
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001006 d->idx, d->data.path,
Jiri Olsa1d81c7f2012-12-01 21:56:03 +01001007 !d->idx ? "(Baseline)" : "");
1008
1009 fprintf(stdout, "#\n");
1010}
1011
Jiri Olsaec308422013-03-25 00:02:01 +01001012static void data_process(void)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001013{
Jiri Olsa63503db2019-07-21 13:23:52 +02001014 struct evlist *evlist_base = data__files[0].session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001015 struct evsel *evsel_base;
Jiri Olsa863e4512012-09-06 17:46:55 +02001016 bool first = true;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001017
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001018 evlist__for_each_entry(evlist_base, evsel_base) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001019 struct hists *hists_base = evsel__hists(evsel_base);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001020 struct data__file *d;
1021 int i;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001022
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001023 data__for_each_file_new(i, d) {
Jiri Olsa63503db2019-07-21 13:23:52 +02001024 struct evlist *evlist = d->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001025 struct evsel *evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001026 struct hists *hists;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001027
1028 evsel = evsel_match(evsel_base, evlist);
1029 if (!evsel)
1030 continue;
1031
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001032 hists = evsel__hists(evsel);
1033 d->hists = hists;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001034
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001035 hists__match(hists_base, hists);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001036
1037 if (!show_baseline_only)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001038 hists__link(hists_base, hists);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001039 }
Jiri Olsa863e4512012-09-06 17:46:55 +02001040
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001041 if (!quiet) {
1042 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
1043 perf_evsel__name(evsel_base));
1044 }
Jiri Olsa863e4512012-09-06 17:46:55 +02001045
1046 first = false;
1047
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001048 if (verbose > 0 || ((data__files_cnt > 2) && !quiet))
Jiri Olsa1d81c7f2012-12-01 21:56:03 +01001049 data__fprintf();
1050
Kan Liangf9db0d02015-08-11 06:30:48 -04001051 /* Don't sort callchain for perf diff */
1052 perf_evsel__reset_sample_bit(evsel_base, CALLCHAIN);
1053
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001054 hists__process(hists_base);
Jiri Olsaec308422013-03-25 00:02:01 +01001055 }
1056}
1057
Jiri Olsac818b492012-12-01 21:57:04 +01001058static void data__free(struct data__file *d)
1059{
1060 int col;
1061
1062 for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
1063 struct diff_hpp_fmt *fmt = &d->fmt[col];
1064
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001065 zfree(&fmt->header);
Jiri Olsac818b492012-12-01 21:57:04 +01001066 }
1067}
1068
Jin Yao48021382019-03-05 21:05:41 +08001069static int abstime_str_dup(char **pstr)
1070{
1071 char *str = NULL;
1072
1073 if (pdiff.time_str && strchr(pdiff.time_str, ':')) {
1074 str = strdup(pdiff.time_str);
1075 if (!str)
1076 return -ENOMEM;
1077 }
1078
1079 *pstr = str;
1080 return 0;
1081}
1082
1083static int parse_absolute_time(struct data__file *d, char **pstr)
1084{
1085 char *p = *pstr;
1086 int ret;
1087
1088 /*
1089 * Absolute timestamp for one file has the format: a.b,c.d
1090 * For multiple files, the format is: a.b,c.d:a.b,c.d
1091 */
1092 p = strchr(*pstr, ':');
1093 if (p) {
1094 if (p == *pstr) {
1095 pr_err("Invalid time string\n");
1096 return -EINVAL;
1097 }
1098
1099 *p = 0;
1100 p++;
1101 if (*p == 0) {
1102 pr_err("Invalid time string\n");
1103 return -EINVAL;
1104 }
1105 }
1106
1107 ret = perf_time__parse_for_ranges(*pstr, d->session,
1108 &pdiff.ptime_range,
1109 &pdiff.range_size,
1110 &pdiff.range_num);
1111 if (ret < 0)
1112 return ret;
1113
1114 if (!p || *p == 0)
1115 *pstr = NULL;
1116 else
1117 *pstr = p;
1118
1119 return ret;
1120}
1121
1122static int parse_percent_time(struct data__file *d)
1123{
1124 int ret;
1125
1126 ret = perf_time__parse_for_ranges(pdiff.time_str, d->session,
1127 &pdiff.ptime_range,
1128 &pdiff.range_size,
1129 &pdiff.range_num);
1130 return ret;
1131}
1132
1133static int parse_time_str(struct data__file *d, char *abstime_ostr,
1134 char **pabstime_tmp)
1135{
1136 int ret = 0;
1137
1138 if (abstime_ostr)
1139 ret = parse_absolute_time(d, pabstime_tmp);
1140 else if (pdiff.time_str)
1141 ret = parse_percent_time(d);
1142
1143 return ret;
1144}
1145
Jin Yao30d81552019-06-28 17:23:00 +08001146static int check_file_brstack(void)
1147{
1148 struct data__file *d;
1149 bool has_br_stack;
1150 int i;
1151
1152 data__for_each_file(i, d) {
1153 d->session = perf_session__new(&d->data, false, &pdiff.tool);
1154 if (!d->session) {
1155 pr_err("Failed to open %s\n", d->data.path);
1156 return -1;
1157 }
1158
1159 has_br_stack = perf_header__has_feat(&d->session->header,
1160 HEADER_BRANCH_STACK);
1161 perf_session__delete(d->session);
1162 if (!has_br_stack)
1163 return 0;
1164 }
1165
1166 /* Set only all files having branch stacks */
1167 pdiff.has_br_stack = true;
1168 return 0;
1169}
1170
Jiri Olsaec308422013-03-25 00:02:01 +01001171static int __cmd_diff(void)
1172{
1173 struct data__file *d;
Jin Yao48021382019-03-05 21:05:41 +08001174 int ret, i;
1175 char *abstime_ostr, *abstime_tmp;
1176
1177 ret = abstime_str_dup(&abstime_ostr);
1178 if (ret)
1179 return ret;
1180
1181 abstime_tmp = abstime_ostr;
1182 ret = -EINVAL;
Jiri Olsaec308422013-03-25 00:02:01 +01001183
1184 data__for_each_file(i, d) {
Jin Yao48021382019-03-05 21:05:41 +08001185 d->session = perf_session__new(&d->data, false, &pdiff.tool);
Jiri Olsaec308422013-03-25 00:02:01 +01001186 if (!d->session) {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001187 pr_err("Failed to open %s\n", d->data.path);
Taeung Song52e028342014-09-24 10:33:37 +09001188 ret = -1;
Jiri Olsaec308422013-03-25 00:02:01 +01001189 goto out_delete;
1190 }
1191
Jin Yao48021382019-03-05 21:05:41 +08001192 if (pdiff.time_str) {
1193 ret = parse_time_str(d, abstime_ostr, &abstime_tmp);
1194 if (ret < 0)
1195 goto out_delete;
1196 }
1197
Jin Yaodaca23b2019-03-05 21:05:42 +08001198 if (cpu_list) {
1199 ret = perf_session__cpu_bitmap(d->session, cpu_list,
1200 cpu_bitmap);
1201 if (ret < 0)
1202 goto out_delete;
1203 }
1204
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001205 ret = perf_session__process_events(d->session);
Jiri Olsaec308422013-03-25 00:02:01 +01001206 if (ret) {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001207 pr_err("Failed to process %s\n", d->data.path);
Jiri Olsaec308422013-03-25 00:02:01 +01001208 goto out_delete;
1209 }
1210
1211 perf_evlist__collapse_resort(d->session->evlist);
Jin Yao48021382019-03-05 21:05:41 +08001212
1213 if (pdiff.ptime_range)
1214 zfree(&pdiff.ptime_range);
Jiri Olsa863e4512012-09-06 17:46:55 +02001215 }
1216
Jiri Olsaec308422013-03-25 00:02:01 +01001217 data_process();
1218
1219 out_delete:
1220 data__for_each_file(i, d) {
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001221 perf_session__delete(d->session);
Jiri Olsac818b492012-12-01 21:57:04 +01001222 data__free(d);
Jiri Olsaec308422013-03-25 00:02:01 +01001223 }
1224
1225 free(data__files);
Jin Yao48021382019-03-05 21:05:41 +08001226
1227 if (pdiff.ptime_range)
1228 zfree(&pdiff.ptime_range);
1229
1230 if (abstime_ostr)
1231 free(abstime_ostr);
1232
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001233 return ret;
1234}
1235
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -02001236static const char * const diff_usage[] = {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001237 "perf diff [<options>] [old_file] [new_file]",
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -02001238 NULL,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001239};
1240
1241static const struct option options[] = {
Ian Munsiec0555642010-04-13 18:37:33 +10001242 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001243 "be more verbose (show symbol address, etc)"),
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001244 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
Jiri Olsaa06d1432012-10-05 16:44:40 +02001245 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
1246 "Show only items with match in baseline"),
Jiri Olsa81d5f952012-10-05 16:44:43 +02001247 OPT_CALLBACK('c', "compute", &compute,
Jin Yaob10c78c2019-06-28 17:23:03 +08001248 "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs),cycles",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001249 "Entries differential computation selection",
1250 setup_compute),
Jiri Olsa61949b22012-10-05 16:44:44 +02001251 OPT_BOOLEAN('p', "period", &show_period,
1252 "Show period values."),
Jiri Olsaed279da2012-10-05 16:44:45 +02001253 OPT_BOOLEAN('F', "formula", &show_formula,
1254 "Show formula."),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001255 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1256 "dump raw trace in ASCII"),
1257 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
David Ahern6b1f3422015-03-24 09:51:57 -06001258 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1259 "file", "kallsyms pathname"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001260 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
1261 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001262 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1263 "only consider symbols in these dsos"),
1264 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1265 "only consider symbols in these comms"),
1266 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1267 "only consider these symbols"),
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001268 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Namhyung Kima2ce0672014-03-04 09:06:42 +09001269 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
1270 " Please refer the man page for the complete list."),
Wang Nan8b8ca6e2015-03-20 02:57:52 +00001271 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001272 "separator for columns, no spaces will be added between "
1273 "columns '.' is reserved."),
He Kuanga7066702016-05-19 11:47:37 +00001274 OPT_CALLBACK(0, "symfs", NULL, "directory",
1275 "Look for files with symbols relative to this directory",
1276 symbol__config_symfs),
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001277 OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
Namhyung Kim8810f6c2014-02-07 12:06:07 +09001278 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1279 "How to display percentage of filtered entries", parse_filter_percentage),
Jin Yao48021382019-03-05 21:05:41 +08001280 OPT_STRING(0, "time", &pdiff.time_str, "str",
1281 "Time span (time percent or absolute timestamp)"),
Jin Yaodaca23b2019-03-05 21:05:42 +08001282 OPT_STRING(0, "cpu", &cpu_list, "cpu", "list of cpus to profile"),
Jin Yaoc1d3e632019-03-05 21:05:43 +08001283 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1284 "only consider symbols in these pids"),
1285 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1286 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001287 OPT_END()
1288};
1289
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001290static double baseline_percent(struct hist_entry *he)
Jiri Olsa1d778222012-10-04 21:49:39 +09001291{
Namhyung Kim8810f6c2014-02-07 12:06:07 +09001292 u64 total = hists__total_period(he->hists);
1293
1294 return 100.0 * he->stat.period / total;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001295}
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001296
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001297static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
1298 struct perf_hpp *hpp, struct hist_entry *he)
1299{
1300 struct diff_hpp_fmt *dfmt =
1301 container_of(fmt, struct diff_hpp_fmt, fmt);
1302 double percent = baseline_percent(he);
1303 char pfmt[20] = " ";
1304
1305 if (!he->dummy) {
1306 scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
1307 return percent_color_snprintf(hpp->buf, hpp->size,
1308 pfmt, percent);
1309 } else
1310 return scnprintf(hpp->buf, hpp->size, "%*s",
1311 dfmt->header_width, pfmt);
1312}
1313
1314static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
1315{
1316 double percent = baseline_percent(he);
1317 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
1318 int ret = 0;
1319
1320 if (!he->dummy)
1321 ret = scnprintf(buf, size, fmt, percent);
1322
1323 return ret;
1324}
1325
Jin Yaob10c78c2019-06-28 17:23:03 +08001326static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
1327 struct perf_hpp *hpp, int width)
1328{
1329 struct block_hist *bh = container_of(he, struct block_hist, he);
1330 struct block_hist *bh_pair = container_of(pair, struct block_hist, he);
1331 struct hist_entry *block_he;
1332 struct block_info *bi;
1333 char buf[128];
1334 char *start_line, *end_line;
1335
1336 block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx);
1337 if (!block_he) {
1338 hpp->skip = true;
1339 return 0;
1340 }
1341
1342 /*
1343 * Avoid printing the warning "addr2line_init failed for ..."
1344 */
1345 symbol_conf.disable_add2line_warn = true;
1346
1347 bi = block_he->block_info;
1348
1349 start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
1350 he->ms.sym);
1351
1352 end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
1353 he->ms.sym);
1354
1355 if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
1356 scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld",
1357 start_line, end_line, block_he->diff.cycles);
1358 } else {
1359 scnprintf(buf, sizeof(buf), "[%7lx -> %7lx] %4ld",
1360 bi->start, bi->end, block_he->diff.cycles);
1361 }
1362
1363 free_srcline(start_line);
1364 free_srcline(end_line);
1365
1366 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
1367}
1368
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301369static int __hpp__color_compare(struct perf_hpp_fmt *fmt,
1370 struct perf_hpp *hpp, struct hist_entry *he,
1371 int comparison_method)
1372{
1373 struct diff_hpp_fmt *dfmt =
1374 container_of(fmt, struct diff_hpp_fmt, fmt);
1375 struct hist_entry *pair = get_pair_fmt(he, dfmt);
1376 double diff;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301377 s64 wdiff;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301378 char pfmt[20] = " ";
1379
Jin Yaob10c78c2019-06-28 17:23:03 +08001380 if (!pair) {
1381 if (comparison_method == COMPUTE_CYCLES) {
1382 struct block_hist *bh;
1383
1384 bh = container_of(he, struct block_hist, he);
1385 if (bh->block_idx)
1386 hpp->skip = true;
1387 }
1388
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001389 goto no_print;
Jin Yaob10c78c2019-06-28 17:23:03 +08001390 }
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301391
1392 switch (comparison_method) {
1393 case COMPUTE_DELTA:
1394 if (pair->diff.computed)
1395 diff = pair->diff.period_ratio_delta;
1396 else
1397 diff = compute_delta(he, pair);
1398
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301399 scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1);
1400 return percent_color_snprintf(hpp->buf, hpp->size,
1401 pfmt, diff);
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301402 case COMPUTE_RATIO:
1403 if (he->dummy)
1404 goto dummy_print;
1405 if (pair->diff.computed)
1406 diff = pair->diff.period_ratio;
1407 else
1408 diff = compute_ratio(he, pair);
1409
1410 scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width);
1411 return value_color_snprintf(hpp->buf, hpp->size,
1412 pfmt, diff);
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301413 case COMPUTE_WEIGHTED_DIFF:
1414 if (he->dummy)
1415 goto dummy_print;
1416 if (pair->diff.computed)
1417 wdiff = pair->diff.wdiff;
1418 else
1419 wdiff = compute_wdiff(he, pair);
1420
1421 scnprintf(pfmt, 20, "%%14ld", dfmt->header_width);
1422 return color_snprintf(hpp->buf, hpp->size,
1423 get_percent_color(wdiff),
1424 pfmt, wdiff);
Jin Yaob10c78c2019-06-28 17:23:03 +08001425 case COMPUTE_CYCLES:
1426 return cycles_printf(he, pair, hpp, dfmt->header_width);
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301427 default:
1428 BUG_ON(1);
1429 }
1430dummy_print:
1431 return scnprintf(hpp->buf, hpp->size, "%*s",
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001432 dfmt->header_width, "N/A");
1433no_print:
1434 return scnprintf(hpp->buf, hpp->size, "%*s",
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301435 dfmt->header_width, pfmt);
1436}
1437
1438static int hpp__color_delta(struct perf_hpp_fmt *fmt,
1439 struct perf_hpp *hpp, struct hist_entry *he)
1440{
1441 return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA);
1442}
1443
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301444static int hpp__color_ratio(struct perf_hpp_fmt *fmt,
1445 struct perf_hpp *hpp, struct hist_entry *he)
1446{
1447 return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO);
1448}
1449
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301450static int hpp__color_wdiff(struct perf_hpp_fmt *fmt,
1451 struct perf_hpp *hpp, struct hist_entry *he)
1452{
1453 return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF);
1454}
1455
Jin Yaob10c78c2019-06-28 17:23:03 +08001456static int hpp__color_cycles(struct perf_hpp_fmt *fmt,
1457 struct perf_hpp *hpp, struct hist_entry *he)
1458{
1459 return __hpp__color_compare(fmt, hpp, he, COMPUTE_CYCLES);
1460}
1461
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001462static void
1463hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
1464{
1465 switch (idx) {
1466 case PERF_HPP_DIFF__PERIOD_BASELINE:
1467 scnprintf(buf, size, "%" PRIu64, he->stat.period);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001468 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001469
1470 default:
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001471 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001472 }
1473}
1474
1475static void
1476hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
1477 int idx, char *buf, size_t size)
1478{
1479 double diff;
1480 double ratio;
1481 s64 wdiff;
1482
1483 switch (idx) {
1484 case PERF_HPP_DIFF__DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +09001485 case PERF_HPP_DIFF__DELTA_ABS:
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001486 if (pair->diff.computed)
1487 diff = pair->diff.period_ratio_delta;
1488 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001489 diff = compute_delta(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001490
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001491 scnprintf(buf, size, "%+4.2F%%", diff);
Jiri Olsa81d5f952012-10-05 16:44:43 +02001492 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001493
1494 case PERF_HPP_DIFF__RATIO:
1495 /* No point for ratio number if we are dummy.. */
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001496 if (he->dummy) {
1497 scnprintf(buf, size, "N/A");
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001498 break;
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001499 }
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001500
1501 if (pair->diff.computed)
1502 ratio = pair->diff.period_ratio;
1503 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001504 ratio = compute_ratio(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001505
1506 if (ratio > 0.0)
1507 scnprintf(buf, size, "%14.6F", ratio);
1508 break;
1509
1510 case PERF_HPP_DIFF__WEIGHTED_DIFF:
1511 /* No point for wdiff number if we are dummy.. */
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001512 if (he->dummy) {
1513 scnprintf(buf, size, "N/A");
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001514 break;
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001515 }
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001516
1517 if (pair->diff.computed)
1518 wdiff = pair->diff.wdiff;
1519 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001520 wdiff = compute_wdiff(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001521
1522 if (wdiff != 0)
1523 scnprintf(buf, size, "%14ld", wdiff);
1524 break;
1525
1526 case PERF_HPP_DIFF__FORMULA:
Jiri Olsaef358e62012-10-21 23:31:51 +02001527 formula_fprintf(he, pair, buf, size);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001528 break;
1529
1530 case PERF_HPP_DIFF__PERIOD:
1531 scnprintf(buf, size, "%" PRIu64, pair->stat.period);
1532 break;
1533
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001534 default:
1535 BUG_ON(1);
1536 };
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001537}
1538
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001539static void
1540__hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
1541 char *buf, size_t size)
1542{
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001543 struct hist_entry *pair = get_pair_fmt(he, dfmt);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001544 int idx = dfmt->idx;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001545
1546 /* baseline is special */
1547 if (idx == PERF_HPP_DIFF__BASELINE)
1548 hpp__entry_baseline(he, buf, size);
1549 else {
1550 if (pair)
1551 hpp__entry_pair(he, pair, idx, buf, size);
1552 else
1553 hpp__entry_unpair(he, idx, buf, size);
1554 }
1555}
1556
1557static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
1558 struct hist_entry *he)
1559{
1560 struct diff_hpp_fmt *dfmt =
1561 container_of(_fmt, struct diff_hpp_fmt, fmt);
1562 char buf[MAX_COL_WIDTH] = " ";
1563
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001564 __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001565
1566 if (symbol_conf.field_sep)
1567 return scnprintf(hpp->buf, hpp->size, "%s", buf);
1568 else
1569 return scnprintf(hpp->buf, hpp->size, "%*s",
1570 dfmt->header_width, buf);
1571}
1572
Namhyung Kim94a07932014-03-10 16:43:52 +09001573static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsa74bb43f2016-08-07 17:28:27 +02001574 struct hists *hists __maybe_unused,
Jiri Olsa29659ab2016-08-07 17:28:30 +02001575 int line __maybe_unused,
1576 int *span __maybe_unused)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001577{
1578 struct diff_hpp_fmt *dfmt =
1579 container_of(fmt, struct diff_hpp_fmt, fmt);
1580
1581 BUG_ON(!dfmt->header);
1582 return scnprintf(hpp->buf, hpp->size, dfmt->header);
1583}
1584
1585static int hpp__width(struct perf_hpp_fmt *fmt,
Namhyung Kim94a07932014-03-10 16:43:52 +09001586 struct perf_hpp *hpp __maybe_unused,
Jiri Olsada1b0402016-06-14 20:19:20 +02001587 struct hists *hists __maybe_unused)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001588{
1589 struct diff_hpp_fmt *dfmt =
1590 container_of(fmt, struct diff_hpp_fmt, fmt);
1591
1592 BUG_ON(dfmt->header_width <= 0);
1593 return dfmt->header_width;
1594}
1595
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001596static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001597{
1598#define MAX_HEADER_NAME 100
1599 char buf_indent[MAX_HEADER_NAME];
1600 char buf[MAX_HEADER_NAME];
1601 const char *header = NULL;
1602 int width = 0;
1603
1604 BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
1605 header = columns[dfmt->idx].name;
1606 width = columns[dfmt->idx].width;
1607
1608 /* Only our defined HPP fmts should appear here. */
1609 BUG_ON(!header);
1610
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001611 if (data__files_cnt > 2)
1612 scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
1613
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001614#define NAME (data__files_cnt > 2 ? buf : header)
1615 dfmt->header_width = width;
1616 width = (int) strlen(NAME);
1617 if (dfmt->header_width < width)
1618 dfmt->header_width = width;
1619
1620 scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
1621 dfmt->header_width, NAME);
1622
1623 dfmt->header = strdup(buf_indent);
1624#undef MAX_HEADER_NAME
1625#undef NAME
1626}
1627
Jiri Olsac818b492012-12-01 21:57:04 +01001628static void data__hpp_register(struct data__file *d, int idx)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001629{
Jiri Olsac818b492012-12-01 21:57:04 +01001630 struct diff_hpp_fmt *dfmt = &d->fmt[idx];
1631 struct perf_hpp_fmt *fmt = &dfmt->fmt;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001632
Jiri Olsac818b492012-12-01 21:57:04 +01001633 dfmt->idx = idx;
1634
1635 fmt->header = hpp__header;
1636 fmt->width = hpp__width;
1637 fmt->entry = hpp__entry_global;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001638 fmt->cmp = hist_entry__cmp_nop;
1639 fmt->collapse = hist_entry__cmp_nop;
Jiri Olsac818b492012-12-01 21:57:04 +01001640
1641 /* TODO more colors */
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301642 switch (idx) {
1643 case PERF_HPP_DIFF__BASELINE:
Jiri Olsac818b492012-12-01 21:57:04 +01001644 fmt->color = hpp__color_baseline;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001645 fmt->sort = hist_entry__cmp_baseline;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301646 break;
1647 case PERF_HPP_DIFF__DELTA:
1648 fmt->color = hpp__color_delta;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001649 fmt->sort = hist_entry__cmp_delta;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301650 break;
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301651 case PERF_HPP_DIFF__RATIO:
1652 fmt->color = hpp__color_ratio;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001653 fmt->sort = hist_entry__cmp_ratio;
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301654 break;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301655 case PERF_HPP_DIFF__WEIGHTED_DIFF:
1656 fmt->color = hpp__color_wdiff;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001657 fmt->sort = hist_entry__cmp_wdiff;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301658 break;
Namhyung Kima1668c22017-02-10 16:36:11 +09001659 case PERF_HPP_DIFF__DELTA_ABS:
1660 fmt->color = hpp__color_delta;
1661 fmt->sort = hist_entry__cmp_delta_abs;
1662 break;
Jin Yaob10c78c2019-06-28 17:23:03 +08001663 case PERF_HPP_DIFF__CYCLES:
1664 fmt->color = hpp__color_cycles;
1665 fmt->sort = hist_entry__cmp_nop;
1666 break;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301667 default:
Namhyung Kime7024fc2014-12-27 14:06:29 +09001668 fmt->sort = hist_entry__cmp_nop;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301669 break;
1670 }
Jiri Olsac818b492012-12-01 21:57:04 +01001671
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001672 init_header(d, dfmt);
Jiri Olsac818b492012-12-01 21:57:04 +01001673 perf_hpp__column_register(fmt);
Namhyung Kime7024fc2014-12-27 14:06:29 +09001674 perf_hpp__register_sort_field(fmt);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001675}
1676
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001677static int ui_init(void)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001678{
Jiri Olsac818b492012-12-01 21:57:04 +01001679 struct data__file *d;
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001680 struct perf_hpp_fmt *fmt;
Jiri Olsac818b492012-12-01 21:57:04 +01001681 int i;
Jiri Olsa1d778222012-10-04 21:49:39 +09001682
Jiri Olsac818b492012-12-01 21:57:04 +01001683 data__for_each_file(i, d) {
Jiri Olsaed279da2012-10-05 16:44:45 +02001684
Jiri Olsac818b492012-12-01 21:57:04 +01001685 /*
1686 * Baseline or compute realted columns:
1687 *
1688 * PERF_HPP_DIFF__BASELINE
1689 * PERF_HPP_DIFF__DELTA
1690 * PERF_HPP_DIFF__RATIO
1691 * PERF_HPP_DIFF__WEIGHTED_DIFF
1692 */
1693 data__hpp_register(d, i ? compute_2_hpp[compute] :
1694 PERF_HPP_DIFF__BASELINE);
1695
1696 /*
1697 * And the rest:
1698 *
1699 * PERF_HPP_DIFF__FORMULA
1700 * PERF_HPP_DIFF__PERIOD
1701 * PERF_HPP_DIFF__PERIOD_BASELINE
1702 */
1703 if (show_formula && i)
1704 data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
1705
1706 if (show_period)
1707 data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
1708 PERF_HPP_DIFF__PERIOD_BASELINE);
Jiri Olsa61949b22012-10-05 16:44:44 +02001709 }
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001710
1711 if (!sort_compute)
1712 return 0;
1713
1714 /*
1715 * Prepend an fmt to sort on columns at 'sort_compute' first.
1716 * This fmt is added only to the sort list but not to the
1717 * output fields list.
1718 *
1719 * Note that this column (data) can be compared twice - one
1720 * for this 'sort_compute' fmt and another for the normal
1721 * diff_hpp_fmt. But it shouldn't a problem as most entries
1722 * will be sorted out by first try or baseline and comparing
1723 * is not a costly operation.
1724 */
1725 fmt = zalloc(sizeof(*fmt));
1726 if (fmt == NULL) {
1727 pr_err("Memory allocation failed\n");
1728 return -1;
1729 }
1730
1731 fmt->cmp = hist_entry__cmp_nop;
1732 fmt->collapse = hist_entry__cmp_nop;
1733
1734 switch (compute) {
1735 case COMPUTE_DELTA:
1736 fmt->sort = hist_entry__cmp_delta_idx;
1737 break;
1738 case COMPUTE_RATIO:
1739 fmt->sort = hist_entry__cmp_ratio_idx;
1740 break;
1741 case COMPUTE_WEIGHTED_DIFF:
1742 fmt->sort = hist_entry__cmp_wdiff_idx;
1743 break;
Namhyung Kima1668c22017-02-10 16:36:11 +09001744 case COMPUTE_DELTA_ABS:
1745 fmt->sort = hist_entry__cmp_delta_abs_idx;
1746 break;
Jin Yao99150a12019-06-28 17:23:01 +08001747 case COMPUTE_CYCLES:
1748 /*
1749 * Should set since 'fmt->sort' is called without
1750 * checking valid during sorting
1751 */
1752 fmt->sort = hist_entry__cmp_nop;
1753 break;
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001754 default:
1755 BUG_ON(1);
1756 }
1757
Namhyung Kima1c9f972017-01-18 14:14:57 +09001758 perf_hpp__prepend_sort_field(fmt);
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001759 return 0;
Jiri Olsa1d778222012-10-04 21:49:39 +09001760}
1761
Jiri Olsaec308422013-03-25 00:02:01 +01001762static int data_init(int argc, const char **argv)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001763{
Jiri Olsaec308422013-03-25 00:02:01 +01001764 struct data__file *d;
1765 static const char *defaults[] = {
1766 "perf.data.old",
1767 "perf.data",
1768 };
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001769 bool use_default = true;
Jiri Olsaec308422013-03-25 00:02:01 +01001770 int i;
1771
1772 data__files_cnt = 2;
1773
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001774 if (argc) {
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001775 if (argc == 1)
Jiri Olsaec308422013-03-25 00:02:01 +01001776 defaults[1] = argv[0];
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001777 else {
1778 data__files_cnt = argc;
1779 use_default = false;
1780 }
Dongsheng Yangd8d96082013-12-06 17:25:52 -05001781 } else if (perf_guest) {
Jiri Olsaec308422013-03-25 00:02:01 +01001782 defaults[0] = "perf.data.host";
1783 defaults[1] = "perf.data.guest";
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001784 }
1785
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001786 if (sort_compute >= (unsigned int) data__files_cnt) {
1787 pr_err("Order option out of limit.\n");
1788 return -EINVAL;
1789 }
1790
Jiri Olsaec308422013-03-25 00:02:01 +01001791 data__files = zalloc(sizeof(*data__files) * data__files_cnt);
1792 if (!data__files)
1793 return -ENOMEM;
1794
1795 data__for_each_file(i, d) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001796 struct perf_data *data = &d->data;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001797
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001798 data->path = use_default ? defaults[i] : argv[i];
1799 data->mode = PERF_DATA_MODE_READ,
1800 data->force = force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001801
Jiri Olsaec308422013-03-25 00:02:01 +01001802 d->idx = i;
1803 }
1804
1805 return 0;
1806}
1807
Namhyung Kimd49dd152017-02-10 16:36:12 +09001808static int diff__config(const char *var, const char *value,
1809 void *cb __maybe_unused)
1810{
1811 if (!strcmp(var, "diff.order")) {
Arnaldo Carvalho de Melo25ce4bb2017-06-27 11:44:58 -03001812 int ret;
1813 if (perf_config_int(&ret, var, value) < 0)
1814 return -1;
1815 sort_compute = ret;
Namhyung Kimd49dd152017-02-10 16:36:12 +09001816 return 0;
1817 }
Namhyung Kim4b359942017-02-10 16:36:13 +09001818 if (!strcmp(var, "diff.compute")) {
1819 if (!strcmp(value, "delta")) {
1820 compute = COMPUTE_DELTA;
1821 } else if (!strcmp(value, "delta-abs")) {
1822 compute = COMPUTE_DELTA_ABS;
1823 } else if (!strcmp(value, "ratio")) {
1824 compute = COMPUTE_RATIO;
1825 } else if (!strcmp(value, "wdiff")) {
1826 compute = COMPUTE_WEIGHTED_DIFF;
1827 } else {
1828 pr_err("Invalid compute method: %s\n", value);
1829 return -1;
1830 }
1831 }
Namhyung Kimd49dd152017-02-10 16:36:12 +09001832
1833 return 0;
1834}
1835
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001836int cmd_diff(int argc, const char **argv)
Jiri Olsaec308422013-03-25 00:02:01 +01001837{
Kan Liang9ab1f502014-10-22 15:02:41 -04001838 int ret = hists__init();
1839
1840 if (ret < 0)
1841 return ret;
1842
Namhyung Kimd49dd152017-02-10 16:36:12 +09001843 perf_config(diff__config, NULL);
1844
Jiri Olsaec308422013-03-25 00:02:01 +01001845 argc = parse_options(argc, argv, options, diff_usage, 0);
1846
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001847 if (quiet)
1848 perf_quiet_option();
1849
Jin Yao99150a12019-06-28 17:23:01 +08001850 symbol__annotation_init();
1851
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001852 if (symbol__init(NULL) < 0)
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001853 return -1;
1854
Jiri Olsaec308422013-03-25 00:02:01 +01001855 if (data_init(argc, argv) < 0)
1856 return -1;
1857
Jin Yao30d81552019-06-28 17:23:00 +08001858 if (check_file_brstack() < 0)
1859 return -1;
1860
Jin Yao99150a12019-06-28 17:23:01 +08001861 if (compute == COMPUTE_CYCLES && !pdiff.has_br_stack)
1862 return -1;
1863
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001864 if (ui_init() < 0)
1865 return -1;
Jiri Olsa1d778222012-10-04 21:49:39 +09001866
Namhyung Kim512ae1b2014-03-18 11:31:39 +09001867 sort__mode = SORT_MODE__DIFF;
1868
Namhyung Kim40184c42015-12-23 02:07:01 +09001869 if (setup_sorting(NULL) < 0)
Namhyung Kim55309982013-02-06 14:57:16 +09001870 usage_with_options(diff_usage, options);
1871
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001872 setup_pager();
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001873
Namhyung Kim08e71542013-04-03 21:26:19 +09001874 sort__setup_elide(NULL);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001875
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001876 return __cmd_diff();
1877}