perf stat: Enable BPF counters with --for-each-cgroup
Namhyung Kim
maintainerv4 cover perf stat: Enable BPF counters with --for-each-cgroup
Hello,
This is to add BPF support for --for-each-cgroup to handle many cgroup events on big machines. You can use the --bpf-counters to enable the new behavior.
* changes in v4
- convert cgrp_readings to a per-cpu array map
- remove now-unused cpu_idx map
- move common functions to a header file
- reuse bpftool bootstrap binary
- fix build error in the cgroup code
* changes in v3
- support cgroup hierarchy with ancestor ids
- add and trigger raw_tp BPF program
- add a build rule for vmlinux.h
* changes in v2
- remove incorrect use of BPF_F_PRESERVE_ELEMS
- add missing map elements after lookup
- handle cgroup v1
Basic idea is to use a single set of per-cpu events to count interested events and aggregate them to each cgroup. I used bperf mechanism to use a BPF program for cgroup-switches and save the results in a matching map element for given cgroups.
Without this, we need to have separate events for cgroups, and it creates unnecessary multiplexing overhead (and PMU programming) when tasks in different cgroups are switched. I saw this makes a big difference on 256 cpu machines with hundreds of cgroups.
Actually this is what I wanted to do it in the kernel [1], but we can do the job using BPF!
Thanks,
Namhyung
[1] https://lore.kernel.org/lkml/[email protected]/
Namhyung Kim (4):
perf tools: Add read_cgroup_id() function perf tools: Add cgroup_is_v2() helper
perf tools: Move common bpf functions to bpf_counter.h perf stat: Enable BPF counter with --for-each-cgroup
tools/perf/Makefile.perf | 17 +- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 57 +--- tools/perf/util/bpf_counter.h | 52 ++++ tools/perf/util/bpf_counter_cgroup.c | 299 ++++++++++++++++++++ tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 191 +++++++++++++ tools/perf/util/cgroup.c | 46 +++ tools/perf/util/cgroup.h | 12 + 8 files changed, 622 insertions(+), 53 deletions(-) create mode 100644 tools/perf/util/bpf_counter_cgroup.c create mode 100644 tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
Signature · 1 line
2.32.0.93.g670b81a890-goog
Namhyung Kim
maintainerPATCH v4 1/4 perf tools: Add read_cgroup_id() function
The read_cgroup_id() is to read a cgroup id from a file handle using name_to_handle_at(2) for the given cgroup. It'll be used by bperf cgroup stat later.
--- tools/perf/util/cgroup.c | 25 +++++++++++++++++++++++++ tools/perf/util/cgroup.h | 9 +++++++++ 2 files changed, 34 insertions(+)
tools/perf/util/cgroup.c +25 -0
@@ -45,6 +45,31 @@ static int open_cgroup(const char *name)return fd; } +#ifdef HAVE_FILE_HANDLE +int read_cgroup_id (struct cgroup *cgrp) +{ +char path[PATH_MAX +1 ]; +char mnt[PATH_MAX +1 ]; +struct { +struct file_handle fh; +uint64_t cgroup_id; + } handle; +int mount_id; + +if (cgroupfs_find_mountpoint (mnt,PATH_MAX +1 ,"perf_event" )) +return -1 ; + +scnprintf (path,PATH_MAX ,"%s/%s" , mnt, cgrp->name); + + handle.fh.handle_bytes =sizeof (handle.cgroup_id); +if (name_to_handle_at (AT_FDCWD , path, &handle.fh, &mount_id,0 ) <0 ) +return -1 ; + + cgrp->id = handle.cgroup_id; +return 0 ; +} +#endif /* HAVE_FILE_HANDLE */ +static struct cgroup *evlist__find_cgroup (struct evlist *evlist,const char *str) {struct evsel *counter;
tools/perf/util/cgroup.h +9 -0
@@ -38,4 +38,13 @@ struct cgroup *cgroup__find(struct perf_env *env, uint64_t id);void perf_env__purge_cgroups (struct perf_env *env); +#ifdef HAVE_FILE_HANDLE +int read_cgroup_id (struct cgroup *cgrp); +#else +int read_cgroup_id (struct cgroup *cgrp) +{ +return -1 ; +} +#endif /* HAVE_FILE_HANDLE */ +#endif /* __CGROUP_H__ */
Signature · 1 line
2.32.0.93.g670b81a890-goog
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 1/4] perf tools: Add read_cgroup_id() function
Em Fri, Jun 25, 2021 at 12:18:23AM -0700, Namhyung Kim escreveu:
Quoted 67 lines
The
read_cgroup_id()is to read a cgroup id from a file handle usingname_to_handle_at(2)for the given cgroup. It'll be used by bperf cgroup stat later.Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/cgroup.c | 25 +++++++++++++++++++++++++ tools/perf/util/cgroup.h | 9 +++++++++ 2 files changed, 34 insertions(+)tools/perf/util/cgroup.c +25
-0@@ -45,6 +45,31 @@ static int open_cgroup(const char *name)return fd; } +#ifdef HAVE_FILE_HANDLE +int read_cgroup_id (struct cgroup *cgrp) +{ +char path[PATH_MAX +1 ]; +char mnt[PATH_MAX +1 ]; +struct { +struct file_handle fh; +uint64_t cgroup_id; + } handle; +int mount_id; + +if (cgroupfs_find_mountpoint (mnt,PATH_MAX +1 ,"perf_event" )) +return -1 ; + +scnprintf (path,PATH_MAX ,"%s/%s" , mnt, cgrp->name); + + handle.fh.handle_bytes =sizeof (handle.cgroup_id); +if (name_to_handle_at (AT_FDCWD , path, &handle.fh, &mount_id,0 ) <0 ) +return -1 ; + + cgrp->id = handle.cgroup_id; +return 0 ; +} +#endif /* HAVE_FILE_HANDLE */ +static struct cgroup *evlist__find_cgroup (struct evlist *evlist,const char *str) {struct evsel *counter;tools/perf/util/cgroup.h +9
-0@@ -38,4 +38,13 @@ struct cgroup *cgroup__find(struct perf_env *env, uint64_t id);void perf_env__purge_cgroups (struct perf_env *env); +#ifdef HAVE_FILE_HANDLE +int read_cgroup_id (struct cgroup *cgrp); +#else +int read_cgroup_id (struct cgroup *cgrp) +{ +return -1 ; +} +#endif /* HAVE_FILE_HANDLE */ +#endif /* __CGROUP_H__ */ Signature · 1 line
2.32.0.93.g670b81a890-goog
You forgot the __maybe_unused in the !HAVE_FILE_HANDLE case, also the static inline for functions defined in headers, I'm fixing this up:
Alpine clang version 8.0.0 (tags/RELEASE_800/final) (based on LLVM 8.0.0)
Target: x86_64-alpine-linux-musl
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-alpine-linux-musl/8.3.0 Found candidate GCC installation: /usr/lib/gcc/x86_64-alpine-linux-musl/8.3.0 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-alpine-linux-musl/8.3.0 Candidate multilib: .;@m64
Selected multilib: .;@m64
+ rm -rf /tmp/build/perf
+ mkdir /tmp/build/perf
+ make 'ARCH=' 'CROSS_COMPILE=' 'EXTRA_CFLAGS=' -C tools/perf 'O=/tmp/build/perf' 'CC=clang'
make: Entering directory '/git/perf-5.13.0/tools/perf'
BUILD: Doing 'make -j24' parallel build
HOSTCC /tmp/build/perf/fixdep.o HOSTLD /tmp/build/perf/fixdep-in.o LINK /tmp/build/perf/fixdep
Makefile.config:439: No libdw DWARF unwind found, Please install elfutils-devel/libdw-dev >= 0.158 and/or set LIBDW_DIR Makefile.config:444: No libdw.h found or old libdw.h found or elfutils is older than 0.138, disables dwarf support. Please install new elfutils-devel/libdw-dev Makefile.config:563: DWARF support is off, BPF prologue is disabled
Makefile.config:571: No sys/sdt.h found, no SDT events are defined, please install systemtap-sdt-devel or systemtap-sdt-dev Makefile.config:998: No libbabeltrace found, disables 'perf data' CTF format support, please install libbabeltrace-dev[el]/libbabeltrace-ctf-dev Makefile.config:1024: No alternatives command found, you need to set JDIR= to point to the root of your Java directory
Auto-detecting system features:
... dwarf: [ OFF ]
... dwarf_getlocations: [ OFF ]
... glibc: [ OFF ]
... libbfd: [ on ] ... libbfd-buildid: [ on ] ... libcap: [ on ] ... libelf: [ on ] ... libnuma: [ on ] ... numa_num_possible_cpus: [ on ] ... libperl: [ on ] ... libpython: [ on ] ... libcrypto: [ on ] ... libunwind: [ on ] ... libdw-dwarf-unwind: [ OFF ] ... zlib: [ on ] ... lzma: [ on ] ... get_cpuid: [ on ] ... bpf: [ on ] ... libaio: [ on ] ... libzstd: [ on ] ... disassembler-four-args: [ on ]
GEN /tmp/build/perf/common-cmds.h
PERF_VERSION = 5.13.gfdf19665b1fb
MKDIR /tmp/build/perf/pmu-events/
MKDIR /tmp/build/perf/pmu-events/
MKDIR /tmp/build/perf/pmu-events/
HOSTCC /tmp/build/perf/pmu-events/json.o
HOSTCC /tmp/build/perf/pmu-events/jsmn.o
HOSTCC /tmp/build/perf/pmu-events/jevents.o
GEN perf-archive
GEN perf-with-kcore
GEN perf-iostat
CC /tmp/build/perf/exec-cmd.o
CC /tmp/build/perf/help.o
CC /tmp/build/perf/pager.o
CC /tmp/build/perf/parse-options.o
CC /tmp/build/perf/run-command.o
CC /tmp/build/perf/sigchain.o
CC /tmp/build/perf/subcmd-config.o
CC /tmp/build/perf/cpu.o
CC /tmp/build/perf/debug.o
MKDIR /tmp/build/perf/fd/
MKDIR /tmp/build/perf/fs/
CC /tmp/build/perf/str_error_r.o
MKDIR /tmp/build/perf/fs/
CC /tmp/build/perf/fd/array.o
MKDIR /tmp/build/perf/fs/
CC /tmp/build/perf/fs/fs.o
CC /tmp/build/perf/fs/tracing_path.o
CC /tmp/build/perf/fs/cgroup.o
CC /tmp/build/perf/event-parse.o
CC /tmp/build/perf/event-plugin.o
CC /tmp/build/perf/trace-seq.o
CC /tmp/build/perf/parse-filter.o
CC /tmp/build/perf/parse-utils.o
CC /tmp/build/perf/kbuffer-parse.o
CC /tmp/build/perf/core.o
CC /tmp/build/perf/cpumap.o
GEN /tmp/build/perf/bpf_helper_defs.h
CC /tmp/build/perf/threadmap.o
CC /tmp/build/perf/evsel.o
LD /tmp/build/perf/fd/libapi-in.o
CC /tmp/build/perf/tep_strerror.o
CC /tmp/build/perf/plugin_jbd2.o
CC /tmp/build/perf/plugin_hrtimer.o
CC /tmp/build/perf/event-parse-api.o
HOSTLD /tmp/build/perf/pmu-events/jevents-in.o
CC /tmp/build/perf/plugin_kvm.o
CC /tmp/build/perf/plugin_kmem.o
LINK /tmp/build/perf/pmu-events/jevents
CC /tmp/build/perf/evlist.o
CC /tmp/build/perf/mmap.o
LD /tmp/build/perf/fs/libapi-in.o
CC /tmp/build/perf/plugin_mac80211.o
GEN /tmp/build/perf/pmu-events/pmu-events.c
CC /tmp/build/perf/zalloc.o
LD /tmp/build/perf/libapi-in.o
AR /tmp/build/perf/libapi.a
CC /tmp/build/perf/xyarray.o
CC /tmp/build/perf/lib.o
CC /tmp/build/perf/plugin_sched_switch.o
LD /tmp/build/perf/plugin_kmem-in.o
LD /tmp/build/perf/plugin_jbd2-in.o
CC /tmp/build/perf/plugin_function.o
LD /tmp/build/perf/plugin_hrtimer-in.o
CC /tmp/build/perf/plugin_futex.o
CC /tmp/build/perf/plugin_scsi.o
CC /tmp/build/perf/plugin_cfg80211.o
LD /tmp/build/perf/plugin_kvm-in.o
MKDIR /tmp/build/perf/staticobjs/
CC /tmp/build/perf/staticobjs/libbpf.o
CC /tmp/build/perf/plugin_xen.o
MKDIR /tmp/build/perf/staticobjs/
CC /tmp/build/perf/staticobjs/bpf.o
CC /tmp/build/perf/plugin_tlb.o
CC /tmp/build/perf/staticobjs/nlattr.o
CC /tmp/build/perf/staticobjs/btf.o
LD /tmp/build/perf/plugin_mac80211-in.o
CC /tmp/build/perf/staticobjs/libbpf_errno.o
CC /tmp/build/perf/staticobjs/str_error.o
LD /tmp/build/perf/plugin_sched_switch-in.o
LINK /tmp/build/perf/plugin_jbd2.so
CC /tmp/build/perf/staticobjs/netlink.o
CC /tmp/build/perf/staticobjs/bpf_prog_linfo.o
CC /tmp/build/perf/staticobjs/libbpf_probes.o
LD /tmp/build/perf/plugin_function-in.o
LD /tmp/build/perf/plugin_cfg80211-in.o
LD /tmp/build/perf/plugin_futex-in.o
LD /tmp/build/perf/plugin_xen-in.o
LINK /tmp/build/perf/plugin_hrtimer.so
LINK /tmp/build/perf/plugin_kmem.so
LD /tmp/build/perf/plugin_scsi-in.o
LINK /tmp/build/perf/plugin_kvm.so
LINK /tmp/build/perf/plugin_mac80211.so
LINK /tmp/build/perf/plugin_sched_switch.so
LD /tmp/build/perf/plugin_tlb-in.o
LINK /tmp/build/perf/plugin_futex.so
LINK /tmp/build/perf/plugin_function.so
CC /tmp/build/perf/staticobjs/xsk.o
CC /tmp/build/perf/staticobjs/hashmap.o
LINK /tmp/build/perf/plugin_xen.so
LINK /tmp/build/perf/plugin_scsi.so
CC /tmp/build/perf/staticobjs/btf_dump.o
LINK /tmp/build/perf/plugin_tlb.so
LINK /tmp/build/perf/plugin_cfg80211.so
CC /tmp/build/perf/staticobjs/ringbuf.o
CC /tmp/build/perf/staticobjs/strset.o
CC /tmp/build/perf/staticobjs/linker.o
GEN /tmp/build/perf/libtraceevent-dynamic-list
CC /tmp/build/perf/pmu-events/pmu-events.o
LD /tmp/build/perf/libsubcmd-in.o
AR /tmp/build/perf/libsubcmd.a
LD /tmp/build/perf/libperf-in.o
AR /tmp/build/perf/libperf.a
LD /tmp/build/perf/libtraceevent-in.o
LINK /tmp/build/perf/libtraceevent.a
GEN /tmp/build/perf/python/perf.so
CC /tmp/build/perf/builtin-bench.o
CC /tmp/build/perf/builtin-annotate.o
CC /tmp/build/perf/builtin-config.o
CC /tmp/build/perf/builtin-diff.o
CC /tmp/build/perf/builtin-evlist.o
CC /tmp/build/perf/builtin-ftrace.o
CC /tmp/build/perf/builtin-help.o
CC /tmp/build/perf/builtin-sched.o
CC /tmp/build/perf/builtin-buildid-list.o
CC /tmp/build/perf/builtin-buildid-cache.o
CC /tmp/build/perf/builtin-kallsyms.o
CC /tmp/build/perf/builtin-list.o
CC /tmp/build/perf/builtin-record.o
CC /tmp/build/perf/builtin-report.o
CC /tmp/build/perf/builtin-stat.o
CC /tmp/build/perf/builtin-timechart.o
CC /tmp/build/perf/builtin-top.o
CC /tmp/build/perf/builtin-script.o
CC /tmp/build/perf/builtin-kmem.o
In file included from builtin-top.c:25:
/git/perf-5.13.0/tools/perf/util/cgroup.h:44:35: error: unused parameter 'cgrp' [-Werror,-Wunused-parameter]
int read_cgroup_id(struct cgroup *cgrp)
^
/git/perf-5.13.0/tools/perf/util/cgroup.h:44:5: error: no previous prototype for function 'read_cgroup_id' [-Werror,-Wmissing-prototypes]
int read_cgroup_id(struct cgroup *cgrp)
^
In file included from builtin-stat.c:45:
/git/perf-5.13.0/tools/perf/util/cgroup.h:44:35: error: unused parameter 'cgrp' [-Werror,-Wunused-parameter]
int read_cgroup_id(struct cgroup *cgrp)
^
/git/perf-5.13.0/tools/perf/util/cgroup.h:44:5: error: no previous prototype for function 'read_cgroup_id' [-Werror,-Wmissing-prototypes]
int read_cgroup_id(struct cgroup *cgrp)
^
In file included from builtin-record.c:17:
/git/perf-5.13.0/tools/perf/util/cgroup.h:44:35: error: unused parameter 'cgrp' [-Werror,-Wunused-parameter]
int read_cgroup_id(struct cgroup *cgrp)
^
/git/perf-5.13.0/tools/perf/util/cgroup.h:44:5: error: no previous prototype for function 'read_cgroup_id' [-Werror,-Wmissing-prototypes]
int read_cgroup_id(struct cgroup *cgrp)
^
CC /tmp/build/perf/builtin-lock.o
CC /tmp/build/perf/builtin-kvm.o
CC /tmp/build/perf/builtin-mem.o
CC /tmp/build/perf/builtin-data.o
CC /tmp/build/perf/builtin-version.o
CC /tmp/build/perf/builtin-inject.o2 errors generated.
make[3]: *** [/git/perf-5.13.0/tools/build/Makefile.build:97: /tmp/build/perf/builtin-record.o] Error 1
make[3]: *** Waiting for unfinished jobs....
2 errors generated.
make[3]: *** [/git/perf-5.13.0/tools/build/Makefile.build:97: /tmp/build/perf/builtin-top.o] Error 1
2 errors generated.
make[3]: *** [/git/perf-5.13.0/tools/build/Makefile.build:97: /tmp/build/perf/builtin-stat.o] Error 1
LD /tmp/build/perf/pmu-events/pmu-events-in.o
In file included from /git/perf-5.13.0/tools/perf/util/evsel.c:30:
/git/perf-5.13.0/tools/perf/util/cgroup.h:44:5: error: no previous prototype for function 'read_cgroup_id' [-Werror,-Wmissing-prototypes]
Namhyung Kim
maintainerPATCH v4 2/4 perf tools: Add cgroup_is_v2() helper
In reply to Namhyung KimThe cgroup_is_v2() is to check if the given subsystem is mounted on cgroup v2 or not. It'll be used by BPF cgroup code later.
--- tools/perf/util/cgroup.c | 19 +++++++++++++++++++ tools/perf/util/cgroup.h | 2 ++ 2 files changed, 21 insertions(+)
tools/perf/util/cgroup.c +19 -0
@@ -9,6 +9,7 @@#include <linux/zalloc.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/statfs.h> #include <fcntl.h> #include <stdlib.h> #include <string.h>
@@ -70,6 +71,24 @@ int read_cgroup_id(struct cgroup *cgrp) }#endif /* HAVE_FILE_HANDLE */ +#ifndef CGROUP2_SUPER_MAGIC +#define CGROUP2_SUPER_MAGIC 0x63677270 +#endif + +int cgroup_is_v2 (const char *subsys) +{ +char mnt[PATH_MAX +1 ]; +struct statfs stbuf; + +if (cgroupfs_find_mountpoint (mnt,PATH_MAX +1 , subsys)) +return -1 ; + +if (statfs (mnt, &stbuf) <0 ) +return -1 ; + +return (stbuf.f_type ==CGROUP2_SUPER_MAGIC ); +} +static struct cgroup *evlist__find_cgroup (struct evlist *evlist,const char *str) {struct evsel *counter;
tools/perf/util/cgroup.h +2 -0
@@ -47,4 +47,6 @@ int read_cgroup_id(struct cgroup *cgrp) }#endif /* HAVE_FILE_HANDLE */ +int cgroup_is_v2 (const char *subsys); +#endif /* __CGROUP_H__ */
Signature · 1 line
2.32.0.93.g670b81a890-goog
Re: [PATCH 2/4] perf tools: Add cgroup_is_v2() helper
Quoted 52 lines · Namhyung Kim
The
cgroup_is_v2()is to check if the given subsystem is mounted on cgroup v2 or not. It'll be used by BPF cgroup code later.Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/cgroup.c | 19 +++++++++++++++++++ tools/perf/util/cgroup.h | 2 ++ 2 files changed, 21 insertions(+)tools/perf/util/cgroup.c +19
-0@@ -9,6 +9,7 @@#include <linux/zalloc.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/statfs.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> @@ -70,6 +71,24 @@ int read_cgroup_id(struct cgroup *cgrp) }#endif /* HAVE_FILE_HANDLE */ +#ifndef CGROUP2_SUPER_MAGIC +#define CGROUP2_SUPER_MAGIC 0x63677270 +#endif + +int cgroup_is_v2 (const char *subsys) +{ +char mnt[PATH_MAX +1 ]; +struct statfs stbuf; + +if (cgroupfs_find_mountpoint (mnt,PATH_MAX +1 , subsys)) +return -1 ; + +if (statfs (mnt, &stbuf) <0 ) +return -1 ; + +return (stbuf.f_type ==CGROUP2_SUPER_MAGIC ); +} +static struct cgroup *evlist__find_cgroup (struct evlist *evlist,const char *str) {struct evsel *counter;tools/perf/util/cgroup.h +0
-0
tools/perf/util/cgroup.h int read_cgroup_id(struct cgroup *cgrp) · line 51
Quoted 5 lines
tools/perf/util/cgroup.h +2
-0@@ -47,4 +47,6 @@ int read_cgroup_id(struct cgroup *cgrp) }#endif /* HAVE_FILE_HANDLE */ +int cgroup_is_v2 (const char *subsys); +
I think this is okay. It may make sense to have this in tools/lib/api/fs/fs.h, for example fs__valid_mount is already checking magic numbers. Perhaps we can avoid a statfs call, but it'd need some reorganization of the fs.h code.
Thanks,
Ian
Quoted 4 lines
tools/perf/util/cgroup.h +0
-1#endif /* __CGROUP_H__ */ --2.32.0.93.g670b81a890-goog
Namhyung Kim
maintainerRe: [PATCH 2/4] perf tools: Add cgroup_is_v2() helper
Hi Ian,
Quoted 65 lines · Ian Rogers
Quoted 52 lines · Namhyung Kim
The
cgroup_is_v2()is to check if the given subsystem is mounted on cgroup v2 or not. It'll be used by BPF cgroup code later.Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/cgroup.c | 19 +++++++++++++++++++ tools/perf/util/cgroup.h | 2 ++ 2 files changed, 21 insertions(+)tools/perf/util/cgroup.c +19
-0@@ -9,6 +9,7 @@#include <linux/zalloc.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/statfs.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> @@ -70,6 +71,24 @@ int read_cgroup_id(struct cgroup *cgrp) }#endif /* HAVE_FILE_HANDLE */ +#ifndef CGROUP2_SUPER_MAGIC +#define CGROUP2_SUPER_MAGIC 0x63677270 +#endif + +int cgroup_is_v2 (const char *subsys) +{ +char mnt[PATH_MAX +1 ]; +struct statfs stbuf; + +if (cgroupfs_find_mountpoint (mnt,PATH_MAX +1 , subsys)) +return -1 ; + +if (statfs (mnt, &stbuf) <0 ) +return -1 ; + +return (stbuf.f_type ==CGROUP2_SUPER_MAGIC ); +} +static struct cgroup *evlist__find_cgroup (struct evlist *evlist,const char *str) {struct evsel *counter;tools/perf/util/cgroup.h +0
-0tools/perf/util/cgroup.h int read_cgroup_id(struct cgroup *cgrp) · line 51
Quoted 5 lines
tools/perf/util/cgroup.h +2
-0@@ -47,4 +47,6 @@ int read_cgroup_id(struct cgroup *cgrp) }#endif /* HAVE_FILE_HANDLE */ +int cgroup_is_v2 (const char *subsys); +I think this is okay. It may make sense to have this in
tools/lib/api/fs/fs.h, for examplefs__valid_mountis already checking magic numbers. Perhaps we can avoid a statfs call, but it'd need some reorganization of thefs.hcode.
Quoted 1 line
Acked-by Ian Rogers <[email protected]>
Thanks for your review!
Actually I'm ok with moving it to tools/lib. Will do it in the next spin, if it needs one. :)
Thanks,
Namhyung
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 2/4] perf tools: Add cgroup_is_v2() helper
Em Tue, Jun 29, 2021 at 11:35:17PM -0700, Namhyung Kim escreveu:
Quoted 68 lines
Hi Ian,
Quoted 65 lines · Ian Rogers
Quoted 52 lines · Namhyung Kim
The
cgroup_is_v2()is to check if the given subsystem is mounted on cgroup v2 or not. It'll be used by BPF cgroup code later.Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/cgroup.c | 19 +++++++++++++++++++ tools/perf/util/cgroup.h | 2 ++ 2 files changed, 21 insertions(+)tools/perf/util/cgroup.c +19
-0@@ -9,6 +9,7 @@#include <linux/zalloc.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/statfs.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> @@ -70,6 +71,24 @@ int read_cgroup_id(struct cgroup *cgrp) }#endif /* HAVE_FILE_HANDLE */ +#ifndef CGROUP2_SUPER_MAGIC +#define CGROUP2_SUPER_MAGIC 0x63677270 +#endif + +int cgroup_is_v2 (const char *subsys) +{ +char mnt[PATH_MAX +1 ]; +struct statfs stbuf; + +if (cgroupfs_find_mountpoint (mnt,PATH_MAX +1 , subsys)) +return -1 ; + +if (statfs (mnt, &stbuf) <0 ) +return -1 ; + +return (stbuf.f_type ==CGROUP2_SUPER_MAGIC ); +} +static struct cgroup *evlist__find_cgroup (struct evlist *evlist,const char *str) {struct evsel *counter;tools/perf/util/cgroup.h +0
-0tools/perf/util/cgroup.h int read_cgroup_id(struct cgroup *cgrp) · line 51
Quoted 5 lines
tools/perf/util/cgroup.h +2
-0@@ -47,4 +47,6 @@ int read_cgroup_id(struct cgroup *cgrp) }#endif /* HAVE_FILE_HANDLE */ +int cgroup_is_v2 (const char *subsys); +I think this is okay. It may make sense to have this in
tools/lib/api/fs/fs.h, for examplefs__valid_mountis already checking magic numbers. Perhaps we can avoid a statfs call, but it'd need some reorganization of thefs.hcode.
Quoted 6 lines
Quoted 1 line
Acked-by Ian Rogers <[email protected]>Thanks for your review!
Actually I'm ok with moving it to
tools/lib. Will do it in the next spin, if it needs one. :)
O think I'll take v4, we can improve this in followup work.
- Arnaldo
Namhyung Kim
maintainerPATCH v4 3/4 perf tools: Move common bpf functions to bpf_counter.h
In reply to Namhyung KimSome helper functions will be used for cgroup counting too. Move them to a header file for sharing.
--- tools/perf/util/bpf_counter.c | 52 ----------------------------------- tools/perf/util/bpf_counter.h | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 52 deletions(-)
tools/perf/util/bpf_counter.c +0 -52
@@ -7,12 +7,8 @@#include <unistd.h> #include <sys/file.h> #include <sys/time.h> -#include <sys/resource.h> #include <linux/err.h> #include <linux/zalloc.h> -#include <bpf/bpf.h> -#include <bpf/btf.h> -#include <bpf/libbpf.h> #include <api/fs/fs.h> #include <perf/bpf_perf.h>
@@ -37,13 +33,6 @@ static inline void *u64_to_ptr(__u64 ptr)return (void *)(unsigned long )ptr; } -static void set_max_rlimit (void ) -{ -struct rlimit rinf = {RLIM_INFINITY ,RLIM_INFINITY }; - -setrlimit (RLIMIT_MEMLOCK , &rinf); -} -static struct bpf_counter *bpf_counter_alloc (void ) {struct bpf_counter *counter;
@@ -297,33 +286,6 @@ struct bpf_counter_ops bpf_program_profiler_ops = { .install_pe = bpf_program_profiler__install_pe, }; -static __u32 bpf_link_get_id (int fd) -{ -struct bpf_link_info link_info = {0 }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.id; -} - -static __u32 bpf_link_get_prog_id (int fd) -{ -struct bpf_link_info link_info = {0 }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.prog_id; -} - -static __u32 bpf_map_get_id (int fd) -{ -struct bpf_map_info map_info = {0 }; -__u32 map_info_len =sizeof (map_info); - -bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); -return map_info.id; -} -static bool bperf_attr_map_compatible (int attr_map_fd) {struct bpf_map_info map_info = {0 };
@@ -385,20 +347,6 @@ static int bperf_lock_attr_map(struct target *target)return map_fd; } -/* trigger the leader program on a cpu */ -static int bperf_trigger_reading (int prog_fd,int cpu) -{ -DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, - .ctx_in = NULL, - .ctx_size_in = 0, - .flags = BPF_F_TEST_RUN_ON_CPU, - .cpu = cpu, - .retval = 0, - ); - -return bpf_prog_test_run_opts (prog_fd, &opts); -} - static int bperf_check_target(struct evsel *evsel, struct target *target, enum bperf_filter_type *filter_type,
tools/perf/util/bpf_counter.h +52 -0
62 lines not on this page · raw message
Signature · 1 line
2.32.0.93.g670b81a890-goog
Re: [PATCH 3/4] perf tools: Move common bpf functions to bpf_counter.h
Quoted 8 lines · Namhyung Kim
Some helper functions will be used for cgroup counting too. Move them to a header file for sharing.
Signed-off-by Namhyung Kim <[email protected]>
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 3/4] perf tools: Move common bpf functions to bpf_counter.h
In reply to Namhyung KimEm Fri, Jun 25, 2021 at 12:18:25AM -0700, Namhyung Kim escreveu:
Quoted 41 lines
Some helper functions will be used for cgroup counting too. Move them to a header file for sharing.
Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/bpf_counter.c | 52 ----------------------------------- tools/perf/util/bpf_counter.h | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 52 deletions(-)tools/perf/util/bpf_counter.c +0
-11@@ -7,12 +7,8 @@#include <unistd.h> #include <sys/file.h> #include <sys/time.h> -#include <sys/resource.h> #include <linux/err.h> #include <linux/zalloc.h> -#include <bpf/bpf.h> -#include <bpf/btf.h> -#include <bpf/libbpf.h> #include <api/fs/fs.h> #include <perf/bpf_perf.h> @@ -37,13 +33,6 @@ static inline void *u64_to_ptr(__u64 ptr)return (void *)(unsigned long )ptr; } -static void set_max_rlimit (void ) -{ -struct rlimit rinf = {RLIM_INFINITY ,RLIM_INFINITY }; - -setrlimit (RLIMIT_MEMLOCK , &rinf); -} -static struct bpf_counter *bpf_counter_alloc (void ) {struct bpf_counter *counter;
tools/perf/util/bpf_counter.c struct bpf_counter_ops bpf_program_profiler_ops = { · line 302
Quoted 6 lines
tools/perf/util/bpf_counter.c +0
-3@@ -297,33 +286,6 @@ struct bpf_counter_ops bpf_program_profiler_ops = { .install_pe = bpf_program_profiler__install_pe, }; -static __u32 bpf_link_get_id (int fd) -{ -struct bpf_link_info link_info = {0 };
Moving this from bpf_counter.c to the header made this code be compiled in places where it wasn't before, as bpf_counter.c is built only when:
perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
For instance, this got broken:
23 33.62 debian:9 : FAIL clang version 3.8.1-24 (tags/RELEASE_381/final)
In file included from builtin-stat.c:71:
/git/perf-5.13.0/tools/perf/util/bpf_counter.h:92:37: error: missing field 'id' initializer [-Werror,-Wmissing-field-initializers]
struct bpf_link_info link_info = {0};
^
/git/perf-5.13.0/tools/perf/util/bpf_counter.h:101:37: error: missing field 'id' initializer [-Werror,-Wmissing-field-initializers]
struct bpf_link_info link_info = {0};
^
/git/perf-5.13.0/tools/perf/util/bpf_counter.h:110:35: error: missing field 'id' initializer [-Werror,-Wmissing-field-initializers]
struct bpf_map_info map_info = {0};ITs mostly older systems, but I'll fix it anyway.
Quoted 119 lines
tools/perf/util/bpf_counter.c +0
-38-__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.id; -} - -static __u32 bpf_link_get_prog_id (int fd) -{ -struct bpf_link_info link_info = {0 }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.prog_id; -} - -static __u32 bpf_map_get_id (int fd) -{ -struct bpf_map_info map_info = {0 }; -__u32 map_info_len =sizeof (map_info); - -bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); -return map_info.id; -} -static bool bperf_attr_map_compatible (int attr_map_fd) {struct bpf_map_info map_info = {0 };@@ -385,20 +347,6 @@ static int bperf_lock_attr_map(struct target *target)return map_fd; } -/* trigger the leader program on a cpu */ -static int bperf_trigger_reading (int prog_fd,int cpu) -{ -DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, - .ctx_in = NULL, - .ctx_size_in = 0, - .flags = BPF_F_TEST_RUN_ON_CPU, - .cpu = cpu, - .retval = 0, - ); - -return bpf_prog_test_run_opts (prog_fd, &opts); -} - static int bperf_check_target(struct evsel *evsel, struct target *target, enum bperf_filter_type *filter_type,tools/perf/util/bpf_counter.h +52
-0@@ -3,6 +3,10 @@#define __PERF_BPF_COUNTER_H 1#include <linux/list.h> +#include <sys/resource.h> +#include <bpf/bpf.h> +#include <bpf/btf.h> +#include <bpf/libbpf.h> struct evsel ;struct target ;@@ -76,4 +80,52 @@ static inline int bpf_counter__install_pe(struct evsel *evsel __maybe_unused,#endif /* HAVE_BPF_SKEL */ +static inline void set_max_rlimit (void ) +{ +struct rlimit rinf = {RLIM_INFINITY ,RLIM_INFINITY }; + +setrlimit (RLIMIT_MEMLOCK , &rinf); +} + +static inline __u32 bpf_link_get_id (int fd) +{ +struct bpf_link_info link_info = {0 }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.id; +} + +static inline __u32 bpf_link_get_prog_id (int fd) +{ +struct bpf_link_info link_info = {0 }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.prog_id; +} + +static inline __u32 bpf_map_get_id (int fd) +{ +struct bpf_map_info map_info = {0 }; +__u32 map_info_len =sizeof (map_info); + +bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); +return map_info.id; +} + +/* trigger the leader program on a cpu */ +static inline int bperf_trigger_reading (int prog_fd,int cpu) +{ +DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, + .ctx_in = NULL, + .ctx_size_in = 0, + .flags = BPF_F_TEST_RUN_ON_CPU, + .cpu = cpu, + .retval = 0, + ); + +return bpf_prog_test_run_opts (prog_fd, &opts); +} +#endif /* __PERF_BPF_COUNTER_H */ Signature · 1 line
2.32.0.93.g670b81a890-goog
Signature · 1 line
- Arnaldo
Namhyung Kim
maintainerRe: [PATCH 3/4] perf tools: Move common bpf functions to bpf_counter.h
Quoted 68 lines · Arnaldo Carvalho de Melo
Em Fri, Jun 25, 2021 at 12:18:25AM -0700, Namhyung Kim escreveu:
Quoted 41 lines
Some helper functions will be used for cgroup counting too. Move them to a header file for sharing.
Signed-off-by Namhyung Kim <[email protected]>--- tools/perf/util/bpf_counter.c | 52 ----------------------------------- tools/perf/util/bpf_counter.h | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 52 deletions(-)tools/perf/util/bpf_counter.c +0
-11@@ -7,12 +7,8 @@#include <unistd.h> #include <sys/file.h> #include <sys/time.h> -#include <sys/resource.h> #include <linux/err.h> #include <linux/zalloc.h> -#include <bpf/bpf.h> -#include <bpf/btf.h> -#include <bpf/libbpf.h> #include <api/fs/fs.h> #include <perf/bpf_perf.h> @@ -37,13 +33,6 @@ static inline void *u64_to_ptr(__u64 ptr)return (void *)(unsigned long )ptr; } -static void set_max_rlimit (void ) -{ -struct rlimit rinf = {RLIM_INFINITY ,RLIM_INFINITY }; - -setrlimit (RLIMIT_MEMLOCK , &rinf); -} -static struct bpf_counter *bpf_counter_alloc (void ) {struct bpf_counter *counter;tools/perf/util/bpf_counter.c struct bpf_counter_ops bpf_program_profiler_ops = { · line 302
Quoted 6 lines
tools/perf/util/bpf_counter.c +0
-3@@ -297,33 +286,6 @@ struct bpf_counter_ops bpf_program_profiler_ops = { .install_pe = bpf_program_profiler__install_pe, }; -static __u32 bpf_link_get_id (int fd) -{ -struct bpf_link_info link_info = {0 };Moving this from
bpf_counter.cto the header made this code be compiled in places where it wasn't before, asbpf_counter.cis built only when:perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
For instance, this got broken:
23 33.62 debian:9 : FAIL clang version 3.8.1-24 (tags/RELEASE_381/final)
In file included from builtin-stat.c:71: /git/perf-5.13.0/tools/perf/util/bpf_counter.h:92:37: error: missing field 'id' initializer [-Werror,-Wmissing-field-initializers] struct bpf_link_info link_info = {0}; ^ /git/perf-5.13.0/tools/perf/util/bpf_counter.h:101:37: error: missing field 'id' initializer [-Werror,-Wmissing-field-initializers] struct bpf_link_info link_info = {0}; ^ /git/perf-5.13.0/tools/perf/util/bpf_counter.h:110:35: error: missing field 'id' initializer [-Werror,-Wmissing-field-initializers] struct bpf_map_info map_info = {0};
Quoted 1 line
ITs mostly older systems, but I'll fix it anyway.
Thanks a lot for fixing all the messes.
Assuming you're fixed the preparation patches.
I'll just update the last one next time.
Thanks,
Namhyung
Quoted 124 lines
Quoted 118 lines
tools/perf/util/bpf_counter.c +0
-38-__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.id; -} - -static __u32 bpf_link_get_prog_id (int fd) -{ -struct bpf_link_info link_info = {0 }; -__u32 link_info_len =sizeof (link_info); - -bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); -return link_info.prog_id; -} - -static __u32 bpf_map_get_id (int fd) -{ -struct bpf_map_info map_info = {0 }; -__u32 map_info_len =sizeof (map_info); - -bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); -return map_info.id; -} -static bool bperf_attr_map_compatible (int attr_map_fd) {struct bpf_map_info map_info = {0 };@@ -385,20 +347,6 @@ static int bperf_lock_attr_map(struct target *target)return map_fd; } -/* trigger the leader program on a cpu */ -static int bperf_trigger_reading (int prog_fd,int cpu) -{ -DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, - .ctx_in = NULL, - .ctx_size_in = 0, - .flags = BPF_F_TEST_RUN_ON_CPU, - .cpu = cpu, - .retval = 0, - ); - -return bpf_prog_test_run_opts (prog_fd, &opts); -} - static int bperf_check_target(struct evsel *evsel, struct target *target, enum bperf_filter_type *filter_type,tools/perf/util/bpf_counter.h +52
-1@@ -3,6 +3,10 @@#define __PERF_BPF_COUNTER_H 1#include <linux/list.h> +#include <sys/resource.h> +#include <bpf/bpf.h> +#include <bpf/btf.h> +#include <bpf/libbpf.h> struct evsel ;struct target ;@@ -76,4 +80,52 @@ static inline int bpf_counter__install_pe(struct evsel *evsel __maybe_unused,#endif /* HAVE_BPF_SKEL */ +static inline void set_max_rlimit (void ) +{ +struct rlimit rinf = {RLIM_INFINITY ,RLIM_INFINITY }; + +setrlimit (RLIMIT_MEMLOCK , &rinf); +} + +static inline __u32 bpf_link_get_id (int fd) +{ +struct bpf_link_info link_info = {0 }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.id; +} + +static inline __u32 bpf_link_get_prog_id (int fd) +{ +struct bpf_link_info link_info = {0 }; +__u32 link_info_len =sizeof (link_info); + +bpf_obj_get_info_by_fd (fd, &link_info, &link_info_len); +return link_info.prog_id; +} + +static inline __u32 bpf_map_get_id (int fd) +{ +struct bpf_map_info map_info = {0 }; +__u32 map_info_len =sizeof (map_info); + +bpf_obj_get_info_by_fd (fd, &map_info, &map_info_len); +return map_info.id; +} + +/* trigger the leader program on a cpu */ +static inline int bperf_trigger_reading (int prog_fd,int cpu) +{ +DECLARE_LIBBPF_OPTS (bpf_test_run_opts, opts, + .ctx_in = NULL, + .ctx_size_in = 0, + .flags = BPF_F_TEST_RUN_ON_CPU, + .cpu = cpu, + .retval = 0, + ); + +return bpf_prog_test_run_opts (prog_fd, &opts); +} +#endif /* __PERF_BPF_COUNTER_H */ --Quoted 2 lines
2.32.0.93.g670b81a890-goog
--
- Arnaldo
Namhyung Kim
maintainerPATCH v4 4/4 perf stat: Enable BPF counter with --for-each-cgroup
In reply to Namhyung KimRecently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given perf_events and to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.
--- tools/perf/Makefile.perf | 17 +- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 5 + tools/perf/util/bpf_counter_cgroup.c | 299 ++++++++++++++++++++ tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 191 +++++++++++++ tools/perf/util/cgroup.c | 2 + tools/perf/util/cgroup.h | 1 + 7 files changed, 515 insertions(+), 1 deletion(-) create mode 100644 tools/perf/util/bpf_counter_cgroup.c create mode 100644 tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
tools/perf/Makefile.perf +16 -1
@@ -1015,6 +1015,7 @@ SKEL_OUT := $(abspath $(OUTPUT)util/bpf_skel) SKEL_TMP_OUT := $(abspath $(SKEL_OUT)/.tmp) SKELETONS := $(SKEL_OUT)/bpf_prog_profiler.skel.h SKELETONS += $(SKEL_OUT)/bperf_leader.skel.h $(SKEL_OUT)/bperf_follower.skel.h +SKELETONS += $(SKEL_OUT)/bperf_cgroup.skel.h ifdef BUILD_BPF_SKEL BPFTOOL := $(SKEL_TMP_OUT)/bootstrap/bpftool
@@ -1032,7 +1033,21 @@ $(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(LIBBPF) | $(SKEL_TMP_OUT) $(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -Wall -Werror $(BPF_INCLUDE) \ -c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && $(LLVM_STRIP) -g $@ -$(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL) +VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ + $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ + ../../vmlinux \ + /sys/kernel/btf/vmlinux \ + /boot/vmlinux-$(shell uname -r) +VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) + +$(SKEL_OUT)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) +ifeq ($(VMLINUX_H),) + $(QUIET_GEN)$(BPFTOOL) btf dump file $< format c > $@ +else + $(Q)cp "$(VMLINUX_H)" $@ +endif + +$(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o $(SKEL_OUT)/vmlinux.h | $(BPFTOOL) $(QUIET_GENSKEL)$(BPFTOOL) gen skeleton $< > $@ bpf-skel: $(SKELETONS)
tools/perf/util/Build +1 -0
@@ -140,6 +140,7 @@ perf-y += clockid.o perf-$(CONFIG_LIBBPF) += bpf-loader.o perf-$(CONFIG_LIBBPF) += bpf_map.o perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o +perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter_cgroup.o perf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o perf-$(CONFIG_LIBELF) += symbol-elf.o perf-$(CONFIG_LIBELF) += probe-file.o
tools/perf/util/bpf_counter.c +5 -0
@@ -18,6 +18,7 @@#include "evsel.h" #include "evlist.h" #include "target.h" +#include "cgroup.h" #include "cpumap.h" #include "thread_map.h"
@@ -740,6 +741,8 @@ struct bpf_counter_ops bperf_ops = { .destroy = bperf__destroy, }; +extern struct bpf_counter_ops bperf_cgrp_ops; +static inline bool bpf_counter_skip (struct evsel *evsel) { return list_empty(&evsel->bpf_counter_list) &&
@@ -757,6 +760,8 @@ int bpf_counter__load(struct evsel *evsel, struct target *target) {if (target->bpf_str) evsel->bpf_counter_ops = &bpf_program_profiler_ops; +else if (cgrp_event_expanded && target->use_bpf) + evsel->bpf_counter_ops = &bperf_cgrp_ops;else if (target->use_bpf || evsel->bpf_counter ||evsel__match_bpf_counter_events (evsel->name)) evsel->bpf_counter_ops = &bperf_ops;
tools/perf/util/bpf_counter_cgroup.c new file +299 -0
299 lines not on this page · raw message
tools/perf/util/bpf_skel/bperf_cgroup.bpf.c new file +191 -0
191 lines not on this page · raw message
tools/perf/util/cgroup.c +2 -0
@@ -18,6 +18,7 @@#include <regex.h> int nr_cgroups; +bool cgrp_event_expanded;/* used to match cgroup name with patterns */ struct cgroup_name {
@@ -484,6 +485,7 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, } ret =0 ; + cgrp_event_expanded = true; out_err:evlist__delete (orig_list);
tools/perf/util/cgroup.h +1 -0
@@ -17,6 +17,7 @@ struct cgroup { };extern int nr_cgroups;/* number of explicit cgroups defined */ +extern bool cgrp_event_expanded;struct cgroup *cgroup__get (struct cgroup *cgroup);void cgroup__put (struct cgroup *cgroup);
Signature · 1 line
2.32.0.93.g670b81a890-goog
Re: [PATCH 4/4] perf stat: Enable BPF counter with --for-each-cgroup
Quoted 31 lines · Namhyung Kim
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given
perf_eventsand to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.Cc Song Liu <[email protected]>Signed-off-by Namhyung Kim <[email protected]>---
tools/perf/Makefile.perf | 17 +- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 5 + tools/perf/util/bpf_counter_cgroup.c | 299 ++++++++++++++++++++ tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 191 +++++++++++++ tools/perf/util/cgroup.c | 2 + tools/perf/util/cgroup.h | 1 + 7 files changed, 515 insertions(+), 1 deletion(-)create mode 100644
tools/perf/util/bpf_counter_cgroup.ccreate mode 100644tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
[...]
tools/perf/util/bpf_counter_cgroup.c line 3
Quoted 9 lines
tools/perf/util/bpf_counter_cgroup.c new file +3
-0@@ -0,0 +1,299 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* Copyright (c) 2019 Facebook */
I am not sure whether this ^^^ is accurate.
Quoted 158 lines
tools/perf/util/bpf_counter_cgroup.c new file +158
-0+/* Copyright (c) 2021 Google */ + +#include <assert.h> +#include <limits.h> +#include <unistd.h> +#include <sys/file.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <linux/err.h> +#include <linux/zalloc.h> +#include <linux/perf_event.h> +#include <api/fs/fs.h> +#include <perf/bpf_perf.h> + +#include "affinity.h" +#include "bpf_counter.h" +#include "cgroup.h" +#include "counts.h" +#include "debug.h" +#include "evsel.h" +#include "evlist.h" +#include "target.h" +#include "cpumap.h" +#include "thread_map.h" + +#include "bpf_skel/bperf_cgroup.skel.h" + +static struct perf_event_attr cgrp_switch_attr = { + .type =PERF_TYPE_SOFTWARE , + .config =PERF_COUNT_SW_CGROUP_SWITCHES , + .size =sizeof (cgrp_switch_attr), + .sample_period =1 , + .disabled =1 , +}; + +static struct evsel *cgrp_switch; +static struct bperf_cgroup_bpf *skel; + +#define FD (evt, cpu) (*(int *)xyarray__entry(evt->core.fd, cpu, 0)) + +static int bperf_load_program (struct evlist *evlist) +{ +struct bpf_link *link; +struct evsel *evsel; +struct cgroup *cgrp, *leader_cgrp; +__u32 i, cpu; +int nr_cpus = evlist->core.all_cpus->nr; +int total_cpus =cpu__max_cpu (); +int map_size, map_fd; +int prog_fd, err; + + skel =bperf_cgroup_bpf__open (); +if (!skel) { +pr_err ("Failed to open cgroup skeleton\n" ); +return -1 ; + } + + skel->rodata->num_cpus = total_cpus; + skel->rodata->num_events = evlist->core.nr_entries / nr_cgroups; + +BUG_ON (evlist->core.nr_entries % nr_cgroups !=0 ); + +/* we need one copy of events per cpu for reading */ + map_size = total_cpus * evlist->core.nr_entries / nr_cgroups; +bpf_map__resize (skel->maps.events, map_size); +bpf_map__resize (skel->maps.cgrp_idx, nr_cgroups); +/* previous result is saved in a per-cpu array */ + map_size = evlist->core.nr_entries / nr_cgroups; +bpf_map__resize (skel->maps.prev_readings, map_size); +/* cgroup result needs all events (per-cpu) */ + map_size = evlist->core.nr_entries; +bpf_map__resize (skel->maps.cgrp_readings, map_size); + +set_max_rlimit (); + + err =bperf_cgroup_bpf__load (skel); +if (err) { +pr_err ("Failed to load cgroup skeleton\n" ); + goto out; + } + +if (cgroup_is_v2 ("perf_event" ) >0 ) + skel->bss->use_cgroup_v2 =1 ; + + err =-1 ; + + cgrp_switch =evsel__new (&cgrp_switch_attr); +if (evsel__open_per_cpu (cgrp_switch, evlist->core.all_cpus,-1 ) <0 ) { +pr_err ("Failed to open cgroup switches event\n" ); + goto out; + } + +for (i =0 ; i < nr_cpus; i++) { + link =bpf_program__attach_perf_event (skel->progs.on_cgrp_switch, +FD (cgrp_switch, i)); +if (IS_ERR (link)) { +pr_err ("Failed to attach cgroup program\n" ); + err =PTR_ERR (link); + goto out; + } + } + +/* +* Update cgrp_idx map from cgroup-id to event index. +*/ + cgrp =NULL ; + i =0 ; + +evlist__for_each_entry (evlist, evsel) { +if (cgrp ==NULL || evsel->cgrp == leader_cgrp) { + leader_cgrp = evsel->cgrp; + evsel->cgrp =NULL ; + +/* open single copy of the events w/o cgroup */ + err =evsel__open_per_cpu (evsel, evlist->core.all_cpus,-1 ); +if (err) { +pr_err ("Failed to open first cgroup events\n" ); + goto out; + } + + map_fd =bpf_map__fd (skel->maps.events); +for (cpu =0 ; cpu < nr_cpus; cpu++) { +int fd =FD (evsel, cpu); +__u32 idx = evsel->idx * total_cpus + + evlist->core.all_cpus->map[cpu]; + + err =bpf_map_update_elem (map_fd, &idx, &fd, +BPF_ANY ); +if (err <0 ) { +pr_err ("Failed to update perf_event fd\n" ); + goto out; + } + } + + evsel->cgrp = leader_cgrp; + } + evsel->supported = true; + +if (evsel->cgrp == cgrp) +continue ; + + cgrp = evsel->cgrp; + +if (read_cgroup_id (cgrp) <0 ) { +pr_err ("Failed to get cgroup id\n" ); + err =-1 ; + goto out; + } + + map_fd =bpf_map__fd (skel->maps.cgrp_idx); + err =bpf_map_update_elem (map_fd, &cgrp->id, &i,BPF_ANY ); +if (err <0 ) { +pr_err ("Failed to update cgroup index map\n" ); + goto out; + } + + i++; + }
tools/perf/util/bpf_counter_cgroup.c line 171
Quoted 10 lines
tools/perf/util/bpf_counter_cgroup.c new file +10
-0+ +/* +* bperf uses BPF_PROG_TEST_RUN to get accurate reading. Check +* whether the kernel support it +*/ + prog_fd =bpf_program__fd (skel->progs.trigger_read); + err =bperf_trigger_reading (prog_fd,0 ); +if (err) { +pr_debug ("The kernel does not support test_run for raw_tp BPF programs.\n" +"Therefore, --for-each-cgroup might show inaccurate readings\n" );
I think this should be a warning, and we should set err = 0 to continue?
Quoted 6 lines
tools/perf/util/bpf_counter_cgroup.c new file +6
-0+ } + +out: +return err; +} +
[...]
Quoted 11 lines
tools/perf/util/bpf_counter_cgroup.c new file +11
-0+ +/* +* trigger the leader prog on each cpu, so the cgrp_reading map could get +* the latest results. +*/ +static int bperf_cgrp__sync_counters (struct evlist *evlist) +{ +int i, cpu; +int nr_cpus = evlist->core.all_cpus->nr; +int prog_fd =bpf_program__fd (skel->progs.trigger_read); +
tools/perf/util/bpf_counter_cgroup.c line 220
Quoted 10 lines
tools/perf/util/bpf_counter_cgroup.c new file +10
-0+for (i =0 ; i < nr_cpus; i++) { + cpu = evlist->core.all_cpus->map[i]; +bperf_trigger_reading (prog_fd, cpu); + } + +return 0 ; +} + +static int bperf_cgrp__enable (struct evsel *evsel) +{
Do we need to call bperf_cgrp__sync_counters() before setting enabled to 1? If we don't, we may count some numbers before setting enabled to 1, no?
Quoted 3 lines
tools/perf/util/bpf_counter_cgroup.c new file +3
-0+ skel->bss->enabled =1 ; +return 0 ; +}
[...]
Namhyung Kim
maintainerRe: [PATCH 4/4] perf stat: Enable BPF counter with --for-each-cgroup
Hi Song,
Quoted 35 lines · Song Liu
Quoted 32 lines · Namhyung Kim
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given
perf_eventsand to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.Cc Song Liu <[email protected]>Signed-off-by Namhyung Kim <[email protected]>---
tools/perf/Makefile.perf | 17 +- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 5 + tools/perf/util/bpf_counter_cgroup.c | 299 ++++++++++++++++++++ tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 191 +++++++++++++ tools/perf/util/cgroup.c | 2 + tools/perf/util/cgroup.h | 1 + 7 files changed, 515 insertions(+), 1 deletion(-)create mode 100644
tools/perf/util/bpf_counter_cgroup.ccreate mode 100644tools/perf/util/bpf_skel/bperf_cgroup.bpf.c[...]
Quoted 11 lines
tools/perf/util/bpf_counter_cgroup.c line 3
Quoted 9 lines
tools/perf/util/bpf_counter_cgroup.c new file +3
-0@@ -0,0 +1,299 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* Copyright (c) 2019 Facebook */ I am not sure whether this ^^^ is accurate.
Well, I just copied it from the bpf_counter.c file which was the base of this patch. Now I don't think I have many lines of code directly came from the origin.
So I'm not sure what I can do. Do you want to update the copyright year to 2021? Or are you ok with removing the line at all?
Quoted 163 lines
Quoted 163 lines
tools/perf/util/bpf_counter_cgroup.c new file +162
-0+/* Copyright (c) 2021 Google */ + +#include <assert.h> +#include <limits.h> +#include <unistd.h> +#include <sys/file.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <linux/err.h> +#include <linux/zalloc.h> +#include <linux/perf_event.h> +#include <api/fs/fs.h> +#include <perf/bpf_perf.h> + +#include "affinity.h" +#include "bpf_counter.h" +#include "cgroup.h" +#include "counts.h" +#include "debug.h" +#include "evsel.h" +#include "evlist.h" +#include "target.h" +#include "cpumap.h" +#include "thread_map.h" + +#include "bpf_skel/bperf_cgroup.skel.h" + +static struct perf_event_attr cgrp_switch_attr = { + .type =PERF_TYPE_SOFTWARE , + .config =PERF_COUNT_SW_CGROUP_SWITCHES , + .size =sizeof (cgrp_switch_attr), + .sample_period =1 , + .disabled =1 , +}; + +static struct evsel *cgrp_switch; +static struct bperf_cgroup_bpf *skel; + +#define FD (evt, cpu) (*(int *)xyarray__entry(evt->core.fd, cpu, 0)) + +static int bperf_load_program (struct evlist *evlist) +{ +struct bpf_link *link; +struct evsel *evsel; +struct cgroup *cgrp, *leader_cgrp; +__u32 i, cpu; +int nr_cpus = evlist->core.all_cpus->nr; +int total_cpus =cpu__max_cpu (); +int map_size, map_fd; +int prog_fd, err; + + skel =bperf_cgroup_bpf__open (); +if (!skel) { +pr_err ("Failed to open cgroup skeleton\n" ); +return -1 ; + } + + skel->rodata->num_cpus = total_cpus; + skel->rodata->num_events = evlist->core.nr_entries / nr_cgroups; + +BUG_ON (evlist->core.nr_entries % nr_cgroups !=0 ); + +/* we need one copy of events per cpu for reading */ + map_size = total_cpus * evlist->core.nr_entries / nr_cgroups; +bpf_map__resize (skel->maps.events, map_size); +bpf_map__resize (skel->maps.cgrp_idx, nr_cgroups); +/* previous result is saved in a per-cpu array */ + map_size = evlist->core.nr_entries / nr_cgroups; +bpf_map__resize (skel->maps.prev_readings, map_size); +/* cgroup result needs all events (per-cpu) */ + map_size = evlist->core.nr_entries; +bpf_map__resize (skel->maps.cgrp_readings, map_size); + +set_max_rlimit (); + + err =bperf_cgroup_bpf__load (skel); +if (err) { +pr_err ("Failed to load cgroup skeleton\n" ); + goto out; + } + +if (cgroup_is_v2 ("perf_event" ) >0 ) + skel->bss->use_cgroup_v2 =1 ; + + err =-1 ; + + cgrp_switch =evsel__new (&cgrp_switch_attr); +if (evsel__open_per_cpu (cgrp_switch, evlist->core.all_cpus,-1 ) <0 ) { +pr_err ("Failed to open cgroup switches event\n" ); + goto out; + } + +for (i =0 ; i < nr_cpus; i++) { + link =bpf_program__attach_perf_event (skel->progs.on_cgrp_switch, +FD (cgrp_switch, i)); +if (IS_ERR (link)) { +pr_err ("Failed to attach cgroup program\n" ); + err =PTR_ERR (link); + goto out; + } + } + +/* +* Update cgrp_idx map from cgroup-id to event index. +*/ + cgrp =NULL ; + i =0 ; + +evlist__for_each_entry (evlist, evsel) { +if (cgrp ==NULL || evsel->cgrp == leader_cgrp) { + leader_cgrp = evsel->cgrp; + evsel->cgrp =NULL ; + +/* open single copy of the events w/o cgroup */ + err =evsel__open_per_cpu (evsel, evlist->core.all_cpus,-1 ); +if (err) { +pr_err ("Failed to open first cgroup events\n" ); + goto out; + } + + map_fd =bpf_map__fd (skel->maps.events); +for (cpu =0 ; cpu < nr_cpus; cpu++) { +int fd =FD (evsel, cpu); +__u32 idx = evsel->idx * total_cpus + + evlist->core.all_cpus->map[cpu]; + + err =bpf_map_update_elem (map_fd, &idx, &fd, +BPF_ANY ); +if (err <0 ) { +pr_err ("Failed to update perf_event fd\n" ); + goto out; + } + } + + evsel->cgrp = leader_cgrp; + } + evsel->supported = true; + +if (evsel->cgrp == cgrp) +continue ; + + cgrp = evsel->cgrp; + +if (read_cgroup_id (cgrp) <0 ) { +pr_err ("Failed to get cgroup id\n" ); + err =-1 ; + goto out; + } + + map_fd =bpf_map__fd (skel->maps.cgrp_idx); + err =bpf_map_update_elem (map_fd, &cgrp->id, &i,BPF_ANY ); +if (err <0 ) { +pr_err ("Failed to update cgroup index map\n" ); + goto out; + } + + i++; + } + +/* +* bperf uses BPF_PROG_TEST_RUN to get accurate reading. Check +* whether the kernel support it
Quoted 8 lines
tools/perf/util/bpf_counter_cgroup.c line 171
Quoted 6 lines
tools/perf/util/bpf_counter_cgroup.c new file +6
-0+*/ + prog_fd =bpf_program__fd (skel->progs.trigger_read); + err =bperf_trigger_reading (prog_fd,0 ); +if (err) { +pr_debug ("The kernel does not support test_run for raw_tp BPF programs.\n" +"Therefore, --for-each-cgroup might show inaccurate readings\n" );I think this should be a warning, and we should set err = 0 to continue?
Sounds good, will change.
Quoted 26 lines
Quoted 7 lines
tools/perf/util/bpf_counter_cgroup.c new file +6
-0+ } + +out: +return err; +} +[...]
Quoted 16 lines
tools/perf/util/bpf_counter_cgroup.c new file +16
-0+ +/* +* trigger the leader prog on each cpu, so the cgrp_reading map could get +* the latest results. +*/ +static int bperf_cgrp__sync_counters (struct evlist *evlist) +{ +int i, cpu; +int nr_cpus = evlist->core.all_cpus->nr; +int prog_fd =bpf_program__fd (skel->progs.trigger_read); + +for (i =0 ; i < nr_cpus; i++) { + cpu = evlist->core.all_cpus->map[i]; +bperf_trigger_reading (prog_fd, cpu); + } +
Quoted 8 lines
tools/perf/util/bpf_counter_cgroup.c line 220
Quoted 5 lines
tools/perf/util/bpf_counter_cgroup.c new file +5
-0+return 0 ; +} + +static int bperf_cgrp__enable (struct evsel *evsel) +{Do we need to call
bperf_cgrp__sync_counters()before setting enabled to 1? If we don't, we may count some numbers before setting enabled to 1, no?
Actually it'll update the prev_readings even if enabled = 0. So I think it should get the correct counts after setting it to 1 without the bperf_cgrp__sync_counters().
Thanks,
Namhyung
Quoted 7 lines
Quoted 4 lines
tools/perf/util/bpf_counter_cgroup.c new file +3
-0+ skel->bss->enabled =1 ; +return 0 ; +}[...]
Namhyung Kim
maintainerRe: [PATCH 4/4] perf stat: Enable BPF counter with --for-each-cgroup
Quoted 87 lines · Namhyung Kim
Hi Song,
Quoted 35 lines · Song Liu
Quoted 32 lines · Namhyung Kim
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given
perf_eventsand to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.Cc Song Liu <[email protected]>Signed-off-by Namhyung Kim <[email protected]>---
tools/perf/Makefile.perf | 17 +- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 5 + tools/perf/util/bpf_counter_cgroup.c | 299 ++++++++++++++++++++ tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 191 +++++++++++++ tools/perf/util/cgroup.c | 2 + tools/perf/util/cgroup.h | 1 + 7 files changed, 515 insertions(+), 1 deletion(-)create mode 100644
tools/perf/util/bpf_counter_cgroup.ccreate mode 100644tools/perf/util/bpf_skel/bperf_cgroup.bpf.c[...]
Quoted 11 lines
tools/perf/util/bpf_counter_cgroup.c line 3
Quoted 9 lines
tools/perf/util/bpf_counter_cgroup.c new file +3
-0@@ -0,0 +1,299 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* Copyright (c) 2019 Facebook */ I am not sure whether this ^^^ is accurate.
Well, I just copied it from the
bpf_counter.cfile which was the base of this patch. Now I don't think I have many lines of code directly came from the origin.So I'm not sure what I can do. Do you want to update the copyright year to 2021? Or are you ok with removing the line at all?
Quoted 18 lines
[...]
Quoted 16 lines
tools/perf/util/bpf_counter_cgroup.c new file +16
-0+ +/* +* trigger the leader prog on each cpu, so the cgrp_reading map could get +* the latest results. +*/ +static int bperf_cgrp__sync_counters (struct evlist *evlist) +{ +int i, cpu; +int nr_cpus = evlist->core.all_cpus->nr; +int prog_fd =bpf_program__fd (skel->progs.trigger_read); + +for (i =0 ; i < nr_cpus; i++) { + cpu = evlist->core.all_cpus->map[i]; +bperf_trigger_reading (prog_fd, cpu); + } +Quoted 8 lines
tools/perf/util/bpf_counter_cgroup.c line 220
Quoted 5 lines
tools/perf/util/bpf_counter_cgroup.c new file +5
-0+return 0 ; +} + +static int bperf_cgrp__enable (struct evsel *evsel) +{Do we need to call
bperf_cgrp__sync_counters()before setting enabled to 1? If we don't, we may count some numbers before setting enabled to 1, no?
Quoted 3 lines
Actually it'll update the
prev_readingseven if enabled = 0. So I think it should get the correct counts after setting it to 1 without thebperf_cgrp__sync_counters().
I thought about this again, and you're right. Will change.
Thanks,
Namhyung
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 4/4] perf stat: Enable BPF counter with --for-each-cgroup
In reply to Namhyung KimEm Fri, Jun 25, 2021 at 12:18:26AM -0700, Namhyung Kim escreveu:
Quoted 8 lines
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
Quoted 5 lines
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given
perf_eventsand to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.
I'll take a stab at fixing these:
⬢[acme@toolbox perf]$ make -k CORESIGHT=1 BUILD_BPF_SKEL=1 PYTHON=python3 DEBUG=1 O=/tmp/build/perf -C tools/perf install-bin
make: Entering directory '/var/home/acme/git/perf/tools/perf'
BUILD: Doing 'make -j24' parallel build
Warning: Kernel ABI header at 'tools/include/uapi/linux/kvm.h' differs from latest version at 'include/uapi/linux/kvm.h' diff -u tools/include/uapi/linux/kvm.h include/uapi/linux/kvm.h
Warning: Kernel ABI header at 'tools/include/uapi/linux/mount.h' differs from latest version at 'include/uapi/linux/mount.h' diff -u tools/include/uapi/linux/mount.h include/uapi/linux/mount.h
Warning: Kernel ABI header at 'tools/arch/x86/include/asm/cpufeatures.h' differs from latest version at 'arch/x86/include/asm/cpufeatures.h' diff -u tools/arch/x86/include/asm/cpufeatures.h arch/x86/include/asm/cpufeatures.h
Warning: Kernel ABI header at 'tools/arch/x86/include/asm/msr-index.h' differs from latest version at 'arch/x86/include/asm/msr-index.h' diff -u tools/arch/x86/include/asm/msr-index.h arch/x86/include/asm/msr-index.h
Warning: Kernel ABI header at 'tools/arch/x86/include/uapi/asm/kvm.h' differs from latest version at 'arch/x86/include/uapi/asm/kvm.h' diff -u tools/arch/x86/include/uapi/asm/kvm.h arch/x86/include/uapi/asm/kvm.h
Warning: Kernel ABI header at 'tools/arch/x86/include/uapi/asm/svm.h' differs from latest version at 'arch/x86/include/uapi/asm/svm.h' diff -u tools/arch/x86/include/uapi/asm/svm.h arch/x86/include/uapi/asm/svm.h
Warning: Kernel ABI header at 'tools/arch/arm64/include/uapi/asm/kvm.h' differs from latest version at 'arch/arm64/include/uapi/asm/kvm.h' diff -u tools/arch/arm64/include/uapi/asm/kvm.h arch/arm64/include/uapi/asm/kvm.h
DESCEND plugins
GEN /tmp/build/perf/python/perf.so
INSTALL trace_plugins
CC /tmp/build/perf/util/bpf_counter_cgroup.o CC /tmp/build/perf/util/demangle-java.o CC /tmp/build/perf/util/demangle-rust.o CC /tmp/build/perf/util/jitdump.o CC /tmp/build/perf/util/genelf.o CC /tmp/build/perf/util/genelf_debug.o CC /tmp/build/perf/util/perf-hooks.o CC /tmp/build/perf/util/bpf-event.o
util/bpf_counter_cgroup.c: In function ‘bperf_load_program’:util/bpf_counter_cgroup.c:96:23: error: comparison of integer expressions of different signedness: ‘__u32’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare]
96 | for (i = 0; i < nr_cpus; i++) {
| ^util/bpf_counter_cgroup.c:125:43: error: comparison of integer expressions of different signedness: ‘__u32’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare]
125 | for (cpu = 0; cpu < nr_cpus; cpu++) {
| ^util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__load’:util/bpf_counter_cgroup.c:178:65: error: unused parameter ‘target’ [-Werror=unused-parameter]
178 | static int bperf_cgrp__load(struct evsel *evsel, struct target *target)
| ~~~~~~~~~~~~~~~^~~~~~
util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__install_pe’:
util/bpf_counter_cgroup.c:195:49: error: unused parameter ‘evsel’ [-Werror=unused-parameter]
195 | static int bperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd)
| ~~~~~~~~~~~~~~^~~~~util/bpf_counter_cgroup.c:195:60: error: unused parameter ‘cpu’ [-Werror=unused-parameter]
195 | static int bperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd)
| ~~~~^~~util/bpf_counter_cgroup.c:195:69: error: unused parameter ‘fd’ [-Werror=unused-parameter]
195 | static int bperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd)
| ~~~~^~util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__enable’:util/bpf_counter_cgroup.c:219:45: error: unused parameter ‘evsel’ [-Werror=unused-parameter]
219 | static int bperf_cgrp__enable(struct evsel *evsel)
| ~~~~~~~~~~~~~~^~~~~
cc1: all warnings being treated as errors
make[4]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:96: /tmp/build/perf/util/bpf_counter_cgroup.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:139: util] Error 2
make[2]: *** [Makefile.perf:655: /tmp/build/perf/perf-in.o] Error 2
make[1]: *** [Makefile.perf:238: sub-make] Error 2
make: *** [Makefile:113: install-bin] Error 2
make: Leaving directory '/var/home/acme/git/perf/tools/perf'
⬢[acme@toolbox perf]$
Namhyung Kim
maintainerRe: [PATCH 4/4] perf stat: Enable BPF counter with --for-each-cgroup
Hi Arnaldo,
Quoted 10 lines · Arnaldo Carvalho de Melo
Em Fri, Jun 25, 2021 at 12:18:26AM -0700, Namhyung Kim escreveu:
Quoted 8 lines
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
Quoted 7 lines
Quoted 5 lines
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given
perf_eventsand to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.I'll take a stab at fixing these:
Oops, sorry about that. My build environment didn't catch this.. Will fix it in the v5.
Thanks,
Namhyung
Quoted 63 lines
⬢[acme@toolbox perf]$ make -k CORESIGHT=1 BUILD_BPF_SKEL=1 PYTHON=python3 DEBUG=1 O=/tmp/build/perf -C
tools/perfinstall-bin
make: Entering directory '/var/home/acme/git/perf/tools/perf'
BUILD: Doing 'make -j24' parallel build
Warning: Kernel ABI header at 'tools/include/uapi/linux/kvm.h' differs from latest version at 'include/uapi/linux/kvm.h' diff -utools/include/uapi/linux/kvm.hinclude/uapi/linux/kvm.h
Warning: Kernel ABI header at 'tools/include/uapi/linux/mount.h' differs from latest version at 'include/uapi/linux/mount.h' diff -utools/include/uapi/linux/mount.hinclude/uapi/linux/mount.h
Warning: Kernel ABI header at 'tools/arch/x86/include/asm/cpufeatures.h' differs from latest version at 'arch/x86/include/asm/cpufeatures.h' diff -utools/arch/x86/include/asm/cpufeatures.harch/x86/include/asm/cpufeatures.h
Warning: Kernel ABI header at 'tools/arch/x86/include/asm/msr-index.h' differs from latest version at 'arch/x86/include/asm/msr-index.h' diff -utools/arch/x86/include/asm/msr-index.harch/x86/include/asm/msr-index.h
Warning: Kernel ABI header at 'tools/arch/x86/include/uapi/asm/kvm.h' differs from latest version at 'arch/x86/include/uapi/asm/kvm.h' diff -utools/arch/x86/include/uapi/asm/kvm.harch/x86/include/uapi/asm/kvm.h
Warning: Kernel ABI header at 'tools/arch/x86/include/uapi/asm/svm.h' differs from latest version at 'arch/x86/include/uapi/asm/svm.h' diff -utools/arch/x86/include/uapi/asm/svm.harch/x86/include/uapi/asm/svm.h
Warning: Kernel ABI header at 'tools/arch/arm64/include/uapi/asm/kvm.h' differs from latest version at 'arch/arm64/include/uapi/asm/kvm.h' diff -utools/arch/arm64/include/uapi/asm/kvm.harch/arm64/include/uapi/asm/kvm.h
DESCEND plugins
GEN/tmp/build/perf/python/perf.so
INSTALLtrace_pluginsCC /tmp/build/perf/util/bpf_counter_cgroup.o CC /tmp/build/perf/util/demangle-java.o CC /tmp/build/perf/util/demangle-rust.o CC /tmp/build/perf/util/jitdump.o CC /tmp/build/perf/util/genelf.o CC /tmp/build/perf/util/genelf_debug.o CC /tmp/build/perf/util/perf-hooks.o CC /tmp/build/perf/util/bpf-event.o
util/bpf_counter_cgroup.c: In function ‘bperf_load_program’:util/bpf_counter_cgroup.c:96:23: error: comparison of integer expressions of different signedness: ‘__u32’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare]
96 | for (i = 0; i <nr_cpus; i++) {
| ^util/bpf_counter_cgroup.c:125:43: error: comparison of integer expressions of different signedness: ‘__u32’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare]
125 | for (cpu = 0; cpu <nr_cpus; cpu++) {
| ^util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__load’:util/bpf_counter_cgroup.c:178:65: error: unused parameter ‘target’ [-Werror=unused-parameter]
178 | static intbperf_cgrp__load(struct evsel *evsel, struct target *target)| ~~~~~~~~~~~~~~~^~~~~~ util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__install_pe’: util/bpf_counter_cgroup.c:195:49: error: unused parameter ‘evsel’ [-Werror=unused-parameter] 195 | static int bperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd) | ~~~~~~~~~~~~~~^~~~~
util/bpf_counter_cgroup.c:195:60: error: unused parameter ‘cpu’ [-Werror=unused-parameter]
195 | static intbperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd)
| ~~~~^~~util/bpf_counter_cgroup.c:195:69: error: unused parameter ‘fd’ [-Werror=unused-parameter]
195 | static intbperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd)
| ~~~~^~util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__enable’:util/bpf_counter_cgroup.c:219:45: error: unused parameter ‘evsel’ [-Werror=unused-parameter]
219 | static intbperf_cgrp__enable(struct evsel *evsel)| ~~~~~~~~~~~~~~^~~~~cc1: all warnings being treated as errors
make[4]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:96:/tmp/build/perf/util/bpf_counter_cgroup.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:139: util] Error 2
make[2]: *** [Makefile.perf:655:/tmp/build/perf/perf-in.o] Error 2
make[1]: *** [Makefile.perf:238: sub-make] Error 2
make: *** [Makefile:113: install-bin] Error 2
make: Leaving directory '/var/home/acme/git/perf/tools/perf'
⬢[acme@toolbox perf]$
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 4/4] perf stat: Enable BPF counter with --for-each-cgroup
In reply to Arnaldo Carvalho de MeloEm Wed, Jun 30, 2021 at 03:50:12PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 9 lines
Em Fri, Jun 25, 2021 at 12:18:26AM -0700, Namhyung Kim escreveu:
Quoted 8 lines
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
Quoted 7 lines
Quoted 5 lines
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given
perf_eventsand to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.I'll take a stab at fixing these:
So, tried some 'make -C tools clean', etc but I'm now stuck with:
CLANG /tmp/build/perf/util/bpf_skel/.tmp/bperf_cgroup.bpf.outil/bpf_skel/bperf_cgroup.bpf.c:4:10: fatal error: 'vmlinux.h' file not found
#include "vmlinux.h"
^~~~~~~~~~~1 error generated.
make[2]: *** [Makefile.perf:1033: /tmp/build/perf/util/bpf_skel/.tmp/bperf_cgroup.bpf.o] Error 1 make[2]: *** Waiting for unfinished jobs....
CC /tmp/build/perf/pmu-events/pmu-events.o
LD /tmp/build/perf/pmu-events/pmu-events-in.o
Auto-detecting system features:
... libbfd: [ on ] ... disassembler-four-args: [ on ] ... zlib: [ on ] ... libcap: [ on ] ... clang-bpf-co-re: [ on ]
MKDIR /tmp/build/perf/util/bpf_skel/.tmp//bootstrap/
MKDIR /tmp/build/perf/util/bpf_skel/.tmp//bootstrap/libbpf/
Have to go errands now, will put what I have at tmp.perf/core now. Please see if you can reproduce, I use this to build:
make -k CORESIGHT=1 BUILD_BPF_SKEL=1 PYTHON=python3 DEBUG=1 O=/tmp/build/perf -C tools/perf install-bin
- Arnaldo
Quoted 62 lines
⬢[acme@toolbox perf]$ make -k CORESIGHT=1 BUILD_BPF_SKEL=1 PYTHON=python3 DEBUG=1 O=/tmp/build/perf -C
tools/perfinstall-bin
make: Entering directory '/var/home/acme/git/perf/tools/perf'
BUILD: Doing 'make -j24' parallel build
Warning: Kernel ABI header at 'tools/include/uapi/linux/kvm.h' differs from latest version at 'include/uapi/linux/kvm.h' diff -utools/include/uapi/linux/kvm.hinclude/uapi/linux/kvm.h
Warning: Kernel ABI header at 'tools/include/uapi/linux/mount.h' differs from latest version at 'include/uapi/linux/mount.h' diff -utools/include/uapi/linux/mount.hinclude/uapi/linux/mount.h
Warning: Kernel ABI header at 'tools/arch/x86/include/asm/cpufeatures.h' differs from latest version at 'arch/x86/include/asm/cpufeatures.h' diff -utools/arch/x86/include/asm/cpufeatures.harch/x86/include/asm/cpufeatures.h
Warning: Kernel ABI header at 'tools/arch/x86/include/asm/msr-index.h' differs from latest version at 'arch/x86/include/asm/msr-index.h' diff -utools/arch/x86/include/asm/msr-index.harch/x86/include/asm/msr-index.h
Warning: Kernel ABI header at 'tools/arch/x86/include/uapi/asm/kvm.h' differs from latest version at 'arch/x86/include/uapi/asm/kvm.h' diff -utools/arch/x86/include/uapi/asm/kvm.harch/x86/include/uapi/asm/kvm.h
Warning: Kernel ABI header at 'tools/arch/x86/include/uapi/asm/svm.h' differs from latest version at 'arch/x86/include/uapi/asm/svm.h' diff -utools/arch/x86/include/uapi/asm/svm.harch/x86/include/uapi/asm/svm.h
Warning: Kernel ABI header at 'tools/arch/arm64/include/uapi/asm/kvm.h' differs from latest version at 'arch/arm64/include/uapi/asm/kvm.h' diff -utools/arch/arm64/include/uapi/asm/kvm.harch/arm64/include/uapi/asm/kvm.h
DESCEND plugins
GEN/tmp/build/perf/python/perf.so
INSTALLtrace_pluginsCC /tmp/build/perf/util/bpf_counter_cgroup.o CC /tmp/build/perf/util/demangle-java.o CC /tmp/build/perf/util/demangle-rust.o CC /tmp/build/perf/util/jitdump.o CC /tmp/build/perf/util/genelf.o CC /tmp/build/perf/util/genelf_debug.o CC /tmp/build/perf/util/perf-hooks.o CC /tmp/build/perf/util/bpf-event.o
util/bpf_counter_cgroup.c: In function ‘bperf_load_program’:util/bpf_counter_cgroup.c:96:23: error: comparison of integer expressions of different signedness: ‘__u32’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare]
96 | for (i = 0; i <nr_cpus; i++) {
| ^util/bpf_counter_cgroup.c:125:43: error: comparison of integer expressions of different signedness: ‘__u32’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare]
125 | for (cpu = 0; cpu <nr_cpus; cpu++) {
| ^util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__load’:util/bpf_counter_cgroup.c:178:65: error: unused parameter ‘target’ [-Werror=unused-parameter]
178 | static intbperf_cgrp__load(struct evsel *evsel, struct target *target)| ~~~~~~~~~~~~~~~^~~~~~ util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__install_pe’: util/bpf_counter_cgroup.c:195:49: error: unused parameter ‘evsel’ [-Werror=unused-parameter] 195 | static int bperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd) | ~~~~~~~~~~~~~~^~~~~
util/bpf_counter_cgroup.c:195:60: error: unused parameter ‘cpu’ [-Werror=unused-parameter]
195 | static intbperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd)
| ~~~~^~~util/bpf_counter_cgroup.c:195:69: error: unused parameter ‘fd’ [-Werror=unused-parameter]
195 | static intbperf_cgrp__install_pe(struct evsel *evsel, int cpu, int fd)
| ~~~~^~util/bpf_counter_cgroup.c: In function ‘bperf_cgrp__enable’:util/bpf_counter_cgroup.c:219:45: error: unused parameter ‘evsel’ [-Werror=unused-parameter]
219 | static intbperf_cgrp__enable(struct evsel *evsel)| ~~~~~~~~~~~~~~^~~~~cc1: all warnings being treated as errors
make[4]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:96:/tmp/build/perf/util/bpf_counter_cgroup.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:139: util] Error 2
make[2]: *** [Makefile.perf:655:/tmp/build/perf/perf-in.o] Error 2
make[1]: *** [Makefile.perf:238: sub-make] Error 2
make: *** [Makefile:113: install-bin] Error 2
make: Leaving directory '/var/home/acme/git/perf/tools/perf'
⬢[acme@toolbox perf]$
Signature · 1 line
- Arnaldo
Namhyung Kim
maintainerRe: [PATCH 4/4] perf stat: Enable BPF counter with --for-each-cgroup
Quoted 43 lines · Arnaldo Carvalho de Melo
Em Wed, Jun 30, 2021 at 03:50:12PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 9 lines
Em Fri, Jun 25, 2021 at 12:18:26AM -0700, Namhyung Kim escreveu:
Quoted 8 lines
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
Quoted 7 lines
Quoted 5 lines
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given
perf_eventsand to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.I'll take a stab at fixing these:
So, tried some 'make -C tools clean', etc but I'm now stuck with:
CLANG
/tmp/build/perf/util/bpf_skel/.tmp/bperf_cgroup.bpf.outil/bpf_skel/bperf_cgroup.bpf.c:4:10: fatal error: 'vmlinux.h' file not found#include "vmlinux.h" ^~~~~~~~~~~1 error generated.
make[2]: *** [Makefile.perf:1033:/tmp/build/perf/util/bpf_skel/.tmp/bperf_cgroup.bpf.o] Error 1 make[2]: *** Waiting for unfinished jobs....
CC/tmp/build/perf/pmu-events/pmu-events.o
LD/tmp/build/perf/pmu-events/pmu-events-in.oAuto-detecting system features:
... libbfd: [ on ] ... disassembler-four-args: [ on ] ... zlib: [ on ] ... libcap: [ on ] ... clang-bpf-co-re: [ on ]MKDIR
/tmp/build/perf/util/bpf_skel/.tmp//bootstrap/
MKDIR/tmp/build/perf/util/bpf_skel/.tmp//bootstrap/libbpf/
Quoted 6 lines
Have to go errands now, will put what I have at tmp.perf/core now. Please see if you can reproduce, I use this to build:
make -k CORESIGHT=1 BUILD_BPF_SKEL=1 PYTHON=python3 DEBUG=1 O=/tmp/build/perf -C
tools/perfinstall-bin- Arnaldo
Oh, sorry about that. I found the header generation is misplaced in the Makefile.perf. Will fix it in the next version.
Thanks,
Namhyung
Namhyung Kim
maintainerQuoted 29 lines · Namhyung Kim
Hello,
This is to add BPF support for --for-each-cgroup to handle many cgroup events on big machines. You can use the --bpf-counters to enable the new behavior.
* changes in v4
- convertcgrp_readingsto a per-cpu array map
- remove now-unusedcpu_idxmap
- move common functions to a header file
- reuse bpftool bootstrap binary
- fix build error in the cgroup code* changes in v3
- support cgroup hierarchy with ancestor ids
- add and triggerraw_tpBPF program
- add a build rule forvmlinux.h* changes in v2
- remove incorrect use ofBPF_F_PRESERVE_ELEMS
- add missing map elements after lookup
- handle cgroup v1Basic idea is to use a single set of per-cpu events to count interested events and aggregate them to each cgroup. I used bperf mechanism to use a BPF program for cgroup-switches and save the results in a matching map element for given cgroups.
Quoted 7 lines
Without this, we need to have separate events for cgroups, and it creates unnecessary multiplexing overhead (and PMU programming) when tasks in different cgroups are switched. I saw this makes a big difference on 256 cpu machines with hundreds of cgroups.
Actually this is what I wanted to do it in the kernel [1], but we can do the job using BPF!
Ugh, I found the current kernel bpf verifier doesn't accept the bpf_get_current_ancestor_cgroup_id() helper. Will send the fix to BPF folks.
Thanks,
Namhyung
Namhyung Kim
maintainerQuoted 36 lines · Namhyung Kim
Quoted 34 lines · Namhyung Kim
Hello,
This is to add BPF support for --for-each-cgroup to handle many cgroup events on big machines. You can use the --bpf-counters to enable the new behavior.
* changes in v4
- convertcgrp_readingsto a per-cpu array map
- remove now-unusedcpu_idxmap
- move common functions to a header file
- reuse bpftool bootstrap binary
- fix build error in the cgroup code* changes in v3
- support cgroup hierarchy with ancestor ids
- add and triggerraw_tpBPF program
- add a build rule forvmlinux.h* changes in v2
- remove incorrect use ofBPF_F_PRESERVE_ELEMS
- add missing map elements after lookup
- handle cgroup v1Basic idea is to use a single set of per-cpu events to count interested events and aggregate them to each cgroup. I used bperf mechanism to use a BPF program for cgroup-switches and save the results in a matching map element for given cgroups.
Without this, we need to have separate events for cgroups, and it creates unnecessary multiplexing overhead (and PMU programming) when tasks in different cgroups are switched. I saw this makes a big difference on 256 cpu machines with hundreds of cgroups.
Quoted 6 lines
Quoted 2 lines
Actually this is what I wanted to do it in the kernel [1], but we can do the job using BPF!
Ugh, I found the current kernel bpf verifier doesn't accept the
bpf_get_current_ancestor_cgroup_id()helper. Will send the fix to BPF folks.
The fix landed on the bpf-next tree.
Thanks,
Namhyung