linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] perf tools: Get a perf cgroup more portably in BPF
@ 2022-09-22  4:40 Namhyung Kim
  2022-09-22 19:23 ` Arnaldo Carvalho de Melo
  2022-09-22 19:35 ` Hao Luo
  0 siblings, 2 replies; 4+ messages in thread
From: Namhyung Kim @ 2022-09-22  4:40 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, LKML, Ian Rogers, Adrian Hunter,
	linux-perf-users, Song Liu, bpf

The perf_event_cgrp_id can be different on other configurations.
To be more portable as CO-RE, it needs to get the cgroup subsys id
using the bpf_core_enum_value() helper.

Suggested-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
v2 changes)
 * fix off_cpu.bpf.c too
 * get perf_subsys_id only once

 tools/perf/util/bpf_skel/bperf_cgroup.bpf.c |  6 +++++-
 tools/perf/util/bpf_skel/off_cpu.bpf.c      | 12 ++++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
index 292c430768b5..9223e4b87fe9 100644
--- a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
+++ b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
@@ -48,6 +48,7 @@ const volatile __u32 num_cpus = 1;
 
 int enabled = 0;
 int use_cgroup_v2 = 0;
+int perf_subsys_id = -1;
 
 static inline int get_cgroup_v1_idx(__u32 *cgrps, int size)
 {
@@ -58,7 +59,10 @@ static inline int get_cgroup_v1_idx(__u32 *cgrps, int size)
 	int level;
 	int cnt;
 
-	cgrp = BPF_CORE_READ(p, cgroups, subsys[perf_event_cgrp_id], cgroup);
+	if (perf_subsys_id == -1)
+		perf_subsys_id = bpf_core_enum_value(enum cgroup_subsys_id, perf_event_cgrp_id);
+
+	cgrp = BPF_CORE_READ(p, cgroups, subsys[perf_subsys_id], cgroup);
 	level = BPF_CORE_READ(cgrp, level);
 
 	for (cnt = 0; i < MAX_LEVELS; i++) {
diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_skel/off_cpu.bpf.c
index c4ba2bcf179f..e917ef7b8875 100644
--- a/tools/perf/util/bpf_skel/off_cpu.bpf.c
+++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c
@@ -94,6 +94,8 @@ const volatile bool has_prev_state = false;
 const volatile bool needs_cgroup = false;
 const volatile bool uses_cgroup_v1 = false;
 
+int perf_subsys_id = -1;
+
 /*
  * Old kernel used to call it task_struct->state and now it's '__state'.
  * Use BPF CO-RE "ignored suffix rule" to deal with it like below:
@@ -119,11 +121,13 @@ static inline __u64 get_cgroup_id(struct task_struct *t)
 {
 	struct cgroup *cgrp;
 
-	if (uses_cgroup_v1)
-		cgrp = BPF_CORE_READ(t, cgroups, subsys[perf_event_cgrp_id], cgroup);
-	else
-		cgrp = BPF_CORE_READ(t, cgroups, dfl_cgrp);
+	if (!uses_cgroup_v1)
+		return BPF_CORE_READ(t, cgroups, dfl_cgrp, kn, id);
+
+	if (perf_subsys_id == -1)
+		perf_subsys_id = bpf_core_enum_value(enum cgroup_subsys_id, perf_event_cgrp_id);
 
+	cgrp = BPF_CORE_READ(t, cgroups, subsys[perf_subsys_id], cgroup);
 	return BPF_CORE_READ(cgrp, kn, id);
 }
 
-- 
2.37.3.968.ga6b4b080e4-goog


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

* Re: [PATCH v2] perf tools: Get a perf cgroup more portably in BPF
  2022-09-22  4:40 [PATCH v2] perf tools: Get a perf cgroup more portably in BPF Namhyung Kim
@ 2022-09-22 19:23 ` Arnaldo Carvalho de Melo
  2022-09-22 19:35 ` Hao Luo
  1 sibling, 0 replies; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-09-22 19:23 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Jiri Olsa, Ingo Molnar, Peter Zijlstra, LKML, Ian Rogers,
	Adrian Hunter, linux-perf-users, Song Liu, bpf

Em Wed, Sep 21, 2022 at 09:40:23PM -0700, Namhyung Kim escreveu:
> The perf_event_cgrp_id can be different on other configurations.
> To be more portable as CO-RE, it needs to get the cgroup subsys id
> using the bpf_core_enum_value() helper.
> 
> Suggested-by: Ian Rogers <irogers@google.com>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Applying, Ian, can I have your Reviewed-by?

- Arnaldo

> ---
> v2 changes)
>  * fix off_cpu.bpf.c too
>  * get perf_subsys_id only once
> 
>  tools/perf/util/bpf_skel/bperf_cgroup.bpf.c |  6 +++++-
>  tools/perf/util/bpf_skel/off_cpu.bpf.c      | 12 ++++++++----
>  2 files changed, 13 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
> index 292c430768b5..9223e4b87fe9 100644
> --- a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
> +++ b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
> @@ -48,6 +48,7 @@ const volatile __u32 num_cpus = 1;
>  
>  int enabled = 0;
>  int use_cgroup_v2 = 0;
> +int perf_subsys_id = -1;
>  
>  static inline int get_cgroup_v1_idx(__u32 *cgrps, int size)
>  {
> @@ -58,7 +59,10 @@ static inline int get_cgroup_v1_idx(__u32 *cgrps, int size)
>  	int level;
>  	int cnt;
>  
> -	cgrp = BPF_CORE_READ(p, cgroups, subsys[perf_event_cgrp_id], cgroup);
> +	if (perf_subsys_id == -1)
> +		perf_subsys_id = bpf_core_enum_value(enum cgroup_subsys_id, perf_event_cgrp_id);
> +
> +	cgrp = BPF_CORE_READ(p, cgroups, subsys[perf_subsys_id], cgroup);
>  	level = BPF_CORE_READ(cgrp, level);
>  
>  	for (cnt = 0; i < MAX_LEVELS; i++) {
> diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_skel/off_cpu.bpf.c
> index c4ba2bcf179f..e917ef7b8875 100644
> --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c
> +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c
> @@ -94,6 +94,8 @@ const volatile bool has_prev_state = false;
>  const volatile bool needs_cgroup = false;
>  const volatile bool uses_cgroup_v1 = false;
>  
> +int perf_subsys_id = -1;
> +
>  /*
>   * Old kernel used to call it task_struct->state and now it's '__state'.
>   * Use BPF CO-RE "ignored suffix rule" to deal with it like below:
> @@ -119,11 +121,13 @@ static inline __u64 get_cgroup_id(struct task_struct *t)
>  {
>  	struct cgroup *cgrp;
>  
> -	if (uses_cgroup_v1)
> -		cgrp = BPF_CORE_READ(t, cgroups, subsys[perf_event_cgrp_id], cgroup);
> -	else
> -		cgrp = BPF_CORE_READ(t, cgroups, dfl_cgrp);
> +	if (!uses_cgroup_v1)
> +		return BPF_CORE_READ(t, cgroups, dfl_cgrp, kn, id);
> +
> +	if (perf_subsys_id == -1)
> +		perf_subsys_id = bpf_core_enum_value(enum cgroup_subsys_id, perf_event_cgrp_id);
>  
> +	cgrp = BPF_CORE_READ(t, cgroups, subsys[perf_subsys_id], cgroup);
>  	return BPF_CORE_READ(cgrp, kn, id);
>  }
>  
> -- 
> 2.37.3.968.ga6b4b080e4-goog

-- 

- Arnaldo

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

* Re: [PATCH v2] perf tools: Get a perf cgroup more portably in BPF
  2022-09-22  4:40 [PATCH v2] perf tools: Get a perf cgroup more portably in BPF Namhyung Kim
  2022-09-22 19:23 ` Arnaldo Carvalho de Melo
@ 2022-09-22 19:35 ` Hao Luo
  2022-09-22 20:42   ` Namhyung Kim
  1 sibling, 1 reply; 4+ messages in thread
From: Hao Luo @ 2022-09-22 19:35 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Jiri Olsa, Ingo Molnar, Peter Zijlstra,
	LKML, Ian Rogers, Adrian Hunter, linux-perf-users, Song Liu, bpf

On Wed, Sep 21, 2022 at 9:40 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> The perf_event_cgrp_id can be different on other configurations.
> To be more portable as CO-RE, it needs to get the cgroup subsys id
> using the bpf_core_enum_value() helper.
>

I remember using bpf_core_enum_value requires a compiler built-in. So
the build will fail on old compiler such as clang-11. See [1]. Maybe
we should surround it with #if
__has_builtin(__builtin_preserve_enum_value) to be sure.

[1]  https://www.spinics.net/lists/bpf/msg30859.html

> Suggested-by: Ian Rogers <irogers@google.com>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
[...]

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

* Re: [PATCH v2] perf tools: Get a perf cgroup more portably in BPF
  2022-09-22 19:35 ` Hao Luo
@ 2022-09-22 20:42   ` Namhyung Kim
  0 siblings, 0 replies; 4+ messages in thread
From: Namhyung Kim @ 2022-09-22 20:42 UTC (permalink / raw)
  To: Hao Luo
  Cc: Arnaldo Carvalho de Melo, Jiri Olsa, Ingo Molnar, Peter Zijlstra,
	LKML, Ian Rogers, Adrian Hunter, linux-perf-users, Song Liu, bpf

Hi Hao,

On Thu, Sep 22, 2022 at 12:36 PM Hao Luo <haoluo@google.com> wrote:
>
> On Wed, Sep 21, 2022 at 9:40 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > The perf_event_cgrp_id can be different on other configurations.
> > To be more portable as CO-RE, it needs to get the cgroup subsys id
> > using the bpf_core_enum_value() helper.
> >
>
> I remember using bpf_core_enum_value requires a compiler built-in. So
> the build will fail on old compiler such as clang-11. See [1]. Maybe
> we should surround it with #if
> __has_builtin(__builtin_preserve_enum_value) to be sure.
>
> [1]  https://www.spinics.net/lists/bpf/msg30859.html

Thanks for pointing this out.  As this is a kind of optimization
I think we can fallback to using the existing value if not available.
Will send v3.

Thanks,
Namhyung

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

end of thread, other threads:[~2022-09-22 20:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-22  4:40 [PATCH v2] perf tools: Get a perf cgroup more portably in BPF Namhyung Kim
2022-09-22 19:23 ` Arnaldo Carvalho de Melo
2022-09-22 19:35 ` Hao Luo
2022-09-22 20:42   ` 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).