Skip to contentSkip to conversation

perf evsel: Fix missing exclude_{host,guest} setting

PATCH Superseded Namhyung Kim 3 participants 6 messages Oct 16, 2021 – Oct 18, 2021 perf events

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/1 perf evsel: Fix missing exclude_{host,guest} setting

The current logic for the perf missing feature has a bug that it can wrongly clear some modifiers like G or H. Actually some PMUs don't support any filtering or exclusion while others do. But we check it as a global feature.

For example, the cycles event can have 'G' modifier to enable it only in the guest mode on x86. When you don't run any VMs it'll return 0.

  # perf stat -a -e cycles:G sleep 1

Performance counter stats for 'system wide':

0 cycles:G

1.000721670 seconds time elapsed

But when it's used with other pmu events that don't support G modifier, it'll be reset and return non-zero values.

  # perf stat -a -e cycles:G,msr/tsc/ sleep 1
    Performance counter stats for 'system wide':

          538,029,960      cycles:G
       16,924,010,738      msr/tsc/

          1.001815327 seconds time elapsed

This is because of the missing feature detection logic being global. Add a hashmap to set pmu-specific exclude_host/guest features.

---
 tools/perf/util/evsel.c | 47 +++++++++++++++++++++++++++++++++++------
 tools/perf/util/evsel.h |  6 ++++++
 2 files changed, 47 insertions(+), 6 deletions(-)
tools/perf/util/evsel.c +41 -6
index dbfeceb2546c..437a28e769fe 100644
@@ -1434,6 +1434,10 @@ void evsel__delete(struct evsel *evsel)
 {
 	evsel__exit(evsel);
 	free(evsel);
+
+	/* just free it for the first evsel */
+	hashmap__free(perf_missing_features.pmu);
+	perf_missing_features.pmu = NULL;
 }
 
 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
@@ -1791,6 +1795,23 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	return 0;
 }
 
+#define PMU_HASH_BITS  4
+
+static size_t pmu_hash(const void *key, void *ctx __maybe_unused)
+{
+	const struct evsel *evsel = key;
+
+	return hash_bits(evsel->core.attr.type, PMU_HASH_BITS);
+}
+
+static bool pmu_equal(const void *key1, const void *key2, void *ctx __maybe_unused)
+{
+	const struct evsel *a = key1;
+	const struct evsel *b = key2;
+
+	return a->core.attr.type == b->core.attr.type;
+}
+
 static void evsel__disable_missing_features(struct evsel *evsel)
 {
 	if (perf_missing_features.weight_struct) {
@@ -1807,8 +1828,14 @@ static void evsel__disable_missing_features(struct evsel *evsel)
 		evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
 	if (perf_missing_features.mmap2)
 		evsel->core.attr.mmap2 = 0;
-	if (perf_missing_features.exclude_guest)
-		evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
+	if (perf_missing_features.exclude_guest) {
+		void *pmu;
+
+		if (hashmap__find(perf_missing_features.pmu, evsel, &pmu)) {
+			evsel->core.attr.exclude_guest = 0;
+			evsel->core.attr.exclude_host = 0;
+		}
+	}
 	if (perf_missing_features.lbr_flags)
 		evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
 				     PERF_SAMPLE_BRANCH_NO_CYCLES);
@@ -1840,6 +1867,9 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 bool evsel__detect_missing_features(struct evsel *evsel)
 {
+	if (perf_missing_features.pmu == NULL)
+		perf_missing_features.pmu = hashmap__new(pmu_hash, pmu_equal, NULL);
+
 	/*
 	 * Must probe features in the order they were added to the
 	 * perf_event_attr interface.
@@ -1900,10 +1930,15 @@ bool evsel__detect_missing_features(struct evsel *evsel)
 		perf_missing_features.mmap2 = true;
 		pr_debug2_peo("switching off mmap2\n");
 		return true;
-	} else if (!perf_missing_features.exclude_guest &&
-		   (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
-		perf_missing_features.exclude_guest = true;
-		pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+	} else if ((evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host) &&
+		   !hashmap__find(perf_missing_features.pmu, evsel, NULL)) {
+		struct perf_missing_pmu_features pmu_features = { true };
+		hashmap__add(perf_missing_features.pmu, evsel, &pmu_features);
+
+		if (!perf_missing_features.exclude_guest) {
+			perf_missing_features.exclude_guest = true;
+			pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+		}
 		return true;
 	} else if (!perf_missing_features.sample_id_all) {
 		perf_missing_features.sample_id_all = true;
tools/perf/util/evsel.h +6 -0
index 1f7edfa8568a..8dd11c8e022d 100644
@@ -172,6 +172,12 @@ struct perf_missing_features {
 	bool data_page_size;
 	bool code_page_size;
 	bool weight_struct;
+
+	struct hashmap *pmu;
+};
+
+struct perf_missing_pmu_features {
+	bool exclude_guest;
 };
 
 extern struct perf_missing_features perf_missing_features;
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
#1 lore raw
Quoted 78 lines · Namhyung Kim

The current logic for the perf missing feature has a bug that it can wrongly clear some modifiers like G or H. Actually some PMUs don't support any filtering or exclusion while others do. But we check it as a global feature.

For example, the cycles event can have 'G' modifier to enable it only in the guest mode on x86. When you don't run any VMs it'll return 0.

  # perf stat -a -e cycles:G sleep 1

Performance counter stats for 'system wide':

0 cycles:G

1.000721670 seconds time elapsed

But when it's used with other pmu events that don't support G modifier, it'll be reset and return non-zero values.

  # perf stat -a -e cycles:G,msr/tsc/ sleep 1
    Performance counter stats for 'system wide':

          538,029,960      cycles:G
       16,924,010,738      msr/tsc/

          1.001815327 seconds time elapsed

This is because of the missing feature detection logic being global. Add a hashmap to set pmu-specific exclude_host/guest features.

---
 tools/perf/util/evsel.c | 47 +++++++++++++++++++++++++++++++++++------
 tools/perf/util/evsel.h |  6 ++++++
 2 files changed, 47 insertions(+), 6 deletions(-)
tools/perf/util/evsel.c +21 -0
index dbfeceb2546c..437a28e769fe 100644
@@ -1434,6 +1434,10 @@ void evsel__delete(struct evsel *evsel)
 {
 	evsel__exit(evsel);
 	free(evsel);
+
+	/* just free it for the first evsel */
+	hashmap__free(perf_missing_features.pmu);
+	perf_missing_features.pmu = NULL;
 }
 
 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
@@ -1791,6 +1795,23 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	return 0;
 }
 
+#define PMU_HASH_BITS  4
+
+static size_t pmu_hash(const void *key, void *ctx __maybe_unused)
+{
+	const struct evsel *evsel = key;
+
+	return hash_bits(evsel->core.attr.type, PMU_HASH_BITS);
+}
+
+static bool pmu_equal(const void *key1, const void *key2, void *ctx __maybe_unused)
+{
+	const struct evsel *a = key1;
+	const struct evsel *b = key2;
+
+	return a->core.attr.type == b->core.attr.type;
+}
+
 static void evsel__disable_missing_features(struct evsel *evsel)
 {
 	if (perf_missing_features.weight_struct) {

tools/perf/util/evsel.c static void evsel__disable_missing_features(struct evsel *evsel) · line 1832

Quoted 7 lines
tools/perf/util/evsel.c +2 -2
@@ -1807,8 +1828,14 @@ static void evsel__disable_missing_features(struct evsel *evsel)
 		evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
 	if (perf_missing_features.mmap2)
 		evsel->core.attr.mmap2 = 0;
-	if (perf_missing_features.exclude_guest)
-		evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
+	if (perf_missing_features.exclude_guest) {
+		void *pmu;

could you just pass NULL in here instead of NULL?

Quoted 20 lines
tools/perf/util/evsel.c +9 -0
+
+		if (hashmap__find(perf_missing_features.pmu, evsel, &pmu)) {
+			evsel->core.attr.exclude_guest = 0;
+			evsel->core.attr.exclude_host = 0;
+		}
+	}
 	if (perf_missing_features.lbr_flags)
 		evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
 				     PERF_SAMPLE_BRANCH_NO_CYCLES);
@@ -1840,6 +1867,9 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 bool evsel__detect_missing_features(struct evsel *evsel)
 {
+	if (perf_missing_features.pmu == NULL)
+		perf_missing_features.pmu = hashmap__new(pmu_hash, pmu_equal, NULL);
+
 	/*
 	 * Must probe features in the order they were added to the
 	 * perf_event_attr interface.

tools/perf/util/evsel.c bool evsel__detect_missing_features(struct evsel *evsel) · line 1935

Quoted 10 lines
tools/perf/util/evsel.c +3 -4
@@ -1900,10 +1930,15 @@ bool evsel__detect_missing_features(struct evsel *evsel)
 		perf_missing_features.mmap2 = true;
 		pr_debug2_peo("switching off mmap2\n");
 		return true;
-	} else if (!perf_missing_features.exclude_guest &&
-		   (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
-		perf_missing_features.exclude_guest = true;
-		pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+	} else if ((evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host) &&
+		   !hashmap__find(perf_missing_features.pmu, evsel, NULL)) {
+		struct perf_missing_pmu_features pmu_features = { true };

missing new line after declaration

Quoted 14 lines
tools/perf/util/evsel.c +6 -0
+		hashmap__add(perf_missing_features.pmu, evsel, &pmu_features);
+
+		if (!perf_missing_features.exclude_guest) {
+			perf_missing_features.exclude_guest = true;
+			pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+		}
 		return true;
 	} else if (!perf_missing_features.sample_id_all) {
 		perf_missing_features.sample_id_all = true;
tools/perf/util/evsel.h +0 -0
index 1f7edfa8568a..8dd11c8e022d 100644

tools/perf/util/evsel.h struct perf_missing_features { · line 181

Quoted 10 lines
tools/perf/util/evsel.h +6 -0
@@ -172,6 +172,12 @@ struct perf_missing_features {
 	bool data_page_size;
 	bool code_page_size;
 	bool weight_struct;
+
+	struct hashmap *pmu;
+};
+
+struct perf_missing_pmu_features {
+	bool exclude_guest;
 };

hum, is this really needed? I think you could just pass '1' as value, because you care only if the item is hashed, right?

in any case the value is the current stack address of the
struct perf_missing_pmu_features pmu_features = { true };

so it might as well be just '1' ... I was confused at the beggining and looked for the reason of this struct ;-)

we do that already in util/stat.c

jirka

Quoted 5 lines
tools/perf/util/evsel.h +0 -0
 extern struct perf_missing_features perf_missing_features;
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
#2 lore raw
Quoted 80 lines · Jiri Olsa
Quoted 79 lines · Namhyung Kim

The current logic for the perf missing feature has a bug that it can wrongly clear some modifiers like G or H. Actually some PMUs don't support any filtering or exclusion while others do. But we check it as a global feature.

For example, the cycles event can have 'G' modifier to enable it only in the guest mode on x86. When you don't run any VMs it'll return 0.

  # perf stat -a -e cycles:G sleep 1

Performance counter stats for 'system wide':

0 cycles:G

1.000721670 seconds time elapsed

But when it's used with other pmu events that don't support G modifier, it'll be reset and return non-zero values.

  # perf stat -a -e cycles:G,msr/tsc/ sleep 1
    Performance counter stats for 'system wide':

          538,029,960      cycles:G
       16,924,010,738      msr/tsc/

          1.001815327 seconds time elapsed

This is because of the missing feature detection logic being global. Add a hashmap to set pmu-specific exclude_host/guest features.

---
 tools/perf/util/evsel.c | 47 +++++++++++++++++++++++++++++++++++------
 tools/perf/util/evsel.h |  6 ++++++
 2 files changed, 47 insertions(+), 6 deletions(-)
tools/perf/util/evsel.c +21 -0
index dbfeceb2546c..437a28e769fe 100644
@@ -1434,6 +1434,10 @@ void evsel__delete(struct evsel *evsel)
 {
 	evsel__exit(evsel);
 	free(evsel);
+
+	/* just free it for the first evsel */
+	hashmap__free(perf_missing_features.pmu);
+	perf_missing_features.pmu = NULL;
 }
 
 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
@@ -1791,6 +1795,23 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	return 0;
 }
 
+#define PMU_HASH_BITS  4
+
+static size_t pmu_hash(const void *key, void *ctx __maybe_unused)
+{
+	const struct evsel *evsel = key;
+
+	return hash_bits(evsel->core.attr.type, PMU_HASH_BITS);
+}
+
+static bool pmu_equal(const void *key1, const void *key2, void *ctx __maybe_unused)
+{
+	const struct evsel *a = key1;
+	const struct evsel *b = key2;
+
+	return a->core.attr.type == b->core.attr.type;
+}
+
 static void evsel__disable_missing_features(struct evsel *evsel)
 {
 	if (perf_missing_features.weight_struct) {
@@ -1807,8 +1828,14 @@ static void evsel__disable_missing_features(struct evsel *evsel)
 		evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Quoted 8 lines

tools/perf/util/evsel.c static void evsel__disable_missing_features(struct evsel *evsel) · line 1832

Quoted 6 lines
tools/perf/util/evsel.c +2 -2
 	if (perf_missing_features.mmap2)
 		evsel->core.attr.mmap2 = 0;
-	if (perf_missing_features.exclude_guest)
-		evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
+	if (perf_missing_features.exclude_guest) {
+		void *pmu;

could you just pass NULL in here instead of NULL?

ENOPARSE

Quoted 78 lines
Quoted 21 lines
tools/perf/util/evsel.c +9 -0
+
+		if (hashmap__find(perf_missing_features.pmu, evsel, &pmu)) {
+			evsel->core.attr.exclude_guest = 0;
+			evsel->core.attr.exclude_host = 0;
+		}
+	}
 	if (perf_missing_features.lbr_flags)
 		evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
 				     PERF_SAMPLE_BRANCH_NO_CYCLES);
@@ -1840,6 +1867,9 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 bool evsel__detect_missing_features(struct evsel *evsel)
 {
+	if (perf_missing_features.pmu == NULL)
+		perf_missing_features.pmu = hashmap__new(pmu_hash, pmu_equal, NULL);
+
 	/*
 	 * Must probe features in the order they were added to the
 	 * perf_event_attr interface.

tools/perf/util/evsel.c bool evsel__detect_missing_features(struct evsel *evsel) · line 1935

Quoted 10 lines
tools/perf/util/evsel.c +3 -4
@@ -1900,10 +1930,15 @@ bool evsel__detect_missing_features(struct evsel *evsel)
 		perf_missing_features.mmap2 = true;
 		pr_debug2_peo("switching off mmap2\n");
 		return true;
-	} else if (!perf_missing_features.exclude_guest &&
-		   (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
-		perf_missing_features.exclude_guest = true;
-		pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+	} else if ((evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host) &&
+		   !hashmap__find(perf_missing_features.pmu, evsel, NULL)) {
+		struct perf_missing_pmu_features pmu_features = { true };

missing new line after declaration

Quoted 14 lines
tools/perf/util/evsel.c +6 -0
+		hashmap__add(perf_missing_features.pmu, evsel, &pmu_features);
+
+		if (!perf_missing_features.exclude_guest) {
+			perf_missing_features.exclude_guest = true;
+			pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+		}
 		return true;
 	} else if (!perf_missing_features.sample_id_all) {
 		perf_missing_features.sample_id_all = true;
tools/perf/util/evsel.h +0 -0
index 1f7edfa8568a..8dd11c8e022d 100644

tools/perf/util/evsel.h struct perf_missing_features { · line 181

Quoted 10 lines
tools/perf/util/evsel.h +6 -0
@@ -172,6 +172,12 @@ struct perf_missing_features {
 	bool data_page_size;
 	bool code_page_size;
 	bool weight_struct;
+
+	struct hashmap *pmu;
+};
+
+struct perf_missing_pmu_features {
+	bool exclude_guest;
 };

hum, is this really needed? I think you could just pass '1' as value, because you care only if the item is hashed, right?

in any case the value is the current stack address of the
struct perf_missing_pmu_features pmu_features = { true };

so it might as well be just '1' ... I was confused at the beggining and looked for the reason of this struct ;-)

we do that already in util/stat.c

jirka

Quoted 5 lines
tools/perf/util/evsel.h +0 -0
 extern struct perf_missing_features perf_missing_features;
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
#3 lore raw
Quoted 92 lines · Arnaldo Carvalho de Melo
Quoted 80 lines · Jiri Olsa
Quoted 79 lines · Namhyung Kim

The current logic for the perf missing feature has a bug that it can wrongly clear some modifiers like G or H. Actually some PMUs don't support any filtering or exclusion while others do. But we check it as a global feature.

For example, the cycles event can have 'G' modifier to enable it only in the guest mode on x86. When you don't run any VMs it'll return 0.

  # perf stat -a -e cycles:G sleep 1

Performance counter stats for 'system wide':

0 cycles:G

1.000721670 seconds time elapsed

But when it's used with other pmu events that don't support G modifier, it'll be reset and return non-zero values.

  # perf stat -a -e cycles:G,msr/tsc/ sleep 1
    Performance counter stats for 'system wide':

          538,029,960      cycles:G
       16,924,010,738      msr/tsc/

          1.001815327 seconds time elapsed

This is because of the missing feature detection logic being global. Add a hashmap to set pmu-specific exclude_host/guest features.

---
 tools/perf/util/evsel.c | 47 +++++++++++++++++++++++++++++++++++------
 tools/perf/util/evsel.h |  6 ++++++
 2 files changed, 47 insertions(+), 6 deletions(-)
tools/perf/util/evsel.c +21 -0
index dbfeceb2546c..437a28e769fe 100644
@@ -1434,6 +1434,10 @@ void evsel__delete(struct evsel *evsel)
 {
 	evsel__exit(evsel);
 	free(evsel);
+
+	/* just free it for the first evsel */
+	hashmap__free(perf_missing_features.pmu);
+	perf_missing_features.pmu = NULL;
 }
 
 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
@@ -1791,6 +1795,23 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	return 0;
 }
 
+#define PMU_HASH_BITS  4
+
+static size_t pmu_hash(const void *key, void *ctx __maybe_unused)
+{
+	const struct evsel *evsel = key;
+
+	return hash_bits(evsel->core.attr.type, PMU_HASH_BITS);
+}
+
+static bool pmu_equal(const void *key1, const void *key2, void *ctx __maybe_unused)
+{
+	const struct evsel *a = key1;
+	const struct evsel *b = key2;
+
+	return a->core.attr.type == b->core.attr.type;
+}
+
 static void evsel__disable_missing_features(struct evsel *evsel)
 {
 	if (perf_missing_features.weight_struct) {
@@ -1807,8 +1828,14 @@ static void evsel__disable_missing_features(struct evsel *evsel)
 		evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Quoted 8 lines

tools/perf/util/evsel.c static void evsel__disable_missing_features(struct evsel *evsel) · line 1832

Quoted 6 lines
tools/perf/util/evsel.c +2 -2
 	if (perf_missing_features.mmap2)
 		evsel->core.attr.mmap2 = 0;
-	if (perf_missing_features.exclude_guest)
-		evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
+	if (perf_missing_features.exclude_guest) {
+		void *pmu;

could you just pass NULL in here instead of NULL?

Quoted 1 line

ENOPARSE

ugh right... 'instead of &pmu' ;-)

thanks,
jirka

Quoted 80 lines
Quoted 79 lines
Quoted 22 lines
tools/perf/util/evsel.c +9 -0
+
+		if (hashmap__find(perf_missing_features.pmu, evsel, &pmu)) {
+			evsel->core.attr.exclude_guest = 0;
+			evsel->core.attr.exclude_host = 0;
+		}
+	}
 	if (perf_missing_features.lbr_flags)
 		evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
 				     PERF_SAMPLE_BRANCH_NO_CYCLES);
@@ -1840,6 +1867,9 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 bool evsel__detect_missing_features(struct evsel *evsel)
 {
+	if (perf_missing_features.pmu == NULL)
+		perf_missing_features.pmu = hashmap__new(pmu_hash, pmu_equal, NULL);
+
 	/*
 	 * Must probe features in the order they were added to the
 	 * perf_event_attr interface.

tools/perf/util/evsel.c bool evsel__detect_missing_features(struct evsel *evsel) · line 1935

Quoted 10 lines
tools/perf/util/evsel.c +3 -4
@@ -1900,10 +1930,15 @@ bool evsel__detect_missing_features(struct evsel *evsel)
 		perf_missing_features.mmap2 = true;
 		pr_debug2_peo("switching off mmap2\n");
 		return true;
-	} else if (!perf_missing_features.exclude_guest &&
-		   (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
-		perf_missing_features.exclude_guest = true;
-		pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+	} else if ((evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host) &&
+		   !hashmap__find(perf_missing_features.pmu, evsel, NULL)) {
+		struct perf_missing_pmu_features pmu_features = { true };

missing new line after declaration

Quoted 14 lines
tools/perf/util/evsel.c +6 -0
+		hashmap__add(perf_missing_features.pmu, evsel, &pmu_features);
+
+		if (!perf_missing_features.exclude_guest) {
+			perf_missing_features.exclude_guest = true;
+			pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+		}
 		return true;
 	} else if (!perf_missing_features.sample_id_all) {
 		perf_missing_features.sample_id_all = true;
tools/perf/util/evsel.h +0 -0
index 1f7edfa8568a..8dd11c8e022d 100644

tools/perf/util/evsel.h struct perf_missing_features { · line 181

Quoted 10 lines
tools/perf/util/evsel.h +6 -0
@@ -172,6 +172,12 @@ struct perf_missing_features {
 	bool data_page_size;
 	bool code_page_size;
 	bool weight_struct;
+
+	struct hashmap *pmu;
+};
+
+struct perf_missing_pmu_features {
+	bool exclude_guest;
 };

hum, is this really needed? I think you could just pass '1' as value, because you care only if the item is hashed, right?

in any case the value is the current stack address of the
struct perf_missing_pmu_features pmu_features = { true };

so it might as well be just '1' ... I was confused at the beggining and looked for the reason of this struct ;-)

we do that already in util/stat.c

jirka

Quoted 5 lines
tools/perf/util/evsel.h +0 -0
 extern struct perf_missing_features perf_missing_features;
Signature · 1 line
2.33.0.1079.g6e70778dc9-goog
author maintainer
#4 lore raw
Quoted 94 lines · Jiri Olsa
Quoted 92 lines · Arnaldo Carvalho de Melo
Quoted 88 lines · Jiri Olsa
Quoted 78 lines · Namhyung Kim

The current logic for the perf missing feature has a bug that it can wrongly clear some modifiers like G or H. Actually some PMUs don't support any filtering or exclusion while others do. But we check it as a global feature.

For example, the cycles event can have 'G' modifier to enable it only in the guest mode on x86. When you don't run any VMs it'll return 0.

  # perf stat -a -e cycles:G sleep 1

Performance counter stats for 'system wide':

0 cycles:G

1.000721670 seconds time elapsed

But when it's used with other pmu events that don't support G modifier, it'll be reset and return non-zero values.

  # perf stat -a -e cycles:G,msr/tsc/ sleep 1
    Performance counter stats for 'system wide':

          538,029,960      cycles:G
       16,924,010,738      msr/tsc/

          1.001815327 seconds time elapsed

This is because of the missing feature detection logic being global. Add a hashmap to set pmu-specific exclude_host/guest features.

---
 tools/perf/util/evsel.c | 47 +++++++++++++++++++++++++++++++++++------
 tools/perf/util/evsel.h |  6 ++++++
 2 files changed, 47 insertions(+), 6 deletions(-)
tools/perf/util/evsel.c +21 -0
index dbfeceb2546c..437a28e769fe 100644
@@ -1434,6 +1434,10 @@ void evsel__delete(struct evsel *evsel)
 {
   evsel__exit(evsel);
   free(evsel);
+
+  /* just free it for the first evsel */
+  hashmap__free(perf_missing_features.pmu);
+  perf_missing_features.pmu = NULL;
 }
 
 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
@@ -1791,6 +1795,23 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
   return 0;
 }
 
+#define PMU_HASH_BITS  4
+
+static size_t pmu_hash(const void *key, void *ctx __maybe_unused)
+{
+  const struct evsel *evsel = key;
+
+  return hash_bits(evsel->core.attr.type, PMU_HASH_BITS);
+}
+
+static bool pmu_equal(const void *key1, const void *key2, void *ctx __maybe_unused)
+{
+  const struct evsel *a = key1;
+  const struct evsel *b = key2;
+
+  return a->core.attr.type == b->core.attr.type;
+}
+
 static void evsel__disable_missing_features(struct evsel *evsel)
 {
   if (perf_missing_features.weight_struct) {

tools/perf/util/evsel.c static void evsel__disable_missing_features(struct evsel *evsel) · line 1832

Quoted 7 lines
tools/perf/util/evsel.c +2 -2
@@ -1807,8 +1828,14 @@ static void evsel__disable_missing_features(struct evsel *evsel)
           evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
   if (perf_missing_features.mmap2)
           evsel->core.attr.mmap2 = 0;
-  if (perf_missing_features.exclude_guest)
-          evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
+  if (perf_missing_features.exclude_guest) {
+          void *pmu;

could you just pass NULL in here instead of NULL?

Quoted 3 lines
Quoted 1 line

ENOPARSE

ugh right... 'instead of &pmu' ;-)

Yes, we can pass NULL for the current code. Passing the pointer is just for future change. Maybe I thought too much.. :)

Anyway, I think it should pass the struct itself, not the pointer to the local variable.
I'm not sure if it's possible to cast a struct literal to a pointer, will check and update in v2.

Thanks,
Namhyung

author maintainer
#5 lore raw
In reply to Jiri Olsa

Hi Jiri,

Quoted 114 lines · Jiri Olsa
Quoted 78 lines · Namhyung Kim

The current logic for the perf missing feature has a bug that it can wrongly clear some modifiers like G or H. Actually some PMUs don't support any filtering or exclusion while others do. But we check it as a global feature.

For example, the cycles event can have 'G' modifier to enable it only in the guest mode on x86. When you don't run any VMs it'll return 0.

  # perf stat -a -e cycles:G sleep 1

Performance counter stats for 'system wide':

0 cycles:G

1.000721670 seconds time elapsed

But when it's used with other pmu events that don't support G modifier, it'll be reset and return non-zero values.

  # perf stat -a -e cycles:G,msr/tsc/ sleep 1
    Performance counter stats for 'system wide':

          538,029,960      cycles:G
       16,924,010,738      msr/tsc/

          1.001815327 seconds time elapsed

This is because of the missing feature detection logic being global. Add a hashmap to set pmu-specific exclude_host/guest features.

---
 tools/perf/util/evsel.c | 47 +++++++++++++++++++++++++++++++++++------
 tools/perf/util/evsel.h |  6 ++++++
 2 files changed, 47 insertions(+), 6 deletions(-)
tools/perf/util/evsel.c +21 -0
index dbfeceb2546c..437a28e769fe 100644
@@ -1434,6 +1434,10 @@ void evsel__delete(struct evsel *evsel)
 {
      evsel__exit(evsel);
      free(evsel);
+
+     /* just free it for the first evsel */
+     hashmap__free(perf_missing_features.pmu);
+     perf_missing_features.pmu = NULL;
 }
 
 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
@@ -1791,6 +1795,23 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
      return 0;
 }
 
+#define PMU_HASH_BITS  4
+
+static size_t pmu_hash(const void *key, void *ctx __maybe_unused)
+{
+     const struct evsel *evsel = key;
+
+     return hash_bits(evsel->core.attr.type, PMU_HASH_BITS);
+}
+
+static bool pmu_equal(const void *key1, const void *key2, void *ctx __maybe_unused)
+{
+     const struct evsel *a = key1;
+     const struct evsel *b = key2;
+
+     return a->core.attr.type == b->core.attr.type;
+}
+
 static void evsel__disable_missing_features(struct evsel *evsel)
 {
      if (perf_missing_features.weight_struct) {

tools/perf/util/evsel.c static void evsel__disable_missing_features(struct evsel *evsel) · line 1832

Quoted 7 lines
tools/perf/util/evsel.c +2 -2
@@ -1807,8 +1828,14 @@ static void evsel__disable_missing_features(struct evsel *evsel)
              evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
      if (perf_missing_features.mmap2)
              evsel->core.attr.mmap2 = 0;
-     if (perf_missing_features.exclude_guest)
-             evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
+     if (perf_missing_features.exclude_guest) {
+             void *pmu;

could you just pass NULL in here instead of NULL?

Quoted 24 lines
tools/perf/util/evsel.c +9 -1
+
+             if (hashmap__find(perf_missing_features.pmu, evsel, &pmu)) {
+                     evsel->core.attr.exclude_guest = 0;
+                     evsel->core.attr.exclude_host = 0;
+             }
+     }
      if (perf_missing_features.lbr_flags)
              evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
                                   PERF_SAMPLE_BRANCH_NO_CYCLES);
@@ -1840,6 +1867,9 @@ int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 bool evsel__detect_missing_features(struct evsel *evsel)
 {
+     if (perf_missing_features.pmu == NULL)
+             perf_missing_features.pmu = hashmap__new(pmu_hash, pmu_equal, NULL);
+
      /*
       * Must probe features in the order they were added to the
       * perf_event_attr interface.
@@ -1900,10 +1930,15 @@ bool evsel__detect_missing_features(struct evsel *evsel)
              perf_missing_features.mmap2 = true;
              pr_debug2_peo("switching off mmap2\n");
              return true;
-     } else if (!perf_missing_features.exclude_guest &&
Quoted 8 lines

tools/perf/util/evsel.c bool evsel__detect_missing_features(struct evsel *evsel) · line 1935

Quoted 6 lines
tools/perf/util/evsel.c +3 -3
-                (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) {
-             perf_missing_features.exclude_guest = true;
-             pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+     } else if ((evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host) &&
+                !hashmap__find(perf_missing_features.pmu, evsel, NULL)) {
+             struct perf_missing_pmu_features pmu_features = { true };

missing new line after declaration

ok

Quoted 20 lines
Quoted 20 lines
tools/perf/util/evsel.c +6 -0
+             hashmap__add(perf_missing_features.pmu, evsel, &pmu_features);
+
+             if (!perf_missing_features.exclude_guest) {
+                     perf_missing_features.exclude_guest = true;
+                     pr_debug2_peo("switching off exclude_guest, exclude_host\n");
+             }
              return true;
      } else if (!perf_missing_features.sample_id_all) {
              perf_missing_features.sample_id_all = true;
tools/perf/util/evsel.h +2 -0
index 1f7edfa8568a..8dd11c8e022d 100644
@@ -172,6 +172,12 @@ struct perf_missing_features {
      bool data_page_size;
      bool code_page_size;
      bool weight_struct;
+
+     struct hashmap *pmu;
Quoted 8 lines

tools/perf/util/evsel.h struct perf_missing_features { · line 181

Quoted 5 lines
tools/perf/util/evsel.h +4 -0
+};
+
+struct perf_missing_pmu_features {
+     bool exclude_guest;
 };

hum, is this really needed? I think you could just pass '1' as value, because you care only if the item is hashed, right?

It's not really needed, but I thought it might be useful to extend if we want to add another pmu-specific feature check later. But at this point I don't have a specific feature to add though.

Quoted 6 lines

in any case the value is the current stack address of the
struct perf_missing_pmu_features pmu_features = { true };

so it might as well be just '1' ... I was confused at the beggining and looked for the reason of this struct ;-)

Right, as I said it's not need to be a struct and we can pass a scalar value instead if you want.

Thanks,
Namhyung