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