blob: 83b8c0f3fb16006df4cf5efe0ceb59a73056d59a [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"
19#include "util/util.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"
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020024
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030025#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030026#include <inttypes.h>
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020027#include <stdlib.h>
Jiri Olsa345dc0b2013-02-03 20:08:34 +010028#include <math.h>
29
Jin Yao48021382019-03-05 21:05:41 +080030struct perf_diff {
31 struct perf_tool tool;
32 const char *time_str;
33 struct perf_time_interval *ptime_range;
34 int range_size;
35 int range_num;
Jin Yao30d81552019-06-28 17:23:00 +080036 bool has_br_stack;
Jin Yao48021382019-03-05 21:05:41 +080037};
38
Jiri Olsa345dc0b2013-02-03 20:08:34 +010039/* Diff command specific HPP columns. */
40enum {
41 PERF_HPP_DIFF__BASELINE,
42 PERF_HPP_DIFF__PERIOD,
43 PERF_HPP_DIFF__PERIOD_BASELINE,
44 PERF_HPP_DIFF__DELTA,
45 PERF_HPP_DIFF__RATIO,
46 PERF_HPP_DIFF__WEIGHTED_DIFF,
47 PERF_HPP_DIFF__FORMULA,
Namhyung Kima1668c22017-02-10 16:36:11 +090048 PERF_HPP_DIFF__DELTA_ABS,
Jiri Olsa345dc0b2013-02-03 20:08:34 +010049
50 PERF_HPP_DIFF__MAX_INDEX
51};
52
53struct diff_hpp_fmt {
54 struct perf_hpp_fmt fmt;
55 int idx;
56 char *header;
57 int header_width;
58};
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020059
Jiri Olsaec308422013-03-25 00:02:01 +010060struct data__file {
61 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010062 struct perf_data data;
Jiri Olsaec308422013-03-25 00:02:01 +010063 int idx;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +010064 struct hists *hists;
Jiri Olsac818b492012-12-01 21:57:04 +010065 struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX];
Jiri Olsaec308422013-03-25 00:02:01 +010066};
67
68static struct data__file *data__files;
69static int data__files_cnt;
70
71#define data__for_each_file_start(i, d, s) \
72 for (i = s, d = &data__files[s]; \
73 i < data__files_cnt; \
74 i++, d = &data__files[i])
75
76#define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
Jiri Olsa22aeb7f2012-12-01 22:00:00 +010077#define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
Jiri Olsaec308422013-03-25 00:02:01 +010078
Jiri Olsaec308422013-03-25 00:02:01 +010079static bool force;
Jiri Olsa61949b22012-10-05 16:44:44 +020080static bool show_period;
Jiri Olsaed279da2012-10-05 16:44:45 +020081static bool show_formula;
Jiri Olsaa06d1432012-10-05 16:44:40 +020082static bool show_baseline_only;
Namhyung Kimbe57b3f2017-02-11 01:18:56 +090083static unsigned int sort_compute = 1;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -020084
Jiri Olsa81d5f952012-10-05 16:44:43 +020085static s64 compute_wdiff_w1;
86static s64 compute_wdiff_w2;
87
Jin Yaodaca23b2019-03-05 21:05:42 +080088static const char *cpu_list;
89static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
90
Jin Yao99150a12019-06-28 17:23:01 +080091static struct addr_location dummy_al;
92
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020093enum {
94 COMPUTE_DELTA,
95 COMPUTE_RATIO,
Jiri Olsa81d5f952012-10-05 16:44:43 +020096 COMPUTE_WEIGHTED_DIFF,
Namhyung Kima1668c22017-02-10 16:36:11 +090097 COMPUTE_DELTA_ABS,
Jin Yao99150a12019-06-28 17:23:01 +080098 COMPUTE_CYCLES,
Jiri Olsa7aaf6b32012-10-05 16:44:41 +020099 COMPUTE_MAX,
100};
101
102const char *compute_names[COMPUTE_MAX] = {
103 [COMPUTE_DELTA] = "delta",
Namhyung Kima1668c22017-02-10 16:36:11 +0900104 [COMPUTE_DELTA_ABS] = "delta-abs",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200105 [COMPUTE_RATIO] = "ratio",
Jiri Olsa81d5f952012-10-05 16:44:43 +0200106 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
Jin Yao99150a12019-06-28 17:23:01 +0800107 [COMPUTE_CYCLES] = "cycles",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200108};
109
Namhyung Kimbe57b3f2017-02-11 01:18:56 +0900110static int compute = COMPUTE_DELTA_ABS;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200111
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100112static int compute_2_hpp[COMPUTE_MAX] = {
113 [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA,
Namhyung Kima1668c22017-02-10 16:36:11 +0900114 [COMPUTE_DELTA_ABS] = PERF_HPP_DIFF__DELTA_ABS,
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100115 [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO,
116 [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF,
117};
118
119#define MAX_COL_WIDTH 70
120
121static struct header_column {
122 const char *name;
123 int width;
124} columns[PERF_HPP_DIFF__MAX_INDEX] = {
125 [PERF_HPP_DIFF__BASELINE] = {
126 .name = "Baseline",
127 },
128 [PERF_HPP_DIFF__PERIOD] = {
129 .name = "Period",
130 .width = 14,
131 },
132 [PERF_HPP_DIFF__PERIOD_BASELINE] = {
133 .name = "Base period",
134 .width = 14,
135 },
136 [PERF_HPP_DIFF__DELTA] = {
137 .name = "Delta",
138 .width = 7,
139 },
Namhyung Kima1668c22017-02-10 16:36:11 +0900140 [PERF_HPP_DIFF__DELTA_ABS] = {
141 .name = "Delta Abs",
142 .width = 7,
143 },
Jiri Olsa345dc0b2013-02-03 20:08:34 +0100144 [PERF_HPP_DIFF__RATIO] = {
145 .name = "Ratio",
146 .width = 14,
147 },
148 [PERF_HPP_DIFF__WEIGHTED_DIFF] = {
149 .name = "Weighted diff",
150 .width = 14,
151 },
152 [PERF_HPP_DIFF__FORMULA] = {
153 .name = "Formula",
154 .width = MAX_COL_WIDTH,
155 }
156};
157
Jiri Olsa81d5f952012-10-05 16:44:43 +0200158static int setup_compute_opt_wdiff(char *opt)
159{
160 char *w1_str = opt;
161 char *w2_str;
162
163 int ret = -EINVAL;
164
165 if (!opt)
166 goto out;
167
168 w2_str = strchr(opt, ',');
169 if (!w2_str)
170 goto out;
171
172 *w2_str++ = 0x0;
173 if (!*w2_str)
174 goto out;
175
176 compute_wdiff_w1 = strtol(w1_str, NULL, 10);
177 compute_wdiff_w2 = strtol(w2_str, NULL, 10);
178
179 if (!compute_wdiff_w1 || !compute_wdiff_w2)
180 goto out;
181
182 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
183 compute_wdiff_w1, compute_wdiff_w2);
184
185 ret = 0;
186
187 out:
188 if (ret)
189 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
190
191 return ret;
192}
193
194static int setup_compute_opt(char *opt)
195{
196 if (compute == COMPUTE_WEIGHTED_DIFF)
197 return setup_compute_opt_wdiff(opt);
198
199 if (opt) {
200 pr_err("Failed: extra option specified '%s'", opt);
201 return -EINVAL;
202 }
203
204 return 0;
205}
206
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200207static int setup_compute(const struct option *opt, const char *str,
208 int unset __maybe_unused)
209{
210 int *cp = (int *) opt->value;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200211 char *cstr = (char *) str;
212 char buf[50];
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200213 unsigned i;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200214 char *option;
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200215
216 if (!str) {
217 *cp = COMPUTE_DELTA;
218 return 0;
219 }
220
Jiri Olsa81d5f952012-10-05 16:44:43 +0200221 option = strchr(str, ':');
222 if (option) {
223 unsigned len = option++ - str;
224
225 /*
226 * The str data are not writeable, so we need
227 * to use another buffer.
228 */
229
230 /* No option value is longer. */
231 if (len >= sizeof(buf))
232 return -EINVAL;
233
234 strncpy(buf, str, len);
235 buf[len] = 0x0;
236 cstr = buf;
237 }
238
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200239 for (i = 0; i < COMPUTE_MAX; i++)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200240 if (!strcmp(cstr, compute_names[i])) {
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200241 *cp = i;
Jin Yao99150a12019-06-28 17:23:01 +0800242 if (i == COMPUTE_CYCLES)
243 break;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200244 return setup_compute_opt(option);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200245 }
246
247 pr_err("Failed: '%s' is not computation method "
Jiri Olsa81d5f952012-10-05 16:44:43 +0200248 "(use 'delta','ratio' or 'wdiff')\n", str);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +0200249 return -EINVAL;
250}
251
Jiri Olsaef358e62012-10-21 23:31:51 +0200252static double period_percent(struct hist_entry *he, u64 period)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200253{
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900254 u64 total = hists__total_period(he->hists);
255
Jiri Olsa96c47f12012-10-05 16:44:42 +0200256 return (period * 100.0) / total;
257}
258
Jiri Olsaef358e62012-10-21 23:31:51 +0200259static double compute_delta(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200260{
Jiri Olsaef358e62012-10-21 23:31:51 +0200261 double old_percent = period_percent(he, he->stat.period);
262 double new_percent = period_percent(pair, pair->stat.period);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200263
Jiri Olsa9af303e2012-12-01 21:15:40 +0100264 pair->diff.period_ratio_delta = new_percent - old_percent;
265 pair->diff.computed = true;
266 return pair->diff.period_ratio_delta;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200267}
268
Jiri Olsaef358e62012-10-21 23:31:51 +0200269static double compute_ratio(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa96c47f12012-10-05 16:44:42 +0200270{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100271 double old_period = he->stat.period ?: 1;
272 double new_period = pair->stat.period;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200273
Jiri Olsa9af303e2012-12-01 21:15:40 +0100274 pair->diff.computed = true;
275 pair->diff.period_ratio = new_period / old_period;
276 return pair->diff.period_ratio;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200277}
278
Jiri Olsaef358e62012-10-21 23:31:51 +0200279static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
Jiri Olsa81d5f952012-10-05 16:44:43 +0200280{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100281 u64 old_period = he->stat.period;
282 u64 new_period = pair->stat.period;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200283
Jiri Olsa9af303e2012-12-01 21:15:40 +0100284 pair->diff.computed = true;
285 pair->diff.wdiff = new_period * compute_wdiff_w2 -
286 old_period * compute_wdiff_w1;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200287
Jiri Olsa9af303e2012-12-01 21:15:40 +0100288 return pair->diff.wdiff;
Jiri Olsa81d5f952012-10-05 16:44:43 +0200289}
290
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100291static int formula_delta(struct hist_entry *he, struct hist_entry *pair,
292 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200293{
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900294 u64 he_total = he->hists->stats.total_period;
295 u64 pair_total = pair->hists->stats.total_period;
296
297 if (symbol_conf.filter_relative) {
298 he_total = he->hists->stats.total_non_filtered_period;
299 pair_total = pair->hists->stats.total_non_filtered_period;
300 }
Jiri Olsaed279da2012-10-05 16:44:45 +0200301 return scnprintf(buf, size,
302 "(%" PRIu64 " * 100 / %" PRIu64 ") - "
303 "(%" PRIu64 " * 100 / %" PRIu64 ")",
Namhyung Kim8810f6c2014-02-07 12:06:07 +0900304 pair->stat.period, pair_total,
305 he->stat.period, he_total);
Jiri Olsaed279da2012-10-05 16:44:45 +0200306}
307
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100308static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,
309 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200310{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100311 double old_period = he->stat.period;
312 double new_period = pair->stat.period;
Jiri Olsaed279da2012-10-05 16:44:45 +0200313
314 return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
315}
316
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100317static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,
318 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200319{
Jiri Olsa9af303e2012-12-01 21:15:40 +0100320 u64 old_period = he->stat.period;
321 u64 new_period = pair->stat.period;
Jiri Olsaed279da2012-10-05 16:44:45 +0200322
323 return scnprintf(buf, size,
324 "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
325 new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
326}
327
Jiri Olsaef358e62012-10-21 23:31:51 +0200328static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
329 char *buf, size_t size)
Jiri Olsaed279da2012-10-05 16:44:45 +0200330{
331 switch (compute) {
332 case COMPUTE_DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +0900333 case COMPUTE_DELTA_ABS:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100334 return formula_delta(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200335 case COMPUTE_RATIO:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100336 return formula_ratio(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200337 case COMPUTE_WEIGHTED_DIFF:
Jiri Olsaf4c8bae2012-11-28 14:52:41 +0100338 return formula_wdiff(he, pair, buf, size);
Jiri Olsaed279da2012-10-05 16:44:45 +0200339 default:
340 BUG_ON(1);
341 }
342
343 return -1;
344}
345
Jin Yao99150a12019-06-28 17:23:01 +0800346static void *block_hist_zalloc(size_t size)
347{
348 struct block_hist *bh;
349
350 bh = zalloc(size + sizeof(*bh));
351 if (!bh)
352 return NULL;
353
354 return &bh->he;
355}
356
357static void block_hist_free(void *he)
358{
359 struct block_hist *bh;
360
361 bh = container_of(he, struct block_hist, he);
362 hists__delete_entries(&bh->block_hists);
363 free(bh);
364}
365
366struct hist_entry_ops block_hist_ops = {
367 .new = block_hist_zalloc,
368 .free = block_hist_free,
369};
370
Jin Yao48021382019-03-05 21:05:41 +0800371static int diff__process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200372 union perf_event *event,
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200373 struct perf_sample *sample,
Jiri Olsa863e4512012-09-06 17:46:55 +0200374 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200375 struct machine *machine)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200376{
Jin Yao48021382019-03-05 21:05:41 +0800377 struct perf_diff *pdiff = container_of(tool, struct perf_diff, tool);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200378 struct addr_location al;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300379 struct hists *hists = evsel__hists(evsel);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300380 int ret = -1;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200381
Jin Yao48021382019-03-05 21:05:41 +0800382 if (perf_time__ranges_skip_sample(pdiff->ptime_range, pdiff->range_num,
383 sample->time)) {
384 return 0;
385 }
386
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300387 if (machine__resolve(machine, &al, sample) < 0) {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200388 pr_warning("problem processing %d event, skipping it.\n",
389 event->header.type);
390 return -1;
391 }
392
Jin Yaodaca23b2019-03-05 21:05:42 +0800393 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) {
394 ret = 0;
395 goto out_put;
396 }
397
Jin Yao99150a12019-06-28 17:23:01 +0800398 if (compute != COMPUTE_CYCLES) {
399 if (!hists__add_entry(hists, &al, NULL, NULL, NULL, sample,
400 true)) {
401 pr_warning("problem incrementing symbol period, "
402 "skipping event\n");
403 goto out_put;
404 }
405 } else {
406 if (!hists__add_entry_ops(hists, &block_hist_ops, &al, NULL,
407 NULL, NULL, sample, true)) {
408 pr_warning("problem incrementing symbol period, "
409 "skipping event\n");
410 goto out_put;
411 }
412
413 hist__account_cycles(sample->branch_stack, &al, sample, false);
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200414 }
415
Namhyung Kim820bc812014-04-22 11:44:21 +0900416 /*
417 * The total_period is updated here before going to the output
418 * tree since normally only the baseline hists will call
419 * hists__output_resort() and precompute needs the total
420 * period in order to sort entries by percentage delta.
421 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300422 hists->stats.total_period += sample->period;
Namhyung Kim820bc812014-04-22 11:44:21 +0900423 if (!al.filtered)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300424 hists->stats.total_non_filtered_period += sample->period;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300425 ret = 0;
426out_put:
427 addr_location__put(&al);
428 return ret;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200429}
430
Jin Yao48021382019-03-05 21:05:41 +0800431static struct perf_diff pdiff = {
432 .tool = {
433 .sample = diff__process_sample_event,
434 .mmap = perf_event__process_mmap,
435 .mmap2 = perf_event__process_mmap2,
436 .comm = perf_event__process_comm,
437 .exit = perf_event__process_exit,
438 .fork = perf_event__process_fork,
439 .lost = perf_event__process_lost,
440 .namespaces = perf_event__process_namespaces,
441 .ordered_events = true,
442 .ordering_requires_timestamps = true,
443 },
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200444};
445
Jiri Olsa863e4512012-09-06 17:46:55 +0200446static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
447 struct perf_evlist *evlist)
448{
449 struct perf_evsel *e;
450
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300451 evlist__for_each_entry(evlist, e) {
Jiri Olsa863e4512012-09-06 17:46:55 +0200452 if (perf_evsel__match2(evsel, e))
453 return e;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300454 }
Jiri Olsa863e4512012-09-06 17:46:55 +0200455
456 return NULL;
457}
458
Namhyung Kimce74f602012-12-10 17:29:55 +0900459static void perf_evlist__collapse_resort(struct perf_evlist *evlist)
Jiri Olsadd464342012-10-04 21:49:36 +0900460{
461 struct perf_evsel *evsel;
462
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300463 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300464 struct hists *hists = evsel__hists(evsel);
Jiri Olsadd464342012-10-04 21:49:36 +0900465
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900466 hists__collapse_resort(hists, NULL);
Jiri Olsadd464342012-10-04 21:49:36 +0900467 }
468}
469
Namhyung Kimff21cef2015-01-08 09:45:45 +0900470static struct data__file *fmt_to_data_file(struct perf_hpp_fmt *fmt)
471{
472 struct diff_hpp_fmt *dfmt = container_of(fmt, struct diff_hpp_fmt, fmt);
473 void *ptr = dfmt - dfmt->idx;
474 struct data__file *d = container_of(ptr, struct data__file, fmt);
475
476 return d;
477}
478
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100479static struct hist_entry*
480get_pair_data(struct hist_entry *he, struct data__file *d)
481{
482 if (hist_entry__has_pairs(he)) {
483 struct hist_entry *pair;
484
485 list_for_each_entry(pair, &he->pairs.head, pairs.node)
486 if (pair->hists == d->hists)
487 return pair;
488 }
489
490 return NULL;
491}
492
493static struct hist_entry*
494get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
495{
Namhyung Kimff21cef2015-01-08 09:45:45 +0900496 struct data__file *d = fmt_to_data_file(&dfmt->fmt);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100497
498 return get_pair_data(he, d);
499}
500
Jiri Olsaa06d1432012-10-05 16:44:40 +0200501static void hists__baseline_only(struct hists *hists)
502{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800503 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +0900504 struct rb_node *next;
Jiri Olsaa06d1432012-10-05 16:44:40 +0200505
Jiri Olsa52225032016-05-03 13:54:42 +0200506 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +0900507 root = &hists->entries_collapsed;
508 else
509 root = hists->entries_in;
510
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800511 next = rb_first_cached(root);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200512 while (next != NULL) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900513 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200514
Namhyung Kimce74f602012-12-10 17:29:55 +0900515 next = rb_next(&he->rb_node_in);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200516 if (!hist_entry__next_pair(he)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800517 rb_erase_cached(&he->rb_node_in, root);
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300518 hist_entry__delete(he);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200519 }
520 }
521}
522
Jin Yao99150a12019-06-28 17:23:01 +0800523static int64_t block_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
524 struct hist_entry *left, struct hist_entry *right)
525{
526 struct block_info *bi_l = left->block_info;
527 struct block_info *bi_r = right->block_info;
528 int cmp;
529
530 if (!bi_l->sym || !bi_r->sym) {
531 if (!bi_l->sym && !bi_r->sym)
532 return 0;
533 else if (!bi_l->sym)
534 return -1;
535 else
536 return 1;
537 }
538
539 if (bi_l->sym == bi_r->sym) {
540 if (bi_l->start == bi_r->start) {
541 if (bi_l->end == bi_r->end)
542 return 0;
543 else
544 return (int64_t)(bi_r->end - bi_l->end);
545 } else
546 return (int64_t)(bi_r->start - bi_l->start);
547 } else {
548 cmp = strcmp(bi_l->sym->name, bi_r->sym->name);
549 return cmp;
550 }
551
552 if (bi_l->sym->start != bi_r->sym->start)
553 return (int64_t)(bi_r->sym->start - bi_l->sym->start);
554
555 return (int64_t)(bi_r->sym->end - bi_l->sym->end);
556}
557
558static int64_t block_cycles_diff_cmp(struct hist_entry *left,
559 struct hist_entry *right)
560{
561 bool pairs_left = hist_entry__has_pairs(left);
562 bool pairs_right = hist_entry__has_pairs(right);
563 s64 l, r;
564
565 if (!pairs_left && !pairs_right)
566 return 0;
567
568 l = labs(left->diff.cycles);
569 r = labs(right->diff.cycles);
570 return r - l;
571}
572
573static int64_t block_sort(struct perf_hpp_fmt *fmt __maybe_unused,
574 struct hist_entry *left, struct hist_entry *right)
575{
576 return block_cycles_diff_cmp(right, left);
577}
578
579static void init_block_hist(struct block_hist *bh)
580{
581 __hists__init(&bh->block_hists, &bh->block_list);
582 perf_hpp_list__init(&bh->block_list);
583
584 INIT_LIST_HEAD(&bh->block_fmt.list);
585 INIT_LIST_HEAD(&bh->block_fmt.sort_list);
586 bh->block_fmt.cmp = block_cmp;
587 bh->block_fmt.sort = block_sort;
588 perf_hpp_list__register_sort_field(&bh->block_list,
589 &bh->block_fmt);
590 bh->valid = true;
591}
592
593static void init_block_info(struct block_info *bi, struct symbol *sym,
594 struct cyc_hist *ch, int offset)
595{
596 bi->sym = sym;
597 bi->start = ch->start;
598 bi->end = offset;
599 bi->cycles = ch->cycles;
600 bi->cycles_aggr = ch->cycles_aggr;
601 bi->num = ch->num;
602 bi->num_aggr = ch->num_aggr;
603}
604
605static int process_block_per_sym(struct hist_entry *he)
606{
607 struct annotation *notes;
608 struct cyc_hist *ch;
609 struct block_hist *bh;
610
611 if (!he->ms.map || !he->ms.sym)
612 return 0;
613
614 notes = symbol__annotation(he->ms.sym);
615 if (!notes || !notes->src || !notes->src->cycles_hist)
616 return 0;
617
618 bh = container_of(he, struct block_hist, he);
619 init_block_hist(bh);
620
621 ch = notes->src->cycles_hist;
622 for (unsigned int i = 0; i < symbol__size(he->ms.sym); i++) {
623 if (ch[i].num_aggr) {
624 struct block_info *bi;
625 struct hist_entry *he_block;
626
627 bi = block_info__new();
628 if (!bi)
629 return -1;
630
631 init_block_info(bi, he->ms.sym, &ch[i], i);
632 he_block = hists__add_entry_block(&bh->block_hists,
633 &dummy_al, bi);
634 if (!he_block) {
635 block_info__put(bi);
636 return -1;
637 }
638 }
639 }
640
641 return 0;
642}
643
Jiri Olsa96c47f12012-10-05 16:44:42 +0200644static void hists__precompute(struct hists *hists)
645{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800646 struct rb_root_cached *root;
Jiri Olsa367c53c2012-12-13 14:08:59 +0100647 struct rb_node *next;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200648
Jiri Olsa52225032016-05-03 13:54:42 +0200649 if (hists__has(hists, need_collapse))
Jiri Olsa367c53c2012-12-13 14:08:59 +0100650 root = &hists->entries_collapsed;
651 else
652 root = hists->entries_in;
653
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800654 next = rb_first_cached(root);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200655 while (next != NULL) {
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100656 struct hist_entry *he, *pair;
Namhyung Kim56495a82015-01-08 09:45:47 +0900657 struct data__file *d;
658 int i;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200659
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100660 he = rb_entry(next, struct hist_entry, rb_node_in);
Jiri Olsa367c53c2012-12-13 14:08:59 +0100661 next = rb_next(&he->rb_node_in);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100662
Jin Yao99150a12019-06-28 17:23:01 +0800663 if (compute == COMPUTE_CYCLES)
664 process_block_per_sym(he);
665
Namhyung Kim56495a82015-01-08 09:45:47 +0900666 data__for_each_file_new(i, d) {
667 pair = get_pair_data(he, d);
668 if (!pair)
669 continue;
Jiri Olsa96c47f12012-10-05 16:44:42 +0200670
Namhyung Kim56495a82015-01-08 09:45:47 +0900671 switch (compute) {
672 case COMPUTE_DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +0900673 case COMPUTE_DELTA_ABS:
Namhyung Kim56495a82015-01-08 09:45:47 +0900674 compute_delta(he, pair);
675 break;
676 case COMPUTE_RATIO:
677 compute_ratio(he, pair);
678 break;
679 case COMPUTE_WEIGHTED_DIFF:
680 compute_wdiff(he, pair);
681 break;
Jin Yao99150a12019-06-28 17:23:01 +0800682 case COMPUTE_CYCLES:
683 process_block_per_sym(pair);
684 break;
Namhyung Kim56495a82015-01-08 09:45:47 +0900685 default:
686 BUG_ON(1);
687 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200688 }
689 }
690}
691
692static int64_t cmp_doubles(double l, double r)
693{
694 if (l > r)
695 return -1;
696 else if (l < r)
697 return 1;
698 else
699 return 0;
700}
701
702static int64_t
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100703__hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
Jiri Olsa96c47f12012-10-05 16:44:42 +0200704 int c)
705{
706 switch (c) {
707 case COMPUTE_DELTA:
708 {
709 double l = left->diff.period_ratio_delta;
710 double r = right->diff.period_ratio_delta;
711
712 return cmp_doubles(l, r);
713 }
Namhyung Kima1668c22017-02-10 16:36:11 +0900714 case COMPUTE_DELTA_ABS:
715 {
716 double l = fabs(left->diff.period_ratio_delta);
717 double r = fabs(right->diff.period_ratio_delta);
718
719 return cmp_doubles(l, r);
720 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200721 case COMPUTE_RATIO:
722 {
723 double l = left->diff.period_ratio;
724 double r = right->diff.period_ratio;
725
726 return cmp_doubles(l, r);
727 }
Jiri Olsa81d5f952012-10-05 16:44:43 +0200728 case COMPUTE_WEIGHTED_DIFF:
729 {
730 s64 l = left->diff.wdiff;
731 s64 r = right->diff.wdiff;
732
733 return r - l;
734 }
Jiri Olsa96c47f12012-10-05 16:44:42 +0200735 default:
736 BUG_ON(1);
737 }
738
739 return 0;
740}
741
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100742static int64_t
743hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
Namhyung Kim56495a82015-01-08 09:45:47 +0900744 int c, int sort_idx)
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100745{
746 bool pairs_left = hist_entry__has_pairs(left);
747 bool pairs_right = hist_entry__has_pairs(right);
748 struct hist_entry *p_right, *p_left;
749
750 if (!pairs_left && !pairs_right)
751 return 0;
752
753 if (!pairs_left || !pairs_right)
754 return pairs_left ? -1 : 1;
755
Namhyung Kim56495a82015-01-08 09:45:47 +0900756 p_left = get_pair_data(left, &data__files[sort_idx]);
757 p_right = get_pair_data(right, &data__files[sort_idx]);
Jiri Olsa5f3f8d32012-11-25 23:10:20 +0100758
759 if (!p_left && !p_right)
760 return 0;
761
762 if (!p_left || !p_right)
763 return p_left ? -1 : 1;
764
765 /*
766 * We have 2 entries of same kind, let's
767 * make the data comparison.
768 */
769 return __hist_entry__cmp_compute(p_left, p_right, c);
770}
771
Namhyung Kime7024fc2014-12-27 14:06:29 +0900772static int64_t
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900773hist_entry__cmp_compute_idx(struct hist_entry *left, struct hist_entry *right,
774 int c, int sort_idx)
775{
776 struct hist_entry *p_right, *p_left;
777
778 p_left = get_pair_data(left, &data__files[sort_idx]);
779 p_right = get_pair_data(right, &data__files[sort_idx]);
780
781 if (!p_left && !p_right)
782 return 0;
783
784 if (!p_left || !p_right)
785 return p_left ? -1 : 1;
786
Namhyung Kima1668c22017-02-10 16:36:11 +0900787 if (c != COMPUTE_DELTA && c != COMPUTE_DELTA_ABS) {
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900788 /*
789 * The delta can be computed without the baseline, but
790 * others are not. Put those entries which have no
791 * values below.
792 */
793 if (left->dummy && right->dummy)
794 return 0;
795
796 if (left->dummy || right->dummy)
797 return left->dummy ? 1 : -1;
798 }
799
800 return __hist_entry__cmp_compute(p_left, p_right, c);
801}
802
803static int64_t
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900804hist_entry__cmp_nop(struct perf_hpp_fmt *fmt __maybe_unused,
805 struct hist_entry *left __maybe_unused,
Namhyung Kime7024fc2014-12-27 14:06:29 +0900806 struct hist_entry *right __maybe_unused)
807{
808 return 0;
809}
810
811static int64_t
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900812hist_entry__cmp_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
813 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900814{
Namhyung Kime7024fc2014-12-27 14:06:29 +0900815 if (left->stat.period == right->stat.period)
816 return 0;
817 return left->stat.period > right->stat.period ? 1 : -1;
818}
819
820static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900821hist_entry__cmp_delta(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900822 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900823{
Namhyung Kim56495a82015-01-08 09:45:47 +0900824 struct data__file *d = fmt_to_data_file(fmt);
825
826 return hist_entry__cmp_compute(right, left, COMPUTE_DELTA, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900827}
828
829static int64_t
Namhyung Kima1668c22017-02-10 16:36:11 +0900830hist_entry__cmp_delta_abs(struct perf_hpp_fmt *fmt,
831 struct hist_entry *left, struct hist_entry *right)
832{
833 struct data__file *d = fmt_to_data_file(fmt);
834
835 return hist_entry__cmp_compute(right, left, COMPUTE_DELTA_ABS, d->idx);
836}
837
838static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900839hist_entry__cmp_ratio(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900840 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900841{
Namhyung Kim56495a82015-01-08 09:45:47 +0900842 struct data__file *d = fmt_to_data_file(fmt);
843
844 return hist_entry__cmp_compute(right, left, COMPUTE_RATIO, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900845}
846
847static int64_t
Namhyung Kim56495a82015-01-08 09:45:47 +0900848hist_entry__cmp_wdiff(struct perf_hpp_fmt *fmt,
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900849 struct hist_entry *left, struct hist_entry *right)
Namhyung Kime7024fc2014-12-27 14:06:29 +0900850{
Namhyung Kim56495a82015-01-08 09:45:47 +0900851 struct data__file *d = fmt_to_data_file(fmt);
852
853 return hist_entry__cmp_compute(right, left, COMPUTE_WEIGHTED_DIFF, d->idx);
Namhyung Kime7024fc2014-12-27 14:06:29 +0900854}
855
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900856static int64_t
857hist_entry__cmp_delta_idx(struct perf_hpp_fmt *fmt __maybe_unused,
858 struct hist_entry *left, struct hist_entry *right)
859{
860 return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA,
861 sort_compute);
862}
863
864static int64_t
Namhyung Kima1668c22017-02-10 16:36:11 +0900865hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt *fmt __maybe_unused,
866 struct hist_entry *left, struct hist_entry *right)
867{
868 return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA_ABS,
869 sort_compute);
870}
871
872static int64_t
Namhyung Kim566b5cf2015-01-08 09:45:48 +0900873hist_entry__cmp_ratio_idx(struct perf_hpp_fmt *fmt __maybe_unused,
874 struct hist_entry *left, struct hist_entry *right)
875{
876 return hist_entry__cmp_compute_idx(right, left, COMPUTE_RATIO,
877 sort_compute);
878}
879
880static int64_t
881hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt *fmt __maybe_unused,
882 struct hist_entry *left, struct hist_entry *right)
883{
884 return hist_entry__cmp_compute_idx(right, left, COMPUTE_WEIGHTED_DIFF,
885 sort_compute);
886}
887
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100888static void hists__process(struct hists *hists)
Jiri Olsaa06d1432012-10-05 16:44:40 +0200889{
Jiri Olsaa06d1432012-10-05 16:44:40 +0200890 if (show_baseline_only)
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100891 hists__baseline_only(hists);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200892
Namhyung Kim56495a82015-01-08 09:45:47 +0900893 hists__precompute(hists);
Namhyung Kim38259a12014-12-27 14:06:30 +0900894 hists__output_resort(hists, NULL);
Jiri Olsa96c47f12012-10-05 16:44:42 +0200895
Namhyung Kim63b42fc2017-02-17 17:17:40 +0900896 hists__fprintf(hists, !quiet, 0, 0, 0, stdout,
Arnaldo Carvalho de Meloe9de7e22018-06-20 15:58:20 -0300897 !symbol_conf.use_callchain);
Jiri Olsaa06d1432012-10-05 16:44:40 +0200898}
899
Jiri Olsa1d81c7f2012-12-01 21:56:03 +0100900static void data__fprintf(void)
901{
902 struct data__file *d;
903 int i;
904
905 fprintf(stdout, "# Data files:\n");
906
907 data__for_each_file(i, d)
908 fprintf(stdout, "# [%d] %s %s\n",
Jiri Olsa2d4f2792019-02-21 10:41:30 +0100909 d->idx, d->data.path,
Jiri Olsa1d81c7f2012-12-01 21:56:03 +0100910 !d->idx ? "(Baseline)" : "");
911
912 fprintf(stdout, "#\n");
913}
914
Jiri Olsaec308422013-03-25 00:02:01 +0100915static void data_process(void)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200916{
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100917 struct perf_evlist *evlist_base = data__files[0].session->evlist;
918 struct perf_evsel *evsel_base;
Jiri Olsa863e4512012-09-06 17:46:55 +0200919 bool first = true;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200920
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300921 evlist__for_each_entry(evlist_base, evsel_base) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300922 struct hists *hists_base = evsel__hists(evsel_base);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100923 struct data__file *d;
924 int i;
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -0200925
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100926 data__for_each_file_new(i, d) {
927 struct perf_evlist *evlist = d->session->evlist;
928 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300929 struct hists *hists;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100930
931 evsel = evsel_match(evsel_base, evlist);
932 if (!evsel)
933 continue;
934
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300935 hists = evsel__hists(evsel);
936 d->hists = hists;
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100937
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300938 hists__match(hists_base, hists);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100939
940 if (!show_baseline_only)
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300941 hists__link(hists_base, hists);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +0100942 }
Jiri Olsa863e4512012-09-06 17:46:55 +0200943
Namhyung Kim63b42fc2017-02-17 17:17:40 +0900944 if (!quiet) {
945 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
946 perf_evsel__name(evsel_base));
947 }
Jiri Olsa863e4512012-09-06 17:46:55 +0200948
949 first = false;
950
Namhyung Kim63b42fc2017-02-17 17:17:40 +0900951 if (verbose > 0 || ((data__files_cnt > 2) && !quiet))
Jiri Olsa1d81c7f2012-12-01 21:56:03 +0100952 data__fprintf();
953
Kan Liangf9db0d02015-08-11 06:30:48 -0400954 /* Don't sort callchain for perf diff */
955 perf_evsel__reset_sample_bit(evsel_base, CALLCHAIN);
956
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300957 hists__process(hists_base);
Jiri Olsaec308422013-03-25 00:02:01 +0100958 }
959}
960
Jiri Olsac818b492012-12-01 21:57:04 +0100961static void data__free(struct data__file *d)
962{
963 int col;
964
965 for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
966 struct diff_hpp_fmt *fmt = &d->fmt[col];
967
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300968 zfree(&fmt->header);
Jiri Olsac818b492012-12-01 21:57:04 +0100969 }
970}
971
Jin Yao48021382019-03-05 21:05:41 +0800972static int abstime_str_dup(char **pstr)
973{
974 char *str = NULL;
975
976 if (pdiff.time_str && strchr(pdiff.time_str, ':')) {
977 str = strdup(pdiff.time_str);
978 if (!str)
979 return -ENOMEM;
980 }
981
982 *pstr = str;
983 return 0;
984}
985
986static int parse_absolute_time(struct data__file *d, char **pstr)
987{
988 char *p = *pstr;
989 int ret;
990
991 /*
992 * Absolute timestamp for one file has the format: a.b,c.d
993 * For multiple files, the format is: a.b,c.d:a.b,c.d
994 */
995 p = strchr(*pstr, ':');
996 if (p) {
997 if (p == *pstr) {
998 pr_err("Invalid time string\n");
999 return -EINVAL;
1000 }
1001
1002 *p = 0;
1003 p++;
1004 if (*p == 0) {
1005 pr_err("Invalid time string\n");
1006 return -EINVAL;
1007 }
1008 }
1009
1010 ret = perf_time__parse_for_ranges(*pstr, d->session,
1011 &pdiff.ptime_range,
1012 &pdiff.range_size,
1013 &pdiff.range_num);
1014 if (ret < 0)
1015 return ret;
1016
1017 if (!p || *p == 0)
1018 *pstr = NULL;
1019 else
1020 *pstr = p;
1021
1022 return ret;
1023}
1024
1025static int parse_percent_time(struct data__file *d)
1026{
1027 int ret;
1028
1029 ret = perf_time__parse_for_ranges(pdiff.time_str, d->session,
1030 &pdiff.ptime_range,
1031 &pdiff.range_size,
1032 &pdiff.range_num);
1033 return ret;
1034}
1035
1036static int parse_time_str(struct data__file *d, char *abstime_ostr,
1037 char **pabstime_tmp)
1038{
1039 int ret = 0;
1040
1041 if (abstime_ostr)
1042 ret = parse_absolute_time(d, pabstime_tmp);
1043 else if (pdiff.time_str)
1044 ret = parse_percent_time(d);
1045
1046 return ret;
1047}
1048
Jin Yao30d81552019-06-28 17:23:00 +08001049static int check_file_brstack(void)
1050{
1051 struct data__file *d;
1052 bool has_br_stack;
1053 int i;
1054
1055 data__for_each_file(i, d) {
1056 d->session = perf_session__new(&d->data, false, &pdiff.tool);
1057 if (!d->session) {
1058 pr_err("Failed to open %s\n", d->data.path);
1059 return -1;
1060 }
1061
1062 has_br_stack = perf_header__has_feat(&d->session->header,
1063 HEADER_BRANCH_STACK);
1064 perf_session__delete(d->session);
1065 if (!has_br_stack)
1066 return 0;
1067 }
1068
1069 /* Set only all files having branch stacks */
1070 pdiff.has_br_stack = true;
1071 return 0;
1072}
1073
Jiri Olsaec308422013-03-25 00:02:01 +01001074static int __cmd_diff(void)
1075{
1076 struct data__file *d;
Jin Yao48021382019-03-05 21:05:41 +08001077 int ret, i;
1078 char *abstime_ostr, *abstime_tmp;
1079
1080 ret = abstime_str_dup(&abstime_ostr);
1081 if (ret)
1082 return ret;
1083
1084 abstime_tmp = abstime_ostr;
1085 ret = -EINVAL;
Jiri Olsaec308422013-03-25 00:02:01 +01001086
1087 data__for_each_file(i, d) {
Jin Yao48021382019-03-05 21:05:41 +08001088 d->session = perf_session__new(&d->data, false, &pdiff.tool);
Jiri Olsaec308422013-03-25 00:02:01 +01001089 if (!d->session) {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001090 pr_err("Failed to open %s\n", d->data.path);
Taeung Song52e028342014-09-24 10:33:37 +09001091 ret = -1;
Jiri Olsaec308422013-03-25 00:02:01 +01001092 goto out_delete;
1093 }
1094
Jin Yao48021382019-03-05 21:05:41 +08001095 if (pdiff.time_str) {
1096 ret = parse_time_str(d, abstime_ostr, &abstime_tmp);
1097 if (ret < 0)
1098 goto out_delete;
1099 }
1100
Jin Yaodaca23b2019-03-05 21:05:42 +08001101 if (cpu_list) {
1102 ret = perf_session__cpu_bitmap(d->session, cpu_list,
1103 cpu_bitmap);
1104 if (ret < 0)
1105 goto out_delete;
1106 }
1107
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001108 ret = perf_session__process_events(d->session);
Jiri Olsaec308422013-03-25 00:02:01 +01001109 if (ret) {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001110 pr_err("Failed to process %s\n", d->data.path);
Jiri Olsaec308422013-03-25 00:02:01 +01001111 goto out_delete;
1112 }
1113
1114 perf_evlist__collapse_resort(d->session->evlist);
Jin Yao48021382019-03-05 21:05:41 +08001115
1116 if (pdiff.ptime_range)
1117 zfree(&pdiff.ptime_range);
Jiri Olsa863e4512012-09-06 17:46:55 +02001118 }
1119
Jiri Olsaec308422013-03-25 00:02:01 +01001120 data_process();
1121
1122 out_delete:
1123 data__for_each_file(i, d) {
Arnaldo Carvalho de Meloe1446552016-06-22 10:02:16 -03001124 perf_session__delete(d->session);
Jiri Olsac818b492012-12-01 21:57:04 +01001125 data__free(d);
Jiri Olsaec308422013-03-25 00:02:01 +01001126 }
1127
1128 free(data__files);
Jin Yao48021382019-03-05 21:05:41 +08001129
1130 if (pdiff.ptime_range)
1131 zfree(&pdiff.ptime_range);
1132
1133 if (abstime_ostr)
1134 free(abstime_ostr);
1135
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001136 return ret;
1137}
1138
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -02001139static const char * const diff_usage[] = {
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001140 "perf diff [<options>] [old_file] [new_file]",
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -02001141 NULL,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001142};
1143
1144static const struct option options[] = {
Ian Munsiec0555642010-04-13 18:37:33 +10001145 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001146 "be more verbose (show symbol address, etc)"),
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001147 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
Jiri Olsaa06d1432012-10-05 16:44:40 +02001148 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
1149 "Show only items with match in baseline"),
Jiri Olsa81d5f952012-10-05 16:44:43 +02001150 OPT_CALLBACK('c', "compute", &compute,
Namhyung Kimbe57b3f2017-02-11 01:18:56 +09001151 "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs)",
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001152 "Entries differential computation selection",
1153 setup_compute),
Jiri Olsa61949b22012-10-05 16:44:44 +02001154 OPT_BOOLEAN('p', "period", &show_period,
1155 "Show period values."),
Jiri Olsaed279da2012-10-05 16:44:45 +02001156 OPT_BOOLEAN('F', "formula", &show_formula,
1157 "Show formula."),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001158 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1159 "dump raw trace in ASCII"),
1160 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
David Ahern6b1f3422015-03-24 09:51:57 -06001161 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1162 "file", "kallsyms pathname"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001163 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
1164 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001165 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1166 "only consider symbols in these dsos"),
1167 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1168 "only consider symbols in these comms"),
1169 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1170 "only consider these symbols"),
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001171 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
Namhyung Kima2ce0672014-03-04 09:06:42 +09001172 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
1173 " Please refer the man page for the complete list."),
Wang Nan8b8ca6e2015-03-20 02:57:52 +00001174 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001175 "separator for columns, no spaces will be added between "
1176 "columns '.' is reserved."),
He Kuanga7066702016-05-19 11:47:37 +00001177 OPT_CALLBACK(0, "symfs", NULL, "directory",
1178 "Look for files with symbols relative to this directory",
1179 symbol__config_symfs),
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001180 OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
Namhyung Kim8810f6c2014-02-07 12:06:07 +09001181 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1182 "How to display percentage of filtered entries", parse_filter_percentage),
Jin Yao48021382019-03-05 21:05:41 +08001183 OPT_STRING(0, "time", &pdiff.time_str, "str",
1184 "Time span (time percent or absolute timestamp)"),
Jin Yaodaca23b2019-03-05 21:05:42 +08001185 OPT_STRING(0, "cpu", &cpu_list, "cpu", "list of cpus to profile"),
Jin Yaoc1d3e632019-03-05 21:05:43 +08001186 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1187 "only consider symbols in these pids"),
1188 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1189 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001190 OPT_END()
1191};
1192
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001193static double baseline_percent(struct hist_entry *he)
Jiri Olsa1d778222012-10-04 21:49:39 +09001194{
Namhyung Kim8810f6c2014-02-07 12:06:07 +09001195 u64 total = hists__total_period(he->hists);
1196
1197 return 100.0 * he->stat.period / total;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001198}
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001199
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001200static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
1201 struct perf_hpp *hpp, struct hist_entry *he)
1202{
1203 struct diff_hpp_fmt *dfmt =
1204 container_of(fmt, struct diff_hpp_fmt, fmt);
1205 double percent = baseline_percent(he);
1206 char pfmt[20] = " ";
1207
1208 if (!he->dummy) {
1209 scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
1210 return percent_color_snprintf(hpp->buf, hpp->size,
1211 pfmt, percent);
1212 } else
1213 return scnprintf(hpp->buf, hpp->size, "%*s",
1214 dfmt->header_width, pfmt);
1215}
1216
1217static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
1218{
1219 double percent = baseline_percent(he);
1220 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
1221 int ret = 0;
1222
1223 if (!he->dummy)
1224 ret = scnprintf(buf, size, fmt, percent);
1225
1226 return ret;
1227}
1228
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301229static int __hpp__color_compare(struct perf_hpp_fmt *fmt,
1230 struct perf_hpp *hpp, struct hist_entry *he,
1231 int comparison_method)
1232{
1233 struct diff_hpp_fmt *dfmt =
1234 container_of(fmt, struct diff_hpp_fmt, fmt);
1235 struct hist_entry *pair = get_pair_fmt(he, dfmt);
1236 double diff;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301237 s64 wdiff;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301238 char pfmt[20] = " ";
1239
1240 if (!pair)
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001241 goto no_print;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301242
1243 switch (comparison_method) {
1244 case COMPUTE_DELTA:
1245 if (pair->diff.computed)
1246 diff = pair->diff.period_ratio_delta;
1247 else
1248 diff = compute_delta(he, pair);
1249
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301250 scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1);
1251 return percent_color_snprintf(hpp->buf, hpp->size,
1252 pfmt, diff);
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301253 case COMPUTE_RATIO:
1254 if (he->dummy)
1255 goto dummy_print;
1256 if (pair->diff.computed)
1257 diff = pair->diff.period_ratio;
1258 else
1259 diff = compute_ratio(he, pair);
1260
1261 scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width);
1262 return value_color_snprintf(hpp->buf, hpp->size,
1263 pfmt, diff);
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301264 case COMPUTE_WEIGHTED_DIFF:
1265 if (he->dummy)
1266 goto dummy_print;
1267 if (pair->diff.computed)
1268 wdiff = pair->diff.wdiff;
1269 else
1270 wdiff = compute_wdiff(he, pair);
1271
1272 scnprintf(pfmt, 20, "%%14ld", dfmt->header_width);
1273 return color_snprintf(hpp->buf, hpp->size,
1274 get_percent_color(wdiff),
1275 pfmt, wdiff);
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301276 default:
1277 BUG_ON(1);
1278 }
1279dummy_print:
1280 return scnprintf(hpp->buf, hpp->size, "%*s",
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001281 dfmt->header_width, "N/A");
1282no_print:
1283 return scnprintf(hpp->buf, hpp->size, "%*s",
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301284 dfmt->header_width, pfmt);
1285}
1286
1287static int hpp__color_delta(struct perf_hpp_fmt *fmt,
1288 struct perf_hpp *hpp, struct hist_entry *he)
1289{
1290 return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA);
1291}
1292
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301293static int hpp__color_ratio(struct perf_hpp_fmt *fmt,
1294 struct perf_hpp *hpp, struct hist_entry *he)
1295{
1296 return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO);
1297}
1298
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301299static int hpp__color_wdiff(struct perf_hpp_fmt *fmt,
1300 struct perf_hpp *hpp, struct hist_entry *he)
1301{
1302 return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF);
1303}
1304
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001305static void
1306hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
1307{
1308 switch (idx) {
1309 case PERF_HPP_DIFF__PERIOD_BASELINE:
1310 scnprintf(buf, size, "%" PRIu64, he->stat.period);
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001311 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001312
1313 default:
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001314 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001315 }
1316}
1317
1318static void
1319hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
1320 int idx, char *buf, size_t size)
1321{
1322 double diff;
1323 double ratio;
1324 s64 wdiff;
1325
1326 switch (idx) {
1327 case PERF_HPP_DIFF__DELTA:
Namhyung Kima1668c22017-02-10 16:36:11 +09001328 case PERF_HPP_DIFF__DELTA_ABS:
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001329 if (pair->diff.computed)
1330 diff = pair->diff.period_ratio_delta;
1331 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001332 diff = compute_delta(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001333
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001334 scnprintf(buf, size, "%+4.2F%%", diff);
Jiri Olsa81d5f952012-10-05 16:44:43 +02001335 break;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001336
1337 case PERF_HPP_DIFF__RATIO:
1338 /* No point for ratio number if we are dummy.. */
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001339 if (he->dummy) {
1340 scnprintf(buf, size, "N/A");
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001341 break;
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001342 }
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001343
1344 if (pair->diff.computed)
1345 ratio = pair->diff.period_ratio;
1346 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001347 ratio = compute_ratio(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001348
1349 if (ratio > 0.0)
1350 scnprintf(buf, size, "%14.6F", ratio);
1351 break;
1352
1353 case PERF_HPP_DIFF__WEIGHTED_DIFF:
1354 /* No point for wdiff number if we are dummy.. */
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001355 if (he->dummy) {
1356 scnprintf(buf, size, "N/A");
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001357 break;
Namhyung Kimec3d07c2014-12-27 14:06:31 +09001358 }
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001359
1360 if (pair->diff.computed)
1361 wdiff = pair->diff.wdiff;
1362 else
Jiri Olsaef358e62012-10-21 23:31:51 +02001363 wdiff = compute_wdiff(he, pair);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001364
1365 if (wdiff != 0)
1366 scnprintf(buf, size, "%14ld", wdiff);
1367 break;
1368
1369 case PERF_HPP_DIFF__FORMULA:
Jiri Olsaef358e62012-10-21 23:31:51 +02001370 formula_fprintf(he, pair, buf, size);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001371 break;
1372
1373 case PERF_HPP_DIFF__PERIOD:
1374 scnprintf(buf, size, "%" PRIu64, pair->stat.period);
1375 break;
1376
Jiri Olsa7aaf6b32012-10-05 16:44:41 +02001377 default:
1378 BUG_ON(1);
1379 };
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001380}
1381
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001382static void
1383__hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
1384 char *buf, size_t size)
1385{
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001386 struct hist_entry *pair = get_pair_fmt(he, dfmt);
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001387 int idx = dfmt->idx;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001388
1389 /* baseline is special */
1390 if (idx == PERF_HPP_DIFF__BASELINE)
1391 hpp__entry_baseline(he, buf, size);
1392 else {
1393 if (pair)
1394 hpp__entry_pair(he, pair, idx, buf, size);
1395 else
1396 hpp__entry_unpair(he, idx, buf, size);
1397 }
1398}
1399
1400static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
1401 struct hist_entry *he)
1402{
1403 struct diff_hpp_fmt *dfmt =
1404 container_of(_fmt, struct diff_hpp_fmt, fmt);
1405 char buf[MAX_COL_WIDTH] = " ";
1406
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001407 __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001408
1409 if (symbol_conf.field_sep)
1410 return scnprintf(hpp->buf, hpp->size, "%s", buf);
1411 else
1412 return scnprintf(hpp->buf, hpp->size, "%*s",
1413 dfmt->header_width, buf);
1414}
1415
Namhyung Kim94a07932014-03-10 16:43:52 +09001416static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
Jiri Olsa74bb43f2016-08-07 17:28:27 +02001417 struct hists *hists __maybe_unused,
Jiri Olsa29659ab2016-08-07 17:28:30 +02001418 int line __maybe_unused,
1419 int *span __maybe_unused)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001420{
1421 struct diff_hpp_fmt *dfmt =
1422 container_of(fmt, struct diff_hpp_fmt, fmt);
1423
1424 BUG_ON(!dfmt->header);
1425 return scnprintf(hpp->buf, hpp->size, dfmt->header);
1426}
1427
1428static int hpp__width(struct perf_hpp_fmt *fmt,
Namhyung Kim94a07932014-03-10 16:43:52 +09001429 struct perf_hpp *hpp __maybe_unused,
Jiri Olsada1b0402016-06-14 20:19:20 +02001430 struct hists *hists __maybe_unused)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001431{
1432 struct diff_hpp_fmt *dfmt =
1433 container_of(fmt, struct diff_hpp_fmt, fmt);
1434
1435 BUG_ON(dfmt->header_width <= 0);
1436 return dfmt->header_width;
1437}
1438
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001439static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001440{
1441#define MAX_HEADER_NAME 100
1442 char buf_indent[MAX_HEADER_NAME];
1443 char buf[MAX_HEADER_NAME];
1444 const char *header = NULL;
1445 int width = 0;
1446
1447 BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
1448 header = columns[dfmt->idx].name;
1449 width = columns[dfmt->idx].width;
1450
1451 /* Only our defined HPP fmts should appear here. */
1452 BUG_ON(!header);
1453
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001454 if (data__files_cnt > 2)
1455 scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
1456
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001457#define NAME (data__files_cnt > 2 ? buf : header)
1458 dfmt->header_width = width;
1459 width = (int) strlen(NAME);
1460 if (dfmt->header_width < width)
1461 dfmt->header_width = width;
1462
1463 scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
1464 dfmt->header_width, NAME);
1465
1466 dfmt->header = strdup(buf_indent);
1467#undef MAX_HEADER_NAME
1468#undef NAME
1469}
1470
Jiri Olsac818b492012-12-01 21:57:04 +01001471static void data__hpp_register(struct data__file *d, int idx)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001472{
Jiri Olsac818b492012-12-01 21:57:04 +01001473 struct diff_hpp_fmt *dfmt = &d->fmt[idx];
1474 struct perf_hpp_fmt *fmt = &dfmt->fmt;
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001475
Jiri Olsac818b492012-12-01 21:57:04 +01001476 dfmt->idx = idx;
1477
1478 fmt->header = hpp__header;
1479 fmt->width = hpp__width;
1480 fmt->entry = hpp__entry_global;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001481 fmt->cmp = hist_entry__cmp_nop;
1482 fmt->collapse = hist_entry__cmp_nop;
Jiri Olsac818b492012-12-01 21:57:04 +01001483
1484 /* TODO more colors */
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301485 switch (idx) {
1486 case PERF_HPP_DIFF__BASELINE:
Jiri Olsac818b492012-12-01 21:57:04 +01001487 fmt->color = hpp__color_baseline;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001488 fmt->sort = hist_entry__cmp_baseline;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301489 break;
1490 case PERF_HPP_DIFF__DELTA:
1491 fmt->color = hpp__color_delta;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001492 fmt->sort = hist_entry__cmp_delta;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301493 break;
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301494 case PERF_HPP_DIFF__RATIO:
1495 fmt->color = hpp__color_ratio;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001496 fmt->sort = hist_entry__cmp_ratio;
Ramkumar Ramachandra1f513b22013-12-30 13:04:20 +05301497 break;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301498 case PERF_HPP_DIFF__WEIGHTED_DIFF:
1499 fmt->color = hpp__color_wdiff;
Namhyung Kime7024fc2014-12-27 14:06:29 +09001500 fmt->sort = hist_entry__cmp_wdiff;
Ramkumar Ramachandraa5846e22013-12-30 13:32:35 +05301501 break;
Namhyung Kima1668c22017-02-10 16:36:11 +09001502 case PERF_HPP_DIFF__DELTA_ABS:
1503 fmt->color = hpp__color_delta;
1504 fmt->sort = hist_entry__cmp_delta_abs;
1505 break;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301506 default:
Namhyung Kime7024fc2014-12-27 14:06:29 +09001507 fmt->sort = hist_entry__cmp_nop;
Ramkumar Ramachandra01f10bc2013-12-30 13:04:19 +05301508 break;
1509 }
Jiri Olsac818b492012-12-01 21:57:04 +01001510
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001511 init_header(d, dfmt);
Jiri Olsac818b492012-12-01 21:57:04 +01001512 perf_hpp__column_register(fmt);
Namhyung Kime7024fc2014-12-27 14:06:29 +09001513 perf_hpp__register_sort_field(fmt);
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001514}
1515
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001516static int ui_init(void)
Jiri Olsa345dc0b2013-02-03 20:08:34 +01001517{
Jiri Olsac818b492012-12-01 21:57:04 +01001518 struct data__file *d;
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001519 struct perf_hpp_fmt *fmt;
Jiri Olsac818b492012-12-01 21:57:04 +01001520 int i;
Jiri Olsa1d778222012-10-04 21:49:39 +09001521
Jiri Olsac818b492012-12-01 21:57:04 +01001522 data__for_each_file(i, d) {
Jiri Olsaed279da2012-10-05 16:44:45 +02001523
Jiri Olsac818b492012-12-01 21:57:04 +01001524 /*
1525 * Baseline or compute realted columns:
1526 *
1527 * PERF_HPP_DIFF__BASELINE
1528 * PERF_HPP_DIFF__DELTA
1529 * PERF_HPP_DIFF__RATIO
1530 * PERF_HPP_DIFF__WEIGHTED_DIFF
1531 */
1532 data__hpp_register(d, i ? compute_2_hpp[compute] :
1533 PERF_HPP_DIFF__BASELINE);
1534
1535 /*
1536 * And the rest:
1537 *
1538 * PERF_HPP_DIFF__FORMULA
1539 * PERF_HPP_DIFF__PERIOD
1540 * PERF_HPP_DIFF__PERIOD_BASELINE
1541 */
1542 if (show_formula && i)
1543 data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
1544
1545 if (show_period)
1546 data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
1547 PERF_HPP_DIFF__PERIOD_BASELINE);
Jiri Olsa61949b22012-10-05 16:44:44 +02001548 }
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001549
1550 if (!sort_compute)
1551 return 0;
1552
1553 /*
1554 * Prepend an fmt to sort on columns at 'sort_compute' first.
1555 * This fmt is added only to the sort list but not to the
1556 * output fields list.
1557 *
1558 * Note that this column (data) can be compared twice - one
1559 * for this 'sort_compute' fmt and another for the normal
1560 * diff_hpp_fmt. But it shouldn't a problem as most entries
1561 * will be sorted out by first try or baseline and comparing
1562 * is not a costly operation.
1563 */
1564 fmt = zalloc(sizeof(*fmt));
1565 if (fmt == NULL) {
1566 pr_err("Memory allocation failed\n");
1567 return -1;
1568 }
1569
1570 fmt->cmp = hist_entry__cmp_nop;
1571 fmt->collapse = hist_entry__cmp_nop;
1572
1573 switch (compute) {
1574 case COMPUTE_DELTA:
1575 fmt->sort = hist_entry__cmp_delta_idx;
1576 break;
1577 case COMPUTE_RATIO:
1578 fmt->sort = hist_entry__cmp_ratio_idx;
1579 break;
1580 case COMPUTE_WEIGHTED_DIFF:
1581 fmt->sort = hist_entry__cmp_wdiff_idx;
1582 break;
Namhyung Kima1668c22017-02-10 16:36:11 +09001583 case COMPUTE_DELTA_ABS:
1584 fmt->sort = hist_entry__cmp_delta_abs_idx;
1585 break;
Jin Yao99150a12019-06-28 17:23:01 +08001586 case COMPUTE_CYCLES:
1587 /*
1588 * Should set since 'fmt->sort' is called without
1589 * checking valid during sorting
1590 */
1591 fmt->sort = hist_entry__cmp_nop;
1592 break;
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001593 default:
1594 BUG_ON(1);
1595 }
1596
Namhyung Kima1c9f972017-01-18 14:14:57 +09001597 perf_hpp__prepend_sort_field(fmt);
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001598 return 0;
Jiri Olsa1d778222012-10-04 21:49:39 +09001599}
1600
Jiri Olsaec308422013-03-25 00:02:01 +01001601static int data_init(int argc, const char **argv)
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001602{
Jiri Olsaec308422013-03-25 00:02:01 +01001603 struct data__file *d;
1604 static const char *defaults[] = {
1605 "perf.data.old",
1606 "perf.data",
1607 };
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001608 bool use_default = true;
Jiri Olsaec308422013-03-25 00:02:01 +01001609 int i;
1610
1611 data__files_cnt = 2;
1612
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001613 if (argc) {
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001614 if (argc == 1)
Jiri Olsaec308422013-03-25 00:02:01 +01001615 defaults[1] = argv[0];
Jiri Olsa22aeb7f2012-12-01 22:00:00 +01001616 else {
1617 data__files_cnt = argc;
1618 use_default = false;
1619 }
Dongsheng Yangd8d96082013-12-06 17:25:52 -05001620 } else if (perf_guest) {
Jiri Olsaec308422013-03-25 00:02:01 +01001621 defaults[0] = "perf.data.host";
1622 defaults[1] = "perf.data.guest";
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001623 }
1624
Jiri Olsa5f3f8d32012-11-25 23:10:20 +01001625 if (sort_compute >= (unsigned int) data__files_cnt) {
1626 pr_err("Order option out of limit.\n");
1627 return -EINVAL;
1628 }
1629
Jiri Olsaec308422013-03-25 00:02:01 +01001630 data__files = zalloc(sizeof(*data__files) * data__files_cnt);
1631 if (!data__files)
1632 return -ENOMEM;
1633
1634 data__for_each_file(i, d) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001635 struct perf_data *data = &d->data;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001636
Jiri Olsa2d4f2792019-02-21 10:41:30 +01001637 data->path = use_default ? defaults[i] : argv[i];
1638 data->mode = PERF_DATA_MODE_READ,
1639 data->force = force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001640
Jiri Olsaec308422013-03-25 00:02:01 +01001641 d->idx = i;
1642 }
1643
1644 return 0;
1645}
1646
Namhyung Kimd49dd152017-02-10 16:36:12 +09001647static int diff__config(const char *var, const char *value,
1648 void *cb __maybe_unused)
1649{
1650 if (!strcmp(var, "diff.order")) {
Arnaldo Carvalho de Melo25ce4bb2017-06-27 11:44:58 -03001651 int ret;
1652 if (perf_config_int(&ret, var, value) < 0)
1653 return -1;
1654 sort_compute = ret;
Namhyung Kimd49dd152017-02-10 16:36:12 +09001655 return 0;
1656 }
Namhyung Kim4b359942017-02-10 16:36:13 +09001657 if (!strcmp(var, "diff.compute")) {
1658 if (!strcmp(value, "delta")) {
1659 compute = COMPUTE_DELTA;
1660 } else if (!strcmp(value, "delta-abs")) {
1661 compute = COMPUTE_DELTA_ABS;
1662 } else if (!strcmp(value, "ratio")) {
1663 compute = COMPUTE_RATIO;
1664 } else if (!strcmp(value, "wdiff")) {
1665 compute = COMPUTE_WEIGHTED_DIFF;
1666 } else {
1667 pr_err("Invalid compute method: %s\n", value);
1668 return -1;
1669 }
1670 }
Namhyung Kimd49dd152017-02-10 16:36:12 +09001671
1672 return 0;
1673}
1674
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001675int cmd_diff(int argc, const char **argv)
Jiri Olsaec308422013-03-25 00:02:01 +01001676{
Kan Liang9ab1f502014-10-22 15:02:41 -04001677 int ret = hists__init();
1678
1679 if (ret < 0)
1680 return ret;
1681
Namhyung Kimd49dd152017-02-10 16:36:12 +09001682 perf_config(diff__config, NULL);
1683
Jiri Olsaec308422013-03-25 00:02:01 +01001684 argc = parse_options(argc, argv, options, diff_usage, 0);
1685
Namhyung Kim63b42fc2017-02-17 17:17:40 +09001686 if (quiet)
1687 perf_quiet_option();
1688
Jin Yao99150a12019-06-28 17:23:01 +08001689 symbol__annotation_init();
1690
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001691 if (symbol__init(NULL) < 0)
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001692 return -1;
1693
Jiri Olsaec308422013-03-25 00:02:01 +01001694 if (data_init(argc, argv) < 0)
1695 return -1;
1696
Jin Yao30d81552019-06-28 17:23:00 +08001697 if (check_file_brstack() < 0)
1698 return -1;
1699
Jin Yao99150a12019-06-28 17:23:01 +08001700 if (compute == COMPUTE_CYCLES && !pdiff.has_br_stack)
1701 return -1;
1702
Namhyung Kim566b5cf2015-01-08 09:45:48 +09001703 if (ui_init() < 0)
1704 return -1;
Jiri Olsa1d778222012-10-04 21:49:39 +09001705
Namhyung Kim512ae1b2014-03-18 11:31:39 +09001706 sort__mode = SORT_MODE__DIFF;
1707
Namhyung Kim40184c42015-12-23 02:07:01 +09001708 if (setup_sorting(NULL) < 0)
Namhyung Kim55309982013-02-06 14:57:16 +09001709 usage_with_options(diff_usage, options);
1710
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001711 setup_pager();
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001712
Namhyung Kim08e71542013-04-03 21:26:19 +09001713 sort__setup_elide(NULL);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001714
Arnaldo Carvalho de Melo86a9eee2009-12-14 20:09:31 -02001715 return __cmd_diff();
1716}