1052 lines · 22.9 KB · C
RawDownload
/* * builtin-diff.c * * Builtin diff command: Analyze two perf.data input files, look up and read * DSOs and symbol information, sort them and produce a diff. */#include "builtin.h" #include "util/debug.h"#include "util/event.h"#include "util/hist.h"#include "util/evsel.h"#include "util/evlist.h"#include "util/session.h"#include "util/tool.h"#include "util/sort.h"#include "util/symbol.h"#include "util/util.h"#include "util/data.h" #include <stdlib.h>#include <math.h> /* Diff command specific HPP columns. */enum {	PERF_HPP_DIFF__BASELINE,	PERF_HPP_DIFF__PERIOD,	PERF_HPP_DIFF__PERIOD_BASELINE,	PERF_HPP_DIFF__DELTA,	PERF_HPP_DIFF__RATIO,	PERF_HPP_DIFF__WEIGHTED_DIFF,	PERF_HPP_DIFF__FORMULA, 	PERF_HPP_DIFF__MAX_INDEX}; struct diff_hpp_fmt {	struct perf_hpp_fmt	 fmt;	int			 idx;	char			*header;	int			 header_width;}; struct data__file {	struct perf_session	*session;	struct perf_data_file	file;	int			 idx;	struct hists		*hists;	struct diff_hpp_fmt	 fmt[PERF_HPP_DIFF__MAX_INDEX];}; static struct data__file *data__files;static int data__files_cnt; #define data__for_each_file_start(i, d, s)	\	for (i = s, d = &data__files[s];	\	     i < data__files_cnt;		\	     i++, d = &data__files[i]) #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)#define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1) static char diff__default_sort_order[] = "dso,symbol";static bool force;static bool show_period;static bool show_formula;static bool show_baseline_only;static unsigned int sort_compute; static s64 compute_wdiff_w1;static s64 compute_wdiff_w2; enum {	COMPUTE_DELTA,	COMPUTE_RATIO,	COMPUTE_WEIGHTED_DIFF,	COMPUTE_MAX,}; const char *compute_names[COMPUTE_MAX] = {	[COMPUTE_DELTA] = "delta",	[COMPUTE_RATIO] = "ratio",	[COMPUTE_WEIGHTED_DIFF] = "wdiff",}; static int compute; static int compute_2_hpp[COMPUTE_MAX] = {	[COMPUTE_DELTA]		= PERF_HPP_DIFF__DELTA,	[COMPUTE_RATIO]		= PERF_HPP_DIFF__RATIO,	[COMPUTE_WEIGHTED_DIFF]	= PERF_HPP_DIFF__WEIGHTED_DIFF,}; #define MAX_COL_WIDTH 70 static struct header_column {	const char *name;	int width;} columns[PERF_HPP_DIFF__MAX_INDEX] = {	[PERF_HPP_DIFF__BASELINE] = {		.name  = "Baseline",	},	[PERF_HPP_DIFF__PERIOD] = {		.name  = "Period",		.width = 14,	},	[PERF_HPP_DIFF__PERIOD_BASELINE] = {		.name  = "Base period",		.width = 14,	},	[PERF_HPP_DIFF__DELTA] = {		.name  = "Delta",		.width = 7,	},	[PERF_HPP_DIFF__RATIO] = {		.name  = "Ratio",		.width = 14,	},	[PERF_HPP_DIFF__WEIGHTED_DIFF] = {		.name  = "Weighted diff",		.width = 14,	},	[PERF_HPP_DIFF__FORMULA] = {		.name  = "Formula",		.width = MAX_COL_WIDTH,	}}; static int setup_compute_opt_wdiff(char *opt){	char *w1_str = opt;	char *w2_str; 	int ret = -EINVAL; 	if (!opt)		goto out; 	w2_str = strchr(opt, ',');	if (!w2_str)		goto out; 	*w2_str++ = 0x0;	if (!*w2_str)		goto out; 	compute_wdiff_w1 = strtol(w1_str, NULL, 10);	compute_wdiff_w2 = strtol(w2_str, NULL, 10); 	if (!compute_wdiff_w1 || !compute_wdiff_w2)		goto out; 	pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",		  compute_wdiff_w1, compute_wdiff_w2); 	ret = 0;  out:	if (ret)		pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n"); 	return ret;} static int setup_compute_opt(char *opt){	if (compute == COMPUTE_WEIGHTED_DIFF)		return setup_compute_opt_wdiff(opt); 	if (opt) {		pr_err("Failed: extra option specified '%s'", opt);		return -EINVAL;	} 	return 0;} static int setup_compute(const struct option *opt, const char *str,			 int unset __maybe_unused){	int *cp = (int *) opt->value;	char *cstr = (char *) str;	char buf[50];	unsigned i;	char *option; 	if (!str) {		*cp = COMPUTE_DELTA;		return 0;	} 	option = strchr(str, ':');	if (option) {		unsigned len = option++ - str; 		/*		 * The str data are not writeable, so we need		 * to use another buffer.		 */ 		/* No option value is longer. */		if (len >= sizeof(buf))			return -EINVAL; 		strncpy(buf, str, len);		buf[len] = 0x0;		cstr = buf;	} 	for (i = 0; i < COMPUTE_MAX; i++)		if (!strcmp(cstr, compute_names[i])) {			*cp = i;			return setup_compute_opt(option);		} 	pr_err("Failed: '%s' is not computation method "	       "(use 'delta','ratio' or 'wdiff')\n", str);	return -EINVAL;} static double period_percent(struct hist_entry *he, u64 period){	u64 total = he->hists->stats.total_period;	return (period * 100.0) / total;} static double compute_delta(struct hist_entry *he, struct hist_entry *pair){	double old_percent = period_percent(he, he->stat.period);	double new_percent = period_percent(pair, pair->stat.period); 	pair->diff.period_ratio_delta = new_percent - old_percent;	pair->diff.computed = true;	return pair->diff.period_ratio_delta;} static double compute_ratio(struct hist_entry *he, struct hist_entry *pair){	double old_period = he->stat.period ?: 1;	double new_period = pair->stat.period; 	pair->diff.computed = true;	pair->diff.period_ratio = new_period / old_period;	return pair->diff.period_ratio;} static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair){	u64 old_period = he->stat.period;	u64 new_period = pair->stat.period; 	pair->diff.computed = true;	pair->diff.wdiff = new_period * compute_wdiff_w2 -			   old_period * compute_wdiff_w1; 	return pair->diff.wdiff;} static int formula_delta(struct hist_entry *he, struct hist_entry *pair,			 char *buf, size_t size){	return scnprintf(buf, size,			 "(%" PRIu64 " * 100 / %" PRIu64 ") - "			 "(%" PRIu64 " * 100 / %" PRIu64 ")",			  pair->stat.period, pair->hists->stats.total_period,			  he->stat.period, he->hists->stats.total_period);} static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,			 char *buf, size_t size){	double old_period = he->stat.period;	double new_period = pair->stat.period; 	return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);} static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,			 char *buf, size_t size){	u64 old_period = he->stat.period;	u64 new_period = pair->stat.period; 	return scnprintf(buf, size,		  "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",		  new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);} static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,			   char *buf, size_t size){	switch (compute) {	case COMPUTE_DELTA:		return formula_delta(he, pair, buf, size);	case COMPUTE_RATIO:		return formula_ratio(he, pair, buf, size);	case COMPUTE_WEIGHTED_DIFF:		return formula_wdiff(he, pair, buf, size);	default:		BUG_ON(1);	} 	return -1;} static int hists__add_entry(struct hists *hists,			    struct addr_location *al, u64 period,			    u64 weight, u64 transaction){	if (__hists__add_entry(hists, al, NULL, NULL, NULL, period, weight,			       transaction) != NULL)		return 0;	return -ENOMEM;} static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,				      union perf_event *event,				      struct perf_sample *sample,				      struct perf_evsel *evsel,				      struct machine *machine){	struct addr_location al; 	if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {		pr_warning("problem processing %d event, skipping it.\n",			   event->header.type);		return -1;	} 	if (al.filtered)		return 0; 	if (hists__add_entry(&evsel->hists, &al, sample->period,			     sample->weight, sample->transaction)) {		pr_warning("problem incrementing symbol period, skipping event\n");		return -1;	} 	evsel->hists.stats.total_period += sample->period;	return 0;} static struct perf_tool tool = {	.sample	= diff__process_sample_event,	.mmap	= perf_event__process_mmap,	.comm	= perf_event__process_comm,	.exit	= perf_event__process_exit,	.fork	= perf_event__process_fork,	.lost	= perf_event__process_lost,	.ordered_samples = true,	.ordering_requires_timestamps = true,}; static struct perf_evsel *evsel_match(struct perf_evsel *evsel,				      struct perf_evlist *evlist){	struct perf_evsel *e; 	list_for_each_entry(e, &evlist->entries, node)		if (perf_evsel__match2(evsel, e))			return e; 	return NULL;} static void perf_evlist__collapse_resort(struct perf_evlist *evlist){	struct perf_evsel *evsel; 	list_for_each_entry(evsel, &evlist->entries, node) {		struct hists *hists = &evsel->hists; 		hists__collapse_resort(hists, NULL);	}} static struct hist_entry*get_pair_data(struct hist_entry *he, struct data__file *d){	if (hist_entry__has_pairs(he)) {		struct hist_entry *pair; 		list_for_each_entry(pair, &he->pairs.head, pairs.node)			if (pair->hists == d->hists)				return pair;	} 	return NULL;} static struct hist_entry*get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt){	void *ptr = dfmt - dfmt->idx;	struct data__file *d = container_of(ptr, struct data__file, fmt); 	return get_pair_data(he, d);} static void hists__baseline_only(struct hists *hists){	struct rb_root *root;	struct rb_node *next; 	if (sort__need_collapse)		root = &hists->entries_collapsed;	else		root = hists->entries_in; 	next = rb_first(root);	while (next != NULL) {		struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in); 		next = rb_next(&he->rb_node_in);		if (!hist_entry__next_pair(he)) {			rb_erase(&he->rb_node_in, root);			hist_entry__free(he);		}	}} static void hists__precompute(struct hists *hists){	struct rb_root *root;	struct rb_node *next; 	if (sort__need_collapse)		root = &hists->entries_collapsed;	else		root = hists->entries_in; 	next = rb_first(root);	while (next != NULL) {		struct hist_entry *he, *pair; 		he   = rb_entry(next, struct hist_entry, rb_node_in);		next = rb_next(&he->rb_node_in); 		pair = get_pair_data(he, &data__files[sort_compute]);		if (!pair)			continue; 		switch (compute) {		case COMPUTE_DELTA:			compute_delta(he, pair);			break;		case COMPUTE_RATIO:			compute_ratio(he, pair);			break;		case COMPUTE_WEIGHTED_DIFF:			compute_wdiff(he, pair);			break;		default:			BUG_ON(1);		}	}} static int64_t cmp_doubles(double l, double r){	if (l > r)		return -1;	else if (l < r)		return 1;	else		return 0;} static int64_t__hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,			int c){	switch (c) {	case COMPUTE_DELTA:	{		double l = left->diff.period_ratio_delta;		double r = right->diff.period_ratio_delta; 		return cmp_doubles(l, r);	}	case COMPUTE_RATIO:	{		double l = left->diff.period_ratio;		double r = right->diff.period_ratio; 		return cmp_doubles(l, r);	}	case COMPUTE_WEIGHTED_DIFF:	{		s64 l = left->diff.wdiff;		s64 r = right->diff.wdiff; 		return r - l;	}	default:		BUG_ON(1);	} 	return 0;}

Lines 1–500 of 1052.