linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] perf evsel: Fix missing exclude_{host,guest} setting
@ 2021-11-05 20:58 Namhyung Kim
  2021-11-06 19:09 ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 4+ messages in thread
From: Namhyung Kim @ 2021-11-05 20:58 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, LKML, Andi Kleen, Ian Rogers,
	Stephane Eranian, Ravi Bangoria

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.

Reported-by: Stephane Eranian <eranian@google.com>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
v4 changes)
 * add pmu->missing_features.exclude_guest
 * bail out if evsel->exclude_GH is set

v3 changes)
 * check memory allocation failure
 * add more NULL check

v2 changes)
 * change to enum perf_missing_pmu_features
 * pass NULL to hashmap__find() to skip checking
 * add a blank line after declaration

 tools/perf/util/evsel.c | 27 ++++++++++++++++++++++-----
 tools/perf/util/evsel.h |  4 ++++
 tools/perf/util/pmu.h   |  4 ++++
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 2cfc2935d1d2..3cc1f8fcf15c 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1824,7 +1824,7 @@ 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)
+	if (evsel->pmu && evsel->pmu->missing_features.exclude_guest)
 		evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
 	if (perf_missing_features.lbr_flags)
 		evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
@@ -1917,10 +1917,27 @@ 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) &&
+		   (evsel->pmu == NULL || evsel->pmu->missing_features.exclude_guest)) {
+		if (evsel->pmu == NULL) {
+			evsel->pmu = evsel__find_pmu(evsel);
+			if (evsel->pmu)
+				evsel->pmu->missing_features.exclude_guest = true;
+			else {
+				/* we cannot find PMU, disable attrs now */
+				evsel->core.attr.exclude_host = false;
+				evsel->core.attr.exclude_guest = false;
+			}
+		}
+
+		if (evsel->exclude_GH) {
+			pr_debug2_peo("PMU has no exclude_host/guest support, bailing out\n");
+			return false;
+		}
+		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;
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 846c827934de..dcc87c2881b8 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -22,6 +22,7 @@ struct target;
 struct hashmap;
 struct bperf_leader_bpf;
 struct bperf_follower_bpf;
+struct perf_pmu;
 
 typedef int (evsel__sb_cb_t)(union perf_event *event, void *data);
 
@@ -153,6 +154,9 @@ struct evsel {
 	};
 	unsigned long		open_flags;
 	int			precise_ip_original;
+
+	/* for missing_features */
+	struct perf_pmu		*pmu;
 };
 
 struct perf_missing_features {
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 15bbec3a9959..541889fa9f9c 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -49,6 +49,10 @@ struct perf_pmu {
 	struct list_head caps;    /* HEAD struct perf_pmu_caps -> list */
 	struct list_head list;    /* ELEM */
 	struct list_head hybrid_list;
+
+	struct {
+		bool exclude_guest;
+	} missing_features;
 };
 
 extern struct perf_pmu perf_pmu__fake;
-- 
2.34.0.rc0.344.g81b53c2807-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] perf evsel: Fix missing exclude_{host,guest} setting
  2021-11-05 20:58 [PATCH v4] perf evsel: Fix missing exclude_{host,guest} setting Namhyung Kim
@ 2021-11-06 19:09 ` Arnaldo Carvalho de Melo
  2021-11-06 19:19   ` Arnaldo Carvalho de Melo
  2021-11-08 18:53   ` Namhyung Kim
  0 siblings, 2 replies; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-11-06 19:09 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Jiri Olsa, Ingo Molnar, Peter Zijlstra, LKML, Andi Kleen,
	Ian Rogers, Stephane Eranian, Ravi Bangoria

Em Fri, Nov 05, 2021 at 01:58:47PM -0700, Namhyung Kim escreveu:
> 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.

⬢[acme@toolbox perf]$ perf test python
19: 'import perf' in python                                         : FAILED!
⬢[acme@toolbox perf]$ perf test -v python
Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc
19: 'import perf' in python                                         :
--- start ---
test child forked, pid 11602
python usage test: "echo "import sys ; sys.path.append('/tmp/build/perf/python'); import perf" | '/usr/bin/python3' "
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /tmp/build/perf/python/perf.cpython-39-x86_64-linux-gnu.so: undefined symbol: evsel__find_pmu
test child finished with -1
---- end ----
'import perf' in python: FAILED!
⬢[acme@toolbox perf]$

Trying to fix this now. please do a 'perf test' before submitting
patches.

- Arnaldo

 
> Reported-by: Stephane Eranian <eranian@google.com>
> Cc: Ravi Bangoria <ravi.bangoria@amd.com>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
> v4 changes)
>  * add pmu->missing_features.exclude_guest
>  * bail out if evsel->exclude_GH is set
> 
> v3 changes)
>  * check memory allocation failure
>  * add more NULL check
> 
> v2 changes)
>  * change to enum perf_missing_pmu_features
>  * pass NULL to hashmap__find() to skip checking
>  * add a blank line after declaration
> 
>  tools/perf/util/evsel.c | 27 ++++++++++++++++++++++-----
>  tools/perf/util/evsel.h |  4 ++++
>  tools/perf/util/pmu.h   |  4 ++++
>  3 files changed, 30 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 2cfc2935d1d2..3cc1f8fcf15c 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -1824,7 +1824,7 @@ 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)
> +	if (evsel->pmu && evsel->pmu->missing_features.exclude_guest)
>  		evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0;
>  	if (perf_missing_features.lbr_flags)
>  		evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
> @@ -1917,10 +1917,27 @@ 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) &&
> +		   (evsel->pmu == NULL || evsel->pmu->missing_features.exclude_guest)) {
> +		if (evsel->pmu == NULL) {
> +			evsel->pmu = evsel__find_pmu(evsel);
> +			if (evsel->pmu)
> +				evsel->pmu->missing_features.exclude_guest = true;
> +			else {
> +				/* we cannot find PMU, disable attrs now */
> +				evsel->core.attr.exclude_host = false;
> +				evsel->core.attr.exclude_guest = false;
> +			}
> +		}
> +
> +		if (evsel->exclude_GH) {
> +			pr_debug2_peo("PMU has no exclude_host/guest support, bailing out\n");
> +			return false;
> +		}
> +		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;
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index 846c827934de..dcc87c2881b8 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -22,6 +22,7 @@ struct target;
>  struct hashmap;
>  struct bperf_leader_bpf;
>  struct bperf_follower_bpf;
> +struct perf_pmu;
>  
>  typedef int (evsel__sb_cb_t)(union perf_event *event, void *data);
>  
> @@ -153,6 +154,9 @@ struct evsel {
>  	};
>  	unsigned long		open_flags;
>  	int			precise_ip_original;
> +
> +	/* for missing_features */
> +	struct perf_pmu		*pmu;
>  };
>  
>  struct perf_missing_features {
> diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
> index 15bbec3a9959..541889fa9f9c 100644
> --- a/tools/perf/util/pmu.h
> +++ b/tools/perf/util/pmu.h
> @@ -49,6 +49,10 @@ struct perf_pmu {
>  	struct list_head caps;    /* HEAD struct perf_pmu_caps -> list */
>  	struct list_head list;    /* ELEM */
>  	struct list_head hybrid_list;
> +
> +	struct {
> +		bool exclude_guest;
> +	} missing_features;
>  };
>  
>  extern struct perf_pmu perf_pmu__fake;
> -- 
> 2.34.0.rc0.344.g81b53c2807-goog

-- 

- Arnaldo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] perf evsel: Fix missing exclude_{host,guest} setting
  2021-11-06 19:09 ` Arnaldo Carvalho de Melo
@ 2021-11-06 19:19   ` Arnaldo Carvalho de Melo
  2021-11-08 18:53   ` Namhyung Kim
  1 sibling, 0 replies; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-11-06 19:19 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Jiri Olsa, Ingo Molnar, Peter Zijlstra, LKML, Andi Kleen,
	Ian Rogers, Stephane Eranian, Ravi Bangoria

Em Sat, Nov 06, 2021 at 04:09:49PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Fri, Nov 05, 2021 at 01:58:47PM -0700, Namhyung Kim escreveu:
> > 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.
> 
> ⬢[acme@toolbox perf]$ perf test python
> 19: 'import perf' in python                                         : FAILED!
> ⬢[acme@toolbox perf]$ perf test -v python
> Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc
> 19: 'import perf' in python                                         :
> --- start ---
> test child forked, pid 11602
> python usage test: "echo "import sys ; sys.path.append('/tmp/build/perf/python'); import perf" | '/usr/bin/python3' "
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: /tmp/build/perf/python/perf.cpython-39-x86_64-linux-gnu.so: undefined symbol: evsel__find_pmu
> test child finished with -1
> ---- end ----
> 'import perf' in python: FAILED!
> ⬢[acme@toolbox perf]$
> 
> Trying to fix this now. please do a 'perf test' before submitting
> patches.

Added this bandaid, should be good for the time being.

- Arnaldo

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 8feef3a05af7b31d..563a9ba8954f31b3 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -69,6 +69,18 @@ void perf_stat__collect_metric_expr(struct evlist *evsel_list)
 {
 }

+/*
+ * This one is needed not to drag the PMU bandwagon, jevents generated
+ * pmu_sys_event_tables, etc and evsel__find_pmu() is used so far just for
+ * doing per PMU perf_event_attr.exclude_guest handling, not really needed, so
+ * far, for the perf python binding known usecases, revisit if this become
+ * necessary.
+ */
+struct perf_pmu *evsel__find_pmu(struct evsel *evsel __maybe_unused)
+{
+       return NULL;
+}
+


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] perf evsel: Fix missing exclude_{host,guest} setting
  2021-11-06 19:09 ` Arnaldo Carvalho de Melo
  2021-11-06 19:19   ` Arnaldo Carvalho de Melo
@ 2021-11-08 18:53   ` Namhyung Kim
  1 sibling, 0 replies; 4+ messages in thread
From: Namhyung Kim @ 2021-11-08 18:53 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Jiri Olsa, Ingo Molnar, Peter Zijlstra, LKML, Andi Kleen,
	Ian Rogers, Stephane Eranian, Ravi Bangoria

Hi Arnaldo,

On Sat, Nov 6, 2021 at 12:09 PM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Em Fri, Nov 05, 2021 at 01:58:47PM -0700, Namhyung Kim escreveu:
> > 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.
>
> ⬢[acme@toolbox perf]$ perf test python
> 19: 'import perf' in python                                         : FAILED!
> ⬢[acme@toolbox perf]$ perf test -v python
> Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc
> 19: 'import perf' in python                                         :
> --- start ---
> test child forked, pid 11602
> python usage test: "echo "import sys ; sys.path.append('/tmp/build/perf/python'); import perf" | '/usr/bin/python3' "
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: /tmp/build/perf/python/perf.cpython-39-x86_64-linux-gnu.so: undefined symbol: evsel__find_pmu
> test child finished with -1
> ---- end ----
> 'import perf' in python: FAILED!
> ⬢[acme@toolbox perf]$
>
> Trying to fix this now. please do a 'perf test' before submitting
> patches.

Oops, sorry about that.  Forgot to run it after the change.
I'll check it next time and thanks for the fix.

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-11-08 18:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-05 20:58 [PATCH v4] perf evsel: Fix missing exclude_{host,guest} setting Namhyung Kim
2021-11-06 19:09 ` Arnaldo Carvalho de Melo
2021-11-06 19:19   ` Arnaldo Carvalho de Melo
2021-11-08 18:53   ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).