mbox series

[v2,0/3] Use BPF filters for a "perf top -u" workaround

Message ID 20240521010439.321264-1-irogers@google.com (mailing list archive)
Headers show
Series Use BPF filters for a "perf top -u" workaround | expand

Message

Ian Rogers May 21, 2024, 1:04 a.m. UTC
Allow uid and gid to be terms in BPF filters by first breaking the
connection between filter terms and PERF_SAMPLE_xx values. Calculate
the uid and gid using the bpf_get_current_uid_gid helper, rather than
from a value in the sample. Allow filters to be passed to perf top, this allows:

$ perf top -e cycles:P --filter "uid == $(id -u)"

to work as a "perf top -u" workaround, as "perf top -u" usually fails
due to processes/threads terminating between the /proc scan and the
perf_event_open.

v2. Allow PERF_SAMPLE_xx to be computed from the PBF_TERM_xx value
    using a shift as requested by Namhyung.

Ian Rogers (3):
  perf bpf filter: Give terms their own enum
  perf bpf filter: Add uid and gid terms
  perf top: Allow filters on events

 tools/perf/Documentation/perf-record.txt     |  2 +-
 tools/perf/Documentation/perf-top.txt        |  4 ++
 tools/perf/builtin-top.c                     |  9 +++
 tools/perf/util/bpf-filter.c                 | 33 ++++++----
 tools/perf/util/bpf-filter.h                 |  5 +-
 tools/perf/util/bpf-filter.l                 | 66 ++++++++++----------
 tools/perf/util/bpf-filter.y                 |  7 ++-
 tools/perf/util/bpf_skel/sample-filter.h     | 61 +++++++++++++++++-
 tools/perf/util/bpf_skel/sample_filter.bpf.c | 54 +++++++++++-----
 9 files changed, 171 insertions(+), 70 deletions(-)

Comments

Namhyung Kim May 23, 2024, 4:38 a.m. UTC | #1
On Mon, May 20, 2024 at 6:04 PM Ian Rogers <irogers@google.com> wrote:
>
> Allow uid and gid to be terms in BPF filters by first breaking the
> connection between filter terms and PERF_SAMPLE_xx values. Calculate
> the uid and gid using the bpf_get_current_uid_gid helper, rather than
> from a value in the sample. Allow filters to be passed to perf top, this allows:
>
> $ perf top -e cycles:P --filter "uid == $(id -u)"
>
> to work as a "perf top -u" workaround, as "perf top -u" usually fails
> due to processes/threads terminating between the /proc scan and the
> perf_event_open.
>
> v2. Allow PERF_SAMPLE_xx to be computed from the PBF_TERM_xx value
>     using a shift as requested by Namhyung.
>
> Ian Rogers (3):
>   perf bpf filter: Give terms their own enum
>   perf bpf filter: Add uid and gid terms
>   perf top: Allow filters on events

Acked-by: Namhyung Kim <namhyung@kernel.org>

Thanks,
Namhyung

>
>  tools/perf/Documentation/perf-record.txt     |  2 +-
>  tools/perf/Documentation/perf-top.txt        |  4 ++
>  tools/perf/builtin-top.c                     |  9 +++
>  tools/perf/util/bpf-filter.c                 | 33 ++++++----
>  tools/perf/util/bpf-filter.h                 |  5 +-
>  tools/perf/util/bpf-filter.l                 | 66 ++++++++++----------
>  tools/perf/util/bpf-filter.y                 |  7 ++-
>  tools/perf/util/bpf_skel/sample-filter.h     | 61 +++++++++++++++++-
>  tools/perf/util/bpf_skel/sample_filter.bpf.c | 54 +++++++++++-----
>  9 files changed, 171 insertions(+), 70 deletions(-)
>
> --
> 2.45.0.rc1.225.g2a3ae87e7f-goog
>
Namhyung Kim May 24, 2024, 5:58 p.m. UTC | #2
On Wed, May 22, 2024 at 09:38:57PM -0700, Namhyung Kim wrote:
> On Mon, May 20, 2024 at 6:04 PM Ian Rogers <irogers@google.com> wrote:
> >
> > Allow uid and gid to be terms in BPF filters by first breaking the
> > connection between filter terms and PERF_SAMPLE_xx values. Calculate
> > the uid and gid using the bpf_get_current_uid_gid helper, rather than
> > from a value in the sample. Allow filters to be passed to perf top, this allows:
> >
> > $ perf top -e cycles:P --filter "uid == $(id -u)"
> >
> > to work as a "perf top -u" workaround, as "perf top -u" usually fails
> > due to processes/threads terminating between the /proc scan and the
> > perf_event_open.
> >
> > v2. Allow PERF_SAMPLE_xx to be computed from the PBF_TERM_xx value
> >     using a shift as requested by Namhyung.
> >
> > Ian Rogers (3):
> >   perf bpf filter: Give terms their own enum
> >   perf bpf filter: Add uid and gid terms
> >   perf top: Allow filters on events

Hmm.. I'm seeing this.

  $ make build-test
  ...
  cd . && make GEN_VMLINUX_H=1 FEATURES_DUMP=/home/namhyung/project/linux/tools/perf/BUILD_TEST_FEATURE_DUMP -j64 O=/tmp/tmp.EeXFOfLPt5 DESTDIR=/tmp/tmp.Y0eiZKvc9D
  ...
    CLANG   /tmp/tmp.EeXFOfLPt5/util/bpf_skel/.tmp/sample_filter.bpf.o            
  In file included from util/bpf_skel/sample_filter.bpf.c:8:                      
  In file included from util/bpf_skel/sample-filter.h:4:                          
  /home/namhyung/project/linux/tools/include/uapi/linux/perf_event.h:29:6: error: redefinition of 'perf_type_id'
  enum perf_type_id {                                                             
       ^                                                                          
  /tmp/tmp.EeXFOfLPt5/util/bpf_skel/.tmp/../vmlinux.h:54086:6: note: previous definition is here
  enum perf_type_id {                                                             
       ^                                                                          
  In file included from util/bpf_skel/sample_filter.bpf.c:8:                      
  In file included from util/bpf_skel/sample-filter.h:4:                          
  /home/namhyung/project/linux/tools/include/uapi/linux/perf_event.h:30:2: error: redefinition of enumerator 'PERF_TYPE_HARDWARE'
          PERF_TYPE_HARDWARE                      = 0,                            
          ^                                                                       
  /tmp/tmp.EeXFOfLPt5/util/bpf_skel/.tmp/../vmlinux.h:54087:2: note: previous definition is here
          PERF_TYPE_HARDWARE = 0,                                                 
          ^
  ...
  make[3]: *** [Makefile.perf:264: sub-make] Error 2                              
  make[2]: *** [Makefile:70: all] Error 2                                         
  make[1]: *** [tests/make:340: make_gen_vmlinux_h_O] Error 1                     
  make: *** [Makefile:103: build-test] Error 2

Thanks,
Namhyung
Ian Rogers May 24, 2024, 8:22 p.m. UTC | #3
On Fri, May 24, 2024 at 10:58 AM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Wed, May 22, 2024 at 09:38:57PM -0700, Namhyung Kim wrote:
> > On Mon, May 20, 2024 at 6:04 PM Ian Rogers <irogers@google.com> wrote:
> > >
> > > Allow uid and gid to be terms in BPF filters by first breaking the
> > > connection between filter terms and PERF_SAMPLE_xx values. Calculate
> > > the uid and gid using the bpf_get_current_uid_gid helper, rather than
> > > from a value in the sample. Allow filters to be passed to perf top, this allows:
> > >
> > > $ perf top -e cycles:P --filter "uid == $(id -u)"
> > >
> > > to work as a "perf top -u" workaround, as "perf top -u" usually fails
> > > due to processes/threads terminating between the /proc scan and the
> > > perf_event_open.
> > >
> > > v2. Allow PERF_SAMPLE_xx to be computed from the PBF_TERM_xx value
> > >     using a shift as requested by Namhyung.
> > >
> > > Ian Rogers (3):
> > >   perf bpf filter: Give terms their own enum
> > >   perf bpf filter: Add uid and gid terms
> > >   perf top: Allow filters on events
>
> Hmm.. I'm seeing this.
>
>   $ make build-test
>   ...
>   cd . && make GEN_VMLINUX_H=1 FEATURES_DUMP=/home/namhyung/project/linux/tools/perf/BUILD_TEST_FEATURE_DUMP -j64 O=/tmp/tmp.EeXFOfLPt5 DESTDIR=/tmp/tmp.Y0eiZKvc9D
>   ...
>     CLANG   /tmp/tmp.EeXFOfLPt5/util/bpf_skel/.tmp/sample_filter.bpf.o
>   In file included from util/bpf_skel/sample_filter.bpf.c:8:
>   In file included from util/bpf_skel/sample-filter.h:4:
>   /home/namhyung/project/linux/tools/include/uapi/linux/perf_event.h:29:6: error: redefinition of 'perf_type_id'
>   enum perf_type_id {
>        ^
>   /tmp/tmp.EeXFOfLPt5/util/bpf_skel/.tmp/../vmlinux.h:54086:6: note: previous definition is here
>   enum perf_type_id {
>        ^
>   In file included from util/bpf_skel/sample_filter.bpf.c:8:
>   In file included from util/bpf_skel/sample-filter.h:4:
>   /home/namhyung/project/linux/tools/include/uapi/linux/perf_event.h:30:2: error: redefinition of enumerator 'PERF_TYPE_HARDWARE'
>           PERF_TYPE_HARDWARE                      = 0,
>           ^
>   /tmp/tmp.EeXFOfLPt5/util/bpf_skel/.tmp/../vmlinux.h:54087:2: note: previous definition is here
>           PERF_TYPE_HARDWARE = 0,
>           ^
>   ...
>   make[3]: *** [Makefile.perf:264: sub-make] Error 2
>   make[2]: *** [Makefile:70: all] Error 2
>   make[1]: *** [tests/make:340: make_gen_vmlinux_h_O] Error 1
>   make: *** [Makefile:103: build-test] Error 2

IIRC this only exists for the sake of asserts. I can move the asserts
to placate the generated vmlinux.h. I'll send a v3.

Thanks,
Ian

> Thanks,
> Namhyung
>