Commit 2b62b3a6 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

perf parse-events: Add new "metric-id" term



Add a new "metric-id" term to events so that metric parsing can set an
ID that can be reliably looked up.

Metric parsing currently will turn a metric like "instructions/cycles"
into a parse events string of "{instructions,cycles}:W".

However, parse-events may change "instructions" into "instructions:u" if
perf_event_paranoid=2.

When this happens expr__resolve_id currently fails as stat-shadow adds
the ID "instructions:u" to match with the counter value and the metric
tries to look up the ID just "instructions".

A later patch will use the new term.

An example of the current problem:

  $ echo -1 > /proc/sys/kernel/perf_event_paranoid
  $ perf stat -M IPC /bin/true
   Performance counter stats for '/bin/true':

           1,217,161      inst_retired.any          #     0.97 IPC
           1,250,389      cpu_clk_unhalted.thread

         0.002064773 seconds time elapsed

         0.002378000 seconds user
         0.000000000 seconds sys

  $ echo 2 > /proc/sys/kernel/perf_event_paranoid
  $ perf stat -M IPC /bin/true
   Performance counter stats for '/bin/true':

             150,298      inst_retired.any:u        #      nan IPC
             187,095      cpu_clk_unhalted.thread:u

         0.002042731 seconds time elapsed

         0.000000000 seconds user
         0.002377000 seconds sys

Note: nan IPC is printed as an effect of "perf metric: Use NAN for
missing event IDs." but earlier versions of perf just fail with a parse
error and display no value.

Signed-off-by: default avatarIan Rogers <irogers@google.com>
Acked-by: default avatarAndi Kleen <ak@linux.intel.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Antonov <alexander.antonov@linux.intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andrew Kilroy <andrew.kilroy@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Changbin Du <changbin.du@intel.com>
Cc: Denys Zagorui <dzagorui@cisco.com>
Cc: Fabian Hemmer <copy@copy.sh>
Cc: Felix Fietkau <nbd@nbd.name>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jacob Keller <jacob.e.keller@intel.com>
Cc: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Joakim Zhang <qiangqing.zhang@nxp.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Kees Kook <keescook@chromium.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nicholas Fraser <nfraser@codeweavers.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Paul Clarke <pc@us.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: ShihCheng Tu <mrtoastcheng@gmail.com>
Cc: Song Liu <songliubraving@fb.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Sumanth Korikkar <sumanthk@linux.ibm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Wan Jiabing <wanjiabing@vivo.com>
Cc: Zhen Lei <thunder.leizhen@huawei.com>
Link: https://lore.kernel.org/r/20211015172132.1162559-15-irogers@google.com


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 8e8bbfb3
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -410,6 +410,11 @@ struct evsel *evsel__clone(struct evsel *orig)
		if (evsel->filter == NULL)
			goto out_err;
	}
	if (orig->metric_id) {
		evsel->metric_id = strdup(orig->metric_id);
		if (evsel->metric_id == NULL)
			goto out_err;
	}
	evsel->cgrp = cgroup__get(orig->cgrp);
	evsel->tp_format = orig->tp_format;
	evsel->handler = orig->handler;
@@ -779,6 +784,17 @@ const char *evsel__name(struct evsel *evsel)
	return "unknown";
}

const char *evsel__metric_id(const struct evsel *evsel)
{
	if (evsel->metric_id)
		return evsel->metric_id;

	if (evsel->core.attr.type == PERF_TYPE_SOFTWARE && evsel->tool_event)
		return "duration_time";

	return "unknown";
}

const char *evsel__group_name(struct evsel *evsel)
{
	return evsel->group_name ?: "anon group";
@@ -1423,6 +1439,7 @@ void evsel__exit(struct evsel *evsel)
	zfree(&evsel->group_name);
	zfree(&evsel->name);
	zfree(&evsel->pmu_name);
	zfree(&evsel->metric_id);
	evsel__zero_per_pkg(evsel);
	hashmap__free(evsel->per_pkg_mask);
	evsel->per_pkg_mask = NULL;
+2 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ struct evsel {
		double			scale;
		const char		*unit;
		struct cgroup		*cgrp;
		const char		*metric_id;
		enum perf_tool_event	tool_event;
		/* parse modifier helper */
		int			exclude_GH;
@@ -261,6 +262,7 @@ bool evsel__match_bpf_counter_events(const char *name);

int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size);
const char *evsel__name(struct evsel *evsel);
const char *evsel__metric_id(const struct evsel *evsel);

const char *evsel__group_name(struct evsel *evsel);
int evsel__group_desc(struct evsel *evsel, char *buf, size_t size);
+15 −10
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ static void config_hybrid_attr(struct perf_event_attr *attr,
static int create_event_hybrid(__u32 config_type, int *idx,
			       struct list_head *list,
			       struct perf_event_attr *attr, const char *name,
			       const char *metric_id,
			       struct list_head *config_terms,
			       struct perf_pmu *pmu)
{
@@ -47,7 +48,7 @@ static int create_event_hybrid(__u32 config_type, int *idx,
	__u64 config = attr->config;

	config_hybrid_attr(attr, config_type, pmu->type);
	evsel = parse_events__add_event_hybrid(list, idx, attr, name,
	evsel = parse_events__add_event_hybrid(list, idx, attr, name, metric_id,
					       pmu, config_terms);
	if (evsel)
		evsel->pmu_name = strdup(pmu->name);
@@ -70,7 +71,8 @@ static int pmu_cmp(struct parse_events_state *parse_state,

static int add_hw_hybrid(struct parse_events_state *parse_state,
			 struct list_head *list, struct perf_event_attr *attr,
			 const char *name, struct list_head *config_terms)
			 const char *name, const char *metric_id,
			 struct list_head *config_terms)
{
	struct perf_pmu *pmu;
	int ret;
@@ -84,7 +86,7 @@ static int add_hw_hybrid(struct parse_events_state *parse_state,
		copy_config_terms(&terms, config_terms);
		ret = create_event_hybrid(PERF_TYPE_HARDWARE,
					  &parse_state->idx, list, attr, name,
					  &terms, pmu);
					  metric_id, &terms, pmu);
		free_config_terms(&terms);
		if (ret)
			return ret;
@@ -96,13 +98,14 @@ static int add_hw_hybrid(struct parse_events_state *parse_state,
static int create_raw_event_hybrid(int *idx, struct list_head *list,
				   struct perf_event_attr *attr,
				   const char *name,
				   const char *metric_id,
				   struct list_head *config_terms,
				   struct perf_pmu *pmu)
{
	struct evsel *evsel;

	attr->type = pmu->type;
	evsel = parse_events__add_event_hybrid(list, idx, attr, name,
	evsel = parse_events__add_event_hybrid(list, idx, attr, name, metric_id,
					       pmu, config_terms);
	if (evsel)
		evsel->pmu_name = strdup(pmu->name);
@@ -114,7 +117,8 @@ static int create_raw_event_hybrid(int *idx, struct list_head *list,

static int add_raw_hybrid(struct parse_events_state *parse_state,
			  struct list_head *list, struct perf_event_attr *attr,
			  const char *name, struct list_head *config_terms)
			  const char *name, const char *metric_id,
			  struct list_head *config_terms)
{
	struct perf_pmu *pmu;
	int ret;
@@ -127,7 +131,7 @@ static int add_raw_hybrid(struct parse_events_state *parse_state,

		copy_config_terms(&terms, config_terms);
		ret = create_raw_event_hybrid(&parse_state->idx, list, attr,
					      name, &terms, pmu);
					      name, metric_id, &terms, pmu);
		free_config_terms(&terms);
		if (ret)
			return ret;
@@ -139,7 +143,7 @@ static int add_raw_hybrid(struct parse_events_state *parse_state,
int parse_events__add_numeric_hybrid(struct parse_events_state *parse_state,
				     struct list_head *list,
				     struct perf_event_attr *attr,
				     const char *name,
				     const char *name, const char *metric_id,
				     struct list_head *config_terms,
				     bool *hybrid)
{
@@ -152,17 +156,18 @@ int parse_events__add_numeric_hybrid(struct parse_events_state *parse_state,

	*hybrid = true;
	if (attr->type != PERF_TYPE_RAW) {
		return add_hw_hybrid(parse_state, list, attr, name,
		return add_hw_hybrid(parse_state, list, attr, name, metric_id,
				     config_terms);
	}

	return add_raw_hybrid(parse_state, list, attr, name,
	return add_raw_hybrid(parse_state, list, attr, name, metric_id,
			      config_terms);
}

int parse_events__add_cache_hybrid(struct list_head *list, int *idx,
				   struct perf_event_attr *attr,
				   const char *name,
				   const char *metric_id,
				   struct list_head *config_terms,
				   bool *hybrid,
				   struct parse_events_state *parse_state)
@@ -183,7 +188,7 @@ int parse_events__add_cache_hybrid(struct list_head *list, int *idx,

		copy_config_terms(&terms, config_terms);
		ret = create_event_hybrid(PERF_TYPE_HW_CACHE, idx, list,
					  attr, name, &terms, pmu);
					  attr, name, metric_id, &terms, pmu);
		free_config_terms(&terms);
		if (ret)
			return ret;
+2 −2
Original line number Diff line number Diff line
@@ -11,13 +11,13 @@
int parse_events__add_numeric_hybrid(struct parse_events_state *parse_state,
				     struct list_head *list,
				     struct perf_event_attr *attr,
				     const char *name,
				     const char *name, const char *metric_id,
				     struct list_head *config_terms,
				     bool *hybrid);

int parse_events__add_cache_hybrid(struct list_head *list, int *idx,
				   struct perf_event_attr *attr,
				   const char *name,
				   const char *name, const char *metric_id,
				   struct list_head *config_terms,
				   bool *hybrid,
				   struct parse_events_state *parse_state);
+64 −31
Original line number Diff line number Diff line
@@ -332,12 +332,7 @@ const char *event_type(int type)
	return "unknown";
}

static int parse_events__is_name_term(struct parse_events_term *term)
{
	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
}

static const char *get_config_name(struct list_head *head_terms)
static char *get_config_str(struct list_head *head_terms, int type_term)
{
	struct parse_events_term *term;

@@ -345,17 +340,27 @@ static const char *get_config_name(struct list_head *head_terms)
		return NULL;

	list_for_each_entry(term, head_terms, list)
		if (parse_events__is_name_term(term))
		if (term->type_term == type_term)
			return term->val.str;

	return NULL;
}

static char *get_config_metric_id(struct list_head *head_terms)
{
	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID);
}

static char *get_config_name(struct list_head *head_terms)
{
	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME);
}

static struct evsel *
__add_event(struct list_head *list, int *idx,
	    struct perf_event_attr *attr,
	    bool init_attr,
	    const char *name, struct perf_pmu *pmu,
	    const char *name, const char *metric_id, struct perf_pmu *pmu,
	    struct list_head *config_terms, bool auto_merge_stats,
	    const char *cpu_list)
{
@@ -384,6 +389,9 @@ __add_event(struct list_head *list, int *idx,
	if (name)
		evsel->name = strdup(name);

	if (metric_id)
		evsel->metric_id = strdup(metric_id);

	if (config_terms)
		list_splice_init(config_terms, &evsel->config_terms);

@@ -394,18 +402,21 @@ __add_event(struct list_head *list, int *idx,
}

struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
				      const char *name, struct perf_pmu *pmu)
				      const char *name, const char *metric_id,
				      struct perf_pmu *pmu)
{
	return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
			   NULL);
	return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
			   metric_id, pmu, /*config_terms=*/NULL,
			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL);
}

static int add_event(struct list_head *list, int *idx,
		     struct perf_event_attr *attr, const char *name,
		     struct list_head *config_terms)
		     const char *metric_id, struct list_head *config_terms)
{
	return __add_event(list, idx, attr, true, name, NULL, config_terms,
			   false, NULL) ? 0 : -ENOMEM;
	return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
			   /*pmu=*/NULL, config_terms,
			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM;
}

static int add_event_tool(struct list_head *list, int *idx,
@@ -417,8 +428,10 @@ static int add_event_tool(struct list_head *list, int *idx,
		.config = PERF_COUNT_SW_DUMMY,
	};

	evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
			    "0");
	evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL,
			    /*metric_id=*/NULL, /*pmu=*/NULL,
			    /*config_terms=*/NULL, /*auto_merge_stats=*/false,
			    /*cpu_list=*/"0");
	if (!evsel)
		return -ENOMEM;
	evsel->tool_event = tool_event;
@@ -465,7 +478,7 @@ int parse_events_add_cache(struct list_head *list, int *idx,
	struct perf_event_attr attr;
	LIST_HEAD(config_terms);
	char name[MAX_NAME_LEN];
	const char *config_name;
	const char *config_name, *metric_id;
	int cache_type = -1, cache_op = -1, cache_result = -1;
	char *op_result[2] = { op_result1, op_result2 };
	int i, n, ret;
@@ -530,13 +543,17 @@ int parse_events_add_cache(struct list_head *list, int *idx,
			return -ENOMEM;
	}

	metric_id = get_config_metric_id(head_config);
	ret = parse_events__add_cache_hybrid(list, idx, &attr,
					     config_name ? : name, &config_terms,
					     config_name ? : name,
					     metric_id,
					     &config_terms,
					     &hybrid, parse_state);
	if (hybrid)
		goto out_free_terms;

	ret = add_event(list, idx, &attr, config_name ? : name, &config_terms);
	ret = add_event(list, idx, &attr, config_name ? : name, metric_id,
			&config_terms);
out_free_terms:
	free_config_terms(&config_terms);
	return ret;
@@ -1013,7 +1030,8 @@ int parse_events_add_breakpoint(struct list_head *list, int *idx,
	attr.type = PERF_TYPE_BREAKPOINT;
	attr.sample_period = 1;

	return add_event(list, idx, &attr, NULL, NULL);
	return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL,
			 /*config_terms=*/NULL);
}

static int check_type_val(struct parse_events_term *term,
@@ -1058,6 +1076,7 @@ static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
	[PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]		= "aux-output",
	[PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]	= "aux-sample-size",
	[PARSE_EVENTS__TERM_TYPE_METRIC_ID]		= "metric-id",
};

static bool config_term_shrinked;
@@ -1080,6 +1099,7 @@ config_term_avail(int term_type, struct parse_events_error *err)
	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
	case PARSE_EVENTS__TERM_TYPE_NAME:
	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
	case PARSE_EVENTS__TERM_TYPE_PERCORE:
		return true;
@@ -1170,6 +1190,9 @@ do { \
	case PARSE_EVENTS__TERM_TYPE_NAME:
		CHECK_TYPE_VAL(STR);
		break;
	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
		CHECK_TYPE_VAL(STR);
		break;
	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
		CHECK_TYPE_VAL(NUM);
		break;
@@ -1439,6 +1462,7 @@ int parse_events_add_numeric(struct parse_events_state *parse_state,
{
	struct perf_event_attr attr;
	LIST_HEAD(config_terms);
	const char *name, *metric_id;
	bool hybrid;
	int ret;

@@ -1455,14 +1479,16 @@ int parse_events_add_numeric(struct parse_events_state *parse_state,
			return -ENOMEM;
	}

	name = get_config_name(head_config);
	metric_id = get_config_metric_id(head_config);
	ret = parse_events__add_numeric_hybrid(parse_state, list, &attr,
					       get_config_name(head_config),
					       name, metric_id,
					       &config_terms, &hybrid);
	if (hybrid)
		goto out_free_terms;

	ret = add_event(list, &parse_state->idx, &attr,
			get_config_name(head_config), &config_terms);
	ret = add_event(list, &parse_state->idx, &attr, name, metric_id,
			&config_terms);
out_free_terms:
	free_config_terms(&config_terms);
	return ret;
@@ -1563,8 +1589,11 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,

	if (!head_config) {
		attr.type = pmu->type;
		evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
				    pmu, NULL, auto_merge_stats, NULL);
		evsel = __add_event(list, &parse_state->idx, &attr,
				    /*init_attr=*/true, /*name=*/NULL,
				    /*metric_id=*/NULL, pmu,
				    /*config_terms=*/NULL, auto_merge_stats,
				    /*cpu_list=*/NULL);
		if (evsel) {
			evsel->pmu_name = name ? strdup(name) : NULL;
			evsel->use_uncore_alias = use_uncore_alias;
@@ -1617,9 +1646,10 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
		return -EINVAL;
	}

	evsel = __add_event(list, &parse_state->idx, &attr, true,
			    get_config_name(head_config), pmu,
			    &config_terms, auto_merge_stats, NULL);
	evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
			    get_config_name(head_config),
			    get_config_metric_id(head_config), pmu,
			    &config_terms, auto_merge_stats, /*cpu_list=*/NULL);
	if (!evsel)
		return -ENOMEM;

@@ -3345,9 +3375,12 @@ char *parse_events_formats_error_string(char *additional_terms)

struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx,
					     struct perf_event_attr *attr,
					     const char *name, struct perf_pmu *pmu,
					     const char *name,
					     const char *metric_id,
					     struct perf_pmu *pmu,
					     struct list_head *config_terms)
{
	return __add_event(list, idx, attr, true, name, pmu,
			   config_terms, false, NULL);
	return __add_event(list, idx, attr, /*init_attr=*/true, name, metric_id,
			   pmu, config_terms, /*auto_merge_stats=*/false,
			   /*cpu_list=*/NULL);
}
Loading