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