Skip to contentSkip to conversation

perf, tools: Enable type checking for perf_evsel_config_term types

PATCH In review Andi Kleen 4 participants 8 messages Oct 20, 2017 – Nov 29, 2017 perf events

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/2 perf, tools: Enable type checking for perf_evsel_config_term types

From: Andi Kleen <[email protected]>

Use a typed enum for the perf_evsel_config_term type enum. This allows gcc to do much stronger type checks, and also check for missing case statements.

I removed the unused _MAX member from the number.

It found one missing case. I'm not sure it's a real problem, so I just turned it into a BUG_ON for now.

---
 tools/perf/util/evsel.c | 2 ++
 tools/perf/util/evsel.h | 5 ++---
 2 files changed, 4 insertions(+), 3 deletions(-)
tools/perf/util/evsel.c +2 -0
index f894893c203d..d1f63b93bf69 100644
@@ -775,6 +775,8 @@ static void apply_config_terms(struct perf_evsel *evsel,
 		case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
 			attr->write_backward = term->val.overwrite ? 1 : 0;
 			break;
+		case PERF_EVSEL__CONFIG_TERM_DRV_CFG:
+			BUG_ON(1);
 		default:
 			break;
 		}
tools/perf/util/evsel.h +2 -3
index db658785d828..6728d8d6e513 100644
@@ -37,7 +37,7 @@ struct cgroup_sel;
  * It is allocated within event parsing and attached to
  * perf_evsel::config_terms list head.
 */
-enum {
+enum period_type {
 	PERF_EVSEL__CONFIG_TERM_PERIOD,
 	PERF_EVSEL__CONFIG_TERM_FREQ,
 	PERF_EVSEL__CONFIG_TERM_TIME,
@@ -48,12 +48,11 @@ enum {
 	PERF_EVSEL__CONFIG_TERM_OVERWRITE,
 	PERF_EVSEL__CONFIG_TERM_DRV_CFG,
 	PERF_EVSEL__CONFIG_TERM_BRANCH,
-	PERF_EVSEL__CONFIG_TERM_MAX,
 };
 
 struct perf_evsel_config_term {
 	struct list_head	list;
-	int	type;
+	enum period_type	type;
 	union {
 		u64	period;
 		u64	freq;
Signature · 1 line
2.13.6
author
#1 lore raw

PATCH 2/2 perf, tools, record: Fix -c/-F options for cpu event aliases

From: Andi Kleen <[email protected]>

The Intel PMU event aliases have a implicit
period= specifier to set the default period.

Unfortunately this breaks overriding these periods with -c or -F, because the alias terms look like they are user specified to the internal parser, and user specified event qualifiers override the command line options.

Track that they are coming from aliases by adding a "weak" state to the term. Any weak terms don't override command line options.

I only did it for -c/-F for now, I think that's the only case that's broken currently.

Before:

$ perf record -c 1000 -vv -e uops_issued.any
...
  { sample_period, sample_freq }   2000003

After:

$ perf record -c 1000 -vv -e uops_issued.any
...
  { sample_period, sample_freq }   1000
---
 tools/perf/util/evsel.c        | 12 ++++++++----
 tools/perf/util/evsel.h        |  1 +
 tools/perf/util/parse-events.c |  2 ++
 tools/perf/util/parse-events.h |  3 +++
 tools/perf/util/pmu.c          |  5 +++++
 5 files changed, 19 insertions(+), 4 deletions(-)
tools/perf/util/evsel.c +8 -4
index d1f63b93bf69..4376cdfaea49 100644
@@ -733,12 +733,16 @@ static void apply_config_terms(struct perf_evsel *evsel,
 	list_for_each_entry(term, config_terms, list) {
 		switch (term->type) {
 		case PERF_EVSEL__CONFIG_TERM_PERIOD:
-			attr->sample_period = term->val.period;
-			attr->freq = 0;
+			if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
+				attr->sample_period = term->val.period;
+				attr->freq = 0;
+			}
 			break;
 		case PERF_EVSEL__CONFIG_TERM_FREQ:
-			attr->sample_freq = term->val.freq;
-			attr->freq = 1;
+			if (!(term->weak && opts->user_freq != UINT_MAX)) {
+				attr->sample_freq = term->val.freq;
+				attr->freq = 1;
+			}
 			break;
 		case PERF_EVSEL__CONFIG_TERM_TIME:
 			if (term->val.time)
tools/perf/util/evsel.h +1 -0
index 6728d8d6e513..e315f91739bc 100644
@@ -65,6 +65,7 @@ struct perf_evsel_config_term {
 		bool	overwrite;
 		char	*branch;
 	} val;
+	bool weak;
 };
 
 /** struct perf_evsel - event selector
tools/perf/util/parse-events.c +2 -0
index 04f35db063ee..c773cdd9aa4b 100644
@@ -1115,6 +1115,7 @@ do {								\
 	INIT_LIST_HEAD(&__t->list);				\
 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
 	__t->val.__name = __val;				\
+	__t->weak	= term->weak;				\
 	list_add_tail(&__t->list, head_terms);			\
 } while (0)
 
@@ -2409,6 +2410,7 @@ static int new_term(struct parse_events_term **_term,
 
 	*term = *temp;
 	INIT_LIST_HEAD(&term->list);
+	term->weak = false;
 
 	switch (term->type_val) {
 	case PARSE_EVENTS__TERM_TYPE_NUM:
tools/perf/util/parse-events.h +3 -0
index 3909ca0639f2..e5638f06b431 100644
@@ -100,6 +100,9 @@ struct parse_events_term {
 	/* error string indexes for within parsed string */
 	int err_term;
 	int err_val;
+
+	/* Coming from implicit alias */
+	bool weak;
 };
 
 struct parse_events_error {
tools/perf/util/pmu.c +5 -0
index 94cf2c29fed6..2b12bcb845a8 100644
@@ -404,6 +404,11 @@ static int pmu_alias_terms(struct perf_pmu_alias *alias,
 			parse_events_terms__purge(&list);
 			return ret;
 		}
+		/*
+		 * Weak terms don't override command line options,
+		 * which we don't want for implicit terms in aliases.
+		 */
+		cloned->weak = true;
 		list_add_tail(&cloned->list, &list);
 	}
 	list_splice(&list, terms);
Signature · 1 line
2.13.6
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] perf, tools, record: Fix -c/-F options for cpu event aliases

Quoted 23 lines · Andi Kleen

From: Andi Kleen <[email protected]>

The Intel PMU event aliases have a implicit
period= specifier to set the default period.

Unfortunately this breaks overriding these periods with -c or -F, because the alias terms look like they are user specified to the internal parser, and user specified event qualifiers override the command line options.

Track that they are coming from aliases by adding a "weak" state to the term. Any weak terms don't override command line options.

I only did it for -c/-F for now, I think that's the only case that's broken currently.

Before:

$ perf record -c 1000 -vv -e uops_issued.any
...
  { sample_period, sample_freq }   2000003
Quoted 7 lines

After:

$ perf record -c 1000 -vv -e uops_issued.any
...
  { sample_period, sample_freq }   1000

thanks,
jirka

#3 lore raw

[tip:perf/core] perf record: Fix -c/-F options for cpu event aliases

In reply to Andi Kleen
perf record: Fix -c/-F options for cpu event aliases
Commit-ID:  c2f1cead19b628d7a23d2cfc43e444af669f9eab
Gitweb:     https://git.kernel.org/tip/c2f1cead19b628d7a23d2cfc43e444af669f9eab
Author:     Andi Kleen <[email protected]>
AuthorDate: Fri, 20 Oct 2017 13:27:55 -0700
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 16 Nov 2017 14:49:51 -0300

perf record: Fix -c/-F options for cpu event aliases

The Intel PMU event aliases have a implicit period= specifier to set the default period.

Unfortunately this breaks overriding these periods with -c or -F, because the alias terms look like they are user specified to the internal parser, and user specified event qualifiers override the command line options.

Track that they are coming from aliases by adding a "weak" state to the term. Any weak terms don't override command line options.

I only did it for -c/-F for now, I think that's the only case that's broken currently.

Before:

$ perf record -c 1000 -vv -e uops_issued.any
...
  { sample_period, sample_freq }   2000003

After:

$ perf record -c 1000 -vv -e uops_issued.any
...
  { sample_period, sample_freq }   1000
---
 tools/perf/util/evsel.c        | 12 ++++++++----
 tools/perf/util/evsel.h        |  1 +
 tools/perf/util/parse-events.c |  2 ++
 tools/perf/util/parse-events.h |  3 +++
 tools/perf/util/pmu.c          |  5 +++++
 5 files changed, 19 insertions(+), 4 deletions(-)
tools/perf/util/evsel.c +8 -4
index f894893..bfde6a7 100644
@@ -733,12 +733,16 @@ static void apply_config_terms(struct perf_evsel *evsel,
 	list_for_each_entry(term, config_terms, list) {
 		switch (term->type) {
 		case PERF_EVSEL__CONFIG_TERM_PERIOD:
-			attr->sample_period = term->val.period;
-			attr->freq = 0;
+			if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
+				attr->sample_period = term->val.period;
+				attr->freq = 0;
+			}
 			break;
 		case PERF_EVSEL__CONFIG_TERM_FREQ:
-			attr->sample_freq = term->val.freq;
-			attr->freq = 1;
+			if (!(term->weak && opts->user_freq != UINT_MAX)) {
+				attr->sample_freq = term->val.freq;
+				attr->freq = 1;
+			}
 			break;
 		case PERF_EVSEL__CONFIG_TERM_TIME:
 			if (term->val.time)
tools/perf/util/evsel.h +1 -0
index 9277df9..157f49e 100644
@@ -67,6 +67,7 @@ struct perf_evsel_config_term {
 		bool	overwrite;
 		char	*branch;
 	} val;
+	bool weak;
 };
 
 struct perf_stat_evsel;
tools/perf/util/parse-events.c +2 -0
index a7fcd95..1703167 100644
@@ -1116,6 +1116,7 @@ do {								\
 	INIT_LIST_HEAD(&__t->list);				\
 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
 	__t->val.__name = __val;				\
+	__t->weak	= term->weak;				\
 	list_add_tail(&__t->list, head_terms);			\
 } while (0)
 
@@ -2410,6 +2411,7 @@ static int new_term(struct parse_events_term **_term,
 
 	*term = *temp;
 	INIT_LIST_HEAD(&term->list);
+	term->weak = false;
 
 	switch (term->type_val) {
 	case PARSE_EVENTS__TERM_TYPE_NUM:
tools/perf/util/parse-events.h +3 -0
index be337c2..88108cd 100644
@@ -101,6 +101,9 @@ struct parse_events_term {
 	/* error string indexes for within parsed string */
 	int err_term;
 	int err_val;
+
+	/* Coming from implicit alias */
+	bool weak;
 };
 
 struct parse_events_error {
tools/perf/util/pmu.c +5 -0
index 07cb2ac..80fb159 100644
@@ -405,6 +405,11 @@ static int pmu_alias_terms(struct perf_pmu_alias *alias,
 			parse_events_terms__purge(&list);
 			return ret;
 		}
+		/*
+		 * Weak terms don't override command line options,
+		 * which we don't want for implicit terms in aliases.
+		 */
+		cloned->weak = true;
 		list_add_tail(&cloned->list, &list);
 	}
 	list_splice(&list, terms);
#4 lore raw

[tip:perf/core] perf record: Fix -c/-F options for cpu event aliases

In reply to Andi Kleen
perf record: Fix -c/-F options for cpu event aliases
Commit-ID:  59622fd496a3175c7bf549046e091d81c303ecff
Gitweb:     https://git.kernel.org/tip/59622fd496a3175c7bf549046e091d81c303ecff
Author:     Andi Kleen <[email protected]>
AuthorDate: Fri, 20 Oct 2017 13:27:55 -0700
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 28 Nov 2017 14:19:39 -0300

perf record: Fix -c/-F options for cpu event aliases

The Intel PMU event aliases have a implicit period= specifier to set the default period.

Unfortunately this breaks overriding these periods with -c or -F, because the alias terms look like they are user specified to the internal parser, and user specified event qualifiers override the command line options.

Track that they are coming from aliases by adding a "weak" state to the term. Any weak terms don't override command line options.

I only did it for -c/-F for now, I think that's the only case that's broken currently.

Before:

$ perf record -c 1000 -vv -e uops_issued.any
...
  { sample_period, sample_freq }   2000003

After:

$ perf record -c 1000 -vv -e uops_issued.any
...
  { sample_period, sample_freq }   1000
---
 tools/perf/util/evsel.c        | 12 ++++++++----
 tools/perf/util/evsel.h        |  1 +
 tools/perf/util/parse-events.c |  2 ++
 tools/perf/util/parse-events.h |  3 +++
 tools/perf/util/pmu.c          |  5 +++++
 5 files changed, 19 insertions(+), 4 deletions(-)
tools/perf/util/evsel.c +8 -4
index f894893..bfde6a7 100644
@@ -733,12 +733,16 @@ static void apply_config_terms(struct perf_evsel *evsel,
 	list_for_each_entry(term, config_terms, list) {
 		switch (term->type) {
 		case PERF_EVSEL__CONFIG_TERM_PERIOD:
-			attr->sample_period = term->val.period;
-			attr->freq = 0;
+			if (!(term->weak && opts->user_interval != ULLONG_MAX)) {
+				attr->sample_period = term->val.period;
+				attr->freq = 0;
+			}
 			break;
 		case PERF_EVSEL__CONFIG_TERM_FREQ:
-			attr->sample_freq = term->val.freq;
-			attr->freq = 1;
+			if (!(term->weak && opts->user_freq != UINT_MAX)) {
+				attr->sample_freq = term->val.freq;
+				attr->freq = 1;
+			}
 			break;
 		case PERF_EVSEL__CONFIG_TERM_TIME:
 			if (term->val.time)
tools/perf/util/evsel.h +1 -0
index 9277df9..157f49e 100644
@@ -67,6 +67,7 @@ struct perf_evsel_config_term {
 		bool	overwrite;
 		char	*branch;
 	} val;
+	bool weak;
 };
 
 struct perf_stat_evsel;
tools/perf/util/parse-events.c +2 -0
index a7fcd95..1703167 100644
@@ -1116,6 +1116,7 @@ do {								\
 	INIT_LIST_HEAD(&__t->list);				\
 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
 	__t->val.__name = __val;				\
+	__t->weak	= term->weak;				\
 	list_add_tail(&__t->list, head_terms);			\
 } while (0)
 
@@ -2410,6 +2411,7 @@ static int new_term(struct parse_events_term **_term,
 
 	*term = *temp;
 	INIT_LIST_HEAD(&term->list);
+	term->weak = false;
 
 	switch (term->type_val) {
 	case PARSE_EVENTS__TERM_TYPE_NUM:
tools/perf/util/parse-events.h +3 -0
index be337c2..88108cd 100644
@@ -101,6 +101,9 @@ struct parse_events_term {
 	/* error string indexes for within parsed string */
 	int err_term;
 	int err_val;
+
+	/* Coming from implicit alias */
+	bool weak;
 };
 
 struct parse_events_error {
tools/perf/util/pmu.c +5 -0
index 07cb2ac..80fb159 100644
@@ -405,6 +405,11 @@ static int pmu_alias_terms(struct perf_pmu_alias *alias,
 			parse_events_terms__purge(&list);
 			return ret;
 		}
+		/*
+		 * Weak terms don't override command line options,
+		 * which we don't want for implicit terms in aliases.
+		 */
+		cloned->weak = true;
 		list_add_tail(&cloned->list, &list);
 	}
 	list_splice(&list, terms);
#5 lore raw
In reply to Andi Kleen
Quoted 35 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Use a typed enum for the perf_evsel_config_term type enum. This allows gcc to do much stronger type checks, and also check for missing case statements.

I removed the unused _MAX member from the number.

It found one missing case. I'm not sure it's a real problem, so I just turned it into a BUG_ON for now.

---
 tools/perf/util/evsel.c | 2 ++
 tools/perf/util/evsel.h | 5 ++---
 2 files changed, 4 insertions(+), 3 deletions(-)
tools/perf/util/evsel.c +2 -0
index f894893c203d..d1f63b93bf69 100644
@@ -775,6 +775,8 @@ static void apply_config_terms(struct perf_evsel *evsel,
 		case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
 			attr->write_backward = term->val.overwrite ? 1 : 0;
 			break;
+		case PERF_EVSEL__CONFIG_TERM_DRV_CFG:
+			BUG_ON(1);
 		default:
 			break;
 		}
tools/perf/util/evsel.h +0 -0
index db658785d828..6728d8d6e513 100644

tools/perf/util/evsel.h struct cgroup_sel; · line 40

Quoted 5 lines
tools/perf/util/evsel.h +1 -1
@@ -37,7 +37,7 @@ struct cgroup_sel;
  * It is allocated within event parsing and attached to
  * perf_evsel::config_terms list head.
 */
-enum {
+enum period_type {

seems ok, but I'm puzzled with the 'period_type' name I think it should be more like 'term_type' or something

jirka

Quoted 20 lines
tools/perf/util/evsel.h +1 -2
 	PERF_EVSEL__CONFIG_TERM_PERIOD,
 	PERF_EVSEL__CONFIG_TERM_FREQ,
 	PERF_EVSEL__CONFIG_TERM_TIME,
@@ -48,12 +48,11 @@ enum {
 	PERF_EVSEL__CONFIG_TERM_OVERWRITE,
 	PERF_EVSEL__CONFIG_TERM_DRV_CFG,
 	PERF_EVSEL__CONFIG_TERM_BRANCH,
-	PERF_EVSEL__CONFIG_TERM_MAX,
 };
 
 struct perf_evsel_config_term {
 	struct list_head	list;
-	int	type;
+	enum period_type	type;
 	union {
 		u64	period;
 		u64	freq;
Signature · 1 line
2.13.6
#6 lore raw

Em Tue, Oct 24, 2017 at 09:38:49AM +0200, Jiri Olsa escreveu:

Quoted 36 lines · Andi Kleen
Quoted 35 lines

From: Andi Kleen <[email protected]>

Use a typed enum for the perf_evsel_config_term type enum. This allows gcc to do much stronger type checks, and also check for missing case statements.

I removed the unused _MAX member from the number.

It found one missing case. I'm not sure it's a real problem, so I just turned it into a BUG_ON for now.

---
 tools/perf/util/evsel.c | 2 ++
 tools/perf/util/evsel.h | 5 ++---
 2 files changed, 4 insertions(+), 3 deletions(-)
tools/perf/util/evsel.c +2 -0
index f894893c203d..d1f63b93bf69 100644
@@ -775,6 +775,8 @@ static void apply_config_terms(struct perf_evsel *evsel,
 		case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
 			attr->write_backward = term->val.overwrite ? 1 : 0;
 			break;
+		case PERF_EVSEL__CONFIG_TERM_DRV_CFG:
+			BUG_ON(1);
 		default:
 			break;
 		}
tools/perf/util/evsel.h +0 -0
index db658785d828..6728d8d6e513 100644
@@ -37,7 +37,7 @@ struct cgroup_sel;
Quoted 8 lines

tools/perf/util/evsel.h struct cgroup_sel; · line 40

Quoted 5 lines
tools/perf/util/evsel.h +1 -1
  * It is allocated within event parsing and attached to
  * perf_evsel::config_terms list head.
 */
-enum {
+enum period_type {

seems ok, but I'm puzzled with the 'period_type' name I think it should be more like 'term_type' or something

Ok, renamed it to term_type and added your Acked-by,

- ARnaldo

Quoted 23 lines

jirka

Quoted 20 lines
tools/perf/util/evsel.h +1 -2
 	PERF_EVSEL__CONFIG_TERM_PERIOD,
 	PERF_EVSEL__CONFIG_TERM_FREQ,
 	PERF_EVSEL__CONFIG_TERM_TIME,
@@ -48,12 +48,11 @@ enum {
 	PERF_EVSEL__CONFIG_TERM_OVERWRITE,
 	PERF_EVSEL__CONFIG_TERM_DRV_CFG,
 	PERF_EVSEL__CONFIG_TERM_BRANCH,
-	PERF_EVSEL__CONFIG_TERM_MAX,
 };
 
 struct perf_evsel_config_term {
 	struct list_head	list;
-	int	type;
+	enum period_type	type;
 	union {
 		u64	period;
 		u64	freq;
Signature · 1 line
2.13.6
#7 lore raw

[tip:perf/core] perf evsel: Enable type checking for perf_evsel_config_term types

In reply to Andi Kleen
perf evsel: Enable type checking for perf_evsel_config_term types
Commit-ID:  d0565132605f454179699a1b8a3276fc0f8cc87b
Gitweb:     https://git.kernel.org/tip/d0565132605f454179699a1b8a3276fc0f8cc87b
Author:     Andi Kleen <[email protected]>
AuthorDate: Fri, 20 Oct 2017 13:27:54 -0700
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 16 Nov 2017 14:49:51 -0300

perf evsel: Enable type checking for perf_evsel_config_term types

Use a typed enum for the perf_evsel_config_term type enum. This allows gcc to do much stronger type checks, and also check for missing case statements.

I removed the unused _MAX member from the number.

It found one missing case. I'm not sure it's a real problem, so I just turned it into a BUG_ON for now.

---
 tools/perf/util/evsel.c | 2 ++
 tools/perf/util/evsel.h | 5 ++---
 2 files changed, 4 insertions(+), 3 deletions(-)
tools/perf/util/evsel.c +2 -0
index bfde6a7..4376cdf 100644
@@ -779,6 +779,8 @@ static void apply_config_terms(struct perf_evsel *evsel,
 		case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
 			attr->write_backward = term->val.overwrite ? 1 : 0;
 			break;
+		case PERF_EVSEL__CONFIG_TERM_DRV_CFG:
+			BUG_ON(1);
 		default:
 			break;
 		}
tools/perf/util/evsel.h +2 -3
index 157f49e..0688880 100644
@@ -38,7 +38,7 @@ struct cgroup_sel;
  * It is allocated within event parsing and attached to
  * perf_evsel::config_terms list head.
 */
-enum {
+enum term_type {
 	PERF_EVSEL__CONFIG_TERM_PERIOD,
 	PERF_EVSEL__CONFIG_TERM_FREQ,
 	PERF_EVSEL__CONFIG_TERM_TIME,
@@ -49,12 +49,11 @@ enum {
 	PERF_EVSEL__CONFIG_TERM_OVERWRITE,
 	PERF_EVSEL__CONFIG_TERM_DRV_CFG,
 	PERF_EVSEL__CONFIG_TERM_BRANCH,
-	PERF_EVSEL__CONFIG_TERM_MAX,
 };
 
 struct perf_evsel_config_term {
 	struct list_head	list;
-	int	type;
+	enum term_type	type;
 	union {
 		u64	period;
 		u64	freq;