Introduce perf-stat -b for BPF programs
Subsystems (3)
v3 cover Introduce perf-stat -b for BPF programs
This set introduces perf-stat -b option to count events for BPF programs. This is similar to bpftool-prog-profile. But perf-stat makes it much more flexible.
Changes v2 => v3:
1. Small fixes in Makefile.perf and bpf_counter.c (Jiri)
2. Rebased on top of bpf-next. This is because 1/2 conflicts with some patches in bpftool/Makefile.
Changes PATCH v1 => PATCH v2:
1. Various fixes in Makefiles. (Jiri)
2. Fix an build warning/error with gcc-10. (Jiri)
Changes RFC v2 => PATCH v1:
1. Support counting on multiple BPF programs.
2. Add BPF handling to target__validate().
3. Improve Makefile. (Jiri)
Changes RFC v1 => RFC v2:
1. Use bootstrap version of bpftool. (Jiri)
2. Set default to not building bpf skeletons. (Jiri)
3. Remove util/bpf_skel/Makefile, keep all the logic in Makefile.perf.
(Jiri)
4. Remove dependency to vmlinux.h in the two skeletons. The goal here is to enable building perf without building kernel (vmlinux) first.
Note: I also removed the logic that build vmlinux.h. We can add that back when we have to use it (to access big kernel structures).
Song Liu (2):
perf: support build BPF skeletons with perf
perf-stat: enable counting events for BPF programs
tools/bpf/bpftool/Makefile | 3 + tools/build/Makefile.feature | 4 +- tools/perf/Makefile.config | 9 + tools/perf/Makefile.perf | 48 ++- tools/perf/builtin-stat.c | 77 ++++- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 297 ++++++++++++++++++ tools/perf/util/bpf_counter.h | 73 +++++ tools/perf/util/bpf_skel/.gitignore | 3 + .../util/bpf_skel/bpf_prog_profiler.bpf.c | 93 ++++++ tools/perf/util/evsel.c | 11 + tools/perf/util/evsel.h | 6 + tools/perf/util/stat-display.c | 4 +- tools/perf/util/target.c | 34 +- tools/perf/util/target.h | 10 + tools/scripts/Makefile.include | 1 + 16 files changed, 655 insertions(+), 19 deletions(-) create mode 100644 tools/perf/util/bpf_counter.c create mode 100644 tools/perf/util/bpf_counter.h create mode 100644 tools/perf/util/bpf_skel/.gitignore create mode 100644 tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c
--
2.24.1
PATCH v3 1/2 perf: support build BPF skeletons with perf
BPF programs are useful in perf to profile BPF programs. BPF skeleton is by far the easiest way to write BPF tools. Enable building BPF skeletons in util/bpf_skel. A dummy bpf skeleton is added. More bpf skeletons will be added for different use cases.
--- tools/bpf/bpftool/Makefile | 3 ++ tools/build/Makefile.feature | 4 ++- tools/perf/Makefile.config | 9 ++++++ tools/perf/Makefile.perf | 48 +++++++++++++++++++++++++++-- tools/perf/util/bpf_skel/.gitignore | 3 ++ tools/scripts/Makefile.include | 1 + 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tools/perf/util/bpf_skel/.gitignore
tools/bpf/bpftool/Makefile +3 -0
@@ -139,6 +139,9 @@ endif BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o) + +bootstrap: $(BPFTOOL_BOOTSTRAP) + OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
tools/build/Makefile.feature +3 -1
@@ -99,7 +99,9 @@ FEATURE_TESTS_EXTRA := \ clang \ libbpf \ libpfm4 \ - libdebuginfod + libdebuginfod \ + clang-bpf-co-re + FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
tools/perf/Makefile.config +9 -0
@@ -621,6 +621,15 @@ ifndef NO_LIBBPF endif endif +ifdef BUILD_BPF_SKEL + $(call feature_check,clang-bpf-co-re) + ifeq ($(feature-clang-bpf-co-re), 0) + dummy := $(error Error: clang too old. Please install recent clang) + endif + $(call detected,CONFIG_PERF_BPF_SKEL) + CFLAGS += -DBUILD_BPF_SKEL +endif + dwarf-post-unwind := 1 dwarf-post-unwind-text := BUG
tools/perf/Makefile.perf +46 -2
72 lines not on this page · raw message
tools/perf/util/bpf_skel/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +.tmp +*.skel.h
tools/scripts/Makefile.include +1 -0
@@ -127,6 +127,7 @@ ifneq ($(silent),1) $(MAKE) $(PRINT_DIR) -C $$subdir QUIET_FLEX = @echo ' FLEX '$@; QUIET_BISON = @echo ' BISON '$@; + QUIET_GENSKEL = @echo ' GEN-SKEL '$@; descend = \ +@echo ' DESCEND '$(1); \
Signature · 1 line
2.24.1
Re: [PATCH v3 1/2] perf: support build BPF skeletons with perf
Quoted 6 lines · Song Liu
BPF programs are useful in perf to profile BPF programs. BPF skeleton is by far the easiest way to write BPF tools. Enable building BPF skeletons in util/bpf_skel. A dummy bpf skeleton is added. More bpf skeletons will be added for different use cases.
Signed-off-by Song Liu <[email protected]>
one nit below, but other than that:
thanks,
jirka
Quoted 44 lines
--- tools/bpf/bpftool/Makefile | 3 ++ tools/build/Makefile.feature | 4 ++- tools/perf/Makefile.config | 9 ++++++ tools/perf/Makefile.perf | 48 +++++++++++++++++++++++++++-- tools/perf/util/bpf_skel/.gitignore | 3 ++ tools/scripts/Makefile.include | 1 + 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tools/perf/util/bpf_skel/.gitignoretools/bpf/bpftool/Makefile +3
-0@@ -139,6 +139,9 @@ endif BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o) + +bootstrap: $(BPFTOOL_BOOTSTRAP) + OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \tools/build/Makefile.feature +3
-1@@ -99,7 +99,9 @@ FEATURE_TESTS_EXTRA := \ clang \ libbpf \ libpfm4 \ - libdebuginfod + libdebuginfod \ + clang-bpf-co-re + FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)tools/perf/Makefile.config +0
-0
tools/perf/Makefile.config ifndef NO_LIBBPF · line 630
Quoted 10 lines
tools/perf/Makefile.config +7
-0@@ -621,6 +621,15 @@ ifndef NO_LIBBPF endif endif +ifdef BUILD_BPF_SKEL + $(call feature_check,clang-bpf-co-re) + ifeq ($(feature-clang-bpf-co-re), 0) + dummy := $(error Error: clang too old. Please install recent clang) + endif + $(call detected,CONFIG_PERF_BPF_SKEL) + CFLAGS += -DBUILD_BPF_SKEL
sorry I did not notice before, but we use HAVE_* name style for these C macros
HAVE_BPF_SKEL should fit
Quoted 8 lines
tools/perf/Makefile.config +2
-0+endif + dwarf-post-unwind := 1 dwarf-post-unwind-text := BUGtools/perf/Makefile.perf +0
-1--- a/tools/perf/Makefile.perf
SNIP
Re: [PATCH v3 1/2] perf: support build BPF skeletons with perf
Quoted 10 lines · Jiri Olsa
Quoted 5 lines · Song Liu
BPF programs are useful in perf to profile BPF programs. BPF skeleton is by far the easiest way to write BPF tools. Enable building BPF skeletons in util/bpf_skel. A dummy bpf skeleton is added. More bpf skeletons will be added for different use cases.
Quoted 5 lines
Quoted 1 line
Signed-off-by Song Liu <[email protected]>one nit below, but other than that:
Acked-by Jiri Olsa <[email protected]>
Thanks!
[...]
Quoted 9 lines
tools/perf/Makefile.config ifndef NO_LIBBPF · line 630
Quoted 8 lines
tools/perf/Makefile.config +7
-0+ifdef BUILD_BPF_SKEL + $(call feature_check,clang-bpf-co-re) + ifeq ($(feature-clang-bpf-co-re), 0) + dummy := $(error Error: clang too old. Please install recent clang) + endif + $(call detected,CONFIG_PERF_BPF_SKEL) + CFLAGS += -DBUILD_BPF_SKEL
Quoted 3 lines
sorry I did not notice before, but we use HAVE_* name style for these C macros
HAVE_BPF_SKELshould fit
I will fix this in v4.
PATCH v3 2/2 perf-stat: enable counting events for BPF programs
In reply to Song LiuIntroduce perf-stat -b option, which counts events for BPF programs, like:
[root@localhost ~]# ~/perf stat -e ref-cycles,cycles -b 254 -I 1000
1.487903822 115,200 ref-cycles
1.487903822 86,012 cycles
2.489147029 80,560 ref-cycles
2.489147029 73,784 cycles
3.490341825 60,720 ref-cycles
3.490341825 37,797 cycles
4.491540887 37,120 ref-cycles
4.491540887 31,963 cyclesThe example above counts cycles and ref-cycles of BPF program of id 254. This is similar to bpftool-prog-profile command, but more flexible.
perf-stat -b creates per-cpu perf_event and loads fentry/fexit BPF programs (monitor-progs) to the target BPF program (target-prog). The monitor-progs read perf_event before and after the target-prog, and aggregate the difference in a BPF map. Then the user space reads data from these maps.
A new struct bpf_counter is introduced to provide common interface that uses BPF programs/maps to count perf events.
--- tools/perf/Makefile.perf | 2 +- tools/perf/builtin-stat.c | 77 ++++- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 297 ++++++++++++++++++ tools/perf/util/bpf_counter.h | 73 +++++ .../util/bpf_skel/bpf_prog_profiler.bpf.c | 93 ++++++ tools/perf/util/evsel.c | 11 + tools/perf/util/evsel.h | 6 + tools/perf/util/stat-display.c | 4 +- tools/perf/util/target.c | 34 +- tools/perf/util/target.h | 10 + 11 files changed, 591 insertions(+), 17 deletions(-) create mode 100644 tools/perf/util/bpf_counter.c create mode 100644 tools/perf/util/bpf_counter.h create mode 100644 tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c
tools/perf/Makefile.perf +1 -1
@@ -1015,7 +1015,7 @@ python-clean: SKEL_OUT := $(abspath $(OUTPUT)util/bpf_skel) SKEL_TMP_OUT := $(abspath $(SKEL_OUT)/.tmp) -SKELETONS := +SKELETONS := $(SKEL_OUT)/bpf_prog_profiler.skel.h ifdef BUILD_BPF_SKEL BPFTOOL := $(SKEL_TMP_OUT)/bootstrap/bpftool
tools/perf/builtin-stat.c +63 -14
172 lines not on this page · raw message
tools/perf/util/Build +1 -0
@@ -135,6 +135,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_BPF_PROLOGUE) += bpf-prologue.o perf-$(CONFIG_LIBELF) += symbol-elf.o perf-$(CONFIG_LIBELF) += probe-file.o
tools/perf/util/bpf_counter.c new file +297 -0
297 lines not on this page · raw message
tools/perf/util/bpf_counter.h new file +73 -0
73 lines not on this page · raw message
tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +93 -0
93 lines not on this page · raw message
tools/perf/util/evsel.c +11 -0
@@ -25,6 +25,7 @@#include <stdlib.h> #include <perf/evsel.h> #include "asm/bug.h" +#include "bpf_counter.h" #include "callchain.h" #include "cgroup.h" #include "counts.h"
@@ -51,6 +52,10 @@#include <internal/lib.h> #include <linux/ctype.h> +#include <bpf/bpf.h> +#include <bpf/libbpf.h> +#include <bpf/btf.h> +#include "rlimit.h" struct perf_missing_features perf_missing_features;
@@ -247,6 +252,7 @@ void evsel__init(struct evsel *evsel, evsel->bpf_obj =NULL ; evsel->bpf_fd =-1 ;INIT_LIST_HEAD (&evsel->config_terms); +INIT_LIST_HEAD (&evsel->bpf_counter_list); perf_evsel__object.init (evsel); evsel->sample_size =__evsel__sample_size (attr->sample_type);evsel__calc_id_pos (evsel);
@@ -1365,6 +1371,7 @@ void evsel__exit(struct evsel *evsel) {assert (list_empty (&evsel->core.node));assert (evsel->evlist ==NULL ); +bpf_counter__destroy (evsel);evsel__free_counts (evsel);perf_evsel__free_fd (&evsel->core);perf_evsel__free_id (&evsel->core);
@@ -1770,6 +1777,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, evsel->core.attr.sample_id_all =0 ;display_attr (&evsel->core.attr); +if (!list_empty (&evsel->bpf_counter_list)) + evsel->core.attr.inherit =0 ;for (cpu = start_cpu; cpu < end_cpu; cpu++) {
@@ -1788,6 +1797,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,FD (evsel, cpu, thread) = fd; +bpf_counter__install_pe (evsel, cpu, fd); +if (unlikely (test_attr__enabled)) {test_attr__open (&evsel->core.attr, pid, cpus->map[cpu], fd, group_fd, flags);
tools/perf/util/evsel.h +6 -0
@@ -10,6 +10,7 @@#include <internal/evsel.h> #include <perf/evsel.h> #include "symbol_conf.h" +#include "bpf_counter.h" #include <internal/cpumap.h> struct bpf_object ;
@@ -17,6 +18,8 @@ struct cgroup;struct perf_counts ;struct perf_stat_evsel ;union perf_event ; +struct bpf_counter_ops ; +struct target ;typedef int (evsel__sb_cb_t )(union perf_event *event,void *data);
@@ -127,6 +130,8 @@ struct evsel {* See also evsel__has_callchain(). */ __u64 synth_sample_type; +struct list_head bpf_counter_list; +struct bpf_counter_ops *bpf_counter_ops; };struct perf_missing_features {
@@ -423,4 +428,5 @@ static inline bool evsel__is_dummy_event(struct evsel *evsel)struct perf_env *evsel__env (struct evsel *evsel);int evsel__store_ids (struct evsel *evsel,struct evlist *evlist); +#endif /* __PERF_EVSEL_H */
tools/perf/util/stat-display.c +3 -1
@@ -1026,7 +1026,9 @@ static void print_header(struct perf_stat_config *config,if (!config->csv_output) {fprintf (output,"\n" );fprintf (output," Performance counter stats for " ); -if (_target->system_wide) +if (_target->bpf_str) +fprintf (output,"\'BPF program(s) %s" , _target->bpf_str); +else if (_target->system_wide)fprintf (output,"\'system wide" );else if (_target->cpu_list)fprintf (output,"\'CPU(s) %s" , _target->cpu_list);
tools/perf/util/target.c +33 -1
@@ -56,6 +56,34 @@ enum target_errno target__validate(struct target *target) ret =TARGET_ERRNO__UID_OVERRIDE_SYSTEM ; } +/* BPF and CPU are mutually exclusive */ +if (target->bpf_str && target->cpu_list) { + target->cpu_list =NULL ; +if (ret ==TARGET_ERRNO__SUCCESS ) + ret =TARGET_ERRNO__BPF_OVERRIDE_CPU ; + } + +/* BPF and PID/TID are mutually exclusive */ +if (target->bpf_str && target->tid) { + target->tid =NULL ; +if (ret ==TARGET_ERRNO__SUCCESS ) + ret =TARGET_ERRNO__BPF_OVERRIDE_PID ; + } + +/* BPF and UID are mutually exclusive */ +if (target->bpf_str && target->uid_str) { + target->uid_str =NULL ; +if (ret ==TARGET_ERRNO__SUCCESS ) + ret =TARGET_ERRNO__BPF_OVERRIDE_UID ; + } + +/* BPF and THREADS are mutually exclusive */ +if (target->bpf_str && target->per_thread) { + target->per_thread = false; +if (ret ==TARGET_ERRNO__SUCCESS ) + ret =TARGET_ERRNO__BPF_OVERRIDE_THREAD ; + } +/* THREAD and SYSTEM/CPU are mutually exclusive */ if (target->per_thread && (target->system_wide || target->cpu_list)) { target->per_thread = false;
@@ -109,6 +137,10 @@ static const char *target__error_str[] = {"PID/TID switch overriding SYSTEM" ,"UID switch overriding SYSTEM" ,"SYSTEM/CPU switch overriding PER-THREAD" , +"BPF switch overriding CPU" , +"BPF switch overriding PID/TID" , +"BPF switch overriding UID" , +"BPF switch overriding THREAD" ,"Invalid User: %s" , "Problems obtaining information for user %s", };
@@ -134,7 +166,7 @@ int target__strerror(struct target *target, int errnum,switch (errnum) { case TARGET_ERRNO__PID_OVERRIDE_CPU ... -TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD : +TARGET_ERRNO__BPF_OVERRIDE_THREAD :snprintf (buf, buflen,"%s" , msg);break ;
tools/perf/util/target.h +10 -0
@@ -10,6 +10,7 @@ struct target {const char *tid;const char *cpu_list;const char *uid_str; +const char *bpf_str;uid_t uid;bool system_wide;bool uses_mmap;
@@ -36,6 +37,10 @@ enum target_errno {TARGET_ERRNO__PID_OVERRIDE_SYSTEM ,TARGET_ERRNO__UID_OVERRIDE_SYSTEM ,TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD , +TARGET_ERRNO__BPF_OVERRIDE_CPU , +TARGET_ERRNO__BPF_OVERRIDE_PID , +TARGET_ERRNO__BPF_OVERRIDE_UID , +TARGET_ERRNO__BPF_OVERRIDE_THREAD ,/* for target__parse_uid() */ TARGET_ERRNO__INVALID_UID,
@@ -59,6 +64,11 @@ static inline bool target__has_cpu(struct target *target)return target->system_wide || target->cpu_list; } +static inline bool target__has_bpf (struct target *target) +{ +return target->bpf_str; +} +static inline bool target__none (struct target *target) {return !target__has_task (target) && !target__has_cpu (target);
Signature · 1 line
2.24.1
Re: [PATCH v3 2/2] perf-stat: enable counting events for BPF programs
Quoted 21 lines · Song Liu
Introduce perf-stat -b option, which counts events for BPF programs, like:
[root@localhost ~]# ~/perf stat -e ref-cycles,cycles -b 254 -I 1000
1.487903822 115,200 ref-cycles 1.487903822 86,012 cycles 2.489147029 80,560 ref-cycles 2.489147029 73,784 cycles 3.490341825 60,720 ref-cycles 3.490341825 37,797 cycles 4.491540887 37,120 ref-cycles 4.491540887 31,963 cyclesThe example above counts cycles and ref-cycles of BPF program of id 254. This is similar to bpftool-prog-profile command, but more flexible.
perf-stat -b creates per-cpu
perf_eventand loads fentry/fexit BPF programs (monitor-progs) to the target BPF program (target-prog). The monitor-progs readperf_eventbefore and after the target-prog, and aggregate the difference in a BPF map. Then the user space reads data from these maps.
Quoted 4 lines
A new struct
bpf_counteris introduced to provide common interface that uses BPF programs/maps to count perf events.Signed-off-by Song Liu <[email protected]>
I'm getting this at the end of the compilation:
LINK perf
rm /home/jolsa/linux-perf/tools/perf/util/bpf_skel/.tmp/bpf_prog_profiler.bpf.o
I guess we can keep it or make it silent somehow
Quoted 8 lines
--- tools/perf/Makefile.perf | 2 +- tools/perf/builtin-stat.c | 77 ++++- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 297 ++++++++++++++++++ tools/perf/util/bpf_counter.h | 73 +++++ .../util/bpf_skel/bpf_prog_profiler.bpf.c | 93 ++++++ tools/perf/util/evsel.c | 11 +
Quoted 8 lines
tools/perf/util/evsel.h | 6 + tools/perf/util/stat-display.c | 4 +- tools/perf/util/target.c | 34 +- tools/perf/util/target.h | 10 + 11 files changed, 591 insertions(+), 17 deletions(-) create mode 100644 tools/perf/util/bpf_counter.c create mode 100644 tools/perf/util/bpf_counter.h create mode 100644 tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c
we need man page update, would be great with some example
SNIP
Quoted 11 lines
tools/perf/builtin-stat.c +2
-1-int status = -EINVAL , run_idx; +int status = -EINVAL , run_idx, err;const char *mode;FILE *output = stderr;unsigned int interval, timeout;const char *const stat_subcommands[] = {"record" ,"report" }; +char errbuf[BUFSIZ ];setlocale (LC_ALL ,"" );
tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 2221
Quoted 9 lines
tools/perf/builtin-stat.c +6
-0@@ -2169,6 +2213,12 @@ int cmd_stat(int argc, const char **argv) }else if (big_num_opt ==0 )/* User passed --no-big-num */ stat_config.big_num = false; + err =target__validate (&target); +if (err) { +target__strerror (&target, err, errbuf,BUFSIZ ); +pr_warning ("%s\n" , errbuf); + } +
is there a reason for this to move before setup_system_wide?
I don't think it's a big deal, but just curious if that's intentional
SNIP
Quoted 7 lines
tools/perf/util/bpf_counter.c new file +7
-0+ +int bpf_counter__enable (struct evsel *evsel) +{ +if (list_empty (&evsel->bpf_counter_list)) +return 0 ; +return evsel->bpf_counter_ops->enable (evsel); +}
tools/perf/util/bpf_counter.c line 291
Quoted 10 lines
tools/perf/util/bpf_counter.c new file +10
-0+ +int bpf_counter__read (struct evsel *evsel) +{ +if (list_empty (&evsel->bpf_counter_list)) +return -EAGAIN ; +return evsel->bpf_counter_ops->read (evsel); +} + +int bpf_counter__destroy (struct evsel *evsel) +{
this could return void
SNIP
Quoted 17 lines
tools/perf/util/evsel.c +2
-0@@ -247,6 +252,7 @@ void evsel__init(struct evsel *evsel, evsel->bpf_obj =NULL ; evsel->bpf_fd =-1 ;INIT_LIST_HEAD (&evsel->config_terms); +INIT_LIST_HEAD (&evsel->bpf_counter_list); perf_evsel__object.init (evsel); evsel->sample_size =__evsel__sample_size (attr->sample_type);evsel__calc_id_pos (evsel);@@ -1365,6 +1371,7 @@ void evsel__exit(struct evsel *evsel) {assert (list_empty (&evsel->core.node));assert (evsel->evlist ==NULL ); +bpf_counter__destroy (evsel);evsel__free_counts (evsel);perf_evsel__free_fd (&evsel->core);perf_evsel__free_id (&evsel->core);
tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, · line 1781
Quoted 5 lines
tools/perf/util/evsel.c +2
-0@@ -1770,6 +1777,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, evsel->core.attr.sample_id_all =0 ;display_attr (&evsel->core.attr); +if (!list_empty (&evsel->bpf_counter_list)) + evsel->core.attr.inherit =0 ;
I think this should go to evsel__config where we set all attr bits
thanks,
jirka
Re: [PATCH v3 2/2] perf-stat: enable counting events for BPF programs
Quoted 29 lines · Jiri Olsa
Quoted 24 lines · Song Liu
Introduce perf-stat -b option, which counts events for BPF programs, like:
[root@localhost ~]# ~/perf stat -e ref-cycles,cycles -b 254 -I 1000
1.487903822 115,200 ref-cycles 1.487903822 86,012 cycles 2.489147029 80,560 ref-cycles 2.489147029 73,784 cycles 3.490341825 60,720 ref-cycles 3.490341825 37,797 cycles 4.491540887 37,120 ref-cycles 4.491540887 31,963 cyclesThe example above counts cycles and ref-cycles of BPF program of id 254. This is similar to bpftool-prog-profile command, but more flexible.
perf-stat -b creates per-cpu
perf_eventand loads fentry/fexit BPF programs (monitor-progs) to the target BPF program (target-prog). The monitor-progs readperf_eventbefore and after the target-prog, and aggregate the difference in a BPF map. Then the user space reads data from these maps.A new struct
bpf_counteris introduced to provide common interface that uses BPF programs/maps to count perf events.
Quoted 8 lines
Quoted 1 line
Signed-off-by Song Liu <[email protected]>I'm getting this at the end of the compilation:
LINK perf
rm/home/jolsa/linux-perf/tools/perf/util/bpf_skel/.tmp/bpf_prog_profiler.bpf.oI guess we can keep it or make it silent somehow
I also noticed this, but haven't figured out how to silent it. I guess we can fix it out later.
Quoted 18 lines
Quoted 14 lines
---
tools/perf/Makefile.perf | 2 +- tools/perf/builtin-stat.c | 77 ++++- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 297 ++++++++++++++++++ tools/perf/util/bpf_counter.h | 73 +++++ .../util/bpf_skel/bpf_prog_profiler.bpf.c | 93 ++++++ tools/perf/util/evsel.c | 11 + tools/perf/util/evsel.h | 6 + tools/perf/util/stat-display.c | 4 +- tools/perf/util/target.c | 34 +- tools/perf/util/target.h | 10 + 11 files changed, 591 insertions(+), 17 deletions(-)Quoted 3 lines
create mode 100644
tools/perf/util/bpf_counter.c
create mode 100644tools/perf/util/bpf_counter.h
create mode 100644tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c
Quoted 1 line
we need man page update, would be great with some example
How about we do this in a follow up patch?
Quoted 26 lines
SNIP
Quoted 11 lines
tools/perf/builtin-stat.c +1
-1-int status = -EINVAL , run_idx; +int status = -EINVAL , run_idx, err;const char *mode; FILE *output = stderr; unsigned int interval, timeout; const char * const stat_subcommands[] = { "record", "report" };tools/perf/builtin-stat.c +1
-0+char errbuf[BUFSIZ ];setlocale(LC_ALL, "");tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 2221
Quoted 9 lines
tools/perf/builtin-stat.c +6
-0@@ -2169,6 +2213,12 @@ int cmd_stat(int argc, const char **argv) }else if (big_num_opt ==0 )/* User passed --no-big-num */ stat_config.big_num = false; + err =target__validate (&target); +if (err) { +target__strerror (&target, err, errbuf,BUFSIZ ); +pr_warning ("%s\n" , errbuf); + } +is there a reason for this to move before
setup_system_wide?
Quoted 1 line
I don't think it's a big deal, but just curious if that's intentional
Yes, this is intentional. It is to fix "BPF switch overriding CPU" cases. Without moving target_validate(), we got
# perf stat -e cycles,instructions -b 59 -C 0 BPF switch overriding CPU Error: The sys_perf_event_open() syscall returned with 9 (Bad file descriptor) for event (cycles). /bin/dmesg | grep -i perf may provide additional information.
Quoted 14 lines
SNIP
Quoted 11 lines
tools/perf/util/bpf_counter.c new file +11
-0+ +int bpf_counter__enable (struct evsel *evsel) +{ +if (list_empty (&evsel->bpf_counter_list)) +return 0 ; +return evsel->bpf_counter_ops->enable (evsel); +} + +int bpf_counter__read (struct evsel *evsel) +{ +if (list_empty (&evsel->bpf_counter_list))
Quoted 8 lines
tools/perf/util/bpf_counter.c line 291
Quoted 6 lines
tools/perf/util/bpf_counter.c new file +6
-0+return -EAGAIN ; +return evsel->bpf_counter_ops->read (evsel); +} + +int bpf_counter__destroy (struct evsel *evsel) +{this could return void
Fixed in v4.
Quoted 22 lines
SNIP
Quoted 19 lines
tools/perf/util/evsel.c +2
-0@@ -247,6 +252,7 @@ void evsel__init(struct evsel *evsel, evsel->bpf_obj =NULL ; evsel->bpf_fd =-1 ;INIT_LIST_HEAD (&evsel->config_terms); +INIT_LIST_HEAD (&evsel->bpf_counter_list); perf_evsel__object.init (evsel); evsel->sample_size =__evsel__sample_size (attr->sample_type);evsel__calc_id_pos (evsel);@@ -1365,6 +1371,7 @@ void evsel__exit(struct evsel *evsel) {assert (list_empty (&evsel->core.node));assert (evsel->evlist ==NULL ); +bpf_counter__destroy (evsel);evsel__free_counts (evsel);perf_evsel__free_fd (&evsel->core);perf_evsel__free_id (&evsel->core);@@ -1770,6 +1777,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, evsel->core.attr.sample_id_all =0 ;
Quoted 5 lines
tools/perf/util/evsel.c static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, · line 1781
Quoted 3 lines
tools/perf/util/evsel.c +2
-0display_attr (&evsel->core.attr); +if (!list_empty (&evsel->bpf_counter_list)) + evsel->core.attr.inherit =0 ;I think this should go to
evsel__configwhere we set all attr bits
evsel__config() is not called by perf-stat. I moved the logic to create_perf_stat_counter() instead.
Thanks,
Song
Re: [PATCH v3 2/2] perf-stat: enable counting events for BPF programs
Quoted 33 lines · Song Liu
Quoted 33 lines · Jiri Olsa
Quoted 21 lines · Song Liu
Introduce perf-stat -b option, which counts events for BPF programs, like:
[root@localhost ~]# ~/perf stat -e ref-cycles,cycles -b 254 -I 1000
1.487903822 115,200 ref-cycles 1.487903822 86,012 cycles 2.489147029 80,560 ref-cycles 2.489147029 73,784 cycles 3.490341825 60,720 ref-cycles 3.490341825 37,797 cycles 4.491540887 37,120 ref-cycles 4.491540887 31,963 cyclesThe example above counts cycles and ref-cycles of BPF program of id 254. This is similar to bpftool-prog-profile command, but more flexible.
perf-stat -b creates per-cpu
perf_eventand loads fentry/fexit BPF programs (monitor-progs) to the target BPF program (target-prog). The monitor-progs readperf_eventbefore and after the target-prog, and aggregate the difference in a BPF map. Then the user space reads data from these maps.Quoted 4 lines
A new struct
bpf_counteris introduced to provide common interface that uses BPF programs/maps to count perf events.Signed-off-by Song Liu <[email protected]>I'm getting this at the end of the compilation:
Quoted 7 lines
Quoted 4 lines
LINK perf
rm/home/jolsa/linux-perf/tools/perf/util/bpf_skel/.tmp/bpf_prog_profiler.bpf.oI guess we can keep it or make it silent somehow
I also noticed this, but haven't figured out how to silent it. I guess we can fix it out later.
I think this might do it:
.PRECIOUS: $(SKEL_TMP_OUT)/%.bpf.o
SNIP
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
tools/perf/util/evsel.c | 11 + tools/perf/util/evsel.h | 6 + tools/perf/util/stat-display.c | 4 +- tools/perf/util/target.c | 34 +- tools/perf/util/target.h | 10 + 11 files changed, 591 insertions(+), 17 deletions(-)create mode 100644
tools/perf/util/bpf_counter.c
create mode 100644tools/perf/util/bpf_counter.h
create mode 100644tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c
Quoted 3 lines
Quoted 1 line
we need man page update, would be great with some example
How about we do this in a follow up patch?
sry, we need to add man changes with new option
thanks,
jirka
Arnaldo Carvalho de Melo
maintainerEm Tue, Dec 08, 2020 at 10:16:44AM -0800, Song Liu escreveu:
Quoted 8 lines
This set introduces perf-stat -b option to count events for BPF programs. This is similar to bpftool-prog-profile. But perf-stat makes it much more flexible.
Changes v2 => v3:
1. Small fixes in Makefile.perf andbpf_counter.c(Jiri)
2. Rebased on top of bpf-next. This is because 1/2 conflicts with some patches in bpftool/Makefile.
Can't you split that up so that I can process the perf part and the bpftool goes via the bpf-next tree?
- Arnaldo
Quoted 47 lines
Changes PATCH v1 => PATCH v2:
1. Various fixes in Makefiles. (Jiri)
2. Fix an build warning/error with gcc-10. (Jiri)Changes RFC v2 => PATCH v1:
1. Support counting on multiple BPF programs.
2. Add BPF handling totarget__validate().
3. ImproveMakefile. (Jiri)Changes RFC v1 => RFC v2:
1. Use bootstrap version of bpftool. (Jiri)
2. Set default to not building bpf skeletons. (Jiri)
3. Remove util/bpf_skel/Makefile, keep all the logic in Makefile.perf.(Jiri)4. Remove dependency to
vmlinux.hin the two skeletons. The goal here is to enable building perf without building kernel (vmlinux) first.
Note: I also removed the logic that buildvmlinux.h. We can add that back when we have to use it (to access big kernel structures).Song Liu (2):
perf: support build BPF skeletons with perf
perf-stat: enable counting events for BPF programstools/bpf/bpftool/Makefile | 3 + tools/build/Makefile.feature | 4 +- tools/perf/Makefile.config | 9 + tools/perf/Makefile.perf | 48 ++- tools/perf/builtin-stat.c | 77 ++++- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 297 ++++++++++++++++++ tools/perf/util/bpf_counter.h | 73 +++++ tools/perf/util/bpf_skel/.gitignore | 3 + .../util/bpf_skel/bpf_prog_profiler.bpf.c | 93 ++++++ tools/perf/util/evsel.c | 11 + tools/perf/util/evsel.h | 6 + tools/perf/util/stat-display.c | 4 +- tools/perf/util/target.c | 34 +- tools/perf/util/target.h | 10 + tools/scripts/Makefile.include | 1 + 16 files changed, 655 insertions(+), 19 deletions(-) create mode 100644 tools/perf/util/bpf_counter.c create mode 100644 tools/perf/util/bpf_counter.h create mode 100644 tools/perf/util/bpf_skel/.gitignore create mode 100644 tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c--
2.24.1
Signature · 1 line
- Arnaldo
Quoted 9 lines · Arnaldo Carvalho de Melo
Em Tue, Dec 08, 2020 at 10:16:44AM -0800, Song Liu escreveu:
Quoted 4 lines
This set introduces perf-stat -b option to count events for BPF programs. This is similar to bpftool-prog-profile. But perf-stat makes it much more flexible.
Quoted 7 lines
Quoted 4 lines
Changes v2 => v3:
1. Small fixes in Makefile.perf andbpf_counter.c(Jiri)
2. Rebased on top of bpf-next. This is because 1/2 conflicts with some patches in bpftool/Makefile.Can't you split that up so that I can process the perf part and the bpftool goes via the bpf-next tree?
Actually, I can make it conflict-less, so we can route everything together. I also put the change to bpftool/Makefile in a separate patch in v4.
Thanks,
Song
Quoted 18 lines · Song Liu
Quoted 15 lines · Arnaldo Carvalho de Melo
Em Tue, Dec 08, 2020 at 10:16:44AM -0800, Song Liu escreveu:
Quoted 8 lines
This set introduces perf-stat -b option to count events for BPF programs. This is similar to bpftool-prog-profile. But perf-stat makes it much more flexible.
Changes v2 => v3:
1. Small fixes in Makefile.perf andbpf_counter.c(Jiri)
2. Rebased on top of bpf-next. This is because 1/2 conflicts with some patches in bpftool/Makefile.
Quoted 5 lines
Quoted 2 lines
Can't you split that up so that I can process the perf part and the bpftool goes via the bpf-next tree?
Actually, I can make it conflict-less, so we can route everything together. I also put the change to bpftool/Makefile in a separate patch in v4.
Well, it is a little tricky. bpf-next renamed the bootstrap version binary, from bpftool-bootstrap to bootstrap/bpftool. So we will need a follow up patch after the two merge.
Song