perf tools: filtering events using eBPF programs
Subsystems (3)
Hi Arnaldo,
I know you don't have enough time to review my code. I send
this patchset to let you and other know what we are working on.
In this new patchset, we create a new perf cmdline syntax so
perf users are able to pass perf events created by perf to BPF maps, which makes bpf_perf_event_read() usable. Compare with out previous solution[1] which embedded 'struct perf_event_attr' to "maps" section, this solution is easier to use.
If you or anyone have any different views on this solution,
please let us know so we can stop our further development base on it as soon as possible.
Thank you.
[1] http://lkml.kernel.org/r/[email protected]
The following changes since commit 31eb4360546b4bd890f349db01295a173c09b0fb:
perf hists browser: Add 'm' key for context menu display (2015-10-12 23:29:14 -0300)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pi3orama/linux.git tags/perf-ebpf-for-acme-20151014
for you to fetch changes up to 6df036cb7d42a2e0ebf312e127b02425bd57bc55:
perf tools: Enable BPF object configure syntax (2015-10-14 10:09:17 +0000)
----------------------------------------------------------------
EBPF support for perf
- Rebase to newest perf/core
- Bugfix: kprobe events not remove if bpf__probe() failure occur after it creates some kprobe points successfully.
- Bugfix: when multiple BPF functions reside in one BPF object, the last BPF program would be attached to all kprobe events. This bug is introduced by removal of dummy event placeholder.
- New function: support BPF program reading counter through bpf_perf_event_read() by adding new syntax and support code in event selector. The new BPF object configuration mechanism can be extended to support BPF data output.
In this patchset, following BPF function can be used to measure cycles a kernel function cost:
===== BPF program bpf_program.c =====
struct bpf_map_def SEC("maps") pmu_map = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u32),
.max_entries = __NR_CPUS__,
};
SEC("func_write=sys_write")
int func_write(void *ctx)
{
unsigned long long val;
char fmt[] = "sys_write: pmu=%llu\n";
val = bpf_perf_event_read(&pmu_map, bpf_get_smp_processor_id());
bpf_trace_printk(fmt, sizeof(fmt), val);
return 0;
}
SEC("func_write_return=sys_write%return")
int func_write_return(void *ctx)
{
unsigned long long val = 0;
char fmt[] = "sys_write_return: pmu=%llu\n";
val = bpf_perf_event_read(&pmu_map, bpf_get_smp_processor_id());
bpf_trace_printk(fmt, sizeof(fmt), val);
return 0;
}With cmdline like this:
===== cmdline =====
# echo "" > /sys/kernel/debug/tracing/trace
# perf record -e evt=cycles/period=0x7fffffffffffffff/ \
-e bpf_program.c/maps.pmu_map.event=evt/
-a ls
# cat /sys/kernel/debug/tracing/trace | grep ls
ls-3363 [003] d... 75475.056190: : sys_write: pmu=3961415
ls-3363 [003] dN.. 75475.056212: : sys_write_return: pmu=4051390
ls-3363 [003] d... 75475.056216: : sys_write: pmu=4065447
ls-3363 [003] dN.. 75475.056227: : sys_write_return: pmu=4109760
ls-3363 [003] d... 75475.056230: : sys_write: pmu=4120776
ls-3363 [003] dN.. 75475.056245: : sys_write_return: pmu=4178441
...
# perf report --stdio
Error:
The perf.data file has no samples!----------------------------------------------------------------
He Kuang (5):
perf tools: Add prologue for BPF programs for fetching arguments perf record: Support custom vmlinux path
bpf tools: Add helper function for updating bpf maps elements perf tools: Support perf event alias name
perf record: Apply config to BPF objects before recording
Wang Nan (26):
perf tools: Make perf depend on libbpf
perf ebpf: Add the libbpf glue
perf tools: Enable passing bpf object file to --event
perf record, bpf: Create probe points for BPF programs
perf record: Load eBPF object into kernel
perf tools: Collect perf_evsel in BPF object files
perf tools: Attach eBPF program to perf event
perf record: Add clang options for compiling BPF scripts
perf tools: Compile scriptlets to BPF objects when passing '.c' to --event perf test: Enforce LLVM test for BPF test
perf test: Add 'perf test BPF'
perf probe: Reset args and nargs for probe_trace_event when failure bpf tools: Load a program with different instances using preprocessor perf tools: Add BPF_PROLOGUE config options for further patches perf tools: Compile dwarf-regs.c if CONFIG_BPF_PROLOGUE is on perf tools: Generate prologue for BPF programs
perf tools: Use same BPF program if arguments are identical perf tools: Allow BPF program attach to uprobe events
perf test: Enforce LLVM test, add kbuild test
perf test: Test BPF prologue
bpf tools: Collect map definition in bpf_object
bpf tools: Extract and collect map names from BPF object file perf tools: Pass available CPU number to clang compiler
perf tools: Add API to config maps in bpf object
perf tools: Add API to apply config to BPF map
perf tools: Enable BPF object configure syntax
tools/build/Makefile.feature | 6 +- tools/lib/bpf/bpf.c | 14 + tools/lib/bpf/bpf.h | 2 + tools/lib/bpf/libbpf.c | 392 +++++++++--- tools/lib/bpf/libbpf.h | 46 ++ tools/perf/MANIFEST | 3 + tools/perf/Makefile.perf | 21 +- tools/perf/arch/x86/util/Build | 1 + tools/perf/builtin-record.c | 21 + tools/perf/config/Makefile | 31 +- tools/perf/perf.c | 2 + tools/perf/tests/Build | 24 +- tools/perf/tests/bpf-script-example.c | 48 ++ tools/perf/tests/bpf-script-test-kbuild.c | 21 + tools/perf/tests/bpf-script-test-prologue.c | 35 ++ tools/perf/tests/bpf.c | 227 +++++++ tools/perf/tests/builtin-test.c | 10 + tools/perf/tests/llvm.c | 210 ++++++- tools/perf/tests/llvm.h | 29 + tools/perf/tests/make | 4 +- tools/perf/tests/tests.h | 5 + tools/perf/util/Build | 2 + tools/perf/util/bpf-loader.c | 933 ++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 148 +++++ tools/perf/util/bpf-prologue.c | 443 +++++++++++++ tools/perf/util/bpf-prologue.h | 34 + tools/perf/util/evlist.c | 16 + tools/perf/util/evlist.h | 4 + tools/perf/util/evsel.c | 17 + tools/perf/util/evsel.h | 2 + tools/perf/util/llvm-utils.c | 24 +- tools/perf/util/parse-events.c | 221 ++++++- tools/perf/util/parse-events.h | 15 + tools/perf/util/parse-events.l | 6 + tools/perf/util/parse-events.y | 55 +- tools/perf/util/probe-finder.c | 4 + 36 files changed, 2968 insertions(+), 108 deletions(-) create mode 100644 tools/perf/tests/bpf-script-example.c create mode 100644 tools/perf/tests/bpf-script-test-kbuild.c create mode 100644 tools/perf/tests/bpf-script-test-prologue.c create mode 100644 tools/perf/tests/bpf.c create mode 100644 tools/perf/tests/llvm.h create mode 100644 tools/perf/util/bpf-loader.c create mode 100644 tools/perf/util/bpf-loader.h create mode 100644 tools/perf/util/bpf-prologue.c create mode 100644 tools/perf/util/bpf-prologue.h
Signature · 1 line
1.8.3.4
PATCH 1/31 perf tools: Make perf depend on libbpf
By adding libbpf into perf's Makefile, this patch enables perf to build libbpf during building if libelf is found and neither NO_LIBELF nor NO_LIBBPF is set. The newly introduced code is similar to libapi and libtraceevent building in Makefile.perf.
MANIFEST is also updated for 'make perf-*-src-pkg'.
Append make_no_libbpf to tools/perf/tests/make.
'bpf' feature check is appended into default FEATURE_TESTS and FEATURE_DISPLAY, so perf will check API version of bpf in /path/to/kernel/include/uapi/linux/bpf.h. Which should not fail except when we are trying to port this code to an old kernel.
Error messages are also updated to notify users about the disable of BPF support of 'perf record' if libelf is missed or BPF API check failed.
tools/lib/bpf is added into TAG_FOLDERS to allow us to navigate on libbpf files when working on perf using tools/perf/tags.
--- tools/build/Makefile.feature | 6 ++++-- tools/perf/MANIFEST | 3 +++ tools/perf/Makefile.perf | 21 +++++++++++++++++++-- tools/perf/config/Makefile | 19 ++++++++++++++++++- tools/perf/tests/make | 4 +++- 5 files changed, 47 insertions(+), 6 deletions(-)
tools/build/Makefile.feature +4 -2
@@ -53,7 +53,8 @@ FEATURE_TESTS ?= \ libdw-dwarf-unwind \ zlib \ lzma \ - get_cpuid + get_cpuid \ + bpf FEATURE_DISPLAY ?= \ dwarf \
@@ -71,7 +72,8 @@ FEATURE_DISPLAY ?= \ libdw-dwarf-unwind \ zlib \ lzma \ - get_cpuid + get_cpuid \ + bpf # Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features. # If in the future we need per-feature checks/flags for features not
tools/perf/MANIFEST +3 -0
@@ -17,6 +17,7 @@ tools/build tools/arch/x86/include/asm/atomic.h tools/arch/x86/include/asm/rmwcc.h tools/lib/traceevent +tools/lib/bpf tools/lib/api tools/lib/bpf tools/lib/hweight.c
@@ -69,6 +70,8 @@ arch/*/lib/memset*.S include/linux/poison.h include/linux/hw_breakpoint.h include/uapi/linux/perf_event.h +include/uapi/linux/bpf.h +include/uapi/linux/bpf_common.h include/uapi/linux/const.h include/uapi/linux/swab.h include/uapi/linux/hw_breakpoint.h
tools/perf/Makefile.perf +19 -2
@@ -75,6 +75,8 @@ include config/utilities.mak # Define NO_LZMA if you do not want to support compressed (xz) kernel modules # # Define NO_AUXTRACE if you do not want AUX area tracing support +# +# Define NO_LIBBPF if you do not want BPF support # As per kernel Makefile, avoid funny character set dependencies unexport LC_ALL
@@ -145,6 +147,7 @@ AWK = awk LIB_DIR = $(srctree)/tools/lib/api/ TRACE_EVENT_DIR = $(srctree)/tools/lib/traceevent/ +BPF_DIR = $(srctree)/tools/lib/bpf/ # include config/Makefile by default and rule out # non-config cases
@@ -180,6 +183,7 @@ strip-libs = $(filter-out -l%,$(1)) ifneq ($(OUTPUT),) TE_PATH=$(OUTPUT) + BPF_PATH=$(OUTPUT) ifneq ($(subdir),) LIB_PATH=$(OUTPUT)/../lib/api/ else
@@ -188,6 +192,7 @@ endif else TE_PATH=$(TRACE_EVENT_DIR) LIB_PATH=$(LIB_DIR) + BPF_PATH=$(BPF_DIR) endif LIBTRACEEVENT = $(TE_PATH)libtraceevent.a
@@ -199,6 +204,8 @@ LIBTRACEEVENT_DYNAMIC_LIST_LDFLAGS = -Xlinker --dynamic-list=$(LIBTRACEEVENT_DYN LIBAPI = $(LIB_PATH)libapi.a export LIBAPI +LIBBPF = $(BPF_PATH)libbpf.a + # python extension build directories PYTHON_EXTBUILD := $(OUTPUT)python_ext_build/ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/
@@ -251,6 +258,9 @@ export PERL_PATH LIB_FILE=$(OUTPUT)libperf.a PERFLIBS = $(LIB_FILE) $(LIBAPI) $(LIBTRACEEVENT) +ifndef NO_LIBBPF + PERFLIBS += $(LIBBPF) +endif # We choose to avoid "if .. else if .. else .. endif endif" # because maintaining the nesting to match is a pain. If
@@ -420,6 +430,13 @@ $(LIBAPI)-clean: $(call QUIET_CLEAN, libapi) $(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) clean >/dev/null +$(LIBBPF): FORCE + $(Q)$(MAKE) -C $(BPF_DIR) O=$(OUTPUT) $(OUTPUT)libbpf.a + +$(LIBBPF)-clean: + $(call QUIET_CLEAN, libbpf) + $(Q)$(MAKE) -C $(BPF_DIR) O=$(OUTPUT) clean >/dev/null + help: @echo 'Perf make targets:' @echo ' doc - make *all* documentation (see below)'
@@ -459,7 +476,7 @@ INSTALL_DOC_TARGETS += quick-install-doc quick-install-man quick-install-html $(DOC_TARGETS): $(QUIET_SUBDIR0)Documentation $(QUIET_SUBDIR1) $(@:doc=all) -TAG_FOLDERS= . ../lib/traceevent ../lib/api ../lib/symbol ../include +TAG_FOLDERS= . ../lib/traceevent ../lib/api ../lib/symbol ../include ../lib/bpf TAG_FILES= ../../include/uapi/linux/perf_event.h TAGS:
@@ -567,7 +584,7 @@ config-clean: $(call QUIET_CLEAN, config) $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null -clean: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean config-clean +clean: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean $(LIBBPF)-clean config-clean $(call QUIET_CLEAN, core-objs) $(RM) $(LIB_FILE) $(OUTPUT)perf-archive $(OUTPUT)perf-with-kcore $(LANG_BINDINGS) $(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete $(Q)$(RM) $(OUTPUT).config-detected
tools/perf/config/Makefile +18 -1
@@ -106,6 +106,7 @@ ifdef LIBBABELTRACE FEATURE_CHECK_LDFLAGS-libbabeltrace := $(LIBBABELTRACE_LDFLAGS) -lbabeltrace-ctf endif +FEATURE_CHECK_CFLAGS-bpf = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi # include ARCH specific config -include $(src-perf)/arch/$(ARCH)/Makefile
@@ -237,6 +238,7 @@ ifdef NO_LIBELF NO_DEMANGLE := 1 NO_LIBUNWIND := 1 NO_LIBDW_DWARF_UNWIND := 1 + NO_LIBBPF := 1 else ifeq ($(feature-libelf), 0) ifeq ($(feature-glibc), 1)
@@ -246,13 +248,14 @@ else LIBC_SUPPORT := 1 endif ifeq ($(LIBC_SUPPORT),1) - msg := $(warning No libelf found, disables 'probe' tool, please install elfutils-libelf-devel/libelf-dev); + msg := $(warning No libelf found, disables 'probe' tool and BPF support in 'perf record', please install elfutils-libelf-devel/libelf-dev); NO_LIBELF := 1 NO_DWARF := 1 NO_DEMANGLE := 1 NO_LIBUNWIND := 1 NO_LIBDW_DWARF_UNWIND := 1 + NO_LIBBPF := 1 else ifneq ($(filter s% -static%,$(LDFLAGS),),) msg := $(error No static glibc found, please install glibc-static);
@@ -309,6 +312,13 @@ ifndef NO_LIBELF $(call detected,CONFIG_DWARF) endif # PERF_HAVE_DWARF_REGS endif # NO_DWARF + + ifndef NO_LIBBPF + ifeq ($(feature-bpf), 1) + CFLAGS += -DHAVE_LIBBPF_SUPPORT + $(call detected,CONFIG_LIBBPF) + endif + endif # NO_LIBBPF endif # NO_LIBELF ifeq ($(ARCH),powerpc)
@@ -324,6 +334,13 @@ ifndef NO_LIBUNWIND endif endif +ifndef NO_LIBBPF + ifneq ($(feature-bpf), 1) + msg := $(warning BPF API too old. Please install recent kernel headers. BPF support in 'perf record' is disabled.) + NO_LIBBPF := 1 + endif +endif + dwarf-post-unwind := 1 dwarf-post-unwind-text := BUG
tools/perf/tests/make +3 -1
@@ -44,6 +44,7 @@ make_no_libnuma := NO_LIBNUMA=1 make_no_libaudit := NO_LIBAUDIT=1 make_no_libbionic := NO_LIBBIONIC=1 make_no_auxtrace := NO_AUXTRACE=1 +make_no_libbpf := NO_LIBBPF=1 make_tags := tags make_cscope := cscope make_help := help
@@ -66,7 +67,7 @@ make_static := LDFLAGS=-static make_minimal := NO_LIBPERL=1 NO_LIBPYTHON=1 NO_NEWT=1 NO_GTK2=1 make_minimal += NO_DEMANGLE=1 NO_LIBELF=1 NO_LIBUNWIND=1 NO_BACKTRACE=1 make_minimal += NO_LIBNUMA=1 NO_LIBAUDIT=1 NO_LIBBIONIC=1 -make_minimal += NO_LIBDW_DWARF_UNWIND=1 NO_AUXTRACE=1 +make_minimal += NO_LIBDW_DWARF_UNWIND=1 NO_AUXTRACE=1 NO_LIBBPF=1 # $(run) contains all available tests run := make_pure
@@ -94,6 +95,7 @@ run += make_no_libnuma run += make_no_libaudit run += make_no_libbionic run += make_no_auxtrace +run += make_no_libbpf run += make_help run += make_doc run += make_perf_o
Signature · 1 line
1.8.3.4
tip-bot for Wang Nan
merge bot[tip:perf/core] perf tools: Make perf depend on libbpf
perf tools: Make perf depend on libbpf
Commit-ID: ed63f34c026e9a60d17fa750ecdfe3f600d49393 Gitweb: http://git.kernel.org/tip/ed63f34c026e9a60d17fa750ecdfe3f600d49393 Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:12 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 28 Oct 2015 12:48:12 -0300
perf tools: Make perf depend on libbpf
By adding libbpf into perf's Makefile, this patch enables perf to build libbpf if libelf is found and neither NO_LIBELF nor NO_LIBBPF is set.
The newly introduced code is similar to how libapi and libtraceevent are wired into Makefile.perf.
MANIFEST is also updated for 'make perf-*-src-pkg'.
Append make_no_libbpf to tools/perf/tests/make.
The 'bpf' feature check is appended into default FEATURE_TESTS and FEATURE_DISPLAY, so perf will check the API version of bpf in /path/to/kernel/include/uapi/linux/bpf.h. Which should not fail except when we are trying to port this code to an old kernel.
Error messages are also updated to notify users about the lack of BPF support in 'perf record' if libelf is missing or the BPF API check failed.
tools/lib/bpf is added to TAG_FOLDERS to allow us to navigate libbpf files when working on perf using tools/perf/tags.
--- tools/build/Makefile.feature | 6 ++++-- tools/perf/MANIFEST | 3 +++ tools/perf/Makefile.perf | 21 +++++++++++++++++++-- tools/perf/config/Makefile | 19 ++++++++++++++++++- tools/perf/tests/make | 4 +++- 5 files changed, 47 insertions(+), 6 deletions(-)
tools/build/Makefile.feature +4 -2
@@ -53,7 +53,8 @@ FEATURE_TESTS ?= \ libdw-dwarf-unwind \ zlib \ lzma \ - get_cpuid + get_cpuid \ + bpf FEATURE_DISPLAY ?= \ dwarf \
@@ -71,7 +72,8 @@ FEATURE_DISPLAY ?= \ libdw-dwarf-unwind \ zlib \ lzma \ - get_cpuid + get_cpuid \ + bpf # Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features. # If in the future we need per-feature checks/flags for features not
tools/perf/MANIFEST +3 -0
@@ -17,6 +17,7 @@ tools/build tools/arch/x86/include/asm/atomic.h tools/arch/x86/include/asm/rmwcc.h tools/lib/traceevent +tools/lib/bpf tools/lib/api tools/lib/bpf tools/lib/hweight.c
@@ -69,6 +70,8 @@ arch/*/lib/memset*.S include/linux/poison.h include/linux/hw_breakpoint.h include/uapi/linux/perf_event.h +include/uapi/linux/bpf.h +include/uapi/linux/bpf_common.h include/uapi/linux/const.h include/uapi/linux/swab.h include/uapi/linux/hw_breakpoint.h
tools/perf/Makefile.perf +19 -2
@@ -75,6 +75,8 @@ include config/utilities.mak # Define NO_LZMA if you do not want to support compressed (xz) kernel modules # # Define NO_AUXTRACE if you do not want AUX area tracing support +# +# Define NO_LIBBPF if you do not want BPF support # As per kernel Makefile, avoid funny character set dependencies unexport LC_ALL
@@ -145,6 +147,7 @@ AWK = awk LIB_DIR = $(srctree)/tools/lib/api/ TRACE_EVENT_DIR = $(srctree)/tools/lib/traceevent/ +BPF_DIR = $(srctree)/tools/lib/bpf/ # include config/Makefile by default and rule out # non-config cases
@@ -180,6 +183,7 @@ strip-libs = $(filter-out -l%,$(1)) ifneq ($(OUTPUT),) TE_PATH=$(OUTPUT) + BPF_PATH=$(OUTPUT) ifneq ($(subdir),) LIB_PATH=$(OUTPUT)/../lib/api/ else
@@ -188,6 +192,7 @@ endif else TE_PATH=$(TRACE_EVENT_DIR) LIB_PATH=$(LIB_DIR) + BPF_PATH=$(BPF_DIR) endif LIBTRACEEVENT = $(TE_PATH)libtraceevent.a
@@ -199,6 +204,8 @@ LIBTRACEEVENT_DYNAMIC_LIST_LDFLAGS = -Xlinker --dynamic-list=$(LIBTRACEEVENT_DYN LIBAPI = $(LIB_PATH)libapi.a export LIBAPI +LIBBPF = $(BPF_PATH)libbpf.a + # python extension build directories PYTHON_EXTBUILD := $(OUTPUT)python_ext_build/ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/
@@ -251,6 +258,9 @@ export PERL_PATH LIB_FILE=$(OUTPUT)libperf.a PERFLIBS = $(LIB_FILE) $(LIBAPI) $(LIBTRACEEVENT) +ifndef NO_LIBBPF + PERFLIBS += $(LIBBPF) +endif # We choose to avoid "if .. else if .. else .. endif endif" # because maintaining the nesting to match is a pain. If
@@ -420,6 +430,13 @@ $(LIBAPI)-clean: $(call QUIET_CLEAN, libapi) $(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) clean >/dev/null +$(LIBBPF): FORCE + $(Q)$(MAKE) -C $(BPF_DIR) O=$(OUTPUT) $(OUTPUT)libbpf.a + +$(LIBBPF)-clean: + $(call QUIET_CLEAN, libbpf) + $(Q)$(MAKE) -C $(BPF_DIR) O=$(OUTPUT) clean >/dev/null + help: @echo 'Perf make targets:' @echo ' doc - make *all* documentation (see below)'
@@ -459,7 +476,7 @@ INSTALL_DOC_TARGETS += quick-install-doc quick-install-man quick-install-html $(DOC_TARGETS): $(QUIET_SUBDIR0)Documentation $(QUIET_SUBDIR1) $(@:doc=all) -TAG_FOLDERS= . ../lib/traceevent ../lib/api ../lib/symbol ../include +TAG_FOLDERS= . ../lib/traceevent ../lib/api ../lib/symbol ../include ../lib/bpf TAG_FILES= ../../include/uapi/linux/perf_event.h TAGS:
@@ -567,7 +584,7 @@ config-clean: $(call QUIET_CLEAN, config) $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null -clean: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean config-clean +clean: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean $(LIBBPF)-clean config-clean $(call QUIET_CLEAN, core-objs) $(RM) $(LIB_FILE) $(OUTPUT)perf-archive $(OUTPUT)perf-with-kcore $(LANG_BINDINGS) $(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete $(Q)$(RM) $(OUTPUT).config-detected
tools/perf/config/Makefile +18 -1
@@ -106,6 +106,7 @@ ifdef LIBBABELTRACE FEATURE_CHECK_LDFLAGS-libbabeltrace := $(LIBBABELTRACE_LDFLAGS) -lbabeltrace-ctf endif +FEATURE_CHECK_CFLAGS-bpf = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi # include ARCH specific config -include $(src-perf)/arch/$(ARCH)/Makefile
@@ -237,6 +238,7 @@ ifdef NO_LIBELF NO_DEMANGLE := 1 NO_LIBUNWIND := 1 NO_LIBDW_DWARF_UNWIND := 1 + NO_LIBBPF := 1 else ifeq ($(feature-libelf), 0) ifeq ($(feature-glibc), 1)
@@ -246,13 +248,14 @@ else LIBC_SUPPORT := 1 endif ifeq ($(LIBC_SUPPORT),1) - msg := $(warning No libelf found, disables 'probe' tool, please install elfutils-libelf-devel/libelf-dev); + msg := $(warning No libelf found, disables 'probe' tool and BPF support in 'perf record', please install elfutils-libelf-devel/libelf-dev); NO_LIBELF := 1 NO_DWARF := 1 NO_DEMANGLE := 1 NO_LIBUNWIND := 1 NO_LIBDW_DWARF_UNWIND := 1 + NO_LIBBPF := 1 else ifneq ($(filter s% -static%,$(LDFLAGS),),) msg := $(error No static glibc found, please install glibc-static);
@@ -309,6 +312,13 @@ ifndef NO_LIBELF $(call detected,CONFIG_DWARF) endif # PERF_HAVE_DWARF_REGS endif # NO_DWARF + + ifndef NO_LIBBPF + ifeq ($(feature-bpf), 1) + CFLAGS += -DHAVE_LIBBPF_SUPPORT + $(call detected,CONFIG_LIBBPF) + endif + endif # NO_LIBBPF endif # NO_LIBELF ifeq ($(ARCH),powerpc)
@@ -324,6 +334,13 @@ ifndef NO_LIBUNWIND endif endif +ifndef NO_LIBBPF + ifneq ($(feature-bpf), 1) + msg := $(warning BPF API too old. Please install recent kernel headers. BPF support in 'perf record' is disabled.) + NO_LIBBPF := 1 + endif +endif + dwarf-post-unwind := 1 dwarf-post-unwind-text := BUG
tools/perf/tests/make +3 -1
@@ -44,6 +44,7 @@ make_no_libnuma := NO_LIBNUMA=1 make_no_libaudit := NO_LIBAUDIT=1 make_no_libbionic := NO_LIBBIONIC=1 make_no_auxtrace := NO_AUXTRACE=1 +make_no_libbpf := NO_LIBBPF=1 make_tags := tags make_cscope := cscope make_help := help
@@ -66,7 +67,7 @@ make_static := LDFLAGS=-static make_minimal := NO_LIBPERL=1 NO_LIBPYTHON=1 NO_NEWT=1 NO_GTK2=1 make_minimal += NO_DEMANGLE=1 NO_LIBELF=1 NO_LIBUNWIND=1 NO_BACKTRACE=1 make_minimal += NO_LIBNUMA=1 NO_LIBAUDIT=1 NO_LIBBIONIC=1 -make_minimal += NO_LIBDW_DWARF_UNWIND=1 NO_AUXTRACE=1 +make_minimal += NO_LIBDW_DWARF_UNWIND=1 NO_AUXTRACE=1 NO_LIBBPF=1 # $(run) contains all available tests run := make_pure
@@ -94,6 +95,7 @@ run += make_no_libnuma run += make_no_libaudit run += make_no_libbionic run += make_no_auxtrace +run += make_no_libbpf run += make_help run += make_doc run += make_perf_o
PATCH 2/31 perf ebpf: Add the libbpf glue
In reply to Wang NanThe 'bpf-loader.[ch]' files are introduced in this patch. Which will be the interface between perf and libbpf. bpf__prepare_load() resides in bpf-loader.c. Following patches will enrich these two files.
--- tools/perf/util/bpf-loader.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 29 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tools/perf/util/bpf-loader.c create mode 100644 tools/perf/util/bpf-loader.h
tools/perf/util/bpf-loader.c new file +57 -0
@@ -0,0 +1,57 @@ +/* +* bpf-loader.c +* +* Copyright (C) 2015 Wang Nan <[email protected]> +* Copyright (C) 2015 Huawei Inc. +*/ + +#include <bpf/libbpf.h> +#include <linux/err.h> +#include "perf.h" +#include "debug.h" +#include "bpf-loader.h" + +#define DEFINE_PRINT_FN (name, level) \ +static int libbpf_##name(const char *fmt, ...) \ +{ \ + va_list args; \ + int ret; \ + \ + va_start(args, fmt); \ + ret = veprintf(level, verbose, pr_fmt(fmt), args);\ + va_end(args); \ + return ret; \ +} + +DEFINE_PRINT_FN(warning, 0) +DEFINE_PRINT_FN(info, 0) +DEFINE_PRINT_FN(debug, 1) + +struct bpf_object *bpf__prepare_load (const char *filename) +{ +struct bpf_object *obj; +static bool libbpf_initialized; + +if (!libbpf_initialized) { +libbpf_set_print (libbpf_warning, + libbpf_info, + libbpf_debug); + libbpf_initialized = true; + } + + obj =bpf_object__open (filename); +if (!obj) { +pr_debug ("bpf: failed to load %s\n" , filename); +return ERR_PTR (-EINVAL ); + } + +return obj; +} + +void bpf__clear (void ) +{ +struct bpf_object *obj, *tmp; + +bpf_object__for_each_safe (obj, tmp) +bpf_object__close (obj); +}
tools/perf/util/bpf-loader.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* Copyright (C) 2015, Wang Nan <[email protected]> +* Copyright (C) 2015, Huawei Inc. +*/ +#ifndef __BPF_LOADER_H +#define __BPF_LOADER_H + +#include <linux/compiler.h> +#include <linux/err.h> +#include <string.h> +#include "debug.h" + +struct bpf_object ; + +#ifdef HAVE_LIBBPF_SUPPORT +struct bpf_object *bpf__prepare_load (const char *filename); + +void bpf__clear (void ); +#else +static inline struct bpf_object * +bpf__prepare_load (const char *filename__maybe_unused ) +{ +pr_debug ("ERROR: eBPF object loading is disabled during compiling.\n" ); +return ERR_PTR (-ENOTSUP ); +} + +static inline void bpf__clear (void ) { } +#endif +#endif
Signature · 1 line
1.8.3.4
tip-bot for Wang Nan
merge bot[tip:perf/core] perf ebpf: Add the libbpf glue
perf ebpf: Add the libbpf glue
Commit-ID: 69d262a93a25cf475012ea2e00aeb29f4932c028 Gitweb: http://git.kernel.org/tip/69d262a93a25cf475012ea2e00aeb29f4932c028 Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:13 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 28 Oct 2015 12:48:12 -0300
perf ebpf: Add the libbpf glue
The 'bpf-loader.[ch]' files are introduced in this patch. Which will be the interface between perf and libbpf. bpf__prepare_load() resides in bpf-loader.c. Following patches will enrich these two files.
--- tools/perf/util/bpf-loader.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 29 ++++++++++++++++++++++ 2 files changed, 86 insertions(+)
tools/perf/util/bpf-loader.c new file +57 -0
@@ -0,0 +1,57 @@ +/* +* bpf-loader.c +* +* Copyright (C) 2015 Wang Nan <[email protected]> +* Copyright (C) 2015 Huawei Inc. +*/ + +#include <bpf/libbpf.h> +#include <linux/err.h> +#include "perf.h" +#include "debug.h" +#include "bpf-loader.h" + +#define DEFINE_PRINT_FN (name, level) \ +static int libbpf_##name(const char *fmt, ...) \ +{ \ + va_list args; \ + int ret; \ + \ + va_start(args, fmt); \ + ret = veprintf(level, verbose, pr_fmt(fmt), args);\ + va_end(args); \ + return ret; \ +} + +DEFINE_PRINT_FN(warning, 0) +DEFINE_PRINT_FN(info, 0) +DEFINE_PRINT_FN(debug, 1) + +struct bpf_object *bpf__prepare_load (const char *filename) +{ +struct bpf_object *obj; +static bool libbpf_initialized; + +if (!libbpf_initialized) { +libbpf_set_print (libbpf_warning, + libbpf_info, + libbpf_debug); + libbpf_initialized = true; + } + + obj =bpf_object__open (filename); +if (!obj) { +pr_debug ("bpf: failed to load %s\n" , filename); +return ERR_PTR (-EINVAL ); + } + +return obj; +} + +void bpf__clear (void ) +{ +struct bpf_object *obj, *tmp; + +bpf_object__for_each_safe (obj, tmp) +bpf_object__close (obj); +}
tools/perf/util/bpf-loader.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* Copyright (C) 2015, Wang Nan <[email protected]> +* Copyright (C) 2015, Huawei Inc. +*/ +#ifndef __BPF_LOADER_H +#define __BPF_LOADER_H + +#include <linux/compiler.h> +#include <linux/err.h> +#include <string.h> +#include "debug.h" + +struct bpf_object ; + +#ifdef HAVE_LIBBPF_SUPPORT +struct bpf_object *bpf__prepare_load (const char *filename); + +void bpf__clear (void ); +#else +static inline struct bpf_object * +bpf__prepare_load (const char *filename__maybe_unused ) +{ +pr_debug ("ERROR: eBPF object loading is disabled during compiling.\n" ); +return ERR_PTR (-ENOTSUP ); +} + +static inline void bpf__clear (void ) { } +#endif +#endif
PATCH 3/31 perf tools: Enable passing bpf object file to --event
In reply to Wang NanBy introducing new rules in tools/perf/util/parse-events.[ly], this patch enables 'perf record --event bpf_file.o' to select events by an eBPF object file. It calls parse_events_load_bpf() to load that file, which uses bpf__prepare_load() and finally calls bpf_object__open() for the object files.
After applying this patch, commands like:
# perf record --event foo.o sleep
become possible.
However, at this point it is unable to link any useful things onto the evsel list because the creating of probe points and BPF program attaching have not been implemented. Before real events are possible to be extracted, to avoid perf report error because of empty evsel list, this patch link a dummy evsel. The dummy event related code will be removed when probing and extracting code is ready.
--- tools/perf/perf.c | 2 ++ tools/perf/util/Build | 1 + tools/perf/util/parse-events.c | 57 ++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/parse-events.h | 8 ++++++ tools/perf/util/parse-events.l | 3 +++ tools/perf/util/parse-events.y | 18 ++++++++++++- 6 files changed, 88 insertions(+), 1 deletion(-)
tools/perf/perf.c +2 -0
@@ -15,6 +15,7 @@#include "util/run-command.h" #include "util/parse-events.h" #include "util/parse-options.h" +#include "util/bpf-loader.h" #include "util/debug.h" #include <api/fs/tracing_path.h> #include <pthread.h>
@@ -385,6 +386,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv) status = p->fn (argc, argv, prefix);exit_browser (status);perf_env__exit (&perf_env); +bpf__clear ();if (status)return status &0xff ;
tools/perf/util/Build +1 -0
@@ -87,6 +87,7 @@ libperf-$(CONFIG_AUXTRACE) += intel-bts.o libperf-y += parse-branch-options.o libperf-y += parse-regs-options.o +libperf-$(CONFIG_LIBBPF) += bpf-loader.o libperf-$(CONFIG_LIBELF) += symbol-elf.o libperf-$(CONFIG_LIBELF) += probe-file.o libperf-$(CONFIG_LIBELF) += probe-event.o
tools/perf/util/parse-events.c +57 -0
69 lines not on this page · raw message
tools/perf/util/parse-events.h +8 -0
@@ -121,6 +121,14 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, char *sys, char *event, struct parse_events_error *error, struct list_head *head_config); +int parse_events_load_bpf (struct parse_events_evlist *data, +struct list_head *list, +char *bpf_file_name); +/* Provide this function for perf test */ +struct bpf_object ; +int parse_events_load_bpf_obj (struct parse_events_evlist *data, +struct list_head *list, +struct bpf_object *obj); int parse_events_add_numeric(struct parse_events_evlist *data, struct list_head *list, u32 type, u64 config,
tools/perf/util/parse-events.l +3 -0
@@ -115,6 +115,7 @@ do { \ group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ +bpf_object .*\.(o|bpf) num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+
@@ -159,6 +160,7 @@ modifier_bp [rwx]{1,3} } {event_pmu} | +{bpf_object} | {event} { BEGIN(INITIAL); REWIND(1);
@@ -264,6 +266,7 @@ r{num_raw_hex} { return raw(yyscanner); } {num_hex} { return value(yyscanner, 16); } {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } +{bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } {name} { return pmu_str_check(yyscanner); } "/" { BEGIN(config); return '/'; } - { return '-'; }
tools/perf/util/parse-events.y +17 -1
@@ -42,6 +42,7 @@ static inc_group_count(struct list_head *list, %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM %token PE_EVENT_NAME %token PE_NAME +%token PE_BPF_OBJECT %token PE_MODIFIER_EVENT PE_MODIFIER_BP %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
@@ -53,6 +54,7 @@ static inc_group_count(struct list_head *list, %type <num> PE_RAW %type <num> PE_TERM %type <str> PE_NAME +%type <str> PE_BPF_OBJECT %type <str> PE_NAME_CACHE_TYPE %type <str> PE_NAME_CACHE_OP_RESULT %type <str> PE_MODIFIER_EVENT
@@ -70,6 +72,7 @@ static inc_group_count(struct list_head *list, %type <tracepoint_name> tracepoint_name %type <head> event_legacy_numeric %type <head> event_legacy_raw +%type <head> event_bpf_file %type <head> event_def %type <head> event_mod %type <head> event_name
@@ -203,7 +206,8 @@ event_def: event_pmu | event_legacy_mem | event_legacy_tracepoint sep_dc | event_legacy_numeric sep_dc | - event_legacy_raw sep_dc + event_legacy_raw sep_dc | + event_bpf_file event_pmu: PE_NAME '/' event_config '/'
@@ -449,6 +453,18 @@ PE_RAW $$ = list; } +event_bpf_file: +PE_BPF_OBJECT +{ + struct parse_events_evlist *data = _data; + struct parse_events_error *error = data->error; + struct list_head *list; + + ALLOC_LIST(list); + ABORT_ON(parse_events_load_bpf(data, list, $1)); + $$ = list; +} + start_terms: event_config { struct parse_events_terms *data = _data;
Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 03/31] perf tools: Enable passing bpf object file to --event
Em Wed, Oct 14, 2015 at 12:41:14PM +0000, Wang Nan escreveu:
Quoted 11 lines
By introducing new rules in tools/perf/util/parse-events.[ly], this patch enables 'perf record --event bpf_file.o' to select events by an eBPF object file. It calls
parse_events_load_bpf()to load that file, which usesbpf__prepare_load()and finally callsbpf_object__open()for the object files.After applying this patch, commands like:
# perf record --event foo.o sleepbecome possible.
So, trying the above command I get almost perfect output:
[root@felicio ~]# perf record --event foo.o sleep
libbpf: failed to open foo.o: No such file or directory event syntax error: 'foo.o'
\___ BPF object file 'foo.o' is invalid
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#
Good thing would be to not have any message from libbpf and the right error message from the parser, i.e. the first three lines become these two:
event syntax error: 'foo.o'
\___ BPF object file 'foo.o' not found.o
But that can be fixed up in an upcoming patch, so I am applying this one now in my new attempt at processing this patchkit.
- Arnaldo
Quoted 244 lines
However, at this point it is unable to link any useful things onto the evsel list because the creating of probe points and BPF program attaching have not been implemented. Before real events are possible to be extracted, to avoid perf report error because of empty evsel list, this patch link a dummy evsel. The dummy event related code will be removed when probing and extracting code is ready.
Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>--- tools/perf/perf.c | 2 ++ tools/perf/util/Build | 1 + tools/perf/util/parse-events.c | 57 ++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/parse-events.h | 8 ++++++ tools/perf/util/parse-events.l | 3 +++ tools/perf/util/parse-events.y | 18 ++++++++++++- 6 files changed, 88 insertions(+), 1 deletion(-)tools/perf/perf.c +2
-0@@ -15,6 +15,7 @@#include "util/run-command.h" #include "util/parse-events.h" #include "util/parse-options.h" +#include "util/bpf-loader.h" #include "util/debug.h" #include <api/fs/tracing_path.h> #include <pthread.h> @@ -385,6 +386,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv) status = p->fn (argc, argv, prefix);exit_browser (status);perf_env__exit (&perf_env); +bpf__clear ();if (status)return status &0xff ;tools/perf/util/Build +1
-0@@ -87,6 +87,7 @@ libperf-$(CONFIG_AUXTRACE) += intel-bts.o libperf-y += parse-branch-options.o libperf-y += parse-regs-options.o +libperf-$(CONFIG_LIBBPF) += bpf-loader.o libperf-$(CONFIG_LIBELF) += symbol-elf.o libperf-$(CONFIG_LIBELF) += probe-file.o libperf-$(CONFIG_LIBELF) += probe-event.otools/perf/util/parse-events.c +57
-0@@ -11,6 +11,7 @@#include "symbol.h" #include "cache.h" #include "header.h" +#include "bpf-loader.h" #include "debug.h" #include <api/fs/tracing_path.h> #include "parse-events-bison.h" @@ -529,6 +530,62 @@ static int add_tracepoint_multi_sys(struct list_head *list, int *idx,return ret; } +int parse_events_load_bpf_obj (struct parse_events_evlist *data, +struct list_head *list, +struct bpf_object *obj) +{ +int err; +char errbuf[BUFSIZ ]; + +if (IS_ERR (obj) || !obj) { +snprintf (errbuf,sizeof (errbuf), +"Internal error: load bpf obj with NULL" ); + err = -EINVAL ; + goto errout; + } + +/* +* Temporary add a dummy event here so we can check whether +* basic bpf loader works. Following patches will replace +* dummy event by useful evsels. +*/ +return parse_events_add_numeric (data, list,PERF_TYPE_SOFTWARE , +PERF_COUNT_SW_DUMMY ,NULL ); +errout: + data->error->help =strdup ("(add -v to see detail)" ); + data->error->str =strdup (errbuf); +return err; +} + +int parse_events_load_bpf (struct parse_events_evlist *data, +struct list_head *list, +char *bpf_file_name) +{ +struct bpf_object *obj; + + obj =bpf__prepare_load (bpf_file_name); +if (IS_ERR (obj) || !obj) { +char errbuf[BUFSIZ ]; +int err; + + err = obj ?PTR_ERR (obj) : -EINVAL ; + +if (err == -ENOTSUP ) +snprintf (errbuf,sizeof (errbuf), +"BPF support is not compiled" ); +else +snprintf (errbuf,sizeof (errbuf), +"BPF object file '%s' is invalid" , + bpf_file_name); + + data->error->help =strdup ("(add -v to see detail)" ); + data->error->str =strdup (errbuf); +return err; + } + +return parse_events_load_bpf_obj (data, list, obj); +} +static int parse_breakpoint_type (const char *type,struct perf_event_attr *attr) {tools/perf/util/parse-events.h +8
-0@@ -121,6 +121,14 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, char *sys, char *event, struct parse_events_error *error, struct list_head *head_config); +int parse_events_load_bpf (struct parse_events_evlist *data, +struct list_head *list, +char *bpf_file_name); +/* Provide this function for perf test */ +struct bpf_object ; +int parse_events_load_bpf_obj (struct parse_events_evlist *data, +struct list_head *list, +struct bpf_object *obj); int parse_events_add_numeric(struct parse_events_evlist *data, struct list_head *list, u32 type, u64 config,tools/perf/util/parse-events.l +3
-0@@ -115,6 +115,7 @@ do { \ group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ +bpf_object .*\.(o|bpf) num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+@@ -159,6 +160,7 @@ modifier_bp [rwx]{1,3} } {event_pmu} | +{bpf_object} | {event} { BEGIN(INITIAL); REWIND(1);@@ -264,6 +266,7 @@ r{num_raw_hex} { return raw(yyscanner); } {num_hex} { return value(yyscanner, 16); } {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } +{bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } {name} { return pmu_str_check(yyscanner); } "/" { BEGIN(config); return '/'; } - { return '-'; }tools/perf/util/parse-events.y +17
-1@@ -42,6 +42,7 @@ static inc_group_count(struct list_head *list, %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM %token PE_EVENT_NAME %token PE_NAME +%token PE_BPF_OBJECT %token PE_MODIFIER_EVENT PE_MODIFIER_BP %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP@@ -53,6 +54,7 @@ static inc_group_count(struct list_head *list, %type <num> PE_RAW %type <num> PE_TERM %type <str> PE_NAME +%type <str> PE_BPF_OBJECT %type <str> PE_NAME_CACHE_TYPE %type <str> PE_NAME_CACHE_OP_RESULT %type <str> PE_MODIFIER_EVENT@@ -70,6 +72,7 @@ static inc_group_count(struct list_head *list, %type <tracepoint_name> tracepoint_name %type <head> event_legacy_numeric %type <head> event_legacy_raw +%type <head> event_bpf_file %type <head> event_def %type <head> event_mod %type <head> event_name@@ -203,7 +206,8 @@ event_def: event_pmu | event_legacy_mem | event_legacy_tracepoint sep_dc | event_legacy_numeric sep_dc | - event_legacy_raw sep_dc + event_legacy_raw sep_dc | + event_bpf_file event_pmu: PE_NAME '/' event_config '/'@@ -449,6 +453,18 @@ PE_RAW $$ = list; } +event_bpf_file: +PE_BPF_OBJECT +{ + struct parse_events_evlist *data = _data; + struct parse_events_error *error = data->error; + struct list_head *list; + + ALLOC_LIST(list); + ABORT_ON(parse_events_load_bpf(data, list, $1)); + $$ = list; +} + start_terms: event_config { struct parse_events_terms *data = _data;Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 03/31] perf tools: Enable passing bpf object file to --event
Em Tue, Oct 20, 2015 at 12:12:55PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 34 lines
Em Wed, Oct 14, 2015 at 12:41:14PM +0000, Wang Nan escreveu:
Quoted 11 lines
By introducing new rules in tools/perf/util/parse-events.[ly], this patch enables 'perf record --event bpf_file.o' to select events by an eBPF object file. It calls
parse_events_load_bpf()to load that file, which usesbpf__prepare_load()and finally callsbpf_object__open()for the object files.After applying this patch, commands like:
# perf record --event foo.o sleepbecome possible.
So, trying the above command I get almost perfect output:
[root@felicio ~]# perf record --event foo.o sleep
libbpf: failed to open foo.o: No such file or directory event syntax error: 'foo.o'
\___ BPF object file 'foo.o' is invalid(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#Good thing would be to not have any message from libbpf and the right error message from the parser, i.e. the first three lines become these two:
Quoted 5 lines
event syntax error: 'foo.o'
\___ BPF object file 'foo.o' not found.oBut that can be fixed up in an upcoming patch, so I am applying this one now in my new attempt at processing this patchkit.
Ditto for:
[acme@felicio linux]$ perf record --event /tmp/build/perf/perf.o sleep
libbpf: /tmp/build/perf/perf.o is not an eBPF object file
event syntax error: '/tmp/build/perf/perf.o'
\___ BPF object file '/tmp/build/perf/perf.o' is invalid
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
[acme@felicio linux]$
Now trying to find a _valid_ ebpf object file to test with.
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 03/31] perf tools: Enable passing bpf object file to --event
Em Tue, Oct 20, 2015 at 12:15:58PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 51 lines
Em Tue, Oct 20, 2015 at 12:12:55PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 34 lines
Em Wed, Oct 14, 2015 at 12:41:14PM +0000, Wang Nan escreveu:
Quoted 11 lines
By introducing new rules in tools/perf/util/parse-events.[ly], this patch enables 'perf record --event bpf_file.o' to select events by an eBPF object file. It calls
parse_events_load_bpf()to load that file, which usesbpf__prepare_load()and finally callsbpf_object__open()for the object files.After applying this patch, commands like:
# perf record --event foo.o sleepbecome possible.
So, trying the above command I get almost perfect output:
[root@felicio ~]# perf record --event foo.o sleep
libbpf: failed to open foo.o: No such file or directory event syntax error: 'foo.o'
\___ BPF object file 'foo.o' is invalid(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#Good thing would be to not have any message from libbpf and the right error message from the parser, i.e. the first three lines become these two:
Quoted 5 lines
event syntax error: 'foo.o'
\___ BPF object file 'foo.o' not found.oBut that can be fixed up in an upcoming patch, so I am applying this one now in my new attempt at processing this patchkit.
Ditto for:
[acme@felicio linux]$ perf record --event
/tmp/build/perf/perf.osleep
libbpf:/tmp/build/perf/perf.ois not an eBPF object file
event syntax error: '/tmp/build/perf/perf.o'
\___ BPF object file '/tmp/build/perf/perf.o' is invalid(add -v to see detail)
Run 'perf list' for a list of valid events
Quoted 7 lines
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[acme@felicio linux]$Now trying to find a _valid_ ebpf object file to test with.
Managed after running:
perf test LLVM
copy'n'pasting the output of those "set env" lines, replacing it with export, etc to get to:
[acme@felicio linux]$ echo '__attribute__((section("do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | $CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o /tmp/foo.o [acme@felicio linux]$ file /tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not stripped
[acme@felicio linux]$
And finally:
[acme@felicio linux]$ file /tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not stripped
[acme@felicio linux]$ perf record --event /tmp/foo.o sleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data ]
[acme@felicio linux]$ perf evlist -v/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|PERIOD, disabled: 1, inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1 [acme@felicio linux]$ perf evlist/tmp/foo.o
[acme@felicio linux]$
So, type 1 is PERF_TYPE_SOFTWARE, config 0x9 is PERF_COUNT_SW_DUMMY, ok.
And it behaves accordingly, no samples, etc.
Added the sequence of testing with a non-existing file, with a normal ELF file and then with a valid eBPF ELF file as committer notes.
Continuing.
- Arnaldo
Re: [PATCH 03/31] perf tools: Enable passing bpf object file to --event
Quoted 30 lines · Arnaldo Carvalho de Melo
Em Tue, Oct 20, 2015 at 12:15:58PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 3 lines
Em Tue, Oct 20, 2015 at 12:12:55PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 2 lines
Em Wed, Oct 14, 2015 at 12:41:14PM +0000, Wang Nan escreveu:
Managed after running:
perf test LLVM
copy'n'pasting the output of those "set env" lines, replacing it with export, etc to get to:
[acme@felicio linux]$ echo '
__attribute__((section("do_fork"), used))intfork(void *ctx){return 0;} char _license[]__attribute__((section("license"), used))= "GPL";int_version__attribute__((section("version"), used))=0x40100;' | $CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o/tmp/foo.o[acme@felicio linux]$ file/tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not stripped
[acme@felicio linux]$And finally:
[acme@felicio linux]$ file
/tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not stripped
[acme@felicio linux]$ perf record --event/tmp/foo.osleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data ]
[acme@felicio linux]$ perf evlist -v/tmp/foo.o: type: 1, size: 112, config:0x9, {sample_period,sample_freq}: 4000,sample_type: IP|TID|TIME|PERIOD, disabled: 1, inherit: 1, mmap: 1, comm: 1, freq: 1,enable_on_exec: 1, task: 1,sample_id_all: 1,exclude_guest: 1, mmap2: 1,comm_exec: 1 [acme@felicio linux]$ perf evlist/tmp/foo.o
[acme@felicio linux]$
Quoted 1 line
So, type 1 is
PERF_TYPE_SOFTWARE, config0x9isPERF_COUNT_SW_DUMMY, ok.
You won't see this dummy event after patch 'perf tools: Collect perf_evsel in BPF object files'. I use dummy event here so we can test basic parsing and loading before that enabler patch to avoid enable too much code by one patch.
Thank you, and glad to see you start working on this patch set again!
Quoted 8 lines
And it behaves accordingly, no samples, etc.
Added the sequence of testing with a non-existing file, with a normal ELF file and then with a valid eBPF ELF file as committer notes.
Continuing.
- Arnaldo
Re: [PATCH 03/31] perf tools: Enable passing bpf object file to --event
In reply to Arnaldo Carvalho de MeloQuoted 39 lines · Arnaldo Carvalho de Melo
Em Tue, Oct 20, 2015 at 12:12:55PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 38 lines
Em Wed, Oct 14, 2015 at 12:41:14PM +0000, Wang Nan escreveu:
Quoted 11 lines
By introducing new rules in tools/perf/util/parse-events.[ly], this patch enables 'perf record --event bpf_file.o' to select events by an eBPF object file. It calls
parse_events_load_bpf()to load that file, which usesbpf__prepare_load()and finally callsbpf_object__open()for the object files.After applying this patch, commands like:
# perf record --event foo.o sleepbecome possible.
So, trying the above command I get almost perfect output:
[root@felicio ~]# perf record --event foo.o sleep
libbpf: failed to open foo.o: No such file or directory event syntax error: 'foo.o'
\___ BPF object file 'foo.o' is invalid(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#Good thing would be to not have any message from libbpf and the right error message from the parser, i.e. the first three lines become these two:
event syntax error: 'foo.o'
\___ BPF object file 'foo.o' not found.oBut that can be fixed up in an upcoming patch, so I am applying this one now in my new attempt at processing this patchkit.
I think fixing that is not very hard. When designing libbpf, I thought the problem like this so makes libbpf output controled by caller using 'libbpf_set_print()'. What we need to do is to pass different output functions to libbpf. We can easily disable all output from libbpf if verbose is set 0. The only question is: do you want me to provide a new version of patch 'perf ebpf: Add the libbpf glue' or you prefer another patch to adjust the output functions?
Thank you.
Quoted 19 lines
Ditto for:
[acme@felicio linux]$ perf record --event
/tmp/build/perf/perf.osleep
libbpf:/tmp/build/perf/perf.ois not an eBPF object file
event syntax error: '/tmp/build/perf/perf.o'
\___ BPF object file '/tmp/build/perf/perf.o' is invalid(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[acme@felicio linux]$Now trying to find a _valid_ ebpf object file to test with.
- Arnaldo
tip-bot for Wang Nan
merge bot[tip:perf/core] perf tools: Enable passing bpf object file to --event
In reply to Wang Nanperf tools: Enable passing bpf object file to --event
Commit-ID: 84c86ca12b2189df751eed7b2d67cb63bc8feda5 Gitweb: http://git.kernel.org/tip/84c86ca12b2189df751eed7b2d67cb63bc8feda5 Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:14 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 28 Oct 2015 12:48:12 -0300
perf tools: Enable passing bpf object file to --event
By introducing new rules in tools/perf/util/parse-events.[ly], this patch enables 'perf record --event bpf_file.o' to select events by an eBPF object file. It calls parse_events_load_bpf() to load that file, which uses bpf__prepare_load() and finally calls bpf_object__open() for the object files.
After applying this patch, commands like:
# perf record --event foo.o sleep
become possible.
However, at this point it is unable to link any useful things onto the evsel list because the creating of probe points and BPF program attaching have not been implemented. Before real events are possible to be extracted, to avoid perf report error because of empty evsel list, this patch link a dummy evsel. The dummy event related code will be removed when probing and extracting code is ready.
Commiter notes:
Using it:
$ ls -la foo.o
ls: cannot access foo.o: No such file or directory
$ perf record --event foo.o sleep
libbpf: failed to open foo.o: No such file or directory
event syntax error: 'foo.o'
\___ BPF object file 'foo.o' is invalid (add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
$
$ file /tmp/build/perf/perf.o
/tmp/build/perf/perf.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
$ perf record --event /tmp/build/perf/perf.o sleep
libbpf: /tmp/build/perf/perf.o is not an eBPF object file
event syntax error: '/tmp/build/perf/perf.o'
\___ BPF object file '/tmp/build/perf/perf.o' is invalid (add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
$
$ file /tmp/foo.o
/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not stripped
$ perf record --event /tmp/foo.o sleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data ]
$ perf evlist
/tmp/foo.o
$ perf evlist -v
/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|PERIOD, disabled: 1, inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1
$So, type 1 is PERF_TYPE_SOFTWARE, config 0x9 is PERF_COUNT_SW_DUMMY, ok.
$ perf report --stdio Error: The perf.data file has no samples! # To display the perf.data header info, please use --header/--header-only options. # $
--- tools/perf/perf.c | 2 ++ tools/perf/util/Build | 1 + tools/perf/util/parse-events.c | 57 ++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/parse-events.h | 8 ++++++ tools/perf/util/parse-events.l | 3 +++ tools/perf/util/parse-events.y | 18 ++++++++++++- 6 files changed, 88 insertions(+), 1 deletion(-)
tools/perf/perf.c +2 -0
@@ -15,6 +15,7 @@#include "util/run-command.h" #include "util/parse-events.h" #include "util/parse-options.h" +#include "util/bpf-loader.h" #include "util/debug.h" #include <api/fs/tracing_path.h> #include <pthread.h>
@@ -385,6 +386,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv) status = p->fn (argc, argv, prefix);exit_browser (status);perf_env__exit (&perf_env); +bpf__clear ();if (status)return status &0xff ;
tools/perf/util/Build +1 -0
@@ -87,6 +87,7 @@ libperf-$(CONFIG_AUXTRACE) += intel-bts.o libperf-y += parse-branch-options.o libperf-y += parse-regs-options.o +libperf-$(CONFIG_LIBBPF) += bpf-loader.o libperf-$(CONFIG_LIBELF) += symbol-elf.o libperf-$(CONFIG_LIBELF) += probe-file.o libperf-$(CONFIG_LIBELF) += probe-event.o
tools/perf/util/parse-events.c +57 -0
69 lines not on this page · raw message
tools/perf/util/parse-events.h +8 -0
@@ -123,6 +123,14 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, char *sys, char *event, struct parse_events_error *error, struct list_head *head_config); +int parse_events_load_bpf (struct parse_events_evlist *data, +struct list_head *list, +char *bpf_file_name); +/* Provide this function for perf test */ +struct bpf_object ; +int parse_events_load_bpf_obj (struct parse_events_evlist *data, +struct list_head *list, +struct bpf_object *obj); int parse_events_add_numeric(struct parse_events_evlist *data, struct list_head *list, u32 type, u64 config,
tools/perf/util/parse-events.l +3 -0
@@ -115,6 +115,7 @@ do { \ group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ +bpf_object .*\.(o|bpf) num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+
@@ -159,6 +160,7 @@ modifier_bp [rwx]{1,3} } {event_pmu} | +{bpf_object} | {event} { BEGIN(INITIAL); REWIND(1);
@@ -266,6 +268,7 @@ r{num_raw_hex} { return raw(yyscanner); } {num_hex} { return value(yyscanner, 16); } {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } +{bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } {name} { return pmu_str_check(yyscanner); } "/" { BEGIN(config); return '/'; } - { return '-'; }
tools/perf/util/parse-events.y +17 -1
@@ -42,6 +42,7 @@ static inc_group_count(struct list_head *list, %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM %token PE_EVENT_NAME %token PE_NAME +%token PE_BPF_OBJECT %token PE_MODIFIER_EVENT PE_MODIFIER_BP %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
@@ -53,6 +54,7 @@ static inc_group_count(struct list_head *list, %type <num> PE_RAW %type <num> PE_TERM %type <str> PE_NAME +%type <str> PE_BPF_OBJECT %type <str> PE_NAME_CACHE_TYPE %type <str> PE_NAME_CACHE_OP_RESULT %type <str> PE_MODIFIER_EVENT
@@ -70,6 +72,7 @@ static inc_group_count(struct list_head *list, %type <tracepoint_name> tracepoint_name %type <head> event_legacy_numeric %type <head> event_legacy_raw +%type <head> event_bpf_file %type <head> event_def %type <head> event_mod %type <head> event_name
@@ -203,7 +206,8 @@ event_def: event_pmu | event_legacy_mem | event_legacy_tracepoint sep_dc | event_legacy_numeric sep_dc | - event_legacy_raw sep_dc + event_legacy_raw sep_dc | + event_bpf_file event_pmu: PE_NAME '/' event_config '/'
@@ -449,6 +453,18 @@ PE_RAW $$ = list; } +event_bpf_file: +PE_BPF_OBJECT +{ + struct parse_events_evlist *data = _data; + struct parse_events_error *error = data->error; + struct list_head *list; + + ALLOC_LIST(list); + ABORT_ON(parse_events_load_bpf(data, list, $1)); + $$ = list; +} + start_terms: event_config { struct parse_events_terms *data = _data;
PATCH 4/31 perf record, bpf: Create probe points for BPF programs
In reply to Wang NanThis patch introduces bpf__{un,}probe() functions to enable callers to create kprobe points based on section names a BPF program. It parses the section names in the program and creates corresponding 'struct perf_probe_event' structures. The parse_perf_probe_command() function is used to do the main parsing work. The resuling 'struct perf_probe_event' is stored into program private data for further using.
By utilizing the new probing API, this patch creates probe points during event parsing.
To ensure probe points be removed correctly, register an atexit hook so even perf quit through exit() bpf__clear() is still called, so probing points are cleared. Note that bpf_clear() should be registered before bpf__probe() is called, so failure of bpf__probe() can still trigger bpf__clear() to remove probe points which are already probed.
strerror style error reporting scaffold is created by this patch. bpf__strerror_probe() is the first error reporting function in bpf-loader.c.
--- tools/perf/util/bpf-loader.c | 222 ++++++++++++++++++++++++++++++++++++++++- tools/perf/util/bpf-loader.h | 30 ++++++ tools/perf/util/parse-events.c | 17 ++++ 3 files changed, 268 insertions(+), 1 deletion(-)
tools/perf/util/bpf-loader.c +221 -1
@@ -10,6 +10,8 @@#include "perf.h" #include "debug.h" #include "bpf-loader.h" +#include "probe-event.h" +#include "probe-finder.h" // for MAX_PROBES #define DEFINE_PRINT_FN (name, level) \ static int libbpf_##name(const char *fmt, ...) \
@@ -27,6 +29,10 @@ DEFINE_PRINT_FN(warning, 0) DEFINE_PRINT_FN(info, 0) DEFINE_PRINT_FN(debug, 1) +struct bpf_prog_priv { +struct perf_probe_event pev; +}; +struct bpf_object *bpf__prepare_load (const char *filename) {struct bpf_object *obj;
@@ -52,6 +58,220 @@ void bpf__clear(void) {struct bpf_object *obj, *tmp; -bpf_object__for_each_safe (obj, tmp) +bpf_object__for_each_safe (obj, tmp) { +bpf__unprobe (obj);bpf_object__close (obj); + } +} + +static void +bpf_prog_priv__clear (struct bpf_program *prog__maybe_unused , +void *_priv) +{ +struct bpf_prog_priv *priv = _priv; + +cleanup_perf_probe_events (&priv->pev,1 ); +free (priv); +} + +static int +config_bpf_program (struct bpf_program *prog) +{ +struct perf_probe_event *pev =NULL ; +struct bpf_prog_priv *priv =NULL ; +const char *config_str; +int err; + + config_str =bpf_program__title (prog, false); +if (!config_str) { +pr_debug ("bpf: unable to get title for program\n" ); +return -EINVAL ; + } + + priv =calloc (sizeof (*priv),1 ); +if (!priv) { +pr_debug ("bpf: failed to alloc priv\n" ); +return -ENOMEM ; + } + pev = &priv->pev; + +pr_debug ("bpf: config program '%s'\n" , config_str); + err =parse_perf_probe_command (config_str, pev); +if (err <0 ) { +pr_debug ("bpf: '%s' is not a valid config string\n" , + config_str); + err = -EINVAL ; + goto errout; + } + +if (pev->group &&strcmp (pev->group,PERF_BPF_PROBE_GROUP )) { +pr_debug ("bpf: '%s': group for event is set and not '%s'.\n" , + config_str,PERF_BPF_PROBE_GROUP ); + err = -EINVAL ; + goto errout; + }else if (!pev->group) + pev->group =strdup (PERF_BPF_PROBE_GROUP ); + +if (!pev->group) { +pr_debug ("bpf: strdup failed\n" ); + err = -ENOMEM ; + goto errout; + } + +if (!pev->event) { +pr_debug ("bpf: '%s': event name is missing\n" , + config_str); + err = -EINVAL ; + goto errout; + } +pr_debug ("bpf: config '%s' is ok\n" , config_str); + + err =bpf_program__set_private (prog, priv, bpf_prog_priv__clear); +if (err) { +pr_debug ("Failed to set priv for program '%s'\n" , config_str); + goto errout; + } + +return 0 ; + +errout: +if (pev) +clear_perf_probe_event (pev); +free (priv); +return err; +} + +static int bpf__prepare_probe (void ) +{ +static int err =0 ; +static bool initialized = false; + +/* +* Make err static, so if init failed the first, bpf__prepare_probe() +* fails each time without calling init_probe_symbol_maps multiple +* times. +*/ +if (initialized) +return err; + + initialized = true; + err =init_probe_symbol_maps (false); +if (err <0 ) +pr_debug ("Failed to init_probe_symbol_maps\n" ); + probe_conf.max_probes =MAX_PROBES ; +return err; +} + +int bpf__probe (struct bpf_object *obj) +{ +int err =0 ; +struct bpf_program *prog; +struct bpf_prog_priv *priv; +struct perf_probe_event *pev; + + err =bpf__prepare_probe (); +if (err) { +pr_debug ("bpf__prepare_probe failed\n" ); +return err; + } + +bpf_object__for_each_program (prog, obj) { + err =config_bpf_program (prog); +if (err) + goto out; + + err =bpf_program__get_private (prog, (void **)&priv); +if (err || !priv) + goto out; + pev = &priv->pev; + + err =convert_perf_probe_events (pev,1 ); +if (err <0 ) { +pr_debug ("bpf_probe: failed to convert perf probe events" ); + goto out; + } + + err =apply_perf_probe_events (pev,1 ); +if (err <0 ) { +pr_debug ("bpf_probe: failed to apply perf probe events" ); + goto out; + } + } +out: +return err <0 ? err :0 ; +} + +#define EVENTS_WRITE_BUFSIZE 4096 +int bpf__unprobe (struct bpf_object *obj) +{ +int err, ret =0 ; +struct bpf_program *prog; +struct bpf_prog_priv *priv; + +bpf_object__for_each_program (prog, obj) { +int i; + + err =bpf_program__get_private (prog, (void **)&priv); +if (err || !priv) +continue ; + +for (i =0 ; i < priv->pev.ntevs; i++) { +struct probe_trace_event *tev = &priv->pev.tevs[i]; +char name_buf[EVENTS_WRITE_BUFSIZE ]; +struct strfilter *delfilter; + +snprintf (name_buf,EVENTS_WRITE_BUFSIZE , +"%s:%s" , tev->group, tev->event); + name_buf[EVENTS_WRITE_BUFSIZE -1 ] ='\0' ; + + delfilter =strfilter__new (name_buf,NULL ); +if (!delfilter) { +pr_debug ("Failed to create filter for unprobing\n" ); + ret = -ENOMEM ; +continue ; + } + + err =del_perf_probe_events (delfilter); +strfilter__delete (delfilter); +if (err) { +pr_debug ("Failed to delete %s\n" , name_buf); + ret = err; +continue ; + } + } + } +return ret; +} + +#define bpf__strerror_head (err, buf, size) \ + char sbuf[STRERR_BUFSIZE], *emsg;\ + if (!size)\ + return 0;\ + if (err < 0)\ + err = -err;\ + emsg = strerror_r(err, sbuf, sizeof(sbuf));\ + switch (err) {\ + default:\ + scnprintf(buf, size, "%s", emsg);\ + break; + +#define bpf__strerror_entry(val, fmt...)\ + case val: {\ + scnprintf(buf, size, fmt);\ + break;\ + } + +#define bpf__strerror_end (buf, size)\ + }\ + buf[size - 1] = '\0'; + +int bpf__strerror_probe (struct bpf_object *obj__maybe_unused , +int err,char *buf,size_t size) +{ +bpf__strerror_head (err, buf, size); +bpf__strerror_entry (EEXIST ,"Probe point exist. Try use 'perf probe -d \"*\"'" ); +bpf__strerror_entry (EPERM ,"You need to be root, and /proc/sys/kernel/kptr_restrict should be 0\n" ); +bpf__strerror_entry (ENOENT ,"You need to check probing points in BPF file\n" ); +bpf__strerror_end (buf, size); +return 0 ; }
tools/perf/util/bpf-loader.h +30 -0
@@ -11,11 +11,18 @@#include "debug.h" struct bpf_object ; +#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"#ifdef HAVE_LIBBPF_SUPPORT struct bpf_object *bpf__prepare_load (const char *filename);void bpf__clear (void ); + +int bpf__probe (struct bpf_object *obj); +int bpf__unprobe (struct bpf_object *obj); +int bpf__strerror_probe (struct bpf_object *obj,int err, +char *buf,size_t size); +#else static inline struct bpf_object *bpf__prepare_load (const char *filename__maybe_unused )
@@ -25,5 +32,28 @@ bpf__prepare_load(const char *filename __maybe_unused) }static inline void bpf__clear (void ) { } + +static inline int bpf__probe (struct bpf_object *obj__maybe_unused ) {return 0 ;} +static inline int bpf__unprobe (struct bpf_object *obj__maybe_unused ) {return 0 ;} + +static inline int +__bpf_strerror (char *buf,size_t size) +{ +if (!size) +return 0 ; +strncpy (buf, +"ERROR: eBPF object loading is disabled during compiling.\n" , + size); + buf[size -1 ] ='\0' ; +return 0 ; +} + +static inline int +bpf__strerror_probe (struct bpf_object *obj__maybe_unused , +int err__maybe_unused , +char *buf,size_t size) +{ +return __bpf_strerror (buf, size); +}#endif #endif
tools/perf/util/parse-events.c +17 -0
@@ -536,6 +536,7 @@ int parse_events_load_bpf_obj(struct parse_events_evlist *data, {int err;char errbuf[BUFSIZ ]; +static bool registered_unprobe_atexit = false;if (IS_ERR (obj) || !obj) { snprintf(errbuf, sizeof(errbuf),
@@ -545,6 +546,22 @@ int parse_events_load_bpf_obj(struct parse_events_evlist *data, }/* +* Register atexit handler before calling bpf__probe() so +* bpf__probe() don't need to unprobe probe points its already +* created when failure. +*/ +if (!registered_unprobe_atexit) { +atexit (bpf__clear); + registered_unprobe_atexit = true; + } + + err =bpf__probe (obj); +if (err) { +bpf__strerror_probe (obj, err, errbuf,sizeof (errbuf)); + goto errout; + } + +/* * Temporary add a dummy event here so we can check whether * basic bpf loader works. Following patches will replace * dummy event by useful evsels.
Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 04/31] perf record, bpf: Create probe points for BPF programs
Em Wed, Oct 14, 2015 at 12:41:15PM +0000, Wang Nan escreveu:
Quoted 10 lines
This patch introduces bpf__{un,}probe() functions to enable callers to create kprobe points based on section names a BPF program. It parses the section names in the program and creates corresponding 'struct
perf_probe_event' structures. Theparse_perf_probe_command()function is used to do the main parsing work. The resuling 'structperf_probe_event' is stored into program private data for further using.By utilizing the new probing API, this patch creates probe points during event parsing.
Quoted 8 lines
To ensure probe points be removed correctly, register an atexit hook so even perf quit through
exit()bpf__clear()is still called, so probing points are cleared. Note thatbpf_clear()should be registered beforebpf__probe()is called, so failure ofbpf__probe()can still triggerbpf__clear()to remove probe points which are already probed.strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe()is the first error reporting function inbpf-loader.c.
So, this one, for a non-root user gives me:
[acme@felicio linux]$ perf record --event /tmp/foo.o sleep 1 event syntax error: '/tmp/foo.o'
\___ Invalid argument
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
[acme@felicio linux]$
--------------------
I.e. no libbpf error (good!) but then, just an -EINVAL as the "event syntax error", which clearly isn't a syntax error, we need to tell the user that he or she needs special perfmissions for using sys_bpf() :-)
As root:
[root@felicio ~]# perf record --event /tmp/foo.o sleep event syntax error: '/tmp/foo.o'
\___ Invalid argument
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]# ls -la /tmp/foo.o
-rw-rw-r--. 1 acme acme 824 Oct 20 12:35 /tmp/foo.o
[root@felicio ~]# file /tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not stripped
Humm, its something else, this is an ancient kernel, 4.2.0, probably without eBPF support? Nope, its there:
[root@felicio ~]# grep -i sys_bpf /proc/kallsymsffffffff811829d0 T SyS_bpfffffffff811829d0 T sys_bpf
[root@felicio ~]#
Its something else, we need to improve this error reporting:
[root@felicio ~]# perf record -v --event /tmp/foo.o sleep 1
libbpf: loading /tmp/foo.o
libbpf: section .strtab, size 60, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: section do_fork, size 16, link 0, flags 6, type=1
libbpf: found program do_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of /tmp/foo.o is GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of /tmp/foo.o is 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program 'do_fork'
symbol:do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: 'do_fork': event name is missing
event syntax error: '/tmp/foo.o'
\___ Invalid argument
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#
[root@felicio ~]# grep do_fork /proc/kallsymsffffffff81099ab0 T _do_forkffffffff81ccc800 d do_fork_test
[root@felicio ~]#
$ echo '__attribute__((section("_do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c - -target bpf -O2 -o /tmp/foo.o[root@felicio ~]# perf record -v --event /tmp/foo.o sleep 1
libbpf: loading /tmp/foo.o
libbpf: section .strtab, size 61, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: section _do_fork, size 16, link 0, flags 6, type=1
libbpf: found program _do_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of /tmp/foo.o is GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of /tmp/foo.o is 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program '_do_fork'
symbol:_do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: '_do_fork': event name is missing
event syntax error: '/tmp/foo.o'
\___ Invalid argument
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#
So it still doesn't work, doesn't look like it is trying to find a vmlinux, will look at another patch IIRC is in this patchkit allowing us to tell 'perf record' where to find it... But it can as well use kallsyms...
- Arnaldo
Re: [PATCH 04/31] perf record, bpf: Create probe points for BPF programs
Quoted 3 lines · Arnaldo Carvalho de Melo
I.e. no libbpf error (good!) but then, just an
-EINVALas the "event syntax error", which clearly isn't a syntax error, we need to tell the user that he or she needs special perfmissions for using sys_bpf():-)
perfmissions? Are you coining new words -- permissions for perf? ;-)
Re: [PATCH 04/31] perf record, bpf: Create probe points for BPF programs
Em Tue, Oct 20, 2015 at 01:16:04PM -0600, David Ahern escreveu:
Quoted 6 lines · Arnaldo Carvalho de Melo
Quoted 3 lines
I.e. no libbpf error (good!) but then, just an
-EINVALas the "event syntax error", which clearly isn't a syntax error, we need to tell the user that he or she needs special perfmissions for using sys_bpf():-)perfmissions? Are you coining new words -- permissions for perf? ;-)
LOL, totally unintended 8-)
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 04/31] perf record, bpf: Create probe points for BPF programs
In reply to Arnaldo Carvalho de MeloEm Tue, Oct 20, 2015 at 04:12:14PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 130 lines
Em Wed, Oct 14, 2015 at 12:41:15PM +0000, Wang Nan escreveu:
Quoted 10 lines
This patch introduces bpf__{un,}probe() functions to enable callers to create kprobe points based on section names a BPF program. It parses the section names in the program and creates corresponding 'struct
perf_probe_event' structures. Theparse_perf_probe_command()function is used to do the main parsing work. The resuling 'structperf_probe_event' is stored into program private data for further using.By utilizing the new probing API, this patch creates probe points during event parsing.
Quoted 8 lines
To ensure probe points be removed correctly, register an atexit hook so even perf quit through
exit()bpf__clear()is still called, so probing points are cleared. Note thatbpf_clear()should be registered beforebpf__probe()is called, so failure ofbpf__probe()can still triggerbpf__clear()to remove probe points which are already probed.strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe()is the first error reporting function inbpf-loader.c.So, this one, for a non-root user gives me:
[acme@felicio linux]$ perf record --event
/tmp/foo.osleep 1 event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[acme@felicio linux]$--------------------
I.e. no libbpf error (good!) but then, just an
-EINVALas the "event syntax error", which clearly isn't a syntax error, we need to tell the user that he or she needs special perfmissions for usingsys_bpf():-)As root:
[root@felicio ~]# perf record --event
/tmp/foo.osleep event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]# ls -la/tmp/foo.o
-rw-rw-r--. 1 acme acme 824 Oct 20 12:35/tmp/foo.o
[root@felicio ~]# file/tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not strippedHumm, its something else, this is an ancient kernel, 4.2.0, probably without eBPF support? Nope, its there:
[root@felicio ~]# grep -i
sys_bpf/proc/kallsymsffffffff811829d0TSyS_bpfffffffff811829d0Tsys_bpf
[root@felicio ~]#Its something else, we need to improve this error reporting:
[root@felicio ~]# perf record -v --event
/tmp/foo.osleep 1
libbpf: loading/tmp/foo.o
libbpf: section .strtab, size 60, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: sectiondo_fork, size 16, link 0, flags 6, type=1
libbpf: found programdo_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of/tmp/foo.ois GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of/tmp/foo.ois 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program 'do_fork'
symbol:do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: 'do_fork': event name is missing
event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#[root@felicio ~]# grep
do_fork/proc/kallsymsffffffff81099ab0T_do_forkffffffff81ccc800ddo_fork_test
[root@felicio ~]#$ echo '__attribute__((section("_do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c - -target bpf -O2 -o /tmp/foo.o[root@felicio ~]# perf record -v --event
/tmp/foo.osleep 1
libbpf: loading/tmp/foo.o
libbpf: section .strtab, size 61, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: section_do_fork, size 16, link 0, flags 6, type=1
libbpf: found program_do_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of/tmp/foo.ois GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of/tmp/foo.ois 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program '_do_fork'
symbol:_do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: '_do_fork': event name is missing
event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
Quoted 6 lines
-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#So it still doesn't work, doesn't look like it is trying to find a vmlinux, will look at another patch IIRC is in this patchkit allowing us to tell 'perf record' where to find it... But it can as well use kallsyms...
That doesn't seem to be the problem, the last check after parse_perf_probe_command() is failing:
+ pr_debug("bpf: config program '%s'\n", config_str);
+ err = parse_perf_probe_command(config_str, pev);
+ if (err < 0) {+ pr_debug("bpf: '%s' is not a valid config string\n",
+ config_str);
+ err = -EINVAL;
+ goto errout;
+ }
+
+ if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) {
+ pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
+ config_str, PERF_BPF_PROBE_GROUP);
+ err = -EINVAL;
+ goto errout;
+ } else if (!pev->group)
+ pev->group = strdup(PERF_BPF_PROBE_GROUP);
+
+ if (!pev->group) {
+ pr_debug("bpf: strdup failed\n");
+ err = -ENOMEM;
+ goto errout;+ }
+
+ if (!pev->event) {
+ pr_debug("bpf: '%s': event name is missing\n",
+ config_str);
+ err = -EINVAL;
+ goto errout;
+ }
+ pr_debug("bpf: config '%s' is ok\n", config_str);
I.e. pev->event is NULL, is this expected at this point? I'll continue investigating later.
What I have is at the perf/ebpf branch in my tree.
- Arnaldo
Re: [PATCH 04/31] perf record, bpf: Create probe points for BPF programs
In reply to Arnaldo Carvalho de MeloQuoted 25 lines · Arnaldo Carvalho de Melo
Em Wed, Oct 14, 2015 at 12:41:15PM +0000, Wang Nan escreveu:
Quoted 10 lines
This patch introduces bpf__{un,}probe() functions to enable callers to create kprobe points based on section names a BPF program. It parses the section names in the program and creates corresponding 'struct
perf_probe_event' structures. Theparse_perf_probe_command()function is used to do the main parsing work. The resuling 'structperf_probe_event' is stored into program private data for further using.By utilizing the new probing API, this patch creates probe points during event parsing.
Quoted 8 lines
To ensure probe points be removed correctly, register an atexit hook so even perf quit through
exit()bpf__clear()is still called, so probing points are cleared. Note thatbpf_clear()should be registered beforebpf__probe()is called, so failure ofbpf__probe()can still triggerbpf__clear()to remove probe points which are already probed.strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe()is the first error reporting function inbpf-loader.c.So, this one, for a non-root user gives me:
[acme@felicio linux]$ perf record --event
/tmp/foo.osleep 1 event syntax error: '/tmp/foo.o'
\___ Invalid argument
Quoted 8 lines
(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[acme@felicio linux]$
It should be:
# perf record -e ./test_config_map.o ls
Failed to init vmlinux path.
event syntax error: './test_config_map.o'
\___ You need to be root, and
/proc/sys/kernel/kptr_restrict should be 0 (add -v to see detail)
Run 'perf list' for a list of valid events
usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list
available events
So this is another problem related to probing. I'll have a look at your lot.
Thank you.
Quoted 103 lines
--------------------
I.e. no libbpf error (good!) but then, just an
-EINVALas the "event syntax error", which clearly isn't a syntax error, we need to tell the user that he or she needs special perfmissions for usingsys_bpf():-)As root:
[root@felicio ~]# perf record --event
/tmp/foo.osleep event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]# ls -la/tmp/foo.o
-rw-rw-r--. 1 acme acme 824 Oct 20 12:35/tmp/foo.o
[root@felicio ~]# file/tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not strippedHumm, its something else, this is an ancient kernel, 4.2.0, probably without eBPF support? Nope, its there:
[root@felicio ~]# grep -i
sys_bpf/proc/kallsymsffffffff811829d0TSyS_bpfffffffff811829d0Tsys_bpf
[root@felicio ~]#Its something else, we need to improve this error reporting:
[root@felicio ~]# perf record -v --event
/tmp/foo.osleep 1
libbpf: loading/tmp/foo.o
libbpf: section .strtab, size 60, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: sectiondo_fork, size 16, link 0, flags 6, type=1
libbpf: found programdo_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of/tmp/foo.ois GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of/tmp/foo.ois 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program 'do_fork'
symbol:do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: 'do_fork': event name is missing
event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#[root@felicio ~]# grep
do_fork/proc/kallsymsffffffff81099ab0T_do_forkffffffff81ccc800ddo_fork_test
[root@felicio ~]#$ echo '__attribute__((section("_do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c - -target bpf -O2 -o /tmp/foo.o[root@felicio ~]# perf record -v --event
/tmp/foo.osleep 1
libbpf: loading/tmp/foo.o
libbpf: section .strtab, size 61, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: section_do_fork, size 16, link 0, flags 6, type=1
libbpf: found program_do_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of/tmp/foo.ois GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of/tmp/foo.ois 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program '_do_fork'
symbol:_do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: '_do_fork': event name is missing
event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#So it still doesn't work, doesn't look like it is trying to find a vmlinux, will look at another patch IIRC is in this patchkit allowing us to tell 'perf record' where to find it... But it can as well use kallsyms...
- Arnaldo
Re: [PATCH 04/31] perf record, bpf: Create probe points for BPF programs
In reply to Arnaldo Carvalho de MeloQuoted 69 lines · Arnaldo Carvalho de Melo
Em Wed, Oct 14, 2015 at 12:41:15PM +0000, Wang Nan escreveu:
Quoted 10 lines
This patch introduces bpf__{un,}probe() functions to enable callers to create kprobe points based on section names a BPF program. It parses the section names in the program and creates corresponding 'struct
perf_probe_event' structures. Theparse_perf_probe_command()function is used to do the main parsing work. The resuling 'structperf_probe_event' is stored into program private data for further using.By utilizing the new probing API, this patch creates probe points during event parsing.
Quoted 8 lines
To ensure probe points be removed correctly, register an atexit hook so even perf quit through
exit()bpf__clear()is still called, so probing points are cleared. Note thatbpf_clear()should be registered beforebpf__probe()is called, so failure ofbpf__probe()can still triggerbpf__clear()to remove probe points which are already probed.strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe()is the first error reporting function inbpf-loader.c.So, this one, for a non-root user gives me:
[acme@felicio linux]$ perf record --event
/tmp/foo.osleep 1 event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[acme@felicio linux]$--------------------
I.e. no libbpf error (good!) but then, just an
-EINVALas the "event syntax error", which clearly isn't a syntax error, we need to tell the user that he or she needs special perfmissions for usingsys_bpf():-)As root:
[root@felicio ~]# perf record --event
/tmp/foo.osleep event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]# ls -la/tmp/foo.o
-rw-rw-r--. 1 acme acme 824 Oct 20 12:35/tmp/foo.o
[root@felicio ~]# file/tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not strippedHumm, its something else, this is an ancient kernel, 4.2.0, probably without eBPF support? Nope, its there:
[root@felicio ~]# grep -i
sys_bpf/proc/kallsymsffffffff811829d0TSyS_bpfffffffff811829d0Tsys_bpf
[root@felicio ~]#Its something else, we need to improve this error reporting:
Quoted 16 lines
[root@felicio ~]# perf record -v --event
/tmp/foo.osleep 1
libbpf: loading/tmp/foo.o
libbpf: section .strtab, size 60, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: sectiondo_fork, size 16, link 0, flags 6, type=1
libbpf: found programdo_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of/tmp/foo.ois GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of/tmp/foo.ois 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program 'do_fork'
symbol:do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: 'do_fork': event name is missing
BPF report the problem, but it is a little bit hard to understand...
Quoted 12 lines
event syntax error: '
/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#
Quoted 6 lines
[root@felicio ~]# grep
do_fork/proc/kallsymsffffffff81099ab0T_do_forkffffffff81ccc800ddo_fork_test
[root@felicio ~]#$ echo '__attribute__((section("_do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c - -target bpf -O2 -o /tmp/foo.o
In your program you only provide "do_fork", but we need "key=value" syntax.
"key" will become the name of created kprobe. Please try "__attribute__((section("func=do_fork"), used)) " instead.
I think when event name is missing we'd better construct one name for it like perf probe, but then we need to deal with perf probe code again. It may require another patch.
For this patch, I think we can assign a new errorno so bpf__strerror_probe() can give more information to let user know whether the problem is reside in bpf program or perf configuration. Do you think ENOEXEC is a good choice?
Thank you.
Quoted 34 lines
[root@felicio ~]# perf record -v --event
/tmp/foo.osleep 1
libbpf: loading/tmp/foo.o
libbpf: section .strtab, size 61, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: section_do_fork, size 16, link 0, flags 6, type=1
libbpf: found program_do_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of/tmp/foo.ois GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of/tmp/foo.ois 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program '_do_fork'
symbol:_do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: '_do_fork': event name is missing
event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#So it still doesn't work, doesn't look like it is trying to find a vmlinux, will look at another patch IIRC is in this patchkit allowing us to tell 'perf record' where to find it... But it can as well use kallsyms...
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 04/31] perf record, bpf: Create probe points for BPF programs
Em Wed, Oct 21, 2015 at 11:31:57AM +0800, Wangnan (F) escreveu:
Quoted 110 lines · Arnaldo Carvalho de Melo
Quoted 69 lines
Em Wed, Oct 14, 2015 at 12:41:15PM +0000, Wang Nan escreveu:
Quoted 10 lines
This patch introduces bpf__{un,}probe() functions to enable callers to create kprobe points based on section names a BPF program. It parses the section names in the program and creates corresponding 'struct
perf_probe_event' structures. Theparse_perf_probe_command()function is used to do the main parsing work. The resuling 'structperf_probe_event' is stored into program private data for further using.By utilizing the new probing API, this patch creates probe points during event parsing.
Quoted 8 lines
To ensure probe points be removed correctly, register an atexit hook so even perf quit through
exit()bpf__clear()is still called, so probing points are cleared. Note thatbpf_clear()should be registered beforebpf__probe()is called, so failure ofbpf__probe()can still triggerbpf__clear()to remove probe points which are already probed.strerror style error reporting scaffold is created by this patch.
bpf__strerror_probe()is the first error reporting function inbpf-loader.c.So, this one, for a non-root user gives me:
[acme@felicio linux]$ perf record --event
/tmp/foo.osleep 1 event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[acme@felicio linux]$--------------------
I.e. no libbpf error (good!) but then, just an
-EINVALas the "event syntax error", which clearly isn't a syntax error, we need to tell the user that he or she needs special perfmissions for usingsys_bpf():-)As root:
[root@felicio ~]# perf record --event
/tmp/foo.osleep event syntax error: '/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]# ls -la/tmp/foo.o
-rw-rw-r--. 1 acme acme 824 Oct 20 12:35/tmp/foo.o
[root@felicio ~]# file/tmp/foo.o/tmp/foo.o: ELF 64-bit LSB relocatable, no machine, version 1 (SYSV), not strippedHumm, its something else, this is an ancient kernel, 4.2.0, probably without eBPF support? Nope, its there:
[root@felicio ~]# grep -i
sys_bpf/proc/kallsymsffffffff811829d0TSyS_bpfffffffff811829d0Tsys_bpf
[root@felicio ~]#Its something else, we need to improve this error reporting:
Quoted 16 lines
[root@felicio ~]# perf record -v --event
/tmp/foo.osleep 1
libbpf: loading/tmp/foo.o
libbpf: section .strtab, size 60, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: sectiondo_fork, size 16, link 0, flags 6, type=1
libbpf: found programdo_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of/tmp/foo.ois GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of/tmp/foo.ois 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program 'do_fork'
symbol:do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: 'do_fork': event name is missingBPF report the problem, but it is a little bit hard to understand...
Quoted 12 lines
event syntax error: '
/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#Quoted 6 lines
[root@felicio ~]# grep
do_fork/proc/kallsymsffffffff81099ab0T_do_forkffffffff81ccc800ddo_fork_test
[root@felicio ~]#$ echo '__attribute__((section("_do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c - -target bpf -O2 -o /tmp/foo.o
Quoted 7 lines
In your program you only provide "
do_fork", but we need "key=value" syntax. "key" will become the name of created kprobe. Please try "__attribute__((section("func=do_fork"), used))" instead.I think when event name is missing we'd better construct one name for it like perf probe, but then we need to deal with perf probe code again. It may require another patch.
Nah, lets go with what we have, i.e. I'll take that into account and test with the expected form.
Quoted 4 lines
For this patch, I think we can assign a new errorno so
bpf__strerror_probe()can give more information to let user know whether the problem is reside in bpf program or perf configuration. Do you think ENOEXEC is a good choice?
Unsure if we should use existing errno codes in cases like this, probably better to use a BPF_ERRNO__ENOALIAS or somesuch.
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 04/31] perf record, bpf: Create probe points for BPF programs
Em Wed, Oct 21, 2015 at 10:28:03AM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 30 lines
Em Wed, Oct 21, 2015 at 11:31:57AM +0800, Wangnan (F) escreveu:
Quoted 28 lines
Quoted 3 lines
bpf: config program '
do_fork'
symbol:do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: 'do_fork': event name is missingBPF report the problem, but it is a little bit hard to understand...
Quoted 12 lines
event syntax error: '
/tmp/foo.o'
\___ Invalid argument(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
[root@felicio ~]#Quoted 6 lines
[root@felicio ~]# grep
do_fork/proc/kallsymsffffffff81099ab0T_do_forkffffffff81ccc800ddo_fork_test
[root@felicio ~]#$ echo '__attribute__((section("_do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c - -target bpf -O2 -o /tmp/foo.oIn your program you only provide "
do_fork", but we need "key=value" syntax. "key" will become the name of created kprobe. Please try "__attribute__((section("func=do_fork"), used))" instead.
Quoted 6 lines
Quoted 3 lines
I think when event name is missing we'd better construct one name for it like perf probe, but then we need to deal with perf probe code again. It may require another patch.
Nah, lets go with what we have, i.e. I'll take that into account and test with the expected form.
So, pushed this cset with my committer testing notes:
continuing...
- Arnaldo
tip-bot for Wang Nan
merge bot[tip:perf/core] perf tools: Create probe points for BPF programs
In reply to Wang Nanperf tools: Create probe points for BPF programs
Commit-ID: aa3abf30bb28addcf593578d37447d42e3f65fc3 Gitweb: http://git.kernel.org/tip/aa3abf30bb28addcf593578d37447d42e3f65fc3 Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:15 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 28 Oct 2015 12:48:13 -0300
perf tools: Create probe points for BPF programs
This patch introduces bpf__{un,}probe() functions to enable callers to create kprobe points based on section names a BPF program. It parses the section names in the program and creates corresponding 'struct perf_probe_event' structures. The parse_perf_probe_command() function is used to do the main parsing work. The resuling 'struct perf_probe_event' is stored into program private data for further using.
By utilizing the new probing API, this patch creates probe points during event parsing.
To ensure probe points be removed correctly, register an atexit hook so even perf quit through exit() bpf__clear() is still called, so probing points are cleared. Note that bpf_clear() should be registered before bpf__probe() is called, so failure of bpf__probe() can still trigger bpf__clear() to remove probe points which are already probed.
strerror style error reporting scaffold is created by this patch. bpf__strerror_probe() is the first error reporting function in bpf-loader.c.
Committer note:
Trying it:
To build a test eBPF object file:
I am testing using a script I built from the 'perf test -v LLVM' output:
$ cat ~/bin/hello-ebpf export KERNEL_INC_OPTIONS="-nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.3/include -I/home/acme/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/acme/git/linux/include -Iinclude -I/home/acme/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -Iinclude/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h" export WORKING_DIR=/lib/modules/4.2.0/build export CLANG_SOURCE=- export CLANG_OPTIONS=-xc
OBJ=/tmp/foo.o
rm -f $OBJ
echo '__attribute__((section("fork=do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | \
clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o /tmp/foo.o && file $OBJ---
First asking to put a probe in a function not present in the kernel (misses the initial _):
$ perf record --event /tmp/foo.o sleep 1
Probe point 'do_fork' not found.
event syntax error: '/tmp/foo.o'
\___ You need to check probing points in BPF file (add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
$
---
Now, with "__attribute__((section("fork=_do_fork"), used)):
$ grep _do_fork /proc/kallsyms
ffffffff81099ab0 T _do_fork
$ perf record --event /tmp/foo.o sleep 1
Failed to open kprobe_events: Permission denied
event syntax error: '/tmp/foo.o'
\___ Permission denied---
Cool, we need to provide some better hints, "kprobe_events" is too low level, one doesn't strictly need to know the precise details of how these things are put in place, so something that shows the command needed to fix the permissions would be more helpful.
Lets try as root instead:
# perf record --event /tmp/foo.o sleep 1
Lowering default frequency rate to 1000.
Please consider tweaking /proc/sys/kernel/perf_event_max_sample_rate.
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.013 MB perf.data ]
# perf evlist
/tmp/foo.o
[root@felicio ~]# perf evlist -v
/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period,
sample_freq }: 1000, sample_type: IP|TID|TIME|PERIOD, disabled: 1,
inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1,
sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1---
--- tools/perf/util/bpf-loader.c | 222 ++++++++++++++++++++++++++++++++++++++++- tools/perf/util/bpf-loader.h | 30 ++++++ tools/perf/util/parse-events.c | 17 ++++ 3 files changed, 268 insertions(+), 1 deletion(-)
tools/perf/util/bpf-loader.c +221 -1
@@ -10,6 +10,8 @@#include "perf.h" #include "debug.h" #include "bpf-loader.h" +#include "probe-event.h" +#include "probe-finder.h" // for MAX_PROBES #define DEFINE_PRINT_FN (name, level) \ static int libbpf_##name(const char *fmt, ...) \
@@ -27,6 +29,10 @@ DEFINE_PRINT_FN(warning, 0) DEFINE_PRINT_FN(info, 0) DEFINE_PRINT_FN(debug, 1) +struct bpf_prog_priv { +struct perf_probe_event pev; +}; +struct bpf_object *bpf__prepare_load (const char *filename) {struct bpf_object *obj;
@@ -52,6 +58,220 @@ void bpf__clear(void) {struct bpf_object *obj, *tmp; -bpf_object__for_each_safe (obj, tmp) +bpf_object__for_each_safe (obj, tmp) { +bpf__unprobe (obj);bpf_object__close (obj); + } +} + +static void +bpf_prog_priv__clear (struct bpf_program *prog__maybe_unused , +void *_priv) +{ +struct bpf_prog_priv *priv = _priv; + +cleanup_perf_probe_events (&priv->pev,1 ); +free (priv); +} + +static int +config_bpf_program (struct bpf_program *prog) +{ +struct perf_probe_event *pev =NULL ; +struct bpf_prog_priv *priv =NULL ; +const char *config_str; +int err; + + config_str =bpf_program__title (prog, false); +if (!config_str) { +pr_debug ("bpf: unable to get title for program\n" ); +return -EINVAL ; + } + + priv =calloc (sizeof (*priv),1 ); +if (!priv) { +pr_debug ("bpf: failed to alloc priv\n" ); +return -ENOMEM ; + } + pev = &priv->pev; + +pr_debug ("bpf: config program '%s'\n" , config_str); + err =parse_perf_probe_command (config_str, pev); +if (err <0 ) { +pr_debug ("bpf: '%s' is not a valid config string\n" , + config_str); + err = -EINVAL ; + goto errout; + } + +if (pev->group &&strcmp (pev->group,PERF_BPF_PROBE_GROUP )) { +pr_debug ("bpf: '%s': group for event is set and not '%s'.\n" , + config_str,PERF_BPF_PROBE_GROUP ); + err = -EINVAL ; + goto errout; + }else if (!pev->group) + pev->group =strdup (PERF_BPF_PROBE_GROUP ); + +if (!pev->group) { +pr_debug ("bpf: strdup failed\n" ); + err = -ENOMEM ; + goto errout; + } + +if (!pev->event) { +pr_debug ("bpf: '%s': event name is missing\n" , + config_str); + err = -EINVAL ; + goto errout; + } +pr_debug ("bpf: config '%s' is ok\n" , config_str); + + err =bpf_program__set_private (prog, priv, bpf_prog_priv__clear); +if (err) { +pr_debug ("Failed to set priv for program '%s'\n" , config_str); + goto errout; + } + +return 0 ; + +errout: +if (pev) +clear_perf_probe_event (pev); +free (priv); +return err; +} + +static int bpf__prepare_probe (void ) +{ +static int err =0 ; +static bool initialized = false; + +/* +* Make err static, so if init failed the first, bpf__prepare_probe() +* fails each time without calling init_probe_symbol_maps multiple +* times. +*/ +if (initialized) +return err; + + initialized = true; + err =init_probe_symbol_maps (false); +if (err <0 ) +pr_debug ("Failed to init_probe_symbol_maps\n" ); + probe_conf.max_probes =MAX_PROBES ; +return err; +} + +int bpf__probe (struct bpf_object *obj) +{ +int err =0 ; +struct bpf_program *prog; +struct bpf_prog_priv *priv; +struct perf_probe_event *pev; + + err =bpf__prepare_probe (); +if (err) { +pr_debug ("bpf__prepare_probe failed\n" ); +return err; + } + +bpf_object__for_each_program (prog, obj) { + err =config_bpf_program (prog); +if (err) + goto out; + + err =bpf_program__get_private (prog, (void **)&priv); +if (err || !priv) + goto out; + pev = &priv->pev; + + err =convert_perf_probe_events (pev,1 ); +if (err <0 ) { +pr_debug ("bpf_probe: failed to convert perf probe events" ); + goto out; + } + + err =apply_perf_probe_events (pev,1 ); +if (err <0 ) { +pr_debug ("bpf_probe: failed to apply perf probe events" ); + goto out; + } + } +out: +return err <0 ? err :0 ; +} + +#define EVENTS_WRITE_BUFSIZE 4096 +int bpf__unprobe (struct bpf_object *obj) +{ +int err, ret =0 ; +struct bpf_program *prog; +struct bpf_prog_priv *priv; + +bpf_object__for_each_program (prog, obj) { +int i; + + err =bpf_program__get_private (prog, (void **)&priv); +if (err || !priv) +continue ; + +for (i =0 ; i < priv->pev.ntevs; i++) { +struct probe_trace_event *tev = &priv->pev.tevs[i]; +char name_buf[EVENTS_WRITE_BUFSIZE ]; +struct strfilter *delfilter; + +snprintf (name_buf,EVENTS_WRITE_BUFSIZE , +"%s:%s" , tev->group, tev->event); + name_buf[EVENTS_WRITE_BUFSIZE -1 ] ='\0' ; + + delfilter =strfilter__new (name_buf,NULL ); +if (!delfilter) { +pr_debug ("Failed to create filter for unprobing\n" ); + ret = -ENOMEM ; +continue ; + } + + err =del_perf_probe_events (delfilter); +strfilter__delete (delfilter); +if (err) { +pr_debug ("Failed to delete %s\n" , name_buf); + ret = err; +continue ; + } + } + } +return ret; +} + +#define bpf__strerror_head (err, buf, size) \ + char sbuf[STRERR_BUFSIZE], *emsg;\ + if (!size)\ + return 0;\ + if (err < 0)\ + err = -err;\ + emsg = strerror_r(err, sbuf, sizeof(sbuf));\ + switch (err) {\ + default:\ + scnprintf(buf, size, "%s", emsg);\ + break; + +#define bpf__strerror_entry(val, fmt...)\ + case val: {\ + scnprintf(buf, size, fmt);\ + break;\ + } + +#define bpf__strerror_end (buf, size)\ + }\ + buf[size - 1] = '\0'; + +int bpf__strerror_probe (struct bpf_object *obj__maybe_unused , +int err,char *buf,size_t size) +{ +bpf__strerror_head (err, buf, size); +bpf__strerror_entry (EEXIST ,"Probe point exist. Try use 'perf probe -d \"*\"'" ); +bpf__strerror_entry (EPERM ,"You need to be root, and /proc/sys/kernel/kptr_restrict should be 0\n" ); +bpf__strerror_entry (ENOENT ,"You need to check probing points in BPF file\n" ); +bpf__strerror_end (buf, size); +return 0 ; }
tools/perf/util/bpf-loader.h +30 -0
@@ -11,11 +11,18 @@#include "debug.h" struct bpf_object ; +#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"#ifdef HAVE_LIBBPF_SUPPORT struct bpf_object *bpf__prepare_load (const char *filename);void bpf__clear (void ); + +int bpf__probe (struct bpf_object *obj); +int bpf__unprobe (struct bpf_object *obj); +int bpf__strerror_probe (struct bpf_object *obj,int err, +char *buf,size_t size); +#else static inline struct bpf_object *bpf__prepare_load (const char *filename__maybe_unused )
@@ -25,5 +32,28 @@ bpf__prepare_load(const char *filename __maybe_unused) }static inline void bpf__clear (void ) { } + +static inline int bpf__probe (struct bpf_object *obj__maybe_unused ) {return 0 ;} +static inline int bpf__unprobe (struct bpf_object *obj__maybe_unused ) {return 0 ;} + +static inline int +__bpf_strerror (char *buf,size_t size) +{ +if (!size) +return 0 ; +strncpy (buf, +"ERROR: eBPF object loading is disabled during compiling.\n" , + size); + buf[size -1 ] ='\0' ; +return 0 ; +} + +static inline int +bpf__strerror_probe (struct bpf_object *obj__maybe_unused , +int err__maybe_unused , +char *buf,size_t size) +{ +return __bpf_strerror (buf, size); +}#endif #endif
tools/perf/util/parse-events.c +17 -0
@@ -536,6 +536,7 @@ int parse_events_load_bpf_obj(struct parse_events_evlist *data, {int err;char errbuf[BUFSIZ ]; +static bool registered_unprobe_atexit = false;if (IS_ERR (obj) || !obj) { snprintf(errbuf, sizeof(errbuf),
@@ -545,6 +546,22 @@ int parse_events_load_bpf_obj(struct parse_events_evlist *data, }/* +* Register atexit handler before calling bpf__probe() so +* bpf__probe() don't need to unprobe probe points its already +* created when failure. +*/ +if (!registered_unprobe_atexit) { +atexit (bpf__clear); + registered_unprobe_atexit = true; + } + + err =bpf__probe (obj); +if (err) { +bpf__strerror_probe (obj, err, errbuf,sizeof (errbuf)); + goto errout; + } + +/* * Temporary add a dummy event here so we can check whether * basic bpf loader works. Following patches will replace * dummy event by useful evsels.
PATCH 5/31 perf record: Load eBPF object into kernel
In reply to Wang NanThis patch utilizes bpf_object__load() provided by libbpf to load all objects into kernel.
--- tools/perf/util/bpf-loader.c | 22 ++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 11 +++++++++++ tools/perf/util/parse-events.c | 6 ++++++ 3 files changed, 39 insertions(+)
tools/perf/util/bpf-loader.c +22 -0
@@ -243,6 +243,18 @@ int bpf__unprobe(struct bpf_object *obj)return ret; } +int bpf__load (struct bpf_object *obj) +{ +int err; + + err =bpf_object__load (obj); +if (err) { +pr_debug ("bpf: load objects failed\n" ); +return err; + } +return 0 ; +} +#define bpf__strerror_head (err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\
@@ -275,3 +287,13 @@ int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,bpf__strerror_end (buf, size);return 0 ; } + +int bpf__strerror_load (struct bpf_object *obj__maybe_unused , +int err,char *buf,size_t size) +{ +bpf__strerror_head (err, buf, size); +bpf__strerror_entry (EINVAL ,"%s: Are you root and runing a CONFIG_BPF_SYSCALL kernel?" , + emsg) +bpf__strerror_end (buf, size); +return 0 ; +}
tools/perf/util/bpf-loader.h +11 -0
@@ -23,6 +23,9 @@ int bpf__unprobe(struct bpf_object *obj);int bpf__strerror_probe (struct bpf_object *obj,int err,char *buf,size_t size); +int bpf__load (struct bpf_object *obj); +int bpf__strerror_load (struct bpf_object *obj,int err, +char *buf,size_t size);#else static inline struct bpf_object *bpf__prepare_load (const char *filename__maybe_unused )
@@ -35,6 +38,7 @@ static inline void bpf__clear(void) { }static inline int bpf__probe (struct bpf_object *obj__maybe_unused ) {return 0 ;}static inline int bpf__unprobe (struct bpf_object *obj__maybe_unused ) {return 0 ;} +static inline int bpf__load (struct bpf_object *obj__maybe_unused ) {return 0 ; }static inline int __bpf_strerror (char *buf,size_t size)
@@ -55,5 +59,12 @@ bpf__strerror_probe(struct bpf_object *obj __maybe_unused, {return __bpf_strerror (buf, size); } + +static inline int bpf__strerror_load (struct bpf_object *obj__maybe_unused , +int err__maybe_unused , +char *buf,size_t size) +{ +return __bpf_strerror (buf, size); +}#endif #endif
tools/perf/util/parse-events.c +6 -0
@@ -561,6 +561,12 @@ int parse_events_load_bpf_obj(struct parse_events_evlist *data, goto errout; } + err =bpf__load (obj); +if (err) { +bpf__strerror_load (obj, err, errbuf,sizeof (errbuf)); + goto errout; + } +/* * Temporary add a dummy event here so we can check whether * basic bpf loader works. Following patches will replace
Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 05/31] perf record: Load eBPF object into kernel
Em Wed, Oct 14, 2015 at 12:41:16PM +0000, Wang Nan escreveu:
Quoted 2 lines
This patch utilizes
bpf_object__load()provided by libbpf to load all objects into kernel.
So, testing this, using that other proggie, I get;
# perf record --event /tmp/foo.o sleep 1 libbpf: load bpf program failed: Invalid argument libbpf: -- BEGIN DUMP LOG --- libbpf:
libbpf: -- END LOG --
libbpf: failed to load program 'fork=_do_fork'
libbpf: failed to load object '/tmp/foo.o'
event syntax error: '/tmp/foo.o'
\___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel?
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
But:
[root@felicio ~]# grep -i bpf /proc/kallsyms | wc -l
117
[root@felicio ~]# grep -i sys_bpf /proc/kallsyms ffffffff811829d0 T SyS_bpfffffffff811829d0 T sys_bpf
[root@felicio ~]# uname -a
Linux felicio.ghostprotocols.net 4.2.0 #1 SMP Mon Aug 31 12:25:38 BRT 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@felicio ~]#
I'll try updating it to torvalds/master...
For reference, that foo.o eBPF proggie was built with:
[acme@felicio linux]$ cat ~/bin/hello-ebpf
# Taken from 'perf test LLVM'
# Thu Oct 22 12:07:26 BRT 2015
export KERNEL_INC_OPTIONS="-nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.3/include -I/home/acme/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/acme/git/linux/include -Iinclude -I/home/acme/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -Iinclude/generated/uapi -include /home/acme/git/linux/include/linux/kconfig.h"
export WORKING_DIR=/lib/modules/4.2.0/build
export CLANG_SOURCE=-
export CLANG_OPTIONS=-xc
OBJ=/tmp/foo.o
rm -f $OBJ
echo '__attribute__((section("fork=_do_fork"), used)) int fork(void *ctx) {return 0;} char _license[] __attribute__((section("license"), used)) = "GPL";int _version __attribute__((section("version"), used)) = 0x40100;' | clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o /tmp/foo.o && file $OBJ
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 05/31] perf record: Load eBPF object into kernel
Em Fri, Oct 23, 2015 at 01:58:40PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 1 line
I'll try updating it to torvalds/master...
Ok, still the same problem, this time with -vv, what does the following message mean?
libbpf: don't need create maps for /tmp/foo.o
Full output:
[acme@felicio linux]$ sudo ~/bin/perf record -vv --event /tmp/foo.o sleep 1
libbpf: loading /tmp/foo.o
libbpf: section .strtab, size 66, link 0, flags 0, type=3
libbpf: section .text, size 0, link 0, flags 6, type=1
libbpf: section .data, size 0, link 0, flags 3, type=1
libbpf: section .bss, size 0, link 0, flags 3, type=8
libbpf: section fork=_do_fork, size 16, link 0, flags 6, type=1
libbpf: found program fork=_do_fork
libbpf: section license, size 4, link 0, flags 3, type=1
libbpf: license of /tmp/foo.o is GPL
libbpf: section version, size 4, link 0, flags 3, type=1
libbpf: kernel version of /tmp/foo.o is 40100
libbpf: section .symtab, size 96, link 1, flags 0, type=2
bpf: config program 'fork=_do_fork'
symbol:_do_fork file:(null) line:0 offset:0 return:0 lazy:(null)
bpf: config 'fork=_do_fork' is ok
Blacklist: 0xffffffff81016f10-0xffffffff81016f50, do_device_not_available
Blacklist: 0xffffffff810171f0-0xffffffff810173e0, do_debug
Blacklist: 0xffffffff81016eb0-0xffffffff81016f10, fixup_bad_iret
Blacklist: 0xffffffff81016bd0-0xffffffff81016c00, sync_regs
Blacklist: 0xffffffff81016f50-0xffffffff810170a0, do_int3
Blacklist: 0xffffffff810170a0-0xffffffff810171f0, do_general_protection
Blacklist: 0xffffffff81016c00-0xffffffff81016d40, do_trap
Blacklist: 0xffffffff81019a10-0xffffffff81019af0, __die
Blacklist: 0xffffffff810198e0-0xffffffff810199b0, oops_end
Blacklist: 0xffffffff81019830-0xffffffff810198e0, oops_begin
Blacklist: 0xffffffff8101a770-0xffffffff8101a8c0, do_nmi
Blacklist: 0xffffffff8101a680-0xffffffff8101a770, default_do_nmi
Blacklist: 0xffffffff8101a560-0xffffffff8101a5f0, unknown_nmi_error
Blacklist: 0xffffffff8101a5f0-0xffffffff8101a680, io_check_error
Blacklist: 0xffffffff8101a4e0-0xffffffff8101a560, pci_serr_error
Blacklist: 0xffffffff8101a0d0-0xffffffff8101a1f0, nmi_handle
Blacklist: 0xffffffff81027a90-0xffffffff81027b10, debug_stack_reset
Blacklist: 0xffffffff81027a40-0xffffffff81027a90, debug_stack_set_zero
Blacklist: 0xffffffff81027a00-0xffffffff81027a40, is_debug_stack
Blacklist: 0xffffffff8102b400-0xffffffff8102b440, perf_event_nmi_handler
Blacklist: 0xffffffff8104b020-0xffffffff8104b060, perf_ibs_nmi_handler
Blacklist: 0xffffffff810526a0-0xffffffff810526c0, arch_trigger_all_cpu_backtrace_handler
Blacklist: 0xffffffff8105b090-0xffffffff8105b180, longjmp_break_handler
Blacklist: 0xffffffff8105ae21-0xffffffff8105ae30, jprobe_return_end
Blacklist: 0xffffffff8105ae00-0xffffffff8105ae21, jprobe_return
Blacklist: 0xffffffff8105ae70-0xffffffff8105af10, setjmp_pre_handler
Blacklist: 0xffffffff8105b040-0xffffffff8105b090, kprobe_exceptions_notify
Blacklist: 0xffffffff8105af10-0xffffffff8105b040, kprobe_fault_handler
Blacklist: 0xffffffff8105b8f0-0xffffffff8105b9c0, kprobe_debug_handler
Blacklist: 0xffffffff8105b720-0xffffffff8105b8f0, resume_execution
Blacklist: 0xffffffff8105b180-0xffffffff8105b380, trampoline_handler
Blacklist: 0xffffffff8105ada9-0xffffffff8105ae00, kretprobe_trampoline
Blacklist: 0xffffffff8105ada0-0xffffffff8105ada9, kretprobe_trampoline_holder
Blacklist: 0xffffffff8105b5c0-0xffffffff8105b720, kprobe_int3_handler
Blacklist: 0xffffffff8105b520-0xffffffff8105b5c0, reenter_kprobe
Blacklist: 0xffffffff8105b380-0xffffffff8105b520, setup_singlestep
Blacklist: 0xffffffff8105ad80-0xffffffff8105ada0, arch_prepare_kretprobe
Blacklist: 0xffffffff8105ae30-0xffffffff8105ae70, skip_prefixes
Blacklist: 0xffffffff8105ad60-0xffffffff8105ad80, synthesize_relcall
Blacklist: 0xffffffff8105ad40-0xffffffff8105ad60, synthesize_reljump
Blacklist: 0xffffffff8105c0c0-0xffffffff8105c110, setup_detour_execution
Blacklist: 0xffffffff8105c020-0xffffffff8105c0c0, optimized_callback
Blacklist: 0xffffffff8105c860-0xffffffff8105c970, kprobe_ftrace_handler
Blacklist: 0xffffffff8105c970-0xffffffff8105c9c0, skip_singlestep
Blacklist: 0xffffffff8105fec0-0xffffffff8105ff70, do_async_page_fault
Blacklist: 0xffffffff8105f820-0xffffffff8105f850, kvm_read_and_reset_pf_reason
Blacklist: 0xffffffff810607a0-0xffffffff810607b0, native_load_idt
Blacklist: 0xffffffff81060a30-0xffffffff81060a80, native_set_debugreg
Blacklist: 0xffffffff81060a80-0xffffffff81060ad0, native_get_debugreg
Blacklist: 0xffffffff81064f00-0xffffffff81064ff0, trace_do_page_fault
Blacklist: 0xffffffff81064e80-0xffffffff81064f00, do_page_fault
Blacklist: 0xffffffff81064a90-0xffffffff81064e80, __do_page_fault
Blacklist: 0xffffffff81064900-0xffffffff81064a90, spurious_fault
Blacklist: 0xffffffff81063e50-0xffffffff810640d0, vmalloc_fault
Blacklist: 0xffffffff810bb8d0-0xffffffff810bb930, notify_die
Blacklist: 0xffffffff810bb7f0-0xffffffff810bb810, atomic_notifier_call_chain
Blacklist: 0xffffffff810bb7d0-0xffffffff810bb7f0, __atomic_notifier_call_chain
Blacklist: 0xffffffff810bb760-0xffffffff810bb7d0, notifier_call_chain
Blacklist: 0xffffffff8113fb70-0xffffffff8113fbb0, dump_kprobe
Blacklist: 0xffffffff8113fdf0-0xffffffff8113ffa0, pre_handler_kretprobe
Blacklist: 0xffffffff81141290-0xffffffff81141360, cleanup_rp_inst
Blacklist: 0xffffffff811408a0-0xffffffff811409c0, kprobe_flush_task
Blacklist: 0xffffffff8113fce0-0xffffffff8113fd00, kretprobe_table_unlock
Blacklist: 0xffffffff8113fcb0-0xffffffff8113fce0, kretprobe_hash_unlock
Blacklist: 0xffffffff8113fc80-0xffffffff8113fcb0, kretprobe_table_lock
Blacklist: 0xffffffff8113fc40-0xffffffff8113fc80, kretprobe_hash_lock
Blacklist: 0xffffffff81140800-0xffffffff811408a0, recycle_rp_inst
Blacklist: 0xffffffff81140300-0xffffffff81140370, kprobes_inc_nmissed_count
Blacklist: 0xffffffff8113f840-0xffffffff8113f880, aggr_break_handler
Blacklist: 0xffffffff8113f800-0xffffffff8113f840, aggr_fault_handler
Blacklist: 0xffffffff8113fa30-0xffffffff8113fad0, aggr_post_handler
Blacklist: 0xffffffff8113f990-0xffffffff8113fa30, aggr_pre_handler
Blacklist: 0xffffffff8113f900-0xffffffff8113f990, opt_pre_handler
Blacklist: 0xffffffff8113f7b0-0xffffffff8113f800, get_kprobe
Blacklist: 0xffffffff81173100-0xffffffff811731e0, perf_trace_buf_prepare
Blacklist: 0xffffffff81179380-0xffffffff811793e0, kretprobe_dispatcher
Blacklist: 0xffffffff8117a650-0xffffffff8117a6a0, kprobe_dispatcher
Blacklist: 0xffffffff81178280-0xffffffff811784d0, kretprobe_perf_func
Blacklist: 0xffffffff81178030-0xffffffff81178280, kprobe_perf_func
Blacklist: 0xffffffff81178ff0-0xffffffff81179380, kretprobe_trace_func
Blacklist: 0xffffffff8117a2e0-0xffffffff8117a650, kprobe_trace_func
Blacklist: 0xffffffff81178e90-0xffffffff81178eb0, fetch_symbol_string_size
Blacklist: 0xffffffff81178010-0xffffffff81178030, fetch_symbol_string
Blacklist: 0xffffffff8117a210-0xffffffff8117a230, fetch_symbol_u64
Blacklist: 0xffffffff8117a2c0-0xffffffff8117a2e0, fetch_symbol_u32
Blacklist: 0xffffffff8117a160-0xffffffff8117a180, fetch_symbol_u16
Blacklist: 0xffffffff8117a0b0-0xffffffff8117a0d0, fetch_symbol_u8
Blacklist: 0xffffffff81178de0-0xffffffff81178e90, fetch_memory_string_size
Blacklist: 0xffffffff81177fc0-0xffffffff81178010, fetch_memory_string
Blacklist: 0xffffffff8117a180-0xffffffff8117a210, fetch_memory_u64
Blacklist: 0xffffffff8117a230-0xffffffff8117a2c0, fetch_memory_u32
Blacklist: 0xffffffff8117a0d0-0xffffffff8117a160, fetch_memory_u16
Blacklist: 0xffffffff8117a020-0xffffffff8117a0b0, fetch_memory_u8
Blacklist: 0xffffffff81177f90-0xffffffff81177fc0, fetch_stack_u64
Blacklist: 0xffffffff81177f60-0xffffffff81177f90, fetch_stack_u32
Blacklist: 0xffffffff81177f30-0xffffffff81177f60, fetch_stack_u16
Blacklist: 0xffffffff81177f00-0xffffffff81177f30, fetch_stack_u8
Blacklist: 0xffffffff8117e210-0xffffffff8117e220, fetch_user_stack_address
Blacklist: 0xffffffff8117e200-0xffffffff8117e210, fetch_kernel_stack_address
Blacklist: 0xffffffff8117e190-0xffffffff8117e200, fetch_bitfield_u64
Blacklist: 0xffffffff8117e120-0xffffffff8117e190, fetch_bitfield_u32
Blacklist: 0xffffffff8117e0b0-0xffffffff8117e120, fetch_bitfield_u16
Blacklist: 0xffffffff8117e040-0xffffffff8117e0b0, fetch_bitfield_u8
Blacklist: 0xffffffff8117e590-0xffffffff8117e630, free_deref_fetch_param
Blacklist: 0xffffffff8117e500-0xffffffff8117e590, update_deref_fetch_param
Blacklist: 0xffffffff8117dfc0-0xffffffff8117e040, fetch_deref_string_size
Blacklist: 0xffffffff8117df40-0xffffffff8117dfc0, fetch_deref_string
Blacklist: 0xffffffff8117dec0-0xffffffff8117df40, fetch_deref_u64
Blacklist: 0xffffffff8117de40-0xffffffff8117dec0, fetch_deref_u32
Blacklist: 0xffffffff8117ddc0-0xffffffff8117de40, fetch_deref_u16
Blacklist: 0xffffffff8117dd50-0xffffffff8117ddc0, fetch_deref_u8
Blacklist: 0xffffffff8117dd40-0xffffffff8117dd50, fetch_retval_u64
Blacklist: 0xffffffff8117dd30-0xffffffff8117dd40, fetch_retval_u32
Blacklist: 0xffffffff8117dd20-0xffffffff8117dd30, fetch_retval_u16
Blacklist: 0xffffffff8117dd10-0xffffffff8117dd20, fetch_retval_u8
Blacklist: 0xffffffff8117e4e0-0xffffffff8117e500, fetch_reg_u64
Blacklist: 0xffffffff8117e4c0-0xffffffff8117e4e0, fetch_reg_u32
Blacklist: 0xffffffff8117e4a0-0xffffffff8117e4c0, fetch_reg_u16
Blacklist: 0xffffffff8117e480-0xffffffff8117e4a0, fetch_reg_u8
Blacklist: 0xffffffff8117e420-0xffffffff8117e480, print_type_string
Blacklist: 0xffffffff8117e3e0-0xffffffff8117e420, print_type_s64
Blacklist: 0xffffffff8117e3a0-0xffffffff8117e3e0, print_type_s32
Blacklist: 0xffffffff8117e360-0xffffffff8117e3a0, print_type_s16
Blacklist: 0xffffffff8117e320-0xffffffff8117e360, print_type_s8
Blacklist: 0xffffffff8117e2e0-0xffffffff8117e320, print_type_u64
Blacklist: 0xffffffff8117e2a0-0xffffffff8117e2e0, print_type_u32
Blacklist: 0xffffffff8117e260-0xffffffff8117e2a0, print_type_u16
Blacklist: 0xffffffff8117e220-0xffffffff8117e260, print_type_u8
Blacklist: 0xffffffff8119a9a0-0xffffffff8119a9c0, context_tracking_user_exit
Blacklist: 0xffffffff8119a8d0-0xffffffff8119a9a0, context_tracking_exit
Blacklist: 0xffffffff8119aad0-0xffffffff8119aaf0, context_tracking_user_enter
Blacklist: 0xffffffff8119a9c0-0xffffffff8119aad0, context_tracking_enter
Blacklist: 0xffffffff8139f400-0xffffffff8139f470, nmi_cpu_backtrace Looking at the vmlinux_path (7 entries long)
Using /lib/modules/4.3.0-rc6+/build/vmlinux for symbols
Open Debuginfo file: /lib/modules/4.3.0-rc6+/build/vmlinux
Try to find probe point from debuginfo.
Matched function: _do_fork
Probe point found: _do_fork+0
Found 1 probe_trace_events.
Opening /sys/kernel/debug/tracing//kprobe_events write=1
Writing event: p:perf_bpf_probe/fork _text+638512
libbpf: don't need create maps for /tmp/foo.o
libbpf: load bpf program failed: Invalid argument
libbpf: -- BEGIN DUMP LOG ---
libbpf:
libbpf: -- END LOG --
libbpf: failed to load program 'fork=_do_fork'
libbpf: failed to load object '/tmp/foo.o'
bpf: load objects failed
event syntax error: '/tmp/foo.o'
\___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel?
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
Opening /sys/kernel/debug/tracing//kprobe_events write=1
Opening /sys/kernel/debug/tracing//uprobe_events write=1
Parsing probe_events: p:perf_bpf_probe/fork _text+638512
Group:perf_bpf_probe Event:fork probe:p
Writing event: -:perf_bpf_probe/fork
[acme@felicio linux]$ uname -a
Linux felicio.ghostprotocols.net 4.3.0-rc6+ #1 SMP Fri Oct 23 16:40:25 BRT 2015 x86_64 x86_64 x86_64 GNU/Linux [acme@felicio linux]$
Re: [PATCH 05/31] perf record: Load eBPF object into kernel
Hi Arnaldo,
Quoted 17 lines · Arnaldo Carvalho de Melo
libbpf: load bpf program failed: Invalid argument
libbpf: -- BEGIN DUMP LOG ---
libbpf:libbpf: -- END LOG --
libbpf: failed to load program 'fork=_do_fork'
libbpf: failed to load object '/tmp/foo.o'
bpf: load objects failed
event syntax error: '/tmp/foo.o'
\___ Invalid argument: Are you root and runing aCONFIG_BPF_SYSCALLkernel?(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
Quoted 10 lines
-e, --event <event> event selector. use 'perf list' to list available events
Opening/sys/kernel/debug/tracing//kprobe_eventswrite=1
Opening/sys/kernel/debug/tracing//uprobe_eventswrite=1
Parsingprobe_events: p:perf_bpf_probe/fork _text+638512
Group:perf_bpf_probe Event:fork probe:p
Writing event: -:perf_bpf_probe/fork
[acme@felicio linux]$ uname -a
Linux felicio.ghostprotocols.net 4.3.0-rc6+ #1 SMP Fri Oct 23 16:40:25 BRT 2015x86_64x86_64x86_64GNU/Linux [acme@felicio linux]$
Please have a look at patch [1]. With that patch kernel will dump something into log buffer. In your case, you will see:
root@sandybridge:~# /tmp/perf record -e /tmp/test_config_base.o ls libbpf: load bpf program failed: Invalid argument libbpf: -- BEGIN DUMP LOG --- libbpf: Kernel version mismatch: 0x40400 != 0x40300
libbpf: -- END LOG --
libbpf: failed to load program 'func_write=sys_write'
libbpf: failed to load object '/tmp/test_config_base.o' event syntax error: '/tmp/test_config_base.o'
\___ Invalid argument: Are you root and runing aCONFIG_BPF_SYSCALL kernel?
(add -v to see detail)
Run 'perf list' for a list of valid events
...
And I'd like to improve the error message output by perf.
Do you want me to send a patch for fixing this problem or modifying existing patches in my git tree?
Thank you.
Re: [PATCH 05/31] perf record: Load eBPF object into kernel
In reply to Arnaldo Carvalho de Melo发自我的 iPhone
Quoted 6 lines · 在 2015年10月24日,上午12:58,Arnaldo Carvalho de Melo <[email protected]> 写道:
Em Wed, Oct 14, 2015 at 12:41:16PM +0000, Wang Nan escreveu:
Quoted 2 lines
This patch utilizes
bpf_object__load()provided by libbpf to load all objects into kernel.
Quoted 8 lines
So, testing this, using that other proggie, I get;
# perf record --event /tmp/foo.o sleep 1 libbpf: load bpf program failed: Invalid argument libbpf: -- BEGIN DUMP LOG --- libbpf:libbpf: -- END LOG --
This means your kernel version and version section in your BPF object doesn't match each other. Please try to pass a correct version code when compiling the object. I think we should output something through error log so by -v we can see the problem. I told Alexei about this problem once. Let me improve perf's error message next week.
Thank you.
Quoted 57 lines
libbpf: failed to load program 'fork=_do_fork'
libbpf: failed to load object '/tmp/foo.o'
event syntax error: '/tmp/foo.o'
\___ Invalid argument: Are you root and runing aCONFIG_BPF_SYSCALLkernel?(add -v to see detail)
Run 'perf list' for a list of valid eventsUsage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]-e, --event <event> event selector. use 'perf list' to list available events
But:
[root@felicio ~]# grep -i bpf
/proc/kallsyms| wc -l
117
[root@felicio ~]# grep -isys_bpf/proc/kallsymsffffffff811829d0TSyS_bpfffffffff811829d0Tsys_bpf
[root@felicio ~]# uname -a
Linux felicio.ghostprotocols.net 4.2.0 #1 SMP Mon Aug 31 12:25:38 BRT 2015x86_64x86_64x86_64GNU/Linux
[root@felicio ~]#I'll try updating it to torvalds/master...
For reference, that foo.o eBPF proggie was built with:
[acme@felicio linux]$ cat ~/bin/hello-ebpf
# Taken from 'perf test LLVM'
# Thu Oct 22 12:07:26 BRT 2015export KERNEL_INC_OPTIONS="-nostdinc -isystem
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/include(quoted diff) +0
-6-I/home/acme/git/linux/arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated -I/home/acme/git/linux/include -Iinclude -I/home/acme/git/linux/arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I/home/acme/git/linux/include/uapi -Iinclude/generated/uapi -include
/home/acme/git/linux/include/linux/kconfig.h"
export WORKING_DIR=/lib/modules/4.2.0/build
export CLANG_SOURCE=-
export CLANG_OPTIONS=-xcOBJ=/tmp/foo.o
rm -f $OBJ
echo '__attribute__((section("fork=_do_fork"), used))intfork(void *ctx){return 0;} char _license[]__attribute__((section("license"), used))= "GPL";int_version__attribute__((section("version"), used))=0x40100;' | clang -D__KERNEL__ $CLANG_OPTIONS $KERNEL_INC_OPTIONS -Wno-unused-value -Wno-pointer-sign -working-directory $WORKING_DIR -c "$CLANG_SOURCE" -target bpf -O2 -o/tmp/foo.o&& file $OBJ- Arnaldo
tip-bot for Wang Nan
merge bot[tip:perf/core] perf tools: Load eBPF object into kernel
In reply to Wang Nanperf tools: Load eBPF object into kernel
Commit-ID: 1e5e3ee8ff3877db6943032b54a6ac21c095affd Gitweb: http://git.kernel.org/tip/1e5e3ee8ff3877db6943032b54a6ac21c095affd Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:16 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 28 Oct 2015 13:09:50 -0300
perf tools: Load eBPF object into kernel
This patch utilizes bpf_object__load() provided by libbpf to load all objects into kernel.
Committer notes:
Testing it:
When using an incorrect kernel version number, i.e., having this in your eBPF proggie:
int _version __attribute__((section("version"), used)) = 0x40100;For a 4.3.0-rc6+ kernel, say, this happens and needs checking at event parsing time, to provide a better error report to the user:
# perf record --event /tmp/foo.o sleep 1 libbpf: load bpf program failed: Invalid argument libbpf: -- BEGIN DUMP LOG --- libbpf:
libbpf: -- END LOG --
libbpf: failed to load program 'fork=_do_fork'
libbpf: failed to load object '/tmp/foo.o'
event syntax error: '/tmp/foo.o'
\___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel?
(add -v to see detail)
Run 'perf list' for a list of valid events
Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]
-e, --event <event> event selector. use 'perf list' to list available events
If we instead make it match, i.e. use 0x40300 on this v4.3.0-rc6+ kernel, the whole process goes thru:
# perf record --event /tmp/foo.o -a usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.202 MB perf.data ]
# perf evlist -v
/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period,
sample_freq }: 4000, sample_type: IP|TID|TIME|CPU|PERIOD, disabled: 1,
inherit: 1, mmap: 1, comm: 1, freq: 1, task: 1, sample_id_all: 1,
exclude_guest: 1, mmap2: 1, comm_exec: 1
#--- tools/perf/util/bpf-loader.c | 22 ++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 11 +++++++++++ tools/perf/util/parse-events.c | 6 ++++++ 3 files changed, 39 insertions(+)
tools/perf/util/bpf-loader.c +22 -0
@@ -243,6 +243,18 @@ int bpf__unprobe(struct bpf_object *obj)return ret; } +int bpf__load (struct bpf_object *obj) +{ +int err; + + err =bpf_object__load (obj); +if (err) { +pr_debug ("bpf: load objects failed\n" ); +return err; + } +return 0 ; +} +#define bpf__strerror_head (err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\
@@ -275,3 +287,13 @@ int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,bpf__strerror_end (buf, size);return 0 ; } + +int bpf__strerror_load (struct bpf_object *obj__maybe_unused , +int err,char *buf,size_t size) +{ +bpf__strerror_head (err, buf, size); +bpf__strerror_entry (EINVAL ,"%s: Are you root and runing a CONFIG_BPF_SYSCALL kernel?" , + emsg) +bpf__strerror_end (buf, size); +return 0 ; +}
tools/perf/util/bpf-loader.h +11 -0
@@ -23,6 +23,9 @@ int bpf__unprobe(struct bpf_object *obj);int bpf__strerror_probe (struct bpf_object *obj,int err,char *buf,size_t size); +int bpf__load (struct bpf_object *obj); +int bpf__strerror_load (struct bpf_object *obj,int err, +char *buf,size_t size);#else static inline struct bpf_object *bpf__prepare_load (const char *filename__maybe_unused )
@@ -35,6 +38,7 @@ static inline void bpf__clear(void) { }static inline int bpf__probe (struct bpf_object *obj__maybe_unused ) {return 0 ;}static inline int bpf__unprobe (struct bpf_object *obj__maybe_unused ) {return 0 ;} +static inline int bpf__load (struct bpf_object *obj__maybe_unused ) {return 0 ; }static inline int __bpf_strerror (char *buf,size_t size)
@@ -55,5 +59,12 @@ bpf__strerror_probe(struct bpf_object *obj __maybe_unused, {return __bpf_strerror (buf, size); } + +static inline int bpf__strerror_load (struct bpf_object *obj__maybe_unused , +int err__maybe_unused , +char *buf,size_t size) +{ +return __bpf_strerror (buf, size); +}#endif #endif
tools/perf/util/parse-events.c +6 -0
@@ -561,6 +561,12 @@ int parse_events_load_bpf_obj(struct parse_events_evlist *data, goto errout; } + err =bpf__load (obj); +if (err) { +bpf__strerror_load (obj, err, errbuf,sizeof (errbuf)); + goto errout; + } +/* * Temporary add a dummy event here so we can check whether * basic bpf loader works. Following patches will replace
PATCH 6/31 perf tools: Collect perf_evsel in BPF object files
In reply to Wang NanThis patch collects 'struct perf_evsel' for every probing points in BPF object file(s) and fill 'struct evlist'. The previous introduced dummy event now removed. After this patch, following command:
# perf record --event filter.o ls
Can trace on each probing points defined in filter.o.
The core of this patch is bpf__foreach_tev(), which calls a callback function for each 'struct probe_trace_event' events for a bpf program with their file descriptors. Callback function add_bpf_event() creates evsels by calling parse_events_add_tracepoint().
Since bpf-loader.c will not be built if libbpf is turned off, an empty bpf__foreach_tev() is defined in bpf-loader.h to avoid compiling error.
--- tools/perf/util/bpf-loader.c | 40 ++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 14 ++++++++++++ tools/perf/util/parse-events.c | 52 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 99 insertions(+), 7 deletions(-)
tools/perf/util/bpf-loader.c +40 -0
@@ -255,6 +255,46 @@ int bpf__load(struct bpf_object *obj)return 0 ; } +int bpf__foreach_tev (struct bpf_object *obj, +bpf_prog_iter_callback_t func, +void *arg) +{ +struct bpf_program *prog; +int err; + +bpf_object__for_each_program (prog, obj) { +struct probe_trace_event *tev; +struct perf_probe_event *pev; +struct bpf_prog_priv *priv; +int i, fd; + + err =bpf_program__get_private (prog, + (void **)&priv); +if (err || !priv) { +pr_debug ("bpf: failed to get private field\n" ); +return -EINVAL ; + } + + pev = &priv->pev; +for (i =0 ; i < pev->ntevs; i++) { + tev = &pev->tevs[i]; + + fd =bpf_program__fd (prog); +if (fd <0 ) { +pr_debug ("bpf: failed to get file descriptor\n" ); +return fd; + } + + err = (*func)(tev, fd, arg); +if (err) { +pr_debug ("bpf: call back failed, stop iterate\n" ); +return err; + } + } + } +return 0 ; +} +#define bpf__strerror_head (err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\
tools/perf/util/bpf-loader.h +14 -0
@@ -8,11 +8,15 @@#include <linux/compiler.h> #include <linux/err.h> #include <string.h> +#include "probe-event.h" #include "debug.h" struct bpf_object ;#define PERF_BPF_PROBE_GROUP "perf_bpf_probe" +typedef int (*bpf_prog_iter_callback_t )(struct probe_trace_event *tev, +int fd,void *arg); +#ifdef HAVE_LIBBPF_SUPPORT struct bpf_object *bpf__prepare_load (const char *filename);
@@ -26,6 +30,8 @@ int bpf__strerror_probe(struct bpf_object *obj, int err,int bpf__load (struct bpf_object *obj);int bpf__strerror_load (struct bpf_object *obj,int err,char *buf,size_t size); +int bpf__foreach_tev (struct bpf_object *obj, +bpf_prog_iter_callback_t func,void *arg);#else static inline struct bpf_object *bpf__prepare_load (const char *filename__maybe_unused )
@@ -41,6 +47,14 @@ static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0static inline int bpf__load (struct bpf_object *obj__maybe_unused ) {return 0 ; }static inline int +bpf__foreach_tev (struct bpf_object *obj__maybe_unused , +bpf_prog_iter_callback_t func__maybe_unused , +void *arg__maybe_unused ) +{ +return 0 ; +} + +static inline int __bpf_strerror (char *buf,size_t size) {if (!size)
tools/perf/util/parse-events.c +45 -7
70 lines not on this page · raw message
Signature · 1 line
1.8.3.4
tip-bot for Wang Nan
merge bot[tip:perf/core] perf bpf: Collect perf_evsel in BPF object files
perf bpf: Collect perf_evsel in BPF object files
Commit-ID: 4edf30e39e6cff32390eaff6a1508969b3cd967b Gitweb: http://git.kernel.org/tip/4edf30e39e6cff32390eaff6a1508969b3cd967b Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:17 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 28 Oct 2015 13:11:59 -0300
perf bpf: Collect perf_evsel in BPF object files
This patch creates a 'struct perf_evsel' for every probe in a BPF object file(s) and fills 'struct evlist' with them. The previously introduced dummy event is now removed. After this patch, the following command:
# perf record --event filter.o ls
Can trace on each of the probes defined in filter.o.
The core of this patch is bpf__foreach_tev(), which calls a callback function for each 'struct probe_trace_event' event for a bpf program with each associated file descriptors. The add_bpf_event() callback creates evsels by calling parse_events_add_tracepoint().
Since bpf-loader.c will not be built if libbpf is turned off, an empty bpf__foreach_tev() is defined in bpf-loader.h to avoid build errors.
Committer notes:
Before:
# /tmp/oldperf record --event /tmp/foo.o -a usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.198 MB perf.data ]
# perf evlist
/tmp/foo.o
# perf evlist -v
/tmp/foo.o: type: 1, size: 112, config: 0x9, { sample_period,
sample_freq }: 4000, sample_type: IP|TID|TIME|CPU|PERIOD, disabled: 1,
inherit: 1, mmap: 1, comm: 1, freq: 1, task: 1, sample_id_all: 1,
exclude_guest: 1, mmap2: 1, comm_exec: 1I.e. we create just the PERF_TYPE_SOFTWARE (type: 1), PERF_COUNT_SW_DUMMY(config 0x9) event, now, with this patch:
# perf record --event /tmp/foo.o -a usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.210 MB perf.data ]
# perf evlist -v
perf_bpf_probe:fork: type: 2, size: 112, config: 0x6bd, { sample_period,
sample_freq }: 1, sample_type: IP|TID|TIME|CPU|PERIOD|RAW, disabled: 1,
inherit: 1, mmap: 1, comm: 1, task: 1, sample_id_all: 1, exclude_guest:
1, mmap2: 1, comm_exec: 1
#We now have a PERF_TYPE_SOFTWARE (type: 1), but the config states 0x6bd, which is how, after setting up the event via the kprobes interface, the 'perf_bpf_probe:fork' event is accessible via the perf_event_open syscall. This is all transient, as soon as the 'perf record' session ends, these probes will go away.
To see how it looks like, lets try doing a neverending session, one that expects a control+C to end:
# perf record --event /tmp/foo.o -a
So, with that in place, we can use 'perf probe' to see what is in place:
# perf probe -l
perf_bpf_probe:fork (on _do_fork@acme/git/linux/kernel/fork.c)We also can use debugfs:
[root@felicio ~]# cat /sys/kernel/debug/tracing/kprobe_events p:perf_bpf_probe/fork _text+638512
Ok, now lets stop and see if we got some forks:
[root@felicio linux]# perf record --event /tmp/foo.o -a ^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.325 MB perf.data (111 samples) ]
[root@felicio linux]# perf script
sshd 1271 [003] 81797.507678: perf_bpf_probe:fork: (ffffffff8109be30)
sshd 18309 [000] 81797.524917: perf_bpf_probe:fork: (ffffffff8109be30)
sshd 18309 [001] 81799.381603: perf_bpf_probe:fork: (ffffffff8109be30)
sshd 18309 [001] 81799.408635: perf_bpf_probe:fork: (ffffffff8109be30)<SNIP>
Sure enough, we have 111 forks :-)
Callchains seems to work as well:
# perf report --stdio --no-child
# To display the perf.data header info, please use --header/--header-only options.
#
# Total Lost Samples: 0
#
# Samples: 562 of event 'perf_bpf_probe:fork'
# Event count (approx.): 562
#
# Overhead Command Shared Object Symbol
# ........ ........ ................ ............
#
44.66% sh [kernel.vmlinux] [k] _do_fork
|
---_do_fork
entry_SYSCALL_64_fastpath
__libc_fork
make_child26.16% make [kernel.vmlinux] [k] _do_fork
<SNIP>
#
--- tools/perf/util/bpf-loader.c | 40 ++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 14 ++++++++++++ tools/perf/util/parse-events.c | 52 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 99 insertions(+), 7 deletions(-)
tools/perf/util/bpf-loader.c +40 -0
@@ -255,6 +255,46 @@ int bpf__load(struct bpf_object *obj)return 0 ; } +int bpf__foreach_tev (struct bpf_object *obj, +bpf_prog_iter_callback_t func, +void *arg) +{ +struct bpf_program *prog; +int err; + +bpf_object__for_each_program (prog, obj) { +struct probe_trace_event *tev; +struct perf_probe_event *pev; +struct bpf_prog_priv *priv; +int i, fd; + + err =bpf_program__get_private (prog, + (void **)&priv); +if (err || !priv) { +pr_debug ("bpf: failed to get private field\n" ); +return -EINVAL ; + } + + pev = &priv->pev; +for (i =0 ; i < pev->ntevs; i++) { + tev = &pev->tevs[i]; + + fd =bpf_program__fd (prog); +if (fd <0 ) { +pr_debug ("bpf: failed to get file descriptor\n" ); +return fd; + } + + err = (*func)(tev, fd, arg); +if (err) { +pr_debug ("bpf: call back failed, stop iterate\n" ); +return err; + } + } + } +return 0 ; +} +#define bpf__strerror_head (err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\
tools/perf/util/bpf-loader.h +14 -0
@@ -8,11 +8,15 @@#include <linux/compiler.h> #include <linux/err.h> #include <string.h> +#include "probe-event.h" #include "debug.h" struct bpf_object ;#define PERF_BPF_PROBE_GROUP "perf_bpf_probe" +typedef int (*bpf_prog_iter_callback_t )(struct probe_trace_event *tev, +int fd,void *arg); +#ifdef HAVE_LIBBPF_SUPPORT struct bpf_object *bpf__prepare_load (const char *filename);
@@ -26,6 +30,8 @@ int bpf__strerror_probe(struct bpf_object *obj, int err,int bpf__load (struct bpf_object *obj);int bpf__strerror_load (struct bpf_object *obj,int err,char *buf,size_t size); +int bpf__foreach_tev (struct bpf_object *obj, +bpf_prog_iter_callback_t func,void *arg);#else static inline struct bpf_object *bpf__prepare_load (const char *filename__maybe_unused )
@@ -41,6 +47,14 @@ static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0static inline int bpf__load (struct bpf_object *obj__maybe_unused ) {return 0 ; }static inline int +bpf__foreach_tev (struct bpf_object *obj__maybe_unused , +bpf_prog_iter_callback_t func__maybe_unused , +void *arg__maybe_unused ) +{ +return 0 ; +} + +static inline int __bpf_strerror (char *buf,size_t size) {if (!size)
tools/perf/util/parse-events.c +45 -7
70 lines not on this page · raw message
PATCH 7/31 perf tools: Attach eBPF program to perf event
In reply to Wang NanThis is the final patch which makes basic BPF filter work. After applying this patch, users are allowed to use BPF filter like:
# perf record --event ./hello_world.o ls
A bpf_fd field is appended to 'struct evsel', and setup during the callback function add_bpf_event() for each 'probe_trace_event'.
PERF_EVENT_IOC_SET_BPF ioctl is used to attach eBPF program to a newly created perf event. The file descriptor of the eBPF program is passed to perf record using previous patches, and stored into evsel->bpf_fd.
It is possible that different perf event are created for one kprobe events for different CPUs. In this case, when trying to call the ioctl, EEXIST will be return. This patch doesn't treat it as an error.
--- tools/perf/util/evsel.c | 17 +++++++++++++++++ tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 6 ++++++ 3 files changed, 24 insertions(+)
tools/perf/util/evsel.c +17 -0
@@ -208,6 +208,7 @@ void perf_evsel__init(struct perf_evsel *evsel, evsel->unit ="" ; evsel->scale =1.0 ; evsel->evlist =NULL ; + evsel->bpf_fd =-1 ;INIT_LIST_HEAD (&evsel->node);INIT_LIST_HEAD (&evsel->config_terms); perf_evsel__object.init (evsel);
@@ -1346,6 +1347,22 @@ retry_open: err); goto try_fallback; } + +if (evsel->bpf_fd >=0 ) { +int evt_fd =FD (evsel, cpu, thread); +int bpf_fd = evsel->bpf_fd; + + err =ioctl (evt_fd, +PERF_EVENT_IOC_SET_BPF , + bpf_fd); +if (err && errno !=EEXIST ) { +pr_err ("failed to attach bpf fd %d: %s\n" , + bpf_fd,strerror (errno)); + err = -EINVAL ; + goto out_close; + } + } + set_rlimit =NO_CHANGE ;/*
tools/perf/util/evsel.h +1 -0
@@ -121,6 +121,7 @@ struct perf_evsel {char *group_name;bool cmdline_group_boundary;struct list_head config_terms; +int bpf_fd; };union u64_swap {
tools/perf/util/parse-events.c +6 -0
@@ -542,6 +542,7 @@ static int add_bpf_event(struct probe_trace_event *tev, int fd,struct __add_bpf_event_param *param = _param;struct parse_events_evlist *evlist = param->data;struct list_head *list = param->list; +struct perf_evsel *pos;int err; pr_debug("add bpf event %s:%s and attach bpf program %d\n",
@@ -562,6 +563,11 @@ static int add_bpf_event(struct probe_trace_event *tev, int fd, }pr_debug ("adding %s:%s\n" , tev->group, tev->event); +list_for_each_entry (pos, &new_evsels, node) { +pr_debug ("adding %s:%s to %p\n" , + tev->group, tev->event, pos); + pos->bpf_fd = fd; + }list_splice (&new_evsels, list);return 0 ; }
Signature · 1 line
1.8.3.4
tip-bot for Wang Nan
merge bot[tip:perf/core] perf bpf: Attach eBPF filter to perf event
perf bpf: Attach eBPF filter to perf event
Commit-ID: 1f45b1d49073541947193bd7dac9e904142576aa Gitweb: http://git.kernel.org/tip/1f45b1d49073541947193bd7dac9e904142576aa Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:18 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 29 Oct 2015 17:16:22 -0300
perf bpf: Attach eBPF filter to perf event
This is the final patch which makes basic BPF filter work. After applying this patch, users are allowed to use BPF filter like:
# perf record --event ./hello_world.o ls
A bpf_fd field is appended to 'struct evsel', and setup during the callback function add_bpf_event() for each 'probe_trace_event'.
PERF_EVENT_IOC_SET_BPF ioctl is used to attach eBPF program to a newly created perf event. The file descriptor of the eBPF program is passed to perf record using previous patches, and stored into evsel->bpf_fd.
It is possible that different perf event are created for one kprobe events for different CPUs. In this case, when trying to call the ioctl, EEXIST will be return. This patch doesn't treat it as an error.
Committer note:
The bpf proggie used so far:
__attribute__((section("fork=_do_fork"), used))
int fork(void *ctx)
{
return 0;}
char _license[] __attribute__((section("license"), used)) = "GPL";
int _version __attribute__((section("version"), used)) = 0x40300;failed to produce any samples, even with forks happening and it being running in system wide mode.
That is because now the filter is being associated, and the code above always returns zero, meaning that all forks will be probed but filtered away ;-/
Change it to 'return 1;' instead and after that:
# trace --no-syscalls --event /tmp/foo.o
0.000 perf_bpf_probe:fork:(ffffffff8109be30))
2.333 perf_bpf_probe:fork:(ffffffff8109be30))
3.725 perf_bpf_probe:fork:(ffffffff8109be30))
4.550 perf_bpf_probe:fork:(ffffffff8109be30))
^C#And it works with all tools, including 'perf trace'.
--- tools/perf/util/evsel.c | 17 +++++++++++++++++ tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 6 ++++++ 3 files changed, 24 insertions(+)
tools/perf/util/evsel.c +17 -0
@@ -208,6 +208,7 @@ void perf_evsel__init(struct perf_evsel *evsel, evsel->unit ="" ; evsel->scale =1.0 ; evsel->evlist =NULL ; + evsel->bpf_fd =-1 ;INIT_LIST_HEAD (&evsel->node);INIT_LIST_HEAD (&evsel->config_terms); perf_evsel__object.init (evsel);
@@ -1356,6 +1357,22 @@ retry_open: err); goto try_fallback; } + +if (evsel->bpf_fd >=0 ) { +int evt_fd =FD (evsel, cpu, thread); +int bpf_fd = evsel->bpf_fd; + + err =ioctl (evt_fd, +PERF_EVENT_IOC_SET_BPF , + bpf_fd); +if (err && errno !=EEXIST ) { +pr_err ("failed to attach bpf fd %d: %s\n" , + bpf_fd,strerror (errno)); + err = -EINVAL ; + goto out_close; + } + } + set_rlimit =NO_CHANGE ;/*
tools/perf/util/evsel.h +1 -0
@@ -123,6 +123,7 @@ struct perf_evsel {char *group_name;bool cmdline_group_boundary;struct list_head config_terms; +int bpf_fd; };union u64_swap {
tools/perf/util/parse-events.c +6 -0
@@ -542,6 +542,7 @@ static int add_bpf_event(struct probe_trace_event *tev, int fd,struct __add_bpf_event_param *param = _param;struct parse_events_evlist *evlist = param->data;struct list_head *list = param->list; +struct perf_evsel *pos;int err; pr_debug("add bpf event %s:%s and attach bpf program %d\n",
@@ -562,6 +563,11 @@ static int add_bpf_event(struct probe_trace_event *tev, int fd, }pr_debug ("adding %s:%s\n" , tev->group, tev->event); +list_for_each_entry (pos, &new_evsels, node) { +pr_debug ("adding %s:%s to %p\n" , + tev->group, tev->event, pos); + pos->bpf_fd = fd; + }list_splice (&new_evsels, list);return 0 ; }
PATCH 8/31 perf record: Add clang options for compiling BPF scripts
In reply to Wang NanAlthough previous patch allows setting BPF compiler related options in perfconfig, on some ad-hoc situation it still requires passing options through cmdline. This patch introduces 2 options to 'perf record' for this propose: --clang-path and --clang-opt.
--- tools/perf/builtin-record.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/perf/builtin-record.c +7 -0
@@ -31,6 +31,7 @@#include "util/auxtrace.h" #include "util/parse-branch-options.h" #include "util/parse-regs-options.h" +#include "util/llvm-utils.h" #include <unistd.h> #include <sched.h>
@@ -1117,6 +1118,12 @@ struct option __record_options[] = { "per thread proc mmap processing timeout in ms"),OPT_BOOLEAN (0 ,"switch-events" , &record.opts.record_switch_events,"Record context switch events" ), +#ifdef HAVE_LIBBPF_SUPPORT +OPT_STRING (0 ,"clang-path" , &llvm_param.clang_path,"clang path" , +"clang binary to use for compiling BPF scriptlets" ), +OPT_STRING (0 ,"clang-opt" , &llvm_param.clang_opt,"clang options" , +"options passed to clang when compiling BPF scriptlets" ), +#endif OPT_END() };
Signature · 1 line
1.8.3.4
tip-bot for Wang Nan
merge bot[tip:perf/core] perf record: Add clang options for compiling BPF scripts
perf record: Add clang options for compiling BPF scripts
Commit-ID: 71dc2326252ff1bcdddc05db03c0f831d16c9447 Gitweb: http://git.kernel.org/tip/71dc2326252ff1bcdddc05db03c0f831d16c9447 Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:19 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 29 Oct 2015 17:16:22 -0300
perf record: Add clang options for compiling BPF scripts
Although previous patch allows setting BPF compiler related options in perfconfig, on some ad-hoc situation it still requires passing options through cmdline. This patch introduces 2 options to 'perf record' for this propose: --clang-path and --clang-opt.
--- tools/perf/Documentation/perf-record.txt | 6 ++++++ tools/perf/builtin-record.c | 7 +++++++ 2 files changed, 13 insertions(+)
tools/perf/Documentation/perf-record.txt +6 -0
@@ -314,6 +314,12 @@ This option sets the time out limit. The default value is 500 ms. Record context switch events i.e. events of type PERF_RECORD_SWITCH or PERF_RECORD_SWITCH_CPU_WIDE. +--clang-path:: +Path to clang binary to use for compiling BPF scriptlets. + +--clang-opt:: +Options passed to clang when compiling BPF scriptlets. + SEE ALSO -------- linkperf:perf-stat[1], linkperf:perf-list[1]
tools/perf/builtin-record.c +7 -0
@@ -31,6 +31,7 @@#include "util/auxtrace.h" #include "util/parse-branch-options.h" #include "util/parse-regs-options.h" +#include "util/llvm-utils.h" #include <unistd.h> #include <sched.h>
@@ -1112,6 +1113,12 @@ struct option __record_options[] = { "per thread proc mmap processing timeout in ms"),OPT_BOOLEAN (0 ,"switch-events" , &record.opts.record_switch_events,"Record context switch events" ), +#ifdef HAVE_LIBBPF_SUPPORT +OPT_STRING (0 ,"clang-path" , &llvm_param.clang_path,"clang path" , +"clang binary to use for compiling BPF scriptlets" ), +OPT_STRING (0 ,"clang-opt" , &llvm_param.clang_opt,"clang options" , +"options passed to clang when compiling BPF scriptlets" ), +#endif OPT_END() };
PATCH 9/31 perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
In reply to Wang NanThis patch provides infrastructure for passing source files to --event directly using:
# perf record --event bpf-file.c command
This patch does following works:
1) Allow passing '.c' file to '--event'. parse_events_load_bpf() is expanded to allow caller tell it whether the passed file is source file or object.
2) llvm__compile_bpf() is called to compile the '.c' file, the result is saved into memory. Use bpf_object__open_buffer() to load the in-memory object.
Introduces a bpf-script-example.c so we can manually test it:
# perf record --clang-opt "-DLINUX_VERSION_CODE=0x40200" --event ./bpf-script-example.c sleep 1
Note that '--clang-opt' must put before '--event'.
Futher patches will merge it into a testcase so can be tested automatically.
--- tools/perf/tests/bpf-script-example.c | 44 +++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.c | 17 ++++++++++++-- tools/perf/util/bpf-loader.h | 5 ++-- tools/perf/util/parse-events.c | 5 ++-- tools/perf/util/parse-events.h | 3 ++- tools/perf/util/parse-events.l | 3 +++ tools/perf/util/parse-events.y | 15 ++++++++++-- 7 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 tools/perf/tests/bpf-script-example.c
tools/perf/tests/bpf-script-example.c new file +44 -0
@@ -0,0 +1,44 @@ +#ifndef LINUX_VERSION_CODE +# error Need LINUX_VERSION_CODE +# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' +#endif +#define BPF_ANY 0 +#define BPF_MAP_TYPE_ARRAY 2 +#define BPF_FUNC_map_lookup_elem 1 +#define BPF_FUNC_map_update_elem 2 + +static void *(*bpf_map_lookup_elem)(void *map,void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static void *(*bpf_map_update_elem)(void *map,void *key,void *value,int flags) = + (void *) BPF_FUNC_map_update_elem; + +struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +}; + +#define SEC (NAME ) __attribute__((section(NAME), used)) +struct bpf_map_def SEC("maps") flip_table = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(int), + .value_size = sizeof(int), + .max_entries = 1, +}; + +SEC("func=sys_epoll_pwait") +int bpf_func__sys_epoll_pwait (void *ctx) +{ +int ind =0 ; +int *flag =bpf_map_lookup_elem (&flip_table, &ind); +int new_flag; +if (!flag) +return 0 ; +/* flip flag and store back */ + new_flag = !*flag; +bpf_map_update_elem (&flip_table, &ind, &new_flag,BPF_ANY ); +return new_flag; +} +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = LINUX_VERSION_CODE;
tools/perf/util/bpf-loader.c +15 -2
@@ -12,6 +12,7 @@#include "bpf-loader.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES +#include "llvm-utils.h" #define DEFINE_PRINT_FN (name, level) \ static int libbpf_##name(const char *fmt, ...) \
@@ -33,7 +34,7 @@ struct bpf_prog_priv {struct perf_probe_event pev; }; -struct bpf_object *bpf__prepare_load (const char *filename) +struct bpf_object *bpf__prepare_load (const char *filename,bool source) {struct bpf_object *obj;static bool libbpf_initialized;
@@ -45,7 +46,19 @@ struct bpf_object *bpf__prepare_load(const char *filename) libbpf_initialized = true; } - obj =bpf_object__open (filename); +if (source) { +int err; +void *obj_buf; +size_t obj_buf_sz; + + err =llvm__compile_bpf (filename, &obj_buf, &obj_buf_sz); +if (err) +return ERR_PTR (err); + obj =bpf_object__open_buffer (obj_buf, obj_buf_sz, filename); +free (obj_buf); + }else + obj =bpf_object__open (filename); +if (!obj) {pr_debug ("bpf: failed to load %s\n" , filename);return ERR_PTR (-EINVAL );
tools/perf/util/bpf-loader.h +3 -2
@@ -18,7 +18,7 @@ typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, int fd, void *arg);#ifdef HAVE_LIBBPF_SUPPORT -struct bpf_object *bpf__prepare_load (const char *filename); +struct bpf_object *bpf__prepare_load (const char *filename,bool source);void bpf__clear (void );
@@ -34,7 +34,8 @@ int bpf__foreach_tev(struct bpf_object *obj, bpf_prog_iter_callback_t func, void *arg);#else static inline struct bpf_object * -bpf__prepare_load (const char *filename__maybe_unused ) +bpf__prepare_load (const char *filename__maybe_unused , +bool source__maybe_unused ) {pr_debug ("ERROR: eBPF object loading is disabled during compiling.\n" );return ERR_PTR (-ENOTSUP );
tools/perf/util/parse-events.c +3 -2
@@ -626,11 +626,12 @@ errout:int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list, -char *bpf_file_name) +char *bpf_file_name, +bool source) {struct bpf_object *obj; - obj =bpf__prepare_load (bpf_file_name); + obj =bpf__prepare_load (bpf_file_name, source);if (IS_ERR (obj) || !obj) {char errbuf[BUFSIZ ];int err;
tools/perf/util/parse-events.h +2 -1
@@ -123,7 +123,8 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, struct list_head *head_config);int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list, -char *bpf_file_name); +char *bpf_file_name, +bool source);/* Provide this function for perf test */ struct bpf_object ; int parse_events_load_bpf_obj(struct parse_events_evlist *data,
tools/perf/util/parse-events.l +3 -0
@@ -116,6 +116,7 @@ group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ bpf_object .*\.(o|bpf) +bpf_source .*\.c num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+
@@ -161,6 +162,7 @@ modifier_bp [rwx]{1,3} {event_pmu} | {bpf_object} | +{bpf_source} | {event} { BEGIN(INITIAL); REWIND(1);
@@ -267,6 +269,7 @@ r{num_raw_hex} { return raw(yyscanner); } {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } {bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } +{bpf_source} { return str(yyscanner, PE_BPF_SOURCE); } {name} { return pmu_str_check(yyscanner); } "/" { BEGIN(config); return '/'; } - { return '-'; }
tools/perf/util/parse-events.y +13 -2
@@ -42,7 +42,7 @@ static inc_group_count(struct list_head *list, %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM %token PE_EVENT_NAME %token PE_NAME -%token PE_BPF_OBJECT +%token PE_BPF_OBJECT PE_BPF_SOURCE %token PE_MODIFIER_EVENT PE_MODIFIER_BP %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
@@ -55,6 +55,7 @@ static inc_group_count(struct list_head *list, %type <num> PE_TERM %type <str> PE_NAME %type <str> PE_BPF_OBJECT +%type <str> PE_BPF_SOURCE %type <str> PE_NAME_CACHE_TYPE %type <str> PE_NAME_CACHE_OP_RESULT %type <str> PE_MODIFIER_EVENT
@@ -461,7 +462,17 @@ PE_BPF_OBJECT struct list_head *list; ALLOC_LIST(list); - ABORT_ON(parse_events_load_bpf(data, list, $1)); + ABORT_ON(parse_events_load_bpf(data, list, $1, false)); + $$ = list; +} +| +PE_BPF_SOURCE +{ + struct parse_events_evlist *data = _data; + struct list_head *list; + + ALLOC_LIST(list); + ABORT_ON(parse_events_load_bpf(data, list, $1, true)); $$ = list; }
Signature · 1 line
1.8.3.4
Namhyung Kim
maintainerRe: [PATCH 09/31] perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
tools/perf/tests/bpf-script-example.c
Quoted 54 lines · Wang Nan
This patch provides infrastructure for passing source files to --event directly using:
# perf record --event bpf-file.c commandThis patch does following works:
1) Allow passing '.c' file to '--event'.
parse_events_load_bpf()is expanded to allow caller tell it whether the passed file is source file or object.2)
llvm__compile_bpf()is called to compile the '.c' file, the result is saved into memory. Usebpf_object__open_buffer()to load the in-memory object.Introduces a
bpf-script-example.cso we can manually test it:# perf record --clang-opt "-DLINUX_VERSION_CODE=0x40200" --event ./bpf-script-example.c sleep 1Note that '--clang-opt' must put before '--event'.
Futher patches will merge it into a testcase so can be tested automatically.
Signed-off-by Wang Nan <[email protected]>Signed-off-by He Kuang <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Signed-off-by Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/tests/bpf-script-example.c | 44 +++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.c | 17 ++++++++++++-- tools/perf/util/bpf-loader.h | 5 ++-- tools/perf/util/parse-events.c | 5 ++-- tools/perf/util/parse-events.h | 3 ++- tools/perf/util/parse-events.l | 3 +++ tools/perf/util/parse-events.y | 15 ++++++++++-- 7 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 tools/perf/tests/bpf-script-example.ctools/perf/tests/bpf-script-example.c new file +0
-0
Shouldn't it be a part of the next patch?
Thanks,
Namhyung
Quoted 214 lines
tools/perf/tests/bpf-script-example.c new file +44
-0@@ -0,0 +1,44 @@ +#ifndef LINUX_VERSION_CODE +# error Need LINUX_VERSION_CODE +# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' +#endif +#define BPF_ANY 0 +#define BPF_MAP_TYPE_ARRAY 2 +#define BPF_FUNC_map_lookup_elem 1 +#define BPF_FUNC_map_update_elem 2 + +static void *(*bpf_map_lookup_elem)(void *map,void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static void *(*bpf_map_update_elem)(void *map,void *key,void *value,int flags) = + (void *) BPF_FUNC_map_update_elem; + +struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +}; + +#define SEC (NAME ) __attribute__((section(NAME), used)) +struct bpf_map_def SEC("maps") flip_table = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(int), + .value_size = sizeof(int), + .max_entries = 1, +}; + +SEC("func=sys_epoll_pwait") +int bpf_func__sys_epoll_pwait (void *ctx) +{ +int ind =0 ; +int *flag =bpf_map_lookup_elem (&flip_table, &ind); +int new_flag; +if (!flag) +return 0 ; +/* flip flag and store back */ + new_flag = !*flag; +bpf_map_update_elem (&flip_table, &ind, &new_flag,BPF_ANY ); +return new_flag; +} +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = LINUX_VERSION_CODE;tools/perf/util/bpf-loader.c +15
-2@@ -12,6 +12,7 @@#include "bpf-loader.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES +#include "llvm-utils.h" #define DEFINE_PRINT_FN (name, level) \ static int libbpf_##name(const char *fmt, ...) \@@ -33,7 +34,7 @@ struct bpf_prog_priv {struct perf_probe_event pev; }; -struct bpf_object *bpf__prepare_load (const char *filename) +struct bpf_object *bpf__prepare_load (const char *filename,bool source) {struct bpf_object *obj;static bool libbpf_initialized;@@ -45,7 +46,19 @@ struct bpf_object *bpf__prepare_load(const char *filename) libbpf_initialized = true; } - obj =bpf_object__open (filename); +if (source) { +int err; +void *obj_buf; +size_t obj_buf_sz; + + err =llvm__compile_bpf (filename, &obj_buf, &obj_buf_sz); +if (err) +return ERR_PTR (err); + obj =bpf_object__open_buffer (obj_buf, obj_buf_sz, filename); +free (obj_buf); + }else + obj =bpf_object__open (filename); +if (!obj) {pr_debug ("bpf: failed to load %s\n" , filename);return ERR_PTR (-EINVAL );tools/perf/util/bpf-loader.h +3
-2@@ -18,7 +18,7 @@ typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, int fd, void *arg);#ifdef HAVE_LIBBPF_SUPPORT -struct bpf_object *bpf__prepare_load (const char *filename); +struct bpf_object *bpf__prepare_load (const char *filename,bool source);void bpf__clear (void );@@ -34,7 +34,8 @@ int bpf__foreach_tev(struct bpf_object *obj, bpf_prog_iter_callback_t func, void *arg);#else static inline struct bpf_object * -bpf__prepare_load (const char *filename__maybe_unused ) +bpf__prepare_load (const char *filename__maybe_unused , +bool source__maybe_unused ) {pr_debug ("ERROR: eBPF object loading is disabled during compiling.\n" );return ERR_PTR (-ENOTSUP );tools/perf/util/parse-events.c +3
-2@@ -626,11 +626,12 @@ errout:int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list, -char *bpf_file_name) +char *bpf_file_name, +bool source) {struct bpf_object *obj; - obj =bpf__prepare_load (bpf_file_name); + obj =bpf__prepare_load (bpf_file_name, source);if (IS_ERR (obj) || !obj) {char errbuf[BUFSIZ ];int err;tools/perf/util/parse-events.h +2
-1@@ -123,7 +123,8 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, struct list_head *head_config);int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list, -char *bpf_file_name); +char *bpf_file_name, +bool source);/* Provide this function for perf test */ struct bpf_object ; int parse_events_load_bpf_obj(struct parse_events_evlist *data,tools/perf/util/parse-events.l +3
-0@@ -116,6 +116,7 @@ group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ bpf_object .*\.(o|bpf) +bpf_source .*\.c num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+@@ -161,6 +162,7 @@ modifier_bp [rwx]{1,3} {event_pmu} | {bpf_object} | +{bpf_source} | {event} { BEGIN(INITIAL); REWIND(1);@@ -267,6 +269,7 @@ r{num_raw_hex} { return raw(yyscanner); } {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } {bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } +{bpf_source} { return str(yyscanner, PE_BPF_SOURCE); } {name} { return pmu_str_check(yyscanner); } "/" { BEGIN(config); return '/'; } - { return '-'; }tools/perf/util/parse-events.y +13
-2@@ -42,7 +42,7 @@ static inc_group_count(struct list_head *list, %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM %token PE_EVENT_NAME %token PE_NAME -%token PE_BPF_OBJECT +%token PE_BPF_OBJECT PE_BPF_SOURCE %token PE_MODIFIER_EVENT PE_MODIFIER_BP %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP@@ -55,6 +55,7 @@ static inc_group_count(struct list_head *list, %type <num> PE_TERM %type <str> PE_NAME %type <str> PE_BPF_OBJECT +%type <str> PE_BPF_SOURCE %type <str> PE_NAME_CACHE_TYPE %type <str> PE_NAME_CACHE_OP_RESULT %type <str> PE_MODIFIER_EVENT@@ -461,7 +462,17 @@ PE_BPF_OBJECT struct list_head *list; ALLOC_LIST(list); - ABORT_ON(parse_events_load_bpf(data, list, $1)); + ABORT_ON(parse_events_load_bpf(data, list, $1, false)); + $$ = list; +} +| +PE_BPF_SOURCE +{ + struct parse_events_evlist *data = _data; + struct list_head *list; + + ALLOC_LIST(list); + ABORT_ON(parse_events_load_bpf(data, list, $1, true)); $$ = list; }Signature · 1 line
1.8.3.4
Re: [PATCH 09/31] perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
Quoted 50 lines · Namhyung Kim
Quoted 49 lines · Wang Nan
This patch provides infrastructure for passing source files to --event directly using:
# perf record --event bpf-file.c commandThis patch does following works:
1) Allow passing '.c' file to '--event'.
parse_events_load_bpf()is expanded to allow caller tell it whether the passed file is source file or object.2)
llvm__compile_bpf()is called to compile the '.c' file, the result is saved into memory. Usebpf_object__open_buffer()to load the in-memory object.Introduces a
bpf-script-example.cso we can manually test it:# perf record --clang-opt "-DLINUX_VERSION_CODE=0x40200" --event ./bpf-script-example.c sleep 1Note that '--clang-opt' must put before '--event'.
Futher patches will merge it into a testcase so can be tested automatically.
Signed-off-by Wang Nan <[email protected]>Signed-off-by He Kuang <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Signed-off-by Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/tests/bpf-script-example.c | 44 +++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.c | 17 ++++++++++++-- tools/perf/util/bpf-loader.h | 5 ++-- tools/perf/util/parse-events.c | 5 ++-- tools/perf/util/parse-events.h | 3 ++- tools/perf/util/parse-events.l | 3 +++ tools/perf/util/parse-events.y | 15 ++++++++++-- 7 files changed, 83 insertions(+), 9 deletions(-)create mode 100644
tools/perf/tests/bpf-script-example.c
Quoted 6 lines
tools/perf/tests/bpf-script-example.c
Quoted 5 lines
tools/perf/tests/bpf-script-example.c new file +0
-0Shouldn't it be a part of the next patch?
I think putting the sample file in this patch should be better.
In commit message I show a cmdline to utilize scriptlet compiling. If we put the sample code into next patch, then people get to this point have to write his/her own script to test scriptlet compiling manually.
Thank you.
Quoted 218 lines
Thanks,
NamhyungQuoted 214 lines
tools/perf/tests/bpf-script-example.c new file +44
-0@@ -0,0 +1,44 @@ +#ifndef LINUX_VERSION_CODE +# error Need LINUX_VERSION_CODE +# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' +#endif +#define BPF_ANY 0 +#define BPF_MAP_TYPE_ARRAY 2 +#define BPF_FUNC_map_lookup_elem 1 +#define BPF_FUNC_map_update_elem 2 + +static void *(*bpf_map_lookup_elem)(void *map,void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static void *(*bpf_map_update_elem)(void *map,void *key,void *value,int flags) = + (void *) BPF_FUNC_map_update_elem; + +struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +}; + +#define SEC (NAME ) __attribute__((section(NAME), used)) +struct bpf_map_def SEC("maps") flip_table = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(int), + .value_size = sizeof(int), + .max_entries = 1, +}; + +SEC("func=sys_epoll_pwait") +int bpf_func__sys_epoll_pwait (void *ctx) +{ +int ind =0 ; +int *flag =bpf_map_lookup_elem (&flip_table, &ind); +int new_flag; +if (!flag) +return 0 ; +/* flip flag and store back */ + new_flag = !*flag; +bpf_map_update_elem (&flip_table, &ind, &new_flag,BPF_ANY ); +return new_flag; +} +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = LINUX_VERSION_CODE;tools/perf/util/bpf-loader.c +15
-2@@ -12,6 +12,7 @@#include "bpf-loader.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES +#include "llvm-utils.h" #define DEFINE_PRINT_FN (name, level) \ static int libbpf_##name(const char *fmt, ...) \@@ -33,7 +34,7 @@ struct bpf_prog_priv {struct perf_probe_event pev; }; -struct bpf_object *bpf__prepare_load (const char *filename) +struct bpf_object *bpf__prepare_load (const char *filename,bool source) {struct bpf_object *obj;static bool libbpf_initialized;@@ -45,7 +46,19 @@ struct bpf_object *bpf__prepare_load(const char *filename) libbpf_initialized = true; } - obj =bpf_object__open (filename); +if (source) { +int err; +void *obj_buf; +size_t obj_buf_sz; + + err =llvm__compile_bpf (filename, &obj_buf, &obj_buf_sz); +if (err) +return ERR_PTR (err); + obj =bpf_object__open_buffer (obj_buf, obj_buf_sz, filename); +free (obj_buf); + }else + obj =bpf_object__open (filename); +if (!obj) {pr_debug ("bpf: failed to load %s\n" , filename);return ERR_PTR (-EINVAL );tools/perf/util/bpf-loader.h +3
-2@@ -18,7 +18,7 @@ typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, int fd, void *arg);#ifdef HAVE_LIBBPF_SUPPORT -struct bpf_object *bpf__prepare_load (const char *filename); +struct bpf_object *bpf__prepare_load (const char *filename,bool source);void bpf__clear (void );@@ -34,7 +34,8 @@ int bpf__foreach_tev(struct bpf_object *obj, bpf_prog_iter_callback_t func, void *arg);#else static inline struct bpf_object * -bpf__prepare_load (const char *filename__maybe_unused ) +bpf__prepare_load (const char *filename__maybe_unused , +bool source__maybe_unused ) {pr_debug ("ERROR: eBPF object loading is disabled during compiling.\n" );return ERR_PTR (-ENOTSUP );tools/perf/util/parse-events.c +3
-2@@ -626,11 +626,12 @@ errout:int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list, -char *bpf_file_name) +char *bpf_file_name, +bool source) {struct bpf_object *obj; - obj =bpf__prepare_load (bpf_file_name); + obj =bpf__prepare_load (bpf_file_name, source);if (IS_ERR (obj) || !obj) {char errbuf[BUFSIZ ];int err;tools/perf/util/parse-events.h +2
-1@@ -123,7 +123,8 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, struct list_head *head_config);int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list, -char *bpf_file_name); +char *bpf_file_name, +bool source);/* Provide this function for perf test */ struct bpf_object ; int parse_events_load_bpf_obj(struct parse_events_evlist *data,tools/perf/util/parse-events.l +3
-0@@ -116,6 +116,7 @@ group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ bpf_object .*\.(o|bpf) +bpf_source .*\.c num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+@@ -161,6 +162,7 @@ modifier_bp [rwx]{1,3} {event_pmu} | {bpf_object} | +{bpf_source} | {event} { BEGIN(INITIAL); REWIND(1);@@ -267,6 +269,7 @@ r{num_raw_hex} { return raw(yyscanner); } {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } {bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } +{bpf_source} { return str(yyscanner, PE_BPF_SOURCE); } {name} { return pmu_str_check(yyscanner); } "/" { BEGIN(config); return '/'; } - { return '-'; }tools/perf/util/parse-events.y +13
-2@@ -42,7 +42,7 @@ static inc_group_count(struct list_head *list, %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM %token PE_EVENT_NAME %token PE_NAME -%token PE_BPF_OBJECT +%token PE_BPF_OBJECT PE_BPF_SOURCE %token PE_MODIFIER_EVENT PE_MODIFIER_BP %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP@@ -55,6 +55,7 @@ static inc_group_count(struct list_head *list, %type <num> PE_TERM %type <str> PE_NAME %type <str> PE_BPF_OBJECT +%type <str> PE_BPF_SOURCE %type <str> PE_NAME_CACHE_TYPE %type <str> PE_NAME_CACHE_OP_RESULT %type <str> PE_MODIFIER_EVENT@@ -461,7 +462,17 @@ PE_BPF_OBJECT struct list_head *list; ALLOC_LIST(list); - ABORT_ON(parse_events_load_bpf(data, list, $1)); + ABORT_ON(parse_events_load_bpf(data, list, $1, false)); + $$ = list; +} +| +PE_BPF_SOURCE +{ + struct parse_events_evlist *data = _data; + struct list_head *list; + + ALLOC_LIST(list); + ABORT_ON(parse_events_load_bpf(data, list, $1, true)); $$ = list; }Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 09/31] perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
In reply to Wang NanEm Wed, Oct 14, 2015 at 12:41:20PM +0000, Wang Nan escreveu:
Quoted 15 lines
This patch provides infrastructure for passing source files to --event directly using:
# perf record --event bpf-file.c commandThis patch does following works:
1) Allow passing '.c' file to '--event'.
parse_events_load_bpf()is expanded to allow caller tell it whether the passed file is source file or object.2)
llvm__compile_bpf()is called to compile the '.c' file, the result is saved into memory. Usebpf_object__open_buffer()to load the in-memory object.
Quoted 7 lines
Introduces a
bpf-script-example.cso we can manually test it:# perf record --clang-opt "-DLINUX_VERSION_CODE=0x40200" --event ./bpf-script-example.c sleep 1Note that '--clang-opt' must put before '--event'.
Futher patches will merge it into a testcase so can be tested automatically.
Not working here... humm, perhaps the version again...
[root@felicio linux]# perf record --clang-opt "-DLINUX_VERSION_CODE=0x40200" --event tools/perf/tests/bpf-script-example.c sleep 1
libbpf: load bpf program failed: Invalid argument
libbpf: -- BEGIN DUMP LOG ---
libbpf:
libbpf: -- END LOG --
libbpf: failed to load program 'func=sys_epoll_pwait'
Yeah, as soon as I used:
# perf record --clang-opt "-DLINUX_VERSION_CODE=0x40300" --event tools/perf/tests/bpf-script-example.c sleep 1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.013 MB perf.data ] [root@felicio linux]#
I think we should provide some stuff like this LINUX_VERSION_CODE automagically, right?
And also check the version in a .o file, to make sure we don't ask the kernel to load something with a mismatch in that version.
And you could've provided something that would actually do something meaningful, i.e. a filter that would capture some samples for a given event while discarding something not that important...
I.e. show the value provided at the time we build that patch.
I'll try to come up with something like that after I have lunch, but so far so good, I think I'll be able to send this to Ingo today, and that is a major milestone!
Great work, thanks a lot for doing this!
- Arnaldo
P.S.: If we pass a .c ending string to 'trace', 'record', 'top', etc, I think we could do away with that pesky --event/-e, i.e.:
trace cool_stuff.c
Would do the right thing, i.e. be equivalent to:
trace --event cool_stuff.c
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 09/31] perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
Em Thu, Oct 29, 2015 at 01:25:53PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 7 lines
And you could've provided something that would actually do something meaningful, i.e. a filter that would capture some samples for a given event while discarding something not that important...
I.e. show the value provided at the time we build that patch.
I'll try to come up with something like that after I have lunch.
Ok, what I have is at my perf/core branch, will continue after having some food,
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 09/31] perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
Em Thu, Oct 29, 2015 at 01:30:27PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 11 lines
Em Thu, Oct 29, 2015 at 01:25:53PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 7 lines
And you could've provided something that would actually do something meaningful, i.e. a filter that would capture some samples for a given event while discarding something not that important...
I.e. show the value provided at the time we build that patch.
I'll try to come up with something like that after I have lunch.
Ok, what I have is at my perf/core branch, will continue after having some food,
Oh well, more interesting things require that we access the function arguments and that is not yet possible, requires that PROLOGUE + dwarf bits that comes later, reading those patches...
- Arnaldo
tip-bot for Wang Nan
merge bot[tip:perf/core] perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
In reply to Wang Nanperf tools: Compile scriptlets to BPF objects when passing '.c' to --event
Commit-ID: d509db0473e40134286271b1d1adadccf42ac467 Gitweb: http://git.kernel.org/tip/d509db0473e40134286271b1d1adadccf42ac467 Author: Wang Nan <[email protected]> AuthorDate: Wed, 14 Oct 2015 12:41:20 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 29 Oct 2015 17:16:23 -0300
perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
This patch provides infrastructure for passing source files to --event directly using:
# perf record --event bpf-file.c command
This patch does following works:
1) Allow passing '.c' file to '--event'. parse_events_load_bpf() is expanded to allow caller tell it whether the passed file is source file or object.
2) llvm__compile_bpf() is called to compile the '.c' file, the result is saved into memory. Use bpf_object__open_buffer() to load the in-memory object.
Introduces a bpf-script-example.c so we can manually test it:
# perf record --clang-opt "-DLINUX_VERSION_CODE=0x40200" --event ./bpf-script-example.c sleep 1
Note that '--clang-opt' must put before '--event'.
Futher patches will merge it into a testcase so can be tested automatically.
--- tools/perf/tests/bpf-script-example.c | 44 +++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.c | 17 ++++++++++++-- tools/perf/util/bpf-loader.h | 5 ++-- tools/perf/util/parse-events.c | 5 ++-- tools/perf/util/parse-events.h | 3 ++- tools/perf/util/parse-events.l | 3 +++ tools/perf/util/parse-events.y | 15 ++++++++++-- 7 files changed, 83 insertions(+), 9 deletions(-)
tools/perf/tests/bpf-script-example.c new file +44 -0
@@ -0,0 +1,44 @@ +#ifndef LINUX_VERSION_CODE +# error Need LINUX_VERSION_CODE +# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' +#endif +#define BPF_ANY 0 +#define BPF_MAP_TYPE_ARRAY 2 +#define BPF_FUNC_map_lookup_elem 1 +#define BPF_FUNC_map_update_elem 2 + +static void *(*bpf_map_lookup_elem)(void *map,void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static void *(*bpf_map_update_elem)(void *map,void *key,void *value,int flags) = + (void *) BPF_FUNC_map_update_elem; + +struct bpf_map_def { +unsigned int type; +unsigned int key_size; +unsigned int value_size; +unsigned int max_entries; +}; + +#define SEC (NAME ) __attribute__((section(NAME), used)) +struct bpf_map_def SEC("maps") flip_table = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(int), + .value_size = sizeof(int), + .max_entries = 1, +}; + +SEC("func=sys_epoll_pwait") +int bpf_func__sys_epoll_pwait (void *ctx) +{ +int ind =0 ; +int *flag =bpf_map_lookup_elem (&flip_table, &ind); +int new_flag; +if (!flag) +return 0 ; +/* flip flag and store back */ + new_flag = !*flag; +bpf_map_update_elem (&flip_table, &ind, &new_flag,BPF_ANY ); +return new_flag; +} +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = LINUX_VERSION_CODE;
tools/perf/util/bpf-loader.c +15 -2
@@ -12,6 +12,7 @@#include "bpf-loader.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES +#include "llvm-utils.h" #define DEFINE_PRINT_FN (name, level) \ static int libbpf_##name(const char *fmt, ...) \
@@ -33,7 +34,7 @@ struct bpf_prog_priv {struct perf_probe_event pev; }; -struct bpf_object *bpf__prepare_load (const char *filename) +struct bpf_object *bpf__prepare_load (const char *filename,bool source) {struct bpf_object *obj;static bool libbpf_initialized;
@@ -45,7 +46,19 @@ struct bpf_object *bpf__prepare_load(const char *filename) libbpf_initialized = true; } - obj =bpf_object__open (filename); +if (source) { +int err; +void *obj_buf; +size_t obj_buf_sz; + + err =llvm__compile_bpf (filename, &obj_buf, &obj_buf_sz); +if (err) +return ERR_PTR (err); + obj =bpf_object__open_buffer (obj_buf, obj_buf_sz, filename); +free (obj_buf); + }else + obj =bpf_object__open (filename); +if (!obj) {pr_debug ("bpf: failed to load %s\n" , filename);return ERR_PTR (-EINVAL );
tools/perf/util/bpf-loader.h +3 -2
@@ -18,7 +18,7 @@ typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, int fd, void *arg);#ifdef HAVE_LIBBPF_SUPPORT -struct bpf_object *bpf__prepare_load (const char *filename); +struct bpf_object *bpf__prepare_load (const char *filename,bool source);void bpf__clear (void );
@@ -34,7 +34,8 @@ int bpf__foreach_tev(struct bpf_object *obj, bpf_prog_iter_callback_t func, void *arg);#else static inline struct bpf_object * -bpf__prepare_load (const char *filename__maybe_unused ) +bpf__prepare_load (const char *filename__maybe_unused , +bool source__maybe_unused ) {pr_debug ("ERROR: eBPF object loading is disabled during compiling.\n" );return ERR_PTR (-ENOTSUP );
tools/perf/util/parse-events.c +3 -2
@@ -626,11 +626,12 @@ errout:int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list, -char *bpf_file_name) +char *bpf_file_name, +bool source) {struct bpf_object *obj; - obj =bpf__prepare_load (bpf_file_name); + obj =bpf__prepare_load (bpf_file_name, source);if (IS_ERR (obj) || !obj) {char errbuf[BUFSIZ ];int err;
tools/perf/util/parse-events.h +2 -1
@@ -125,7 +125,8 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, struct list_head *head_config);int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list, -char *bpf_file_name); +char *bpf_file_name, +bool source);/* Provide this function for perf test */ struct bpf_object ; int parse_events_load_bpf_obj(struct parse_events_evlist *data,
tools/perf/util/parse-events.l +3 -0
@@ -116,6 +116,7 @@ group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ bpf_object .*\.(o|bpf) +bpf_source .*\.c num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+
@@ -161,6 +162,7 @@ modifier_bp [rwx]{1,3} {event_pmu} | {bpf_object} | +{bpf_source} | {event} { BEGIN(INITIAL); REWIND(1);
@@ -269,6 +271,7 @@ r{num_raw_hex} { return raw(yyscanner); } {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } {bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } +{bpf_source} { return str(yyscanner, PE_BPF_SOURCE); } {name} { return pmu_str_check(yyscanner); } "/" { BEGIN(config); return '/'; } - { return '-'; }
tools/perf/util/parse-events.y +13 -2
@@ -42,7 +42,7 @@ static inc_group_count(struct list_head *list, %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM %token PE_EVENT_NAME %token PE_NAME -%token PE_BPF_OBJECT +%token PE_BPF_OBJECT PE_BPF_SOURCE %token PE_MODIFIER_EVENT PE_MODIFIER_BP %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
@@ -55,6 +55,7 @@ static inc_group_count(struct list_head *list, %type <num> PE_TERM %type <str> PE_NAME %type <str> PE_BPF_OBJECT +%type <str> PE_BPF_SOURCE %type <str> PE_NAME_CACHE_TYPE %type <str> PE_NAME_CACHE_OP_RESULT %type <str> PE_MODIFIER_EVENT
@@ -461,7 +462,17 @@ PE_BPF_OBJECT struct list_head *list; ALLOC_LIST(list); - ABORT_ON(parse_events_load_bpf(data, list, $1)); + ABORT_ON(parse_events_load_bpf(data, list, $1, false)); + $$ = list; +} +| +PE_BPF_SOURCE +{ + struct parse_events_evlist *data = _data; + struct list_head *list; + + ALLOC_LIST(list); + ABORT_ON(parse_events_load_bpf(data, list, $1, true)); $$ = list; }
PATCH 10/31 perf test: Enforce LLVM test for BPF test
In reply to Wang NanThis patch replaces the original toy BPF program with previous introduced bpf-script-example.c. Dynamically embedded it into 'llvm-src.c'.
The newly introduced BPF program attaches a BPF program at 'sys_epoll_pwait()', and collect half samples from it. perf itself never use that syscall, so further test can verify their result with it.
Since BPF program require LINUX_VERSION_CODE of runtime kernel, this patch computes that code from uname.
Since the resuling BPF object is useful for further testcases, this patch introduces 'prepare' and 'cleanup' method to tests, and makes test__llvm() create a MAP_SHARED memory array to hold the resulting object.
--- tools/perf/tests/Build | 9 +++- tools/perf/tests/builtin-test.c | 6 +++ tools/perf/tests/llvm.c | 104 +++++++++++++++++++++++++++++++++++----- tools/perf/tests/llvm.h | 14 ++++++ tools/perf/tests/tests.h | 4 ++ 5 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 tools/perf/tests/llvm.h
tools/perf/tests/Build +8 -1
@@ -31,9 +31,16 @@ perf-y += sample-parsing.o perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o -perf-y += llvm.o +perf-y += llvm.o llvm-src.o perf-y += topology.o +$(OUTPUT)tests/llvm-src.c: tests/bpf-script-example.c + $(call rule_mkdir) + $(Q)echo '#include <tests/llvm.h>' > $@ + $(Q)echo 'const char test_llvm__bpf_prog[] =' >> $@ + $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)echo ';' >> $@ + ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o endif
tools/perf/tests/builtin-test.c +6 -0
@@ -160,6 +160,8 @@ static struct test generic_tests[] = { { .desc = "Test LLVM searching and compiling", .func = test__llvm, + .prepare = test__llvm_prepare, + .cleanup = test__llvm_cleanup, }, { .desc = "Test topology in session",
@@ -261,7 +263,11 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) }pr_debug ("\n--- start ---\n" ); +if (t->prepare) + t->prepare (); err =run_test (t); +if (t->cleanup) + t->cleanup ();pr_debug ("---- end ----\n%s:" , t->desc);switch (err) {
tools/perf/tests/llvm.c +91 -13
144 lines not on this page · raw message
tools/perf/tests/llvm.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef PERF_TEST_LLVM_H +#define PERF_TEST_LLVM_H + +#include <stddef.h> /* for size_t */ + +struct test_llvm__bpf_result { +size_t size; +char object[]; +}; + +extern struct test_llvm__bpf_result *p_test_llvm__bpf_result; +extern const char test_llvm__bpf_prog[]; + +#endif
tools/perf/tests/tests.h +4 -0
@@ -27,6 +27,8 @@ enum {struct test {const char *desc;int (*func)(void ); +void (*prepare)(void ); +void (*cleanup)(void ); };/* Tests */
@@ -66,6 +68,8 @@ int test__fdarray__add(void);int test__kmod_path__parse (void );int test__thread_map (void );int test__llvm (void ); +void test__llvm_prepare (void ); +void test__llvm_cleanup (void );int test_session_topology (void );#if defined(__arm__) || defined(__aarch64__)
Signature · 1 line
1.8.3.4
Namhyung Kim
maintainerRe: [PATCH 10/31] perf test: Enforce LLVM test for BPF test
Quoted 30 lines · Wang Nan
This patch replaces the original toy BPF program with previous introduced
bpf-script-example.c. Dynamically embedded it into 'llvm-src.c'.The newly introduced BPF program attaches a BPF program at '
sys_epoll_pwait()', and collect half samples from it. perf itself never use that syscall, so further test can verify their result with it.Since BPF program require
LINUX_VERSION_CODEof runtime kernel, this patch computes that code from uname.Since the resuling BPF object is useful for further testcases, this patch introduces 'prepare' and 'cleanup' method to tests, and makes
test__llvm()create aMAP_SHAREDmemory array to hold the resulting object.Signed-off-by He Kuang <[email protected]>Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>---
[SNIP]
tools/perf/tests/llvm.c int test__llvm(void) · line 152
Quoted 6 lines
tools/perf/tests/llvm.c +6
-0+void test__llvm_prepare (void ) +{ + p_test_llvm__bpf_result =mmap (NULL ,SHARED_BUF_INIT_SIZE , +PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (!p_test_llvm__bpf_result)
It should check MAP_FAILED instead.
tools/perf/tests/llvm.c int test__llvm(void) · line 161
Quoted 9 lines
tools/perf/tests/llvm.c +9
-0+return ; +memset ((void *)p_test_llvm__bpf_result,'\0' ,SHARED_BUF_INIT_SIZE ); +} + +void test__llvm_cleanup (void ) +{ +unsigned long boundary, buf_end; + +if (!p_test_llvm__bpf_result)
Ditto.
Thanks,
Namhyung
Quoted 60 lines
tools/perf/tests/llvm.c +15
-0+return ; +if (p_test_llvm__bpf_result->size ==0 ) { +munmap ((void *)p_test_llvm__bpf_result,SHARED_BUF_INIT_SIZE ); + p_test_llvm__bpf_result =NULL ; +return ; + } + + buf_end = (unsigned long )p_test_llvm__bpf_result +SHARED_BUF_INIT_SIZE ; + + boundary = (unsigned long )(p_test_llvm__bpf_result); + boundary += p_test_llvm__bpf_result->size; + boundary = (boundary + (page_size -1 )) & + (~((unsigned long )page_size -1 )); +munmap ((void *)boundary, buf_end - boundary); +}tools/perf/tests/llvm.h new file +14
-0@@ -0,0 +1,14 @@ +#ifndef PERF_TEST_LLVM_H +#define PERF_TEST_LLVM_H + +#include <stddef.h> /* for size_t */ + +struct test_llvm__bpf_result { +size_t size; +char object[]; +}; + +extern struct test_llvm__bpf_result *p_test_llvm__bpf_result; +extern const char test_llvm__bpf_prog[]; + +#endif tools/perf/tests/tests.h +4
-0@@ -27,6 +27,8 @@ enum {struct test {const char *desc;int (*func)(void ); +void (*prepare)(void ); +void (*cleanup)(void ); };/* Tests */ @@ -66,6 +68,8 @@ int test__fdarray__add(void);int test__kmod_path__parse (void );int test__thread_map (void );int test__llvm (void ); +void test__llvm_prepare (void ); +void test__llvm_cleanup (void );int test_session_topology (void );#if defined(__arm__) || defined(__aarch64__)Signature · 1 line
1.8.3.4
Re: [PATCH 10/31] perf test: Enforce LLVM test for BPF test
Quoted 33 lines · Namhyung Kim
Quoted 30 lines · Wang Nan
This patch replaces the original toy BPF program with previous introduced
bpf-script-example.c. Dynamically embedded it into 'llvm-src.c'.The newly introduced BPF program attaches a BPF program at '
sys_epoll_pwait()', and collect half samples from it. perf itself never use that syscall, so further test can verify their result with it.Since BPF program require
LINUX_VERSION_CODEof runtime kernel, this patch computes that code from uname.Since the resuling BPF object is useful for further testcases, this patch introduces 'prepare' and 'cleanup' method to tests, and makes
test__llvm()create aMAP_SHAREDmemory array to hold the resulting object.Signed-off-by He Kuang <[email protected]>Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>---
[SNIP]
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+void test__llvm_prepare(void) +{ + p_test_llvm__bpf_result = mmap(NULL, SHARED_BUF_INIT_SIZE, + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (!p_test_llvm__bpf_result)It should check
MAP_FAILEDinstead.
Fixed by this way:
tools/perf/tests/llvm.c +7 -4
@@ -199,12 +199,15 @@ void test__llvm_prepare(void)for (i =0 ; llvm_testcases[i].source; i++) {struct test_llvm__bpf_result *result; +void *p; - result =mmap (NULL ,SHARED_BUF_INIT_SIZE , -PROT_READ |PROT_WRITE , -MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); -if (!result) + p =mmap (NULL ,SHARED_BUF_INIT_SIZE , +PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (p ==MAP_FAILED )return ; + + result = p;memset ((void *)result,'\0' ,SHARED_BUF_INIT_SIZE ); llvm_testcases[i].result = result;
Thank you.
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 10/31] perf test: Enforce LLVM test for BPF test
Em Thu, Oct 15, 2015 at 07:58:38PM +0800, Wangnan (F) escreveu:
Quoted 10 lines
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+void test__llvm_prepare(void) +{ + p_test_llvm__bpf_result = mmap(NULL, SHARED_BUF_INIT_SIZE, + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (!p_test_llvm__bpf_result)It should check
MAP_FAILEDinstead.Fixed by this way:
Can you please try refreshing this patchset on top of what is now in acme/perf/core?
Also why do we need those struct test->{prepare,cleanup} pointers? You introduced it and then, on the next patch that touches 'perf test' and uses test__llvm_{prepare,cleanup} you call them directly, which I think should be enough, i.e. keep them as functions to call from inside the test called from run_test(), right?
- Arnaldo
Quoted 26 lines
tools/perf/tests/llvm.c +7
-4@@ -199,12 +199,15 @@ void test__llvm_prepare(void)for (i =0 ; llvm_testcases[i].source; i++) {struct test_llvm__bpf_result *result; +void *p; - result =mmap (NULL ,SHARED_BUF_INIT_SIZE , -PROT_READ |PROT_WRITE , -MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); -if (!result) + p =mmap (NULL ,SHARED_BUF_INIT_SIZE , +PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (p ==MAP_FAILED )return ; + + result = p;memset ((void *)result,'\0' ,SHARED_BUF_INIT_SIZE ); llvm_testcases[i].result = result;Thank you.
Re: [PATCH 10/31] perf test: Enforce LLVM test for BPF test
Quoted 13 lines · Arnaldo Carvalho de Melo
Em Thu, Oct 15, 2015 at 07:58:38PM +0800, Wangnan (F) escreveu:
Quoted 9 lines
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+void test__llvm_prepare(void) +{ + p_test_llvm__bpf_result = mmap(NULL, SHARED_BUF_INIT_SIZE, + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (!p_test_llvm__bpf_result)It should check
MAP_FAILEDinstead.Fixed by this way:
Can you please try refreshing this patchset on top of what is now in acme/perf/core?
Quoted 8 lines
Also why do we need those struct test->{prepare,cleanup} pointers? You introduced it and then, on the next patch that touches 'perf test' and uses test__llvm_{prepare,cleanup} you call them directly, which I think should be enough, i.e. keep them as functions to call from inside the test called from
run_test(), right?- Arnaldo
This prepare/cleanup functions are introduced because I want BPF test reuse the result of LLVM test, so it don't need to compile those BPF scripts twice. This is the reason I use shared memory. However, now I think compiling twice is acceptable and can make things simpler.
I'll update this patchset in my next pull request.
Thank you.
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 10/31] perf test: Enforce LLVM test for BPF test
In reply to Wang NanEm Wed, Oct 14, 2015 at 12:41:21PM +0000, Wang Nan escreveu:
Quoted 5 lines
This patch replaces the original toy BPF program with previous introduced
bpf-script-example.c. Dynamically embedded it into 'llvm-src.c'.The newly introduced BPF program attaches a BPF program at '
sys_epoll_pwait()', and collect half samples from it. perf itself never
"collect half samples from it"? Can you rephrase this?
Quoted 8 lines
use that syscall, so further test can verify their result with it.
Since BPF program require
LINUX_VERSION_CODEof runtime kernel, this patch computes that code from uname.Since the resuling BPF object is useful for further testcases, this patch introduces 'prepare' and 'cleanup' method to tests, and makes
test__llvm()create aMAP_SHAREDmemory array to hold the resulting object.
I think you're doing lots of things in just one patch, please split it, for instance, that ->prepare()/->cleanup() should stand on its own, with a proper changelog, etc.
The LINUX_VERSION_CODE part should probably also be on its own patch, working much like __FILE__ or __func__, i.e. gcc predefines those, the bpf code that calls clang to build the provided .c file should do the same for LINUX_VERSION_CODE, so that bpf scriptlets can rely on it being defined.
I.e. that compose_source() function, but not just for the 'perf test' entry, for any bpf scriptlet.
Quoted 268 lines
Signed-off-by He Kuang <[email protected]>Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>--- tools/perf/tests/Build | 9 +++- tools/perf/tests/builtin-test.c | 6 +++ tools/perf/tests/llvm.c | 104 +++++++++++++++++++++++++++++++++++----- tools/perf/tests/llvm.h | 14 ++++++ tools/perf/tests/tests.h | 4 ++ 5 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 tools/perf/tests/llvm.htools/perf/tests/Build +8
-1@@ -31,9 +31,16 @@ perf-y += sample-parsing.o perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o -perf-y += llvm.o +perf-y += llvm.o llvm-src.o perf-y += topology.o +$(OUTPUT)tests/llvm-src.c: tests/bpf-script-example.c + $(call rule_mkdir) + $(Q)echo '#include <tests/llvm.h>' > $@ + $(Q)echo 'const char test_llvm__bpf_prog[] =' >> $@ + $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)echo ';' >> $@ + ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o endiftools/perf/tests/builtin-test.c +6
-0@@ -160,6 +160,8 @@ static struct test generic_tests[] = { { .desc = "Test LLVM searching and compiling", .func = test__llvm, + .prepare = test__llvm_prepare, + .cleanup = test__llvm_cleanup, }, { .desc = "Test topology in session",@@ -261,7 +263,11 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) }pr_debug ("\n--- start ---\n" ); +if (t->prepare) + t->prepare (); err =run_test (t); +if (t->cleanup) + t->cleanup ();pr_debug ("---- end ----\n%s:" , t->desc);switch (err) {tools/perf/tests/llvm.c +91
-13@@ -1,9 +1,13 @@#include <stdio.h> +#include <sys/utsname.h> #include <bpf/libbpf.h> #include <util/llvm-utils.h> #include <util/cache.h> +#include <util/util.h> +#include <sys/mman.h> #include "tests.h" #include "debug.h" +#include "llvm.h" static int perf_config_cb (const char *var,const char *val,void *arg__maybe_unused )@@ -11,16 +15,6 @@ static int perf_config_cb(const char *var, const char *val,return perf_default_config (var, val, arg); } -/* -* Randomly give it a "version" section since we don't really load it -* into kernel -*/ -static const char test_bpf_prog[] = -"__attribute__((section(\"do_fork\"), used)) " -"int fork(void *ctx) {return 0;} " -"char _license[] __attribute__((section(\"license\"), used)) = \"GPL\";" -"int _version __attribute__((section(\"version\"), used)) = 0x40100;" ; -#ifdef HAVE_LIBBPF_SUPPORT static int test__bpf_parsing (void *obj_buf,size_t obj_buf_sz) {@@ -41,12 +35,44 @@ static int test__bpf_parsing(void *obj_buf __maybe_unused, }#endif +static char * +compose_source (void ) +{ +struct utsname utsname; +int version, patchlevel, sublevel, err; +unsigned long version_code; +char *code; + +if (uname (&utsname)) +return NULL ; + + err =sscanf (utsname.release,"%d.%d.%d" , + &version, &patchlevel, &sublevel); +if (err !=3 ) { +fprintf (stderr," (Can't get kernel version from uname '%s')" , + utsname.release); +return NULL ; + } + + version_code = (version <<16 ) + (patchlevel <<8 ) + sublevel; + err =asprintf (&code,"#define LINUX_VERSION_CODE 0x%08lx;\n%s" , + version_code, test_llvm__bpf_prog); +if (err <0 ) +return NULL ; + +return code; +} + +#define SHARED_BUF_INIT_SIZE (1 << 20) +struct test_llvm__bpf_result *p_test_llvm__bpf_result; +int test__llvm (void ) {char *tmpl_new, *clang_opt_new;void *obj_buf;size_t obj_buf_sz;int err, old_verbose; +char *source;perf_config (perf_config_cb,NULL );@@ -73,10 +99,22 @@ int test__llvm(void)if (!llvm_param.clang_opt) llvm_param.clang_opt =strdup ("" ); - err =asprintf (&tmpl_new,"echo '%s' | %s" , test_bpf_prog, - llvm_param.clang_bpf_cmd_template); -if (err <0 ) + source =compose_source (); +if (!source) { +pr_err ("Failed to compose source code\n" ); +return -1 ; + } + +/* Quote __EOF__ so strings in source won't be expanded by shell */ + err =asprintf (&tmpl_new,"cat << '__EOF__' | %s\n%s\n__EOF__\n" , + llvm_param.clang_bpf_cmd_template, source); +free (source); + source =NULL ; +if (err <0 ) { +pr_err ("Failed to alloc new template\n" );return -1 ; + } + err =asprintf (&clang_opt_new,"-xc %s" , llvm_param.clang_opt);if (err <0 )return -1 ;@@ -93,6 +131,46 @@ int test__llvm(void) } err =test__bpf_parsing (obj_buf, obj_buf_sz); +if (!err && p_test_llvm__bpf_result) { +if (obj_buf_sz >SHARED_BUF_INIT_SIZE ) { +pr_err ("Resulting object too large\n" ); + }else { + p_test_llvm__bpf_result->size = obj_buf_sz; +memcpy (p_test_llvm__bpf_result->object, + obj_buf, obj_buf_sz); + } + }free (obj_buf);return err; } + +void test__llvm_prepare (void ) +{ + p_test_llvm__bpf_result =mmap (NULL ,SHARED_BUF_INIT_SIZE , +PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (!p_test_llvm__bpf_result) +return ; +memset ((void *)p_test_llvm__bpf_result,'\0' ,SHARED_BUF_INIT_SIZE ); +} + +void test__llvm_cleanup (void ) +{ +unsigned long boundary, buf_end; + +if (!p_test_llvm__bpf_result) +return ; +if (p_test_llvm__bpf_result->size ==0 ) { +munmap ((void *)p_test_llvm__bpf_result,SHARED_BUF_INIT_SIZE ); + p_test_llvm__bpf_result =NULL ; +return ; + } + + buf_end = (unsigned long )p_test_llvm__bpf_result +SHARED_BUF_INIT_SIZE ; + + boundary = (unsigned long )(p_test_llvm__bpf_result); + boundary += p_test_llvm__bpf_result->size; + boundary = (boundary + (page_size -1 )) & + (~((unsigned long )page_size -1 )); +munmap ((void *)boundary, buf_end - boundary); +}tools/perf/tests/llvm.h new file +14
-0@@ -0,0 +1,14 @@ +#ifndef PERF_TEST_LLVM_H +#define PERF_TEST_LLVM_H + +#include <stddef.h> /* for size_t */ + +struct test_llvm__bpf_result { +size_t size; +char object[]; +}; + +extern struct test_llvm__bpf_result *p_test_llvm__bpf_result; +extern const char test_llvm__bpf_prog[]; + +#endif tools/perf/tests/tests.h +4
-0@@ -27,6 +27,8 @@ enum {struct test {const char *desc;int (*func)(void ); +void (*prepare)(void ); +void (*cleanup)(void ); };/* Tests */ @@ -66,6 +68,8 @@ int test__fdarray__add(void);int test__kmod_path__parse (void );int test__thread_map (void );int test__llvm (void ); +void test__llvm_prepare (void ); +void test__llvm_cleanup (void );int test_session_topology (void );#if defined(__arm__) || defined(__aarch64__)Signature · 1 line
1.8.3.4
Re: [PATCH 10/31] perf test: Enforce LLVM test for BPF test
Quoted 7 lines · Arnaldo Carvalho de Melo
Em Wed, Oct 14, 2015 at 12:41:21PM +0000, Wang Nan escreveu:
Quoted 5 lines
This patch replaces the original toy BPF program with previous introduced
bpf-script-example.c. Dynamically embedded it into 'llvm-src.c'.The newly introduced BPF program attaches a BPF program at '
sys_epoll_pwait()', and collect half samples from it. perf itself never"collect half samples from it"? Can you rephrase this?
I mean half of epoll_pwait syscall will be captured. This test case totally issue 111 calls.
Thank you.
PATCH 11/31 perf test: Add 'perf test BPF'
In reply to Wang NanThis patch adds BPF testcase for testing BPF event filtering.
By utilizing the result of 'perf test LLVM', this patch compiles the eBPF sample program then test it ability. The BPF script in 'perf test LLVM' collects half of execution of epoll_pwait(). This patch runs 111 times of it, so the resule should contains 56 samples.
--- tools/perf/tests/Build | 1 + tools/perf/tests/bpf.c | 171 ++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/builtin-test.c | 4 + tools/perf/tests/llvm.c | 19 +++++ tools/perf/tests/llvm.h | 1 + tools/perf/tests/tests.h | 1 + tools/perf/util/bpf-loader.c | 14 ++++ tools/perf/util/bpf-loader.h | 9 +++ 8 files changed, 220 insertions(+) create mode 100644 tools/perf/tests/bpf.c
tools/perf/tests/Build +1 -0
@@ -32,6 +32,7 @@ perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o perf-y += llvm.o llvm-src.o +perf-y += bpf.o perf-y += topology.o $(OUTPUT)tests/llvm-src.c: tests/bpf-script-example.c
tools/perf/tests/bpf.c new file +171 -0
171 lines not on this page · raw message
tools/perf/tests/builtin-test.c +4 -0
@@ -168,6 +168,10 @@ static struct test generic_tests[] = { .func = test_session_topology, }, { + .desc = "Test BPF filter", + .func = test__bpf, + }, + { .func = NULL, }, };
tools/perf/tests/llvm.c +19 -0
@@ -174,3 +174,22 @@ void test__llvm_cleanup(void) (~((unsigned long )page_size -1 ));munmap ((void *)boundary, buf_end - boundary); } + +void +test_llvm__fetch_bpf_obj (void **p_obj_buf,size_t *p_obj_buf_sz) +{ + *p_obj_buf =NULL ; + *p_obj_buf_sz =0 ; + +if (!p_test_llvm__bpf_result) { +test__llvm_prepare (); +test__llvm (); +test__llvm_cleanup (); + } + +if (!p_test_llvm__bpf_result) +return ; + + *p_obj_buf = p_test_llvm__bpf_result->object; + *p_obj_buf_sz = p_test_llvm__bpf_result->size; +}
tools/perf/tests/llvm.h +1 -0
@@ -10,5 +10,6 @@ struct test_llvm__bpf_result {extern struct test_llvm__bpf_result *p_test_llvm__bpf_result;extern const char test_llvm__bpf_prog[]; +void test_llvm__fetch_bpf_obj (void **p_obj_buf,size_t *p_obj_buf_sz);#endif
tools/perf/tests/tests.h +1 -0
@@ -70,6 +70,7 @@ int test__thread_map(void);int test__llvm (void );void test__llvm_prepare (void );void test__llvm_cleanup (void ); +int test__bpf (void );int test_session_topology (void );#if defined(__arm__) || defined(__aarch64__)
tools/perf/util/bpf-loader.c +14 -0
@@ -34,6 +34,20 @@ struct bpf_prog_priv {struct perf_probe_event pev; }; +struct bpf_object * +bpf__prepare_load_buffer (void *obj_buf,size_t obj_buf_sz,const char *name) +{ +struct bpf_object *obj; + + obj =bpf_object__open_buffer (obj_buf, obj_buf_sz, name); +if (!obj) { +pr_debug ("bpf: failed to load buffer\n" ); +return ERR_PTR (-EINVAL ); + } + +return obj; +} +struct bpf_object *bpf__prepare_load (const char *filename,bool source) {struct bpf_object *obj;
tools/perf/util/bpf-loader.h +9 -0
@@ -19,6 +19,8 @@ typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,#ifdef HAVE_LIBBPF_SUPPORT struct bpf_object *bpf__prepare_load (const char *filename,bool source); +struct bpf_object *bpf__prepare_load_buffer (void *obj_buf,size_t obj_buf_sz, +const char *name);void bpf__clear (void );
@@ -41,6 +43,13 @@ bpf__prepare_load(const char *filename __maybe_unused,return ERR_PTR (-ENOTSUP ); } +static inline struct bpf_object * +bpf__prepare_load_buffer (void *obj_buf__maybe_unused , +size_t obj_buf_sz__maybe_unused ) +{ +return ERR_PTR (-ENOTSUP ); +} +static inline void bpf__clear (void ) { }static inline int bpf__probe (struct bpf_object *obj__maybe_unused ) {return 0 ;}
Signature · 1 line
1.8.3.4
PATCH 12/31 perf probe: Reset args and nargs for probe_trace_event when failure
In reply to Wang NanWhen failure occures in add_probe_trace_event(), args in probe_trace_event is incomplete. Since information in it may be used in futher, this patch frees the allocated memory and set it to NULL to avoid dangling pointer.
--- tools/perf/util/probe-finder.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/perf/util/probe-finder.c +4 -0
@@ -1235,6 +1235,10 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) end:free (args); +if (ret) { + tev->nargs =0 ; +zfree (&tev->args); + }return ret; }
Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 12/31] perf probe: Reset args and nargs for probe_trace_event when failure
Em Wed, Oct 14, 2015 at 12:41:23PM +0000, Wang Nan escreveu:
Quoted 4 lines
When failure occures in
add_probe_trace_event(), args inprobe_trace_eventis incomplete. Since information in it may be used in futher, this patch frees the allocated memory and set it to NULL to avoid dangling pointer.
Masami, can I have your Acked-by for this one?
Thanks,
- Arnaldo
Quoted 37 lines
Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/util/probe-finder.c | 4 ++++ 1 file changed, 4 insertions(+)tools/perf/util/probe-finder.c +4
-0@@ -1235,6 +1235,10 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) end:free (args); +if (ret) { + tev->nargs =0 ; +zfree (&tev->args); + }return ret; }Signature · 1 line
1.8.3.4
RE: [PATCH 12/31] perf probe: Reset args and nargs for probe_trace_event when failure
In reply to Wang NanFrom: Wang Nan [mailto:[email protected]]
Quoted 5 lines
When failure occures in
add_probe_trace_event(), args inprobe_trace_eventis incomplete. Since information in it may be used in futher, this patch frees the allocated memory and set it to NULL to avoid dangling pointer.
Sorry for replying late. Please see my comment.
Quoted 28 lines
Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>---
tools/perf/util/probe-finder.c| 4 ++++1 file changed, 4 insertions(+)tools/perf/util/probe-finder.c +0
-0@@ -1235,6 +1235,10 @@ static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf)end:
Quoted 1 line
free(args);
Actually, this args is filled with constant strings allocated in libdw, so it doesn't free args[*].vars.
tools/perf/util/probe-finder.c static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) · line 1240
Quoted 3 lines
tools/perf/util/probe-finder.c +3
-0+if (ret) { + tev->nargs =0 ; +zfree (&tev->args);
However, all the fields in tev->args[*] are allocated in find_variables. If you'd like to clear it, you must free the fields too.
So, you should call clear_probe_trace_event(struct probe_trace_event *tev) defined in util/probe-event.c.
Thank you,
Quoted 6 lines
tools/perf/util/probe-finder.c +1
-0+ }return ret; }--
1.8.3.4
���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�
PATCH 13/31 bpf tools: Load a program with different instances using preprocessor
In reply to Wang NanIn this patch, caller of libbpf is able to control the loaded programs by installing a preprocessor callback for a BPF program. With preprocessor, different instances can be created from one BPF program.
This patch will be used by perf to generate different prologue for different 'struct probe_trace_event' instances matched by one 'struct perf_probe_event'.
bpf_program__set_prep() is added to support this feature. Caller should pass libbpf the number of instances should be created and a preprocessor function which will be called when doing real loading. The callback should return instructions arrays for each instances.
fd field in bpf_programs is replaced by instance, which has an nr field and fds array. bpf_program__nth_fd() is introduced for read fd of instances. Old interface bpf_program__fd() is reimplemented by returning the first fd.
--- tools/lib/bpf/libbpf.c | 143 +++++++++++++++++++++++++++++++++++++++++++++---- tools/lib/bpf/libbpf.h | 22 ++++++++ 2 files changed, 156 insertions(+), 9 deletions(-)
tools/lib/bpf/libbpf.c +134 -9
@@ -98,7 +98,11 @@ struct bpf_program { } *reloc_desc;int nr_reloc; -int fd; +struct { +int nr; +int *fds; + } instance; +bpf_program_prep_t preprocessor;struct bpf_object *obj;void *priv;
@@ -152,10 +156,24 @@ struct bpf_object {static void bpf_program__unload (struct bpf_program *prog) { +int i; +if (!prog)return ; -zclose (prog->fd); +/* +* If the object is opened but the program is never loaded, +* it is possible that prog->instance.nr == -1. +*/ +if (prog->instance.nr >0 ) { +for (i =0 ; i < prog->instance.nr; i++) +zclose (prog->instance.fds[i]); + }else if (prog->instance.nr !=-1 ) +pr_warning ("Internal error: instance.nr is %d\n" , + prog->instance.nr); + + prog->instance.nr =-1 ; +zfree (&prog->instance.fds); }static void bpf_program__exit (struct bpf_program *prog)
@@ -206,7 +224,8 @@ bpf_program__init(void *data, size_t size, char *name, int idx,memcpy (prog->insns, data, prog->insns_cnt *sizeof (struct bpf_insn )); prog->idx = idx; - prog->fd =-1 ; + prog->instance.fds =NULL ; + prog->instance.nr =-1 ;return 0 ; errout:
@@ -795,13 +814,71 @@ static int bpf_program__load(struct bpf_program *prog, char *license, u32 kern_version) { - int err, fd; + int err = 0, fd, i; + +if (prog->instance.nr <0 || !prog->instance.fds) { +if (prog->preprocessor) { +pr_warning ("Internal error: can't load program '%s'\n" , + prog->section_name); +return -EINVAL ; + } + + prog->instance.fds =malloc (sizeof (int )); +if (!prog->instance.fds) { +pr_warning ("No enough memory for fds\n" ); +return -ENOMEM ; + } + prog->instance.nr =1 ; + prog->instance.fds[0 ] =-1 ; + } + +if (!prog->preprocessor) { +if (prog->instance.nr !=1 ) +pr_warning ("Program '%s' inconsistent: nr(%d) not 1\n" , + prog->section_name, prog->instance.nr); - err =load_program (prog->insns, prog->insns_cnt, - license, kern_version, &fd); -if (!err) - prog->fd = fd; + err =load_program (prog->insns, prog->insns_cnt, + license, kern_version, &fd); +if (!err) + prog->instance.fds[0 ] = fd; + goto out; + } + +for (i =0 ; i < prog->instance.nr; i++) { +struct bpf_prog_prep_result result; +bpf_program_prep_t preprocessor = prog->preprocessor; + +bzero (&result,sizeof (result)); + err =preprocessor (prog, i, prog->insns, + prog->insns_cnt, &result); +if (err) { +pr_warning ("Preprocessing %dth instance of program '%s' failed\n" , + i, prog->section_name); + goto out; + } + +if (!result.new_insn_ptr || !result.new_insn_cnt) { +pr_debug ("Skip loading %dth instance of program '%s'\n" , + i, prog->section_name); + prog->instance.fds[i] =-1 ; +continue ; + } + + err =load_program (result.new_insn_ptr, + result.new_insn_cnt, + license, kern_version, &fd); + +if (err) { +pr_warning ("Loading %dth instance of program '%s' failed\n" , + i, prog->section_name); + goto out; + } +if (result.pfd) + *result.pfd = fd; + prog->instance.fds[i] = fd; + } +out:if (err)pr_warning ("failed to load program '%s'\n" , prog->section_name);
@@ -1052,5 +1129,53 @@ const char *bpf_program__title(struct bpf_program *prog, bool dup)int bpf_program__fd (struct bpf_program *prog) { -return prog->fd; +return bpf_program__nth_fd (prog,0 ); +} + +int bpf_program__set_prep (struct bpf_program *prog,int nr_instance, +bpf_program_prep_t prep) +{ +int *instance_fds; + +if (nr_instance <=0 || !prep) +return -EINVAL ; + +if (prog->instance.nr >0 || prog->instance.fds) { +pr_warning ("Can't set pre-processor after loading\n" ); +return -EINVAL ; + } + + instance_fds =malloc (sizeof (int ) * nr_instance); +if (!instance_fds) { +pr_warning ("alloc memory failed for instance of fds\n" ); +return -ENOMEM ; + } + +/* fill all fd with -1 */ +memset (instance_fds,0xff ,sizeof (int ) * nr_instance); + + prog->instance.nr = nr_instance; + prog->instance.fds = instance_fds; + prog->preprocessor = prep; +return 0 ; +} + +int bpf_program__nth_fd (struct bpf_program *prog,int n) +{ +int fd; + +if (n >= prog->instance.nr || n <0 ) { +pr_warning ("Can't get the %dth fd from program %s: only %d instances\n" , + n, prog->section_name, prog->instance.nr); +return -EINVAL ; + } + + fd = prog->instance.fds[n]; +if (fd <0 ) { +pr_warning ("%dth instance of program '%s' is invalid\n" , + n, prog->section_name); +return -ENOENT ; + } + +return fd; }
tools/lib/bpf/libbpf.h +22 -0
@@ -67,6 +67,28 @@ const char *bpf_program__title(struct bpf_program *prog, bool dup);int bpf_program__fd (struct bpf_program *prog); +struct bpf_insn ; +struct bpf_prog_prep_result { +/* +* If not NULL, load new instruction array. +* If set to NULL, don't load this instance. +*/ +struct bpf_insn *new_insn_ptr; +int new_insn_cnt; + +/* If not NULL, result fd is set to it */ +int *pfd; +}; + +typedef int (*bpf_program_prep_t )(struct bpf_program *,int n, +struct bpf_insn *,int insn_cnt, +struct bpf_prog_prep_result *res); + +int bpf_program__set_prep (struct bpf_program *prog,int nr_instance, +bpf_program_prep_t prep); + +int bpf_program__nth_fd (struct bpf_program *prog,int n); +/* * We don't need __attribute__((packed)) now since it is * unnecessary for 'bpf_map_def' because they are all aligned.
Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 13/31] bpf tools: Load a program with different instances using preprocessor
Em Wed, Oct 14, 2015 at 12:41:24PM +0000, Wang Nan escreveu:
Quoted 3 lines
In this patch, caller of libbpf is able to control the loaded programs by installing a preprocessor callback for a BPF program. With preprocessor, different instances can be created from one BPF program.
Why would one want to do that? I'm new to eBPF, as many other here, I guess, so giving some more context as to what will this achieve will help me in processing these patches.
Quoted 6 lines
This patch will be used by perf to generate different prologue for different 'struct
probe_trace_event' instances matched by one 'structperf_probe_event'.
bpf_program__set_prep()is added to support this feature. Caller should pass libbpf the number of instances should be created and a
that
Quoted 1 line
preprocessor function which will be called when doing real loading.
the
Quoted 1 line
The callback should return instructions arrays for each instances.
instruction arrays for each instance.
Quoted 2 lines
fd field in
bpf_programsis replaced by instance, which has an nr field
The fd field in the bpf_programs struct is replaced by instance (instance of what?)
Quoted 246 lines
and fds array.
bpf_program__nth_fd()is introduced for read fd of instances. Old interfacebpf_program__fd()is reimplemented by returning the first fd.Signed-off-by Wang Nan <[email protected]>Signed-off-by He Kuang <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>--- tools/lib/bpf/libbpf.c | 143 +++++++++++++++++++++++++++++++++++++++++++++---- tools/lib/bpf/libbpf.h | 22 ++++++++ 2 files changed, 156 insertions(+), 9 deletions(-)tools/lib/bpf/libbpf.c +134
-9@@ -98,7 +98,11 @@ struct bpf_program { } *reloc_desc;int nr_reloc; -int fd; +struct { +int nr; +int *fds; + } instance; +bpf_program_prep_t preprocessor;struct bpf_object *obj;void *priv;@@ -152,10 +156,24 @@ struct bpf_object {static void bpf_program__unload (struct bpf_program *prog) { +int i; +if (!prog)return ; -zclose (prog->fd); +/* +* If the object is opened but the program is never loaded, +* it is possible that prog->instance.nr == -1. +*/ +if (prog->instance.nr >0 ) { +for (i =0 ; i < prog->instance.nr; i++) +zclose (prog->instance.fds[i]); + }else if (prog->instance.nr !=-1 ) +pr_warning ("Internal error: instance.nr is %d\n" , + prog->instance.nr); + + prog->instance.nr =-1 ; +zfree (&prog->instance.fds); }static void bpf_program__exit (struct bpf_program *prog)@@ -206,7 +224,8 @@ bpf_program__init(void *data, size_t size, char *name, int idx,memcpy (prog->insns, data, prog->insns_cnt *sizeof (struct bpf_insn )); prog->idx = idx; - prog->fd =-1 ; + prog->instance.fds =NULL ; + prog->instance.nr =-1 ;return 0 ; errout:@@ -795,13 +814,71 @@ static int bpf_program__load(struct bpf_program *prog, char *license, u32 kern_version) { - int err, fd; + int err = 0, fd, i; + +if (prog->instance.nr <0 || !prog->instance.fds) { +if (prog->preprocessor) { +pr_warning ("Internal error: can't load program '%s'\n" , + prog->section_name); +return -EINVAL ; + } + + prog->instance.fds =malloc (sizeof (int )); +if (!prog->instance.fds) { +pr_warning ("No enough memory for fds\n" ); +return -ENOMEM ; + } + prog->instance.nr =1 ; + prog->instance.fds[0 ] =-1 ; + } + +if (!prog->preprocessor) { +if (prog->instance.nr !=1 ) +pr_warning ("Program '%s' inconsistent: nr(%d) not 1\n" , + prog->section_name, prog->instance.nr); - err =load_program (prog->insns, prog->insns_cnt, - license, kern_version, &fd); -if (!err) - prog->fd = fd; + err =load_program (prog->insns, prog->insns_cnt, + license, kern_version, &fd); +if (!err) + prog->instance.fds[0 ] = fd; + goto out; + } + +for (i =0 ; i < prog->instance.nr; i++) { +struct bpf_prog_prep_result result; +bpf_program_prep_t preprocessor = prog->preprocessor; + +bzero (&result,sizeof (result)); + err =preprocessor (prog, i, prog->insns, + prog->insns_cnt, &result); +if (err) { +pr_warning ("Preprocessing %dth instance of program '%s' failed\n" , + i, prog->section_name); + goto out; + } + +if (!result.new_insn_ptr || !result.new_insn_cnt) { +pr_debug ("Skip loading %dth instance of program '%s'\n" , + i, prog->section_name); + prog->instance.fds[i] =-1 ; +continue ; + } + + err =load_program (result.new_insn_ptr, + result.new_insn_cnt, + license, kern_version, &fd); + +if (err) { +pr_warning ("Loading %dth instance of program '%s' failed\n" , + i, prog->section_name); + goto out; + } +if (result.pfd) + *result.pfd = fd; + prog->instance.fds[i] = fd; + } +out:if (err)pr_warning ("failed to load program '%s'\n" , prog->section_name);@@ -1052,5 +1129,53 @@ const char *bpf_program__title(struct bpf_program *prog, bool dup)int bpf_program__fd (struct bpf_program *prog) { -return prog->fd; +return bpf_program__nth_fd (prog,0 ); +} + +int bpf_program__set_prep (struct bpf_program *prog,int nr_instance, +bpf_program_prep_t prep) +{ +int *instance_fds; + +if (nr_instance <=0 || !prep) +return -EINVAL ; + +if (prog->instance.nr >0 || prog->instance.fds) { +pr_warning ("Can't set pre-processor after loading\n" ); +return -EINVAL ; + } + + instance_fds =malloc (sizeof (int ) * nr_instance); +if (!instance_fds) { +pr_warning ("alloc memory failed for instance of fds\n" ); +return -ENOMEM ; + } + +/* fill all fd with -1 */ +memset (instance_fds,0xff ,sizeof (int ) * nr_instance); + + prog->instance.nr = nr_instance; + prog->instance.fds = instance_fds; + prog->preprocessor = prep; +return 0 ; +} + +int bpf_program__nth_fd (struct bpf_program *prog,int n) +{ +int fd; + +if (n >= prog->instance.nr || n <0 ) { +pr_warning ("Can't get the %dth fd from program %s: only %d instances\n" , + n, prog->section_name, prog->instance.nr); +return -EINVAL ; + } + + fd = prog->instance.fds[n]; +if (fd <0 ) { +pr_warning ("%dth instance of program '%s' is invalid\n" , + n, prog->section_name); +return -ENOENT ; + } + +return fd; }tools/lib/bpf/libbpf.h +22
-0@@ -67,6 +67,28 @@ const char *bpf_program__title(struct bpf_program *prog, bool dup);int bpf_program__fd (struct bpf_program *prog); +struct bpf_insn ; +struct bpf_prog_prep_result { +/* +* If not NULL, load new instruction array. +* If set to NULL, don't load this instance. +*/ +struct bpf_insn *new_insn_ptr; +int new_insn_cnt; + +/* If not NULL, result fd is set to it */ +int *pfd; +}; + +typedef int (*bpf_program_prep_t )(struct bpf_program *,int n, +struct bpf_insn *,int insn_cnt, +struct bpf_prog_prep_result *res); + +int bpf_program__set_prep (struct bpf_program *prog,int nr_instance, +bpf_program_prep_t prep); + +int bpf_program__nth_fd (struct bpf_program *prog,int n); +/* * We don't need __attribute__((packed)) now since it is * unnecessary for 'bpf_map_def' because they are all aligned. Signature · 1 line
1.8.3.4
Re: [PATCH 13/31] bpf tools: Load a program with different instances using preprocessor
Quoted 8 lines · Arnaldo Carvalho de Melo
Em Wed, Oct 14, 2015 at 12:41:24PM +0000, Wang Nan escreveu:
Quoted 3 lines
In this patch, caller of libbpf is able to control the loaded programs by installing a preprocessor callback for a BPF program. With preprocessor, different instances can be created from one BPF program.
Why would one want to do that? I'm new to eBPF, as many other here, I guess, so giving some more context as to what will this achieve will help me in processing these patches.
I'll explain at the bottom of this reply.
Quoted 6 lines
Quoted 6 lines
This patch will be used by perf to generate different prologue for different 'struct
probe_trace_event' instances matched by one 'structperf_probe_event'.
bpf_program__set_prep()is added to support this feature. Caller should pass libbpf the number of instances should be created and a
Quoted 8 lines
that
Quoted 1 line
preprocessor function which will be called when doing real loading.
the
Quoted 1 line
The callback should return instructions arrays for each instances.
instruction arrays for each instance.
Quoted 1 line
fd field in
bpf_programsis replaced by instance, which has an nr fieldThe fd field in the
bpf_programsstruct is replaced by instance (instance of what?)
Instance of program. It is a new concept introduced in this patch. Before this patch, one program create one fd. This patch enables one program to create multiple fds. Each fd is a program instance.
In summary, this and following patches try to solve a important problem in such tracing tool that: how to access kernel data in BPF program.
Our core methode on this is BPF prologue that we automatically generate some BPF asm code for fetching them. However, after we implement it another problem arises.
We found that there are many cases that multiple entries share one symbol. Most of the time caused by function inlining. We also support using glob to match multiple symbol. For example:
SEC("func=my_func_*_abcd arg1")
int func(...) { }
In the above example, it is highly possible that arg1 exist for all my_func_*_abcd, but in different registers or on different place in the stack.
When probing, perf would create one tev for each actual probing point. Due to prologues for fetching arg1 are different, we need to create multiple BPF prologue (so different BPF fd) for each probing point. This is why we need this patch: Load a program with different instances.
I have to admit that the design of this patch is not very easy to be understand. However I believe it is the best way we can find. The biggest reason of current design is that I don't want libbpf be bounded with perf tightly. The prologue generation should be done by perf, libbpf should never though about it. What it should provide is to give caller (perf) a way to decorate the BPF binary in multiple ways for different instances. Therefore I let caller to inject a helper (preprocessor) for adjusting the code. What the caller need to tell libbpf is the total instances it want to create, and the preprocessor should maintain the mapping between the No. of instance and the binary BPF program it want to create.
For instance, as the caller of libbpf, perf should know how to generate prologue for my_func_a_abcd and my_func_b_abcd, then it should maintain a mapping:
No. 0 --> my_func_a_abcd
No. 1 --> my_func_b_abcd
Then it need to tell libbpf that it would create 2 instances of a BPF program. In the preprocessor perf inject into libbpf, it should generate prologue for each probing point according to the ID.
Thank you.
PATCH 14/31 perf tools: Add BPF_PROLOGUE config options for further patches
In reply to Wang NanIf both LIBBPF and DWARF are detected, it is possible to create prologue for eBPF programs to help them accessing kernel data. HAVE_BPF_PROLOGUE and CONFIG_BPF_PROLOGUE is added as flags for this feature.
PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET indicates an architecture supports converting name of a register to its offset in 'struct pt_regs'. Without this support, BPF_PROLOGUE should be turned off.
HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET is introduced as the corresponding CFLAGS of PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET.
--- tools/perf/config/Makefile | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/perf/config/Makefile +12 -0
@@ -318,6 +318,18 @@ ifndef NO_LIBELF CFLAGS += -DHAVE_LIBBPF_SUPPORT $(call detected,CONFIG_LIBBPF) endif + + ifndef NO_DWARF + ifdef PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET + CFLAGS += -DHAVE_BPF_PROLOGUE + $(call detected,CONFIG_BPF_PROLOGUE) + else + msg := $(warning BPF prologue is not supported by architecture $(ARCH), missing regs_query_register_offset()); + endif + else + msg := $(warning DWARF support is off, BPF prologue is disabled); + endif + endif # NO_LIBBPF endif # NO_LIBELF
Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 14/31] perf tools: Add BPF_PROLOGUE config options for further patches
Em Wed, Oct 14, 2015 at 12:41:25PM +0000, Wang Nan escreveu:
Quoted 2 lines
If both LIBBPF and DWARF are detected, it is possible to create prologue for eBPF programs to help them accessing kernel data.
HAVE_BPF_PROLOGUE
what kernel data? you mean to be
able to map from variable/function argument name to where they are in memory/registers?
Quoted 54 lines
and
CONFIG_BPF_PROLOGUEis added as flags for this feature.
PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSETindicates an architecture supports converting name of a register to its offset in 'structpt_regs'. Without this support,BPF_PROLOGUEshould be turned off.
HAVE_ARCH_REGS_QUERY_REGISTER_OFFSETis introduced as the corresponding CFLAGS ofPERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET.Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/config/Makefile | 12 ++++++++++++ 1 file changed, 12 insertions(+)tools/perf/config/Makefile +12
-0@@ -318,6 +318,18 @@ ifndef NO_LIBELF CFLAGS += -DHAVE_LIBBPF_SUPPORT $(call detected,CONFIG_LIBBPF) endif + + ifndef NO_DWARF + ifdef PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET + CFLAGS += -DHAVE_BPF_PROLOGUE + $(call detected,CONFIG_BPF_PROLOGUE) + else + msg := $(warning BPF prologue is not supported by architecture $(ARCH), missing regs_query_register_offset()); + endif + else + msg := $(warning DWARF support is off, BPF prologue is disabled); + endif + endif # NO_LIBBPF endif # NO_LIBELFSignature · 1 line
1.8.3.4
PATCH 15/31 perf tools: Compile dwarf-regs.c if CONFIG_BPF_PROLOGUE is on
In reply to Wang Nanregs_query_register_offset() in dwarf-regs.c is required by BPF prologue. Make it be compiled if CONFIG_BPF_PROLOGUE is on to avoid building failure when CONFIG_BPF_PROLOGUE is on but CONFIG_DWARF is not set.
--- tools/perf/arch/x86/util/Build | 1 + 1 file changed, 1 insertion(+)
tools/perf/arch/x86/util/Build +1 -0
@@ -5,6 +5,7 @@ libperf-y += kvm-stat.o libperf-y += perf_regs.o libperf-$(CONFIG_DWARF) += dwarf-regs.o +libperf-$(CONFIG_BPF_PROLOGUE) += dwarf-regs.o libperf-$(CONFIG_LIBUNWIND) += unwind-libunwind.o libperf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
Signature · 1 line
1.8.3.4
PATCH 16/31 perf tools: Add prologue for BPF programs for fetching arguments
In reply to Wang NanFrom: He Kuang <[email protected]>
This patch generates prologue for a BPF program which fetch arguments for it. With this patch, the program can have arguments as follow:
SEC("lock_page=__lock_page page->flags")
int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;}
This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx pointer. r2 is used to indicate the successfulness of dereferencing.
This patch uses r6 to hold ctx (struct pt_regs) and r7 to hold stack pointer for result. Result of each arguments first store on stack:
low address
BPF_REG_FP - 24 ARG3 BPF_REG_FP - 16 ARG2 BPF_REG_FP - 8 ARG1
BPF_REG_FP
high address
Then loaded into r3, r4 and r5.
The output prologue for offn(...off2(off1(reg)))) should be:
r6 <- r1 // save ctx into a callee saved register
r7 <- fp
r7 <- r7 - stack_offset // pointer to result slot
/* load r3 with the offset in pt_regs of 'reg' */
(r7) <- r3 // make slot valid
r3 <- r3 + off1 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7 // result put onto stack
call probe_read // read unsafe pointer
jnei r0, 0, err // error checking
r3 <- (r7) // read result
r3 <- r3 + off2 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7
call probe_read
jnei r0, 0, err
...
/* load r2, r3, r4 from stack */
goto success
err:
r2 <- 1
/* load r3, r4, r5 with 0 */
goto usercode
success:
r2 <- 0
usercode:
r1 <- r6 // restore ctx
// original user codeIf all of arguments reside in register (dereferencing is not required), gen_prologue_fastpath() will be used to create fast prologue:
r3 <- (r1 + offset of reg1)
r4 <- (r1 + offset of reg2)
r5 <- (r1 + offset of reg3)
r2 <- 0P.S.
eBPF calling convention is defined as:
* r0 - return value from in-kernel function, and exit value
for eBPF program
* r1 - r5 - arguments from eBPF program to in-kernel function
* r6 - r9 - callee saved registers that in-kernel function will
preserve
* r10 - read-only frame pointer to access stack
--- tools/perf/util/Build | 1 + tools/perf/util/bpf-prologue.c | 443 +++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-prologue.h | 34 ++++ 3 files changed, 478 insertions(+) create mode 100644 tools/perf/util/bpf-prologue.c create mode 100644 tools/perf/util/bpf-prologue.h
tools/perf/util/Build +1 -0
@@ -88,6 +88,7 @@ libperf-y += parse-branch-options.o libperf-y += parse-regs-options.o libperf-$(CONFIG_LIBBPF) += bpf-loader.o +libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o libperf-$(CONFIG_LIBELF) += symbol-elf.o libperf-$(CONFIG_LIBELF) += probe-file.o libperf-$(CONFIG_LIBELF) += probe-event.o
tools/perf/util/bpf-prologue.c new file +443 -0
443 lines not on this page · raw message
tools/perf/util/bpf-prologue.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* Copyright (C) 2015, He Kuang <[email protected]> +* Copyright (C) 2015, Huawei Inc. +*/ +#ifndef __BPF_PROLOGUE_H +#define __BPF_PROLOGUE_H + +#include <linux/compiler.h> +#include <linux/filter.h> +#include "probe-event.h" + +#define BPF_PROLOGUE_MAX_ARGS 3 +#define BPF_PROLOGUE_START_ARG_REG BPF_REG_3 +#define BPF_PROLOGUE_FETCH_RESULT_REG BPF_REG_2 + +#ifdef HAVE_BPF_PROLOGUE +int bpf__gen_prologue (struct probe_trace_arg *args,int nargs, +struct bpf_insn *new_prog,size_t *new_cnt, +size_t cnt_space); +#else +static inline int +bpf__gen_prologue (struct probe_trace_arg *args__maybe_unused , +int nargs__maybe_unused , +struct bpf_insn *new_prog__maybe_unused , +size_t *new_cnt, +size_t cnt_space__maybe_unused ) +{ +if (!new_cnt) +return -EINVAL ; + *new_cnt =0 ; +return 0 ; +} +#endif +#endif /* __BPF_PROLOGUE_H */
Signature · 1 line
1.8.3.4
Namhyung Kim
maintainerRe: [PATCH 16/31] perf tools: Add prologue for BPF programs for fetching arguments
Quoted 96 lines · Wang Nan
From: He Kuang <[email protected]>
This patch generates prologue for a BPF program which fetch arguments for it. With this patch, the program can have arguments as follow:
SEC("lock_page=__lock_page page->flags")
intlock_page(struct pt_regs *ctx, int err, unsigned long flags){ return 1;}
This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx pointer. r2 is used to indicate the successfulness of dereferencing.
This patch uses r6 to hold ctx (struct
pt_regs) and r7 to hold stack pointer for result. Result of each arguments first store on stack:low address
BPF_REG_FP - 24 ARG3 BPF_REG_FP - 16 ARG2 BPF_REG_FP - 8 ARG1
BPF_REG_FP
high addressThen loaded into r3, r4 and r5.
The output prologue for offn(...off2(off1(reg)))) should be:
r6 <- r1 // save ctx into a callee saved register r7 <- fp r7 <- r7 - stack_offset // pointer to result slot /* load r3 with the offset in pt_regs of 'reg' */ (r7) <- r3 // make slot valid r3 <- r3 + off1 // prepare to read unsafe pointer r2 <- 8 r1 <- r7 // result put onto stack call probe_read // read unsafe pointer jnei r0, 0, err // error checking r3 <- (r7) // read result r3 <- r3 + off2 // prepare to read unsafe pointer r2 <- 8 r1 <- r7 call probe_read jnei r0, 0, err ... /* load r2, r3, r4 from stack */ goto success err: r2 <- 1 /* load r3, r4, r5 with 0 */ goto usercode success: r2 <- 0 usercode: r1 <- r6 // restore ctx // original user codeIf all of arguments reside in register (dereferencing is not required),
gen_prologue_fastpath()will be used to create fast prologue:r3 <- (r1 + offset of reg1) r4 <- (r1 + offset of reg2) r5 <- (r1 + offset of reg3) r2 <- 0
P.S.eBPF calling convention is defined as:
* r0 - return value from in-kernel function, and exit value
for eBPF program
* r1 - r5 - arguments from eBPF program to in-kernel function
* r6 - r9 - callee saved registers that in-kernel function will
preserve
* r10 - read-only frame pointer to access stackSigned-off-by He Kuang <[email protected]>Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>---
[SNIP]
Quoted 21 lines
tools/perf/util/bpf-prologue.c new file +21
-0+int bpf__gen_prologue (struct probe_trace_arg *args,int nargs, +struct bpf_insn *new_prog,size_t *new_cnt, +size_t cnt_space) +{ +struct bpf_insn *success_code =NULL ; +struct bpf_insn *error_code =NULL ; +struct bpf_insn *user_code =NULL ; +struct bpf_insn_pos pos; +bool fastpath = true; +int i; + +if (!new_prog || !new_cnt) +return -EINVAL ; + + pos.begin = new_prog; + pos.end = new_prog + cnt_space; + pos.pos = new_prog; + +if (!nargs) { +ins (BPF_ALU64_IMM (BPF_MOV ,BPF_PROLOGUE_FETCH_RESULT_REG ,0 ), + &pos);
tools/perf/util/bpf-prologue.c line 341
Quoted 10 lines
tools/perf/util/bpf-prologue.c new file +10
-0+ +if (check_pos (&pos)) + goto errout; + + *new_cnt =pos_get_cnt (&pos); +return 0 ; + } + +if (nargs >BPF_PROLOGUE_MAX_ARGS ) + nargs =BPF_PROLOGUE_MAX_ARGS ;
Wouldn't it be better to inform user if it ignored some arguments?
Thanks,
Namhyung
Quoted 145 lines
tools/perf/util/bpf-prologue.c new file +102
-0+if (cnt_space >BPF_MAXINSNS ) + cnt_space =BPF_MAXINSNS ; + +/* First pass: validation */ +for (i =0 ; i < nargs; i++) { +struct probe_trace_arg_ref *ref = args[i].ref; + +if (args[i].value[0 ] =='@' ) { +/* TODO: fetch global variable */ +pr_err ("bpf: prologue: global %s%+ld not support\n" , + args[i].value, ref ? ref->offset :0 ); +return -ENOTSUP ; + } + +while (ref) { +/* fastpath is true if all args has ref == NULL */ + fastpath = false; + +/* +* Instruction encodes immediate value using +* s32, ref->offset is long. On systems which +* can't fill long in s32, refuse to process if +* ref->offset too large (or small). +*/ +#ifdef __LP64__ +#define OFFSET_MAX ((1LL << 31) - 1) +#define OFFSET_MIN ((1LL << 31) * -1) +if (ref->offset >OFFSET_MAX || + ref->offset <OFFSET_MIN ) { +pr_err ("bpf: prologue: offset out of bound: %ld\n" , + ref->offset); +return -E2BIG ; + } +#endif + ref = ref->next; + } + } +pr_debug ("prologue: pass validation\n" ); + +if (fastpath) { +/* If all variables are registers... */ +pr_debug ("prologue: fast path\n" ); +if (gen_prologue_fastpath (&pos, args, nargs)) + goto errout; + }else { +pr_debug ("prologue: slow path\n" ); + +/* Initialization: move ctx to a callee saved register. */ +ins (BPF_MOV64_REG (BPF_REG_CTX ,BPF_REG_ARG1 ), &pos); + +if (gen_prologue_slowpath (&pos, args, nargs)) + goto errout; +/* +* start of ERROR_CODE (only slow pass needs error code) +* mov r2 <- 1 +* goto usercode +*/ + error_code = pos.pos; +ins (BPF_ALU64_IMM (BPF_MOV ,BPF_PROLOGUE_FETCH_RESULT_REG ,1 ), + &pos); + +for (i =0 ; i < nargs; i++) +ins (BPF_ALU64_IMM (BPF_MOV , +BPF_PROLOGUE_START_ARG_REG + i, +0 ), + &pos); +ins (BPF_JMP_IMM (BPF_JA ,BPF_REG_0 ,0 ,JMP_TO_USER_CODE ), + &pos); + } + +/* +* start of SUCCESS_CODE: +* mov r2 <- 0 +* goto usercode // skip +*/ + success_code = pos.pos; +ins (BPF_ALU64_IMM (BPF_MOV ,BPF_PROLOGUE_FETCH_RESULT_REG ,0 ), &pos); + +/* +* start of USER_CODE: +* Restore ctx to r1 +*/ + user_code = pos.pos; +if (!fastpath) { +/* +* Only slow path needs restoring of ctx. In fast path, +* register are loaded directly from r1. +*/ +ins (BPF_MOV64_REG (BPF_REG_ARG1 ,BPF_REG_CTX ), &pos); +if (prologue_relocate (&pos, error_code, success_code, + user_code)) + goto errout; + } + +if (check_pos (&pos)) + goto errout; + + *new_cnt =pos_get_cnt (&pos); +return 0 ; +errout: +return -ERANGE ; +}tools/perf/util/bpf-prologue.h new file +34
-0@@ -0,0 +1,34 @@ +/* +* Copyright (C) 2015, He Kuang <[email protected]> +* Copyright (C) 2015, Huawei Inc. +*/ +#ifndef __BPF_PROLOGUE_H +#define __BPF_PROLOGUE_H + +#include <linux/compiler.h> +#include <linux/filter.h> +#include "probe-event.h" + +#define BPF_PROLOGUE_MAX_ARGS 3 +#define BPF_PROLOGUE_START_ARG_REG BPF_REG_3 +#define BPF_PROLOGUE_FETCH_RESULT_REG BPF_REG_2 + +#ifdef HAVE_BPF_PROLOGUE +int bpf__gen_prologue (struct probe_trace_arg *args,int nargs, +struct bpf_insn *new_prog,size_t *new_cnt, +size_t cnt_space); +#else +static inline int +bpf__gen_prologue (struct probe_trace_arg *args__maybe_unused , +int nargs__maybe_unused , +struct bpf_insn *new_prog__maybe_unused , +size_t *new_cnt, +size_t cnt_space__maybe_unused ) +{ +if (!new_cnt) +return -EINVAL ; + *new_cnt =0 ; +return 0 ; +} +#endif +#endif /* __BPF_PROLOGUE_H */ Signature · 1 line
1.8.3.4
Re: [PATCH 16/31] perf tools: Add prologue for BPF programs for fetching arguments
Quoted 122 lines · Namhyung Kim
Quoted 96 lines · Wang Nan
From: He Kuang <[email protected]>
This patch generates prologue for a BPF program which fetch arguments for it. With this patch, the program can have arguments as follow:
SEC("lock_page=__lock_page page->flags")
intlock_page(struct pt_regs *ctx, int err, unsigned long flags){ return 1;}
This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx pointer. r2 is used to indicate the successfulness of dereferencing.
This patch uses r6 to hold ctx (struct
pt_regs) and r7 to hold stack pointer for result. Result of each arguments first store on stack:low address
BPF_REG_FP - 24 ARG3 BPF_REG_FP - 16 ARG2 BPF_REG_FP - 8 ARG1
BPF_REG_FP
high addressThen loaded into r3, r4 and r5.
The output prologue for offn(...off2(off1(reg)))) should be:
r6 <- r1 // save ctx into a callee saved register r7 <- fp r7 <- r7 - stack_offset // pointer to result slot /* load r3 with the offset in pt_regs of 'reg' */ (r7) <- r3 // make slot valid r3 <- r3 + off1 // prepare to read unsafe pointer r2 <- 8 r1 <- r7 // result put onto stack call probe_read // read unsafe pointer jnei r0, 0, err // error checking r3 <- (r7) // read result r3 <- r3 + off2 // prepare to read unsafe pointer r2 <- 8 r1 <- r7 call probe_read jnei r0, 0, err ... /* load r2, r3, r4 from stack */ goto success err: r2 <- 1 /* load r3, r4, r5 with 0 */ goto usercode success: r2 <- 0 usercode: r1 <- r6 // restore ctx // original user codeIf all of arguments reside in register (dereferencing is not required),
gen_prologue_fastpath()will be used to create fast prologue:r3 <- (r1 + offset of reg1) r4 <- (r1 + offset of reg2) r5 <- (r1 + offset of reg3) r2 <- 0
P.S.eBPF calling convention is defined as:
* r0 - return value from in-kernel function, and exit value
for eBPF program
* r1 - r5 - arguments from eBPF program to in-kernel function
* r6 - r9 - callee saved registers that in-kernel function will
preserve
* r10 - read-only frame pointer to access stackSigned-off-by He Kuang <[email protected]>Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>---
[SNIP]
Quoted 24 lines
(quoted diff) +24
-0+int bpf__gen_prologue(struct probe_trace_arg *args, int nargs, + struct bpf_insn *new_prog, size_t *new_cnt, + size_t cnt_space) +{ + struct bpf_insn *success_code = NULL; + struct bpf_insn *error_code = NULL; + struct bpf_insn *user_code = NULL; + struct bpf_insn_pos pos; + bool fastpath = true; + int i; + + if (!new_prog || !new_cnt) + return -EINVAL; + + pos.begin = new_prog; + pos.end = new_prog + cnt_space; + pos.pos = new_prog; + + if (!nargs) { + ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 0), + &pos); + + if (check_pos(&pos)) + goto errout;
Quoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ + *new_cnt = pos_get_cnt(&pos); + return 0; + } + + if (nargs > BPF_PROLOGUE_MAX_ARGS) + nargs = BPF_PROLOGUE_MAX_ARGS;Wouldn't it be better to inform user if it ignored some arguments?
Correct. I'd like to add a notification in next version:
tools/perf/util/bpf-prologue.c +3 -1
@@ -337,8 +337,10 @@ int bpf__gen_prologue(struct probe_trace_arg *args, int nargs,return 0 ; } -if (nargs >BPF_PROLOGUE_MAX_ARGS ) +if (nargs >BPF_PROLOGUE_MAX_ARGS ) { +pr_warning ("bpf: prologue: too many arguments\n" ); nargs =BPF_PROLOGUE_MAX_ARGS ; + }if (cnt_space >BPF_MAXINSNS ) cnt_space =BPF_MAXINSNS ;
Thank you.
Quoted 2 lines
Thanks,
Namhyung
PATCH 17/31 perf tools: Generate prologue for BPF programs
In reply to Wang NanThis patch generates prologue for each 'struct probe_trace_event' for fetching arguments for BPF programs.
After bpf__probe(), iterate over each programs to check whether prologue is required. If none of 'struct perf_probe_event' a program will attach to has at least one argument, simply skip preprocessor hooking. For those who prologue is required, calls bpf__gen_prologue() and paste original instruction after prologue.
--- tools/perf/util/bpf-loader.c | 120 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-)
tools/perf/util/bpf-loader.c +119 -1
@@ -5,11 +5,14 @@* Copyright (C) 2015 Huawei Inc. */ +#include <linux/bpf.h> #include <bpf/libbpf.h> #include <linux/err.h> #include "perf.h" #include "debug.h" #include "bpf-loader.h" +#include "bpf-prologue.h" +#include "llvm-utils.h" #include "probe-event.h" #include "probe-finder.h" // for MAX_PROBES #include "llvm-utils.h"
@@ -32,6 +35,8 @@ DEFINE_PRINT_FN(debug, 1)struct bpf_prog_priv {struct perf_probe_event pev; +bool need_prologue; +struct bpf_insn *insns_buf; }; struct bpf_object *
@@ -98,6 +103,7 @@ bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused,struct bpf_prog_priv *priv = _priv;cleanup_perf_probe_events (&priv->pev,1 ); +zfree (&priv->insns_buf);free (priv); }
@@ -189,6 +195,102 @@ static int bpf__prepare_probe(void)return err; } +static int +preproc_gen_prologue (struct bpf_program *prog,int n, +struct bpf_insn *orig_insns,int orig_insns_cnt, +struct bpf_prog_prep_result *res) +{ +struct probe_trace_event *tev; +struct perf_probe_event *pev; +struct bpf_prog_priv *priv; +struct bpf_insn *buf; +size_t prologue_cnt =0 ; +int err; + + err =bpf_program__get_private (prog, (void **)&priv); +if (err || !priv) + goto errout; + + pev = &priv->pev; + +if (n <0 || n >= pev->ntevs) + goto errout; + + tev = &pev->tevs[n]; + + buf = priv->insns_buf; + err =bpf__gen_prologue (tev->args, tev->nargs, + buf, &prologue_cnt, +BPF_MAXINSNS - orig_insns_cnt); +if (err) { +const char *title; + + title =bpf_program__title (prog, false); +if (!title) + title ="[unknown]" ; + +pr_debug ("Failed to generate prologue for program %s\n" , + title); +return err; + } + +memcpy (&buf[prologue_cnt], orig_insns, +sizeof (struct bpf_insn ) * orig_insns_cnt); + + res->new_insn_ptr = buf; + res->new_insn_cnt = prologue_cnt + orig_insns_cnt; + res->pfd =NULL ; +return 0 ; + +errout: +pr_debug ("Internal error in preproc_gen_prologue\n" ); +return -EINVAL ; +} + +static int hook_load_preprocessor (struct bpf_program *prog) +{ +struct perf_probe_event *pev; +struct bpf_prog_priv *priv; +bool need_prologue = false; +int err, i; + + err =bpf_program__get_private (prog, (void **)&priv); +if (err || !priv) { +pr_debug ("Internal error when hook preprocessor\n" ); +return -EINVAL ; + } + + pev = &priv->pev; +for (i =0 ; i < pev->ntevs; i++) { +struct probe_trace_event *tev = &pev->tevs[i]; + +if (tev->nargs >0 ) { + need_prologue = true; +break ; + } + } + +/* +* Since all tev doesn't have argument, we don't need generate +* prologue. +*/ +if (!need_prologue) { + priv->need_prologue = false; +return 0 ; + } + + priv->need_prologue = true; + priv->insns_buf =malloc (sizeof (struct bpf_insn ) *BPF_MAXINSNS ); +if (!priv->insns_buf) { +pr_debug ("No enough memory: alloc insns_buf failed\n" ); +return -ENOMEM ; + } + + err =bpf_program__set_prep (prog, pev->ntevs, + preproc_gen_prologue); +return err; +} +int bpf__probe (struct bpf_object *obj) {int err =0 ;
@@ -223,6 +325,18 @@ int bpf__probe(struct bpf_object *obj)pr_debug ("bpf_probe: failed to apply perf probe events" ); goto out; } + +/* +* After probing, let's consider prologue, which +* adds program fetcher to BPF programs. +* +* hook_load_preprocessorr() hooks pre-processor +* to bpf_program, let it generate prologue +* dynamically during loading. +*/ + err =hook_load_preprocessor (prog); +if (err) + goto out; } out:return err <0 ? err :0 ;
@@ -306,7 +420,11 @@ int bpf__foreach_tev(struct bpf_object *obj,for (i =0 ; i < pev->ntevs; i++) { tev = &pev->tevs[i]; - fd =bpf_program__fd (prog); +if (priv->need_prologue) + fd =bpf_program__nth_fd (prog, i); +else + fd =bpf_program__fd (prog); +if (fd <0 ) {pr_debug ("bpf: failed to get file descriptor\n" );return fd;
Signature · 1 line
1.8.3.4
PATCH 18/31 perf tools: Use same BPF program if arguments are identical
In reply to Wang NanThis patch allows creating only one BPF program for different 'probe_trace_event'(tev) generated by one 'perf_probe_event'(pev), if their prologues are identical.
This is done by comparing argument list of different tev, and maps type of prologue and tev using a mapping array. This patch utilizes qsort to sort tevs. After sorting, tevs with identical argument list will be grouped together.
--- tools/perf/util/bpf-loader.c | 133 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 126 insertions(+), 7 deletions(-)
tools/perf/util/bpf-loader.c +126 -7
@@ -37,6 +37,8 @@ struct bpf_prog_priv {struct perf_probe_event pev;bool need_prologue;struct bpf_insn *insns_buf; +int nr_types; +int *type_mapping; }; struct bpf_object *
@@ -104,6 +106,7 @@ bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused,cleanup_perf_probe_events (&priv->pev,1 );zfree (&priv->insns_buf); +zfree (&priv->type_mapping);free (priv); }
@@ -205,7 +208,7 @@ preproc_gen_prologue(struct bpf_program *prog, int n,struct bpf_prog_priv *priv;struct bpf_insn *buf;size_t prologue_cnt =0 ; -int err; +int i, err; err =bpf_program__get_private (prog, (void **)&priv);if (err || !priv)
@@ -213,10 +216,20 @@ preproc_gen_prologue(struct bpf_program *prog, int n, pev = &priv->pev; -if (n <0 || n >= pev->ntevs) +if (n <0 || n >= priv->nr_types) goto errout; - tev = &pev->tevs[n]; +/* Find a tev belongs to that type */ +for (i =0 ; i < pev->ntevs; i++) +if (priv->type_mapping[i] == n) +break ; + +if (i >= pev->ntevs) { +pr_debug ("Internal error: prologue type %d not found\n" , n); +return -ENOENT ; + } + + tev = &pev->tevs[i]; buf = priv->insns_buf; err = bpf__gen_prologue(tev->args, tev->nargs,
@@ -247,6 +260,98 @@ errout:return -EINVAL ; } +/* +* compare_tev_args is reflexive, transitive and antisymmetric. +* I can show that but this margin is too narrow to contain. +*/ +static int compare_tev_args (const void *ptev1,const void *ptev2) +{ +int i, ret; +const struct probe_trace_event *tev1 = + *(const struct probe_trace_event **)ptev1; +const struct probe_trace_event *tev2 = + *(const struct probe_trace_event **)ptev2; + + ret = tev2->nargs - tev1->nargs; +if (ret) +return ret; + +for (i =0 ; i < tev1->nargs; i++) { +struct probe_trace_arg *arg1, *arg2; +struct probe_trace_arg_ref *ref1, *ref2; + + arg1 = &tev1->args[i]; + arg2 = &tev2->args[i]; + + ret =strcmp (arg1->value, arg2->value); +if (ret) +return ret; + + ref1 = arg1->ref; + ref2 = arg2->ref; + +while (ref1 && ref2) { + ret = ref2->offset - ref1->offset; +if (ret) +return ret; + + ref1 = ref1->next; + ref2 = ref2->next; + } + +if (ref1 || ref2) +return ref2 ?1 :-1 ; + } + +return 0 ; +} + +static int map_prologue (struct perf_probe_event *pev,int *mapping, +int *nr_types) +{ +int i, type =0 ; +struct { +struct probe_trace_event *tev; +int idx; + } *stevs; +size_t array_sz =sizeof (*stevs) * pev->ntevs; + + stevs =malloc (array_sz); +if (!stevs) { +pr_debug ("No ehough memory: alloc stevs failed\n" ); +return -ENOMEM ; + } + +pr_debug ("In map_prologue, ntevs=%d\n" , pev->ntevs); +for (i =0 ; i < pev->ntevs; i++) { + stevs[i].tev = &pev->tevs[i]; + stevs[i].idx = i; + } +qsort (stevs, pev->ntevs,sizeof (*stevs), + compare_tev_args); + +for (i =0 ; i < pev->ntevs; i++) { +if (i ==0 ) { + mapping[stevs[i].idx] = type; +pr_debug ("mapping[%d]=%d\n" , stevs[i].idx, + type); +continue ; + } + +if (compare_tev_args (stevs + i, stevs + i -1 ) ==0 ) + mapping[stevs[i].idx] = type; +else + mapping[stevs[i].idx] = ++type; + +pr_debug ("mapping[%d]=%d\n" , stevs[i].idx, + mapping[stevs[i].idx]); + } +free (stevs); + *nr_types = type +1 ; + +return 0 ; +} +static int hook_load_preprocessor (struct bpf_program *prog) {struct perf_probe_event *pev;
@@ -286,7 +391,19 @@ static int hook_load_preprocessor(struct bpf_program *prog)return -ENOMEM ; } - err =bpf_program__set_prep (prog, pev->ntevs, + priv->type_mapping =malloc (sizeof (int ) * pev->ntevs); +if (!priv->type_mapping) { +pr_debug ("No enough memory: alloc type_mapping failed\n" ); +return -ENOMEM ; + } +memset (priv->type_mapping,0xff , +sizeof (int ) * pev->ntevs); + + err =map_prologue (pev, priv->type_mapping, &priv->nr_types); +if (err) +return err; + + err =bpf_program__set_prep (prog, priv->nr_types, preproc_gen_prologue);return err; }
@@ -420,9 +537,11 @@ int bpf__foreach_tev(struct bpf_object *obj,for (i =0 ; i < pev->ntevs; i++) { tev = &pev->tevs[i]; -if (priv->need_prologue) - fd =bpf_program__nth_fd (prog, i); -else +if (priv->need_prologue) { +int type = priv->type_mapping[i]; + + fd =bpf_program__nth_fd (prog, type); + }else fd =bpf_program__fd (prog);if (fd <0 ) {
Signature · 1 line
1.8.3.4
PATCH 19/31 perf record: Support custom vmlinux path
In reply to Wang NanFrom: He Kuang <[email protected]>
Make perf-record command support --vmlinux option if BPF_PROLOGUE is on.
'perf record' needs vmlinux as the source of DWARF info to generate prologue for BPF programs, so path of vmlinux should be specified.
Short name 'k' has been taken by 'clockid'. This patch skips the short option name and use '--vmlinux' for vmlinux path.
--- tools/perf/builtin-record.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/perf/builtin-record.c +4 -0
@@ -1123,6 +1123,10 @@ struct option __record_options[] = { "clang binary to use for compiling BPF scriptlets"),OPT_STRING (0 ,"clang-opt" , &llvm_param.clang_opt,"clang options" ,"options passed to clang when compiling BPF scriptlets" ), +#ifdef HAVE_BPF_PROLOGUE +OPT_STRING (0 ,"vmlinux" , &symbol_conf.vmlinux_name, +"file" ,"vmlinux pathname" ), +#endif #endifOPT_END () };
Signature · 1 line
1.8.3.4
PATCH 20/31 perf tools: Allow BPF program attach to uprobe events
In reply to Wang NanThis patch appends new syntax to BPF object section name to support probing at uprobe event. Now we can use BPF program like this:
SEC(
"target=/lib64/libc.so.6\n"
"libcwrite=__write"
)
int libcwrite(void *ctx)
{
return 1;}
Where, in section name of a program, before the main config string, we can use 'key=value' style options. Now the only option key "target" is for uprobe probing.
--- tools/perf/util/bpf-loader.c | 86 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 6 deletions(-)
tools/perf/util/bpf-loader.c +80 -6
@@ -111,6 +111,84 @@ bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused, }static int +do_config (const char *key,const char *value, +struct perf_probe_event *pev) +{ +pr_debug ("config bpf program: %s=%s\n" , key, value); +if (strcmp (key,"target" ) ==0 ) { + pev->uprobes = true; + pev->target =strdup (value); +return 0 ; + } + +pr_warning ("BPF: WARNING: invalid config option in object: %s=%s\n" , + key, value); +pr_warning ("\tHint: Currently only valid option is 'target=<file>'\n" ); +return 0 ; +} + +static const char * +parse_config_kvpair (const char *config_str,struct perf_probe_event *pev) +{ +char *text =strdup (config_str); +char *sep, *line; +const char *main_str =NULL ; +int err =0 ; + +if (!text) { +pr_debug ("No enough memory: dup config_str failed\n" ); +return NULL ; + } + + line = text; +while ((sep =strchr (line,'\n' ))) { +char *equ; + + *sep ='\0' ; + equ =strchr (line,'=' ); +if (!equ) { +pr_warning ("WARNING: invalid config in BPF object: %s\n" , + line); +pr_warning ("\tShould be 'key=value'.\n" ); + goto nextline; + } + *equ ='\0' ; + + err =do_config (line, equ +1 , pev); +if (err) +break ; +nextline: + line = sep +1 ; + } + +if (!err) + main_str = config_str + (line - text); +free (text); + +return main_str; +} + +static int +parse_config (const char *config_str,struct perf_probe_event *pev) +{ +const char *main_str; +int err; + + main_str =parse_config_kvpair (config_str, pev); +if (!main_str) +return -EINVAL ; + + err =parse_perf_probe_command (main_str, pev); +if (err <0 ) { +pr_debug ("bpf: '%s' is not a valid config string\n" , + config_str); +/* parse failed, don't need clear pev. */ +return -EINVAL ; + } +return 0 ; +} + +static int config_bpf_program (struct bpf_program *prog) {struct perf_probe_event *pev =NULL ;
@@ -132,13 +210,9 @@ config_bpf_program(struct bpf_program *prog) pev = &priv->pev;pr_debug ("bpf: config program '%s'\n" , config_str); - err =parse_perf_probe_command (config_str, pev); -if (err <0 ) { -pr_debug ("bpf: '%s' is not a valid config string\n" , - config_str); - err = -EINVAL ; + err =parse_config (config_str, pev); +if (err) goto errout; - }if (pev->group &&strcmp (pev->group,PERF_BPF_PROBE_GROUP )) { pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
Signature · 1 line
1.8.3.4
Re: [PATCH 20/31] perf tools: Allow BPF program attach to uprobe events
Quoted 50 lines · Wang Nan
This patch appends new syntax to BPF object section name to support probing at uprobe event. Now we can use BPF program like this:
SEC( "target=/lib64/libc.so.6\n" "libcwrite=__write" ) int libcwrite(void *ctx) { return 1; }Where, in section name of a program, before the main config string, we can use 'key=value' style options. Now the only option key "target" is for uprobe probing.
Signed-off-by Wang Nan <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/util/bpf-loader.c | 86 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 6 deletions(-)tools/perf/util/bpf-loader.c +6
-0@@ -111,6 +111,84 @@ bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused, }static int +do_config (const char *key,const char *value, +struct perf_probe_event *pev) +{ +pr_debug ("config bpf program: %s=%s\n" , key, value); +if (strcmp (key,"target" ) ==0 ) { + pev->uprobes = true;
tools/perf/util/bpf-loader.c bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused, · line 129
Quoted 10 lines
tools/perf/util/bpf-loader.c +9
-0+ pev->target =strdup (value); +return 0 ; + } + +pr_warning ("BPF: WARNING: invalid config option in object: %s=%s\n" , + key, value); +pr_warning ("\tHint: Currently only valid option is 'target=<file>'\n" ); +return 0 ; +}
This part is very easy to be extended to support probing at modules. I'd like to change "target" to "exec" to make it unify with perf probe, then add another patch to support module probing in next pull request.
Thank you.
PATCH 1/1 perf tools: Allow BPF program attach to modules
By extending the syntax of BPF object section names, this patch allows user to attach BPF programs to symbol in modules. For example:
SEC("module=i915\n"
"parse_cmds=i915_parse_cmds")
int parse_cmds(void *ctx)
{
return 1;}
Implementation is very simple: like what 'perf probe' does, for module, fill 'uprobe' field in 'struct perf_probe_event'. Other parts would be done automatically.
---
This patch is based on commit ad14ba45236a496a7ce25f4ea947d245b1406e40 in my git tree[1], and will be appear in the next pull request of this perf eBPF support patchset.
[1] https://git.kernel.org/cgit/linux/kernel/git/pi3orama/linux.git
--- tools/perf/util/bpf-loader.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/perf/util/bpf-loader.c +8 -1
@@ -120,10 +120,17 @@ do_config(const char *key, const char *value, pev->target =strdup (value);return 0 ; } +if (strcmp (key,"module" ) ==0 ) { + pev->uprobes = false; + pev->target =strdup (value); +return 0 ; + }pr_warning ("BPF: WARNING: invalid config option in object: %s=%s\n" , key, value); -pr_warning ("\tHint: Currently only valid option is 'exec=<full patch of file>'\n" ); +pr_warning ("\tHint: Currently valid options are:\n" ); +pr_warning ("\t 'exec=<full patch of file>'\n" ); +pr_warning ("\t 'module=<module name>'\n" );return 0 ; }
Signature · 1 line
1.8.3.4
PATCH 21/31 perf test: Enforce LLVM test, add kbuild test
In reply to Wang NanThis patch enforces existing LLVM test, makes it compile more than one BPF source file. The compiled results are stored, can be used for other testcases. Except the first testcase (named LLVM_TESTCASE_BASE), failures of other test cases are not considered as failure of the whole test.
Adds a kbuild testcase to check whether kernel headers can be correctly found.
For example:
# perf test LLVM
38: Test LLVM searching and compiling : (llvm.kbuild-dir can be fixed) Ok
--- tools/perf/tests/Build | 11 ++- tools/perf/tests/bpf-script-example.c | 4 + tools/perf/tests/bpf-script-test-kbuild.c | 21 ++++ tools/perf/tests/bpf.c | 3 +- tools/perf/tests/llvm.c | 154 ++++++++++++++++++++++-------- tools/perf/tests/llvm.h | 10 +- 6 files changed, 156 insertions(+), 47 deletions(-) create mode 100644 tools/perf/tests/bpf-script-test-kbuild.c
tools/perf/tests/Build +9 -2
@@ -31,17 +31,24 @@ perf-y += sample-parsing.o perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o -perf-y += llvm.o llvm-src.o +perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o perf-y += bpf.o perf-y += topology.o -$(OUTPUT)tests/llvm-src.c: tests/bpf-script-example.c +$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c $(call rule_mkdir) $(Q)echo '#include <tests/llvm.h>' > $@ $(Q)echo 'const char test_llvm__bpf_prog[] =' >> $@ $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ $(Q)echo ';' >> $@ +$(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c + $(call rule_mkdir) + $(Q)echo '#include <tests/llvm.h>' > $@ + $(Q)echo 'const char test_llvm__bpf_test_kbuild_prog[] =' >> $@ + $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)echo ';' >> $@ + ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o endif
tools/perf/tests/bpf-script-example.c +4 -0
@@ -1,3 +1,7 @@ +/* +* bpf-script-example.c +* Test basic LLVM building +*/ #ifndef LINUX_VERSION_CODE # error Need LINUX_VERSION_CODE# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
tools/perf/tests/bpf-script-test-kbuild.c new file +21 -0
@@ -0,0 +1,21 @@ +/* +* bpf-script-test-kbuild.c +* Test include from kernel header +*/ +#ifndef LINUX_VERSION_CODE +# error Need LINUX_VERSION_CODE +# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' +#endif +#define SEC (NAME ) __attribute__((section(NAME), used)) + +#include <uapi/linux/fs.h> +#include <uapi/asm/ptrace.h> + +SEC("func=vfs_llseek") +int bpf_func__vfs_llseek (struct pt_regs *ctx) +{ +return 0 ; +} + +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = LINUX_VERSION_CODE;
tools/perf/tests/bpf.c +2 -1
@@ -140,7 +140,8 @@ int test__bpf(void)return TEST_SKIP ; } -test_llvm__fetch_bpf_obj (&obj_buf, &obj_buf_sz); +test_llvm__fetch_bpf_obj (&obj_buf, &obj_buf_sz,LLVM_TESTCASE_BASE ); +if (!obj_buf || !obj_buf_sz) {if (verbose ==0 )fprintf (stderr," (fix 'perf test LLVM' first)" );
tools/perf/tests/llvm.c +112 -42
238 lines not on this page · raw message
tools/perf/tests/llvm.h +8 -2
@@ -8,8 +8,14 @@ struct test_llvm__bpf_result { char object[]; }; -extern struct test_llvm__bpf_result *p_test_llvm__bpf_result;extern const char test_llvm__bpf_prog[]; -void test_llvm__fetch_bpf_obj (void **p_obj_buf,size_t *p_obj_buf_sz); +extern const char test_llvm__bpf_test_kbuild_prog[]; + +enum test_llvm__testcase { +LLVM_TESTCASE_BASE , +LLVM_TESTCASE_KBUILD , +NR_LLVM_TESTCASES , +}; +void test_llvm__fetch_bpf_obj (void **p_obj_buf,size_t *p_obj_buf_sz,int index);#endif
Signature · 1 line
1.8.3.4
Namhyung Kim
maintainerRe: [PATCH 21/31] perf test: Enforce LLVM test, add kbuild test
Hi,
Quoted 5 lines · Wang Nan
This patch enforces existing LLVM test, makes it compile more than one BPF source file. The compiled results are stored, can be used for other testcases. Except the first testcase (named
LLVM_TESTCASE_BASE), failures of other test cases are not considered as failure of the whole test.
Quoted 8 lines
Adds a kbuild testcase to check whether kernel headers can be correctly found.
For example:
# perf test LLVM38: Test LLVM searching and compiling : (llvm.kbuild-dir can be fixed) Ok
IMHO it'd be better to keep the test result simply as either "Ok" or "FAILED" and provide details with -v option.
Anyway I found the tracepoint error message is annoying in the current perf test output. Will send a fix soon.
Thanks,
Namhyung
Re: [PATCH 21/31] perf test: Enforce LLVM test, add kbuild test
Em Mon, Oct 19, 2015 at 11:42:31PM +0900, Namhyung Kim escreveu:
Quoted 11 lines
Hi,
Quoted 8 lines · Wang Nan
This patch enforces existing LLVM test, makes it compile more than one BPF source file. The compiled results are stored, can be used for other testcases. Except the first testcase (named
LLVM_TESTCASE_BASE), failures of other test cases are not considered as failure of the whole test.Adds a kbuild testcase to check whether kernel headers can be correctly found.
Quoted 8 lines
Quoted 5 lines
For example:
# perf test LLVM38: Test LLVM searching and compiling : (llvm.kbuild-dir can be fixed) Ok
IMHO it'd be better to keep the test result simply as either "Ok" or "FAILED" and provide details with -v option.
"Skipped" looks more natural here, with the reason for it skipping being shown only with -v.
Ingo made some comments about 'perf test' output recently, have you read that?
Quoted 1 line
Anyway I found the tracepoint error message is annoying in the current
You mean all these warnings:
[root@zoo ~]# perf test "parse event"
5: parse events tests : Warning: [sunrpc:xprt_lookup_rqst] function __builtin_constant_p not defined
Warning: [sunrpc:xprt_transmit] function __builtin_constant_p not defined
Warning: [sunrpc:xprt_complete_rqst] function __builtin_constant_p not defined
<SNIP>
Warning: [xen:xen_mmu_set_pud] function sizeof not defined
Warning: [xen:xen_mmu_set_pgd] function sizeof not defined
Warning: [xen:xen_mmu_ptep_modify_prot_start] function sizeof not defined
Warning: [xen:xen_mmu_ptep_modify_prot_commit] function sizeof not defined
Ok
[root@zoo ~]#
i.e. making those warnings appear only under 'perf test -v'? Cool!
Quoted 4 lines
perf test output. Will send a fix soon.
Thanks,
Namhyung
Namhyung Kim
maintainerRe: [PATCH 21/31] perf test: Enforce LLVM test, add kbuild test
Hi Arnaldo,
Quoted 16 lines · Arnaldo Carvalho de Melo
Em Mon, Oct 19, 2015 at 11:42:31PM +0900, Namhyung Kim escreveu:
Quoted 15 lines
Hi,
Quoted 12 lines · Wang Nan
This patch enforces existing LLVM test, makes it compile more than one BPF source file. The compiled results are stored, can be used for other testcases. Except the first testcase (named
LLVM_TESTCASE_BASE), failures of other test cases are not considered as failure of the whole test.Adds a kbuild testcase to check whether kernel headers can be correctly found.
For example:
# perf test LLVM
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
38: Test LLVM searching and compiling : (llvm.kbuild-dir can be fixed) Ok
IMHO it'd be better to keep the test result simply as either "Ok" or "FAILED" and provide details with -v option.
"Skipped" looks more natural here, with the reason for it skipping being shown only with -v.
Agreed.
Quoted 2 lines
Ingo made some comments about 'perf test' output recently, have you read that?
Nop, sorry. I'll check it (and it'd be great it you could give me the link :)
Quoted 17 lines
Quoted 2 lines
Anyway I found the tracepoint error message is annoying in the current
You mean all these warnings:
[root@zoo ~]# perf test "parse event"
5: parse events tests : Warning: [sunrpc:xprt_lookup_rqst] function__builtin_constant_pnot defined
Warning: [sunrpc:xprt_transmit] function__builtin_constant_pnot defined
Warning: [sunrpc:xprt_complete_rqst] function__builtin_constant_pnot defined
<SNIP>
Warning: [xen:xen_mmu_set_pud] function sizeof not defined
Warning: [xen:xen_mmu_set_pgd] function sizeof not defined
Warning: [xen:xen_mmu_ptep_modify_prot_start] function sizeof not defined
Warning: [xen:xen_mmu_ptep_modify_prot_commit] function sizeof not defined
Ok
[root@zoo ~]#
Quoted 1 line
i.e. making those warnings appear only under 'perf test -v'? Cool!
Will send the fix soon!
Thanks,
Namhyung
Re: [PATCH 21/31] perf test: Enforce LLVM test, add kbuild test
In reply to Arnaldo Carvalho de MeloQuoted 19 lines · Arnaldo Carvalho de Melo
Em Mon, Oct 19, 2015 at 11:42:31PM +0900, Namhyung Kim escreveu:
Quoted 18 lines
Hi,
Quoted 5 lines · Wang Nan
This patch enforces existing LLVM test, makes it compile more than one BPF source file. The compiled results are stored, can be used for other testcases. Except the first testcase (named
LLVM_TESTCASE_BASE), failures of other test cases are not considered as failure of the whole test.Quoted 8 lines
Adds a kbuild testcase to check whether kernel headers can be correctly found.
For example:
# perf test LLVM38: Test LLVM searching and compiling : (llvm.kbuild-dir can be fixed) Ok
IMHO it'd be better to keep the test result simply as either "Ok" or "FAILED" and provide details with -v option.
I have to say I'm not the first one to output things like this:
#perf test dummy attr
14: struct perf_event_attr setup :
(omitted) Ok
23: Test using a dummy software event to keep tracking : (not
supported) Ok
Do you think they also need to be fixed?
Quoted 2 lines
"Skipped" looks more natural here, with the reason for it skipping being shown only with -v.
For this specific case, I will change the logic:
1. When basic test failed (no clang, compiling error...), report failure;
2. When basic test passed but kbuild test or later test cases failed,
report
Skip.
3. Don't output any other information.
Thank you.
Quoted 23 lines
Ingo made some comments about 'perf test' output recently, have you read that?
Quoted 1 line
Anyway I found the tracepoint error message is annoying in the current
You mean all these warnings:
[root@zoo ~]# perf test "parse event"
5: parse events tests : Warning: [sunrpc:xprt_lookup_rqst] function__builtin_constant_pnot defined
Warning: [sunrpc:xprt_transmit] function__builtin_constant_pnot defined
Warning: [sunrpc:xprt_complete_rqst] function__builtin_constant_pnot defined
<SNIP>
Warning: [xen:xen_mmu_set_pud] function sizeof not defined
Warning: [xen:xen_mmu_set_pgd] function sizeof not defined
Warning: [xen:xen_mmu_ptep_modify_prot_start] function sizeof not defined
Warning: [xen:xen_mmu_ptep_modify_prot_commit] function sizeof not defined
Ok
[root@zoo ~]#i.e. making those warnings appear only under 'perf test -v'? Cool!
Quoted 4 lines
perf test output. Will send a fix soon.
Thanks,
Namhyung
Re: [PATCH 21/31] perf test: Enforce LLVM test, add kbuild test
Em Tue, Oct 20, 2015 at 06:36:33PM +0800, Wangnan (F) escreveu:
Quoted 20 lines · Arnaldo Carvalho de Melo
Quoted 16 lines
Em Mon, Oct 19, 2015 at 11:42:31PM +0900, Namhyung Kim escreveu:
Quoted 15 lines · Wang Nan
Quoted 11 lines
This patch enforces existing LLVM test, makes it compile more than one BPF source file. The compiled results are stored, can be used for other testcases. Except the first testcase (named
LLVM_TESTCASE_BASE), failures of other test cases are not considered as failure of the whole test.Adds a kbuild testcase to check whether kernel headers can be correctly found.
For example:
# perf test LLVM 38: Test LLVM searching and compiling : (llvm.kbuild-dir can be fixed) OkIMHO it'd be better to keep the test result simply as either "Ok" or "FAILED" and provide details with -v option.
I have to say I'm not the first one to output things like this:
Quoted 7 lines
#perf test dummy attr
14: structperf_event_attrsetup :
(omitted) Ok
23: Test using a dummy software event to keep tracking : (not
supported) OkDo you think they also need to be fixed?
yes
Quoted 39 lines
Quoted 2 lines
"Skipped" looks more natural here, with the reason for it skipping being shown only with -v.
For this specific case, I will change the logic:
1. When basic test failed (no clang, compiling error...), report failure;
2. When basic test passed but kbuild test or later test cases
failed, report
Skip.3. Don't output any other information.
Thank you.
Quoted 23 lines
Ingo made some comments about 'perf test' output recently, have you read that?
Quoted 1 line
Anyway I found the tracepoint error message is annoying in the current
You mean all these warnings:
[root@zoo ~]# perf test "parse event"
5: parse events tests : Warning: [sunrpc:xprt_lookup_rqst] function__builtin_constant_pnot defined
Warning: [sunrpc:xprt_transmit] function__builtin_constant_pnot defined
Warning: [sunrpc:xprt_complete_rqst] function__builtin_constant_pnot defined
<SNIP>
Warning: [xen:xen_mmu_set_pud] function sizeof not defined
Warning: [xen:xen_mmu_set_pgd] function sizeof not defined
Warning: [xen:xen_mmu_ptep_modify_prot_start] function sizeof not defined
Warning: [xen:xen_mmu_ptep_modify_prot_commit] function sizeof not defined
Ok
[root@zoo ~]#i.e. making those warnings appear only under 'perf test -v'? Cool!
Quoted 4 lines
perf test output. Will send a fix soon.
Thanks,
Namhyung
Re: [PATCH 21/31] perf test: Enforce LLVM test, add kbuild test
In reply to Arnaldo Carvalho de MeloHi Namhyung and Arnaldo,
I changed my testing related patches in my local git tree. All changed combined together is at the end of this mail. I remove all stderr output, and improve debug message. Please have a look at it.
Thank you.
tools/perf/tests/bpf.c +1 -1
@@ -49,7 +49,7 @@ static struct bpf_object *prepare_bpf(const char *name, void *obj_buf, obj =bpf__prepare_load_buffer (obj_buf, obj_buf_sz, name);if (IS_ERR (obj)) { -fprintf (stderr," (compile failed)" ); +pr_debug ("Compile BPF program failed.\n" );return NULL ; }return obj;
@@ -82,7 +82,7 @@ static int do_test(struct bpf_object *obj, int (*func)(void), int expect) err = parse_events_load_bpf_obj(&parse_evlist, &parse_evlist.list,
obj);
if (err || list_empty(&parse_evlist.list)) {
- fprintf(stderr, " (Failed to add events selected by BPF)");
+ pr_debug("Failed to add events selected by BPF\n");
if (!err)
err = -EINVAL;
goto out;
@@ -140,7 +140,7 @@ static int do_test(struct bpf_object *obj, int (*func)(void), int expect)
}
if (count != expect) {
- fprintf(stderr, " (filter result incorrect: %d != %d)", count, expect);
+ pr_debug("BPF filter result incorrect: %d != %d\n", count, expect);
err = -EBADF;
}@@ -164,16 +164,14 @@ static int __test__bpf(int index, const char *name,
test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz, index);
if (!obj_buf || !obj_buf_sz) {
- if (verbose == 0)
- fprintf(stderr, " (%s)", message_compile);
+ pr_debug("Failed to compile: %s\n", message_compile);
return TEST_SKIP;
}
obj = prepare_bpf(name, obj_buf, obj_buf_sz);
if (!obj) {
err = -EINVAL;
- if ((verbose == 0) && (message_load[0] != '\0'))
- fprintf(stderr, " (%s)", message_load);
+ pr_debug("Failed to load: %s", message_load);
goto out;
}@@ -192,7 +190,7 @@ int test__bpf(void)
int err;
if (geteuid() != 0) {
- fprintf(stderr, " (try run as root)");
+ pr_debug("Only root can run BPF test\n");
return TEST_SKIP;
}@@ -214,7 +212,7 @@ int test__bpf(void)
(NR_ITERS + 1) / 4);
return err;
#else
- fprintf(stderr, " (skip BPF prologue test)");
+ pr_debug("BPF prologue is disabled when compiling, skip this test\n");
return TEST_OK; #endif
}
tools/perf/tests/llvm.c +2 -0
@@ -13,19 +13,23 @@struct llvm_testcase {const char *source;const char *errmsg; +const char *hintmsg;struct test_llvm__bpf_result *result;bool tried; } llvm_testcases[NR_LLVM_TESTCASES +1 ] = { [LLVM_TESTCASE_BASE ] = {.source = test_llvm__bpf_prog, .errmsg ="Basic LLVM compiling failed" , + .hintmsg ="Check llvm.clang-path option in ~/.perfconfig" , .tried = false}, [LLVM_TESTCASE_KBUILD] = {.source =
test_llvm__bpf_test_kbuild_prog,
- .errmsg = "llvm.kbuild-dir can be fixed",
+ .errmsg = "Unable to find usable kbuild dir",
+ .hintmsg = "Check llvm.kbuild-dir option in ~/.perfconfig",
.tried = false},
/* Don't output if this one fail. */
[LLVM_TESTCASE_BPF_PROLOGUE] = {
.source = test_llvm__bpf_test_prologue_prog,
- .errmsg = "failed for unknown reason",
+ .errmsg = "Unable to compile BPF prologue testing program",
+ .hintmsg = "This is an internal error, please report it", .tried = false},
{.source = NULL}
};
@@ -43,16 +47,16 @@ static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)
obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
if (!obj)
- return -1;
+ return TEST_FAIL;
bpf_object__close(obj);- return 0;
+ return TEST_OK;
}
#else
static int test__bpf_parsing(void *obj_buf __maybe_unused,
size_t obj_buf_sz __maybe_unused)
{
- fprintf(stderr, " (skip bpf parsing)");
- return 0;
+ pr_debug("BPF support is not compiled, skip BPF parsing\n");
+ return TEST_SKIP; }
#endif
@@ -70,8 +74,8 @@ compose_source(const char *raw_source)
err = sscanf(utsname.release, "%d.%d.%d",
&version, &patchlevel, &sublevel);
if (err != 3) {
- fprintf(stderr, " (Can't get kernel version from uname '%s')",
- utsname.release);
+ pr_debug("Unablt to get kernel version from uname '%s'\n",
+ utsname.release);
return NULL;
}@@ -104,7 +108,7 @@ static int __test__llvm(int i)
* and clang is not found in $PATH, and this is not perf test -v
*/
if (verbose == 0 && !llvm_param.user_set_param &&
llvm__search_clang()) {
- fprintf(stderr, " (no clang, try 'perf test -v LLVM')");
+ pr_debug("Unable to find workable clang\n");
return TEST_SKIP;
}@@ -155,7 +159,7 @@ static int __test__llvm(int i)
verbose = old_verbose;
if (err)- return -1;
+ return TEST_FAIL;
err = test__bpf_parsing(obj_buf, obj_buf_sz);
if (!err && result) {@@ -181,13 +185,12 @@ int test__llvm(void)
* First testcase tests basic LLVM compiling. If it
* fails, no need to check others.
*/
- if (!verbose)
- fprintf(stderr, " (use -v to see error message)");
- return ret;
+ return TEST_FAIL;
} else if (ret) {
- if (!verbose && llvm_testcases[i].errmsg)
- fprintf(stderr, " (%s)", llvm_testcases[i].errmsg);
- return 0;
+ pr_debug("%s\n", llvm_testcases[i].errmsg);
+ if (llvm_testcases[i].hintmsg)
+ pr_debug("Hint:\t%s\n", llvm_testcases[i].hintmsg);
+ return TEST_SKIP;
}
}
return 0;@@ -199,12 +202,15 @@ void test__llvm_prepare(void)
for (i = 0; llvm_testcases[i].source; i++) {
struct test_llvm__bpf_result *result;+ void *p;
- result = mmap(NULL, SHARED_BUF_INIT_SIZE,
- PROT_READ | PROT_WRITE,
- MAP_SHARED | MAP_ANONYMOUS, -1, 0);
- if (!result)
+ p = mmap(NULL, SHARED_BUF_INIT_SIZE,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (p == MAP_FAILED)
return;
+
+ result = p;
memset((void *)result, '\0', SHARED_BUF_INIT_SIZE);
llvm_testcases[i].result = result;tools/perf/util/bpf-prologue.c +3 -1
@@ -337,8 +337,10 @@ int bpf__gen_prologue(struct probe_trace_arg *args, int nargs,return 0 ; } -if (nargs >BPF_PROLOGUE_MAX_ARGS ) +if (nargs >BPF_PROLOGUE_MAX_ARGS ) { +pr_warning ("bpf: prologue: too many arguments\n" ); nargs =BPF_PROLOGUE_MAX_ARGS ; + }if (cnt_space >BPF_MAXINSNS ) cnt_space =BPF_MAXINSNS ;
Re: [PATCH 21/31] perf test: Enforce LLVM test, add kbuild test
Em Tue, Oct 20, 2015 at 08:06:54PM +0800, Wangnan (F) escreveu:
Quoted 8 lines
Hi Namhyung and Arnaldo,
I changed my testing related patches in my local git tree. All changed combined together is at the end of this mail. I remove all stderr output, and improve debug message. Please have a look at it.
Thank you.
Yeah, from a quick look it looks sane, i.e. 'perf test' should state just 'Ok', "FAIL" or "Skip", -v provides a reason, -vv will provide more detail and so on, i.e. a verbosity level, for that we have 'pr_debug(), pr_debug2(), pr_debug3(), etc, and can always, for some specialized use, like grouping multiple warnings or deciding if we need to call some other function only when the verbosity level is greater than some level, check the 'verbose' variable value.
- Arnaldo
Quoted 218 lines
tools/perf/tests/bpf.c +1
-1@@ -49,7 +49,7 @@ static struct bpf_object *prepare_bpf(const char *name, void *obj_buf, obj =bpf__prepare_load_buffer (obj_buf, obj_buf_sz, name);if (IS_ERR (obj)) { -fprintf (stderr," (compile failed)" ); +pr_debug ("Compile BPF program failed.\n" );return NULL ; }return obj;tools/perf/tests/bpf.c +1
-1@@ -82,7 +82,7 @@ static int do_test(struct bpf_object *obj, int (*func)(void), int expect) err =parse_events_load_bpf_obj (&parse_evlist, &parse_evlist.list, obj);if (err ||list_empty (&parse_evlist.list)) { -fprintf (stderr," (Failed to add events selected by BPF)" ); +pr_debug ("Failed to add events selected by BPF\n" );if (!err)err = -EINVAL; goto out;tools/perf/tests/bpf.c +1
-1@@ -140,7 +140,7 @@ static int do_test(struct bpf_object *obj, int (*func)(void), int expect) }if (count != expect) { -fprintf (stderr," (filter result incorrect: %d != %d)" , count, expect); +pr_debug ("BPF filter result incorrect: %d != %d\n" , count, expect); err = -EBADF ;}tools/perf/tests/bpf.c +4
-6@@ -164,16 +164,14 @@ static int __test__bpf(int index, const char *name,test_llvm__fetch_bpf_obj (&obj_buf, &obj_buf_sz, index);if (!obj_buf || !obj_buf_sz) { -if (verbose ==0 ) -fprintf (stderr," (%s)" , message_compile); +pr_debug ("Failed to compile: %s\n" , message_compile);return TEST_SKIP ; } obj =prepare_bpf (name, obj_buf, obj_buf_sz);if (!obj) { err = -EINVAL ; -if ((verbose ==0 ) && (message_load[0 ] !='\0' )) -fprintf (stderr," (%s)" , message_load); +pr_debug ("Failed to load: %s" , message_load); goto out; }@@ -192,7 +190,7 @@ int test__bpf(void)int err;if (geteuid () !=0 ) { -fprintf (stderr," (try run as root)" ); +pr_debug ("Only root can run BPF test\n" );return TEST_SKIP ; }@@ -214,7 +212,7 @@ int test__bpf(void) (NR_ITERS + 1) / 4);return err;#else -fprintf (stderr," (skip BPF prologue test)" ); +pr_debug ("BPF prologue is disabled when compiling, skip this test\n" );return TEST_OK ;#endif }tools/perf/tests/llvm.c +5
-2@@ -13,19 +13,23 @@struct llvm_testcase {const char *source;const char *errmsg; +const char *hintmsg;struct test_llvm__bpf_result *result;bool tried; } llvm_testcases[NR_LLVM_TESTCASES +1 ] = { [LLVM_TESTCASE_BASE ] = {.source = test_llvm__bpf_prog, .errmsg ="Basic LLVM compiling failed" , + .hintmsg ="Check llvm.clang-path option in ~/.perfconfig", .tried = false}, [LLVM_TESTCASE_KBUILD ] = {.source = test_llvm__bpf_test_kbuild_prog, - .errmsg ="llvm.kbuild-dir can be fixed" , + .errmsg ="Unable to find usable kbuild dir" , + .hintmsg ="Check llvm.kbuild-dir option in ~/.perfconfig", .tried = false},/* Don't output if this one fail. */ [LLVM_TESTCASE_BPF_PROLOGUE ] = { .source = test_llvm__bpf_test_prologue_prog, - .errmsg ="failed for unknown reason" , + .errmsg = "Unable to compile BPF prologuetesting program",
+ .hintmsg = "This is an internal error, please
report it",
.tried = false},
{.source = NULL}
};tools/perf/tests/llvm.c +20
-18@@ -43,16 +47,16 @@ static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz) obj =bpf_object__open_buffer (obj_buf, obj_buf_sz,NULL );if (!obj) -return -1 ; +return TEST_FAIL ;bpf_object__close (obj); - return 0; + return TEST_OK; }#else static int test__bpf_parsing (void *obj_buf__maybe_unused ,size_t obj_buf_sz__maybe_unused ) { -fprintf (stderr," (skip bpf parsing)" ); -return 0 ; +pr_debug ("BPF support is not compiled, skip BPF parsing\n" ); +return TEST_SKIP ; }#endif @@ -70,8 +74,8 @@ compose_source(const char *raw_source) err =sscanf (utsname.release,"%d.%d.%d" , &version, &patchlevel, &sublevel);if (err !=3 ) { -fprintf (stderr," (Can't get kernel version from uname '%s')" , - utsname.release); +pr_debug ("Unablt to get kernel version from uname '%s'\n" , + utsname.release);return NULL ; }@@ -104,7 +108,7 @@ static int __test__llvm(int i)* and clang is not found in $PATH, and this is not perf test -v */ if (verbose ==0 && !llvm_param.user_set_param &&llvm__search_clang ()) { -fprintf (stderr," (no clang, try 'perf test -v LLVM')" ); +pr_debug ("Unable to find workable clang\n" );return TEST_SKIP ; }@@ -155,7 +159,7 @@ static int __test__llvm(int i) verbose = old_verbose;if (err) -return -1 ; +return TEST_FAIL ; err =test__bpf_parsing (obj_buf, obj_buf_sz);if (!err && result) {@@ -181,13 +185,12 @@ int test__llvm(void)* First testcase tests basic LLVM compiling. If it * fails, no need to check others. */ -if (!verbose) -fprintf (stderr," (use -v to see error message)" ); -return ret; +return TEST_FAIL ; }else if (ret) { -if (!verbose && llvm_testcases[i].errmsg) -fprintf (stderr," (%s)" , llvm_testcases[i].errmsg); -return 0 ; +pr_debug ("%s\n" , llvm_testcases[i].errmsg); +if (llvm_testcases[i].hintmsg) +pr_debug ("Hint:\t%s\n" , llvm_testcases[i].hintmsg); +return TEST_SKIP ; } }return 0 ;@@ -199,12 +202,15 @@ void test__llvm_prepare(void)for (i =0 ; llvm_testcases[i].source; i++) {struct test_llvm__bpf_result *result; +void *p; - result =mmap (NULL ,SHARED_BUF_INIT_SIZE , -PROT_READ |PROT_WRITE , -MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); -if (!result) + p =mmap (NULL ,SHARED_BUF_INIT_SIZE , +PROT_READ |PROT_WRITE , +MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +if (p ==MAP_FAILED )return ; + + result = p;memset ((void *)result,'\0' ,SHARED_BUF_INIT_SIZE ); llvm_testcases[i].result = result;tools/perf/util/bpf-prologue.c +3
-1@@ -337,8 +337,10 @@ int bpf__gen_prologue(struct probe_trace_arg *args, int nargs, return 0; } -if (nargs >BPF_PROLOGUE_MAX_ARGS ) +if (nargs >BPF_PROLOGUE_MAX_ARGS ) { +pr_warning ("bpf: prologue: too many arguments\n" ); nargs =BPF_PROLOGUE_MAX_ARGS ; + }if (cnt_space >BPF_MAXINSNS ) cnt_space =BPF_MAXINSNS ;
PATCH 22/31 perf test: Test BPF prologue
In reply to Wang NanThis patch introduces a new BPF script to test BPF prologue. The new script probes at null_lseek, which is the function pointer when we try to lseek on '/dev/null'.
null_lseek is chosen because it is a function pointer, so we don't need to consider inlining and LTP.
By extracting file->f_mode, bpf-script-test-prologue.c should know whether the file is writable or readonly. According to llseek_loop() and bpf-script-test-prologue.c, one forth of total lseeks should be collected.
This patch improve test__bpf so it can run multiple BPF programs on different test functions.
--- tools/perf/tests/Build | 9 ++- tools/perf/tests/bpf-script-test-prologue.c | 35 ++++++++++++ tools/perf/tests/bpf.c | 89 +++++++++++++++++++++++------ tools/perf/tests/llvm.c | 5 ++ tools/perf/tests/llvm.h | 8 +++ 5 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 tools/perf/tests/bpf-script-test-prologue.c
tools/perf/tests/Build +8 -1
@@ -31,7 +31,7 @@ perf-y += sample-parsing.o perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o perf-y += thread-map.o -perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o +perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-prologue.o perf-y += bpf.o perf-y += topology.o
@@ -49,6 +49,13 @@ $(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ $(Q)echo ';' >> $@ +$(OUTPUT)tests/llvm-src-prologue.c: tests/bpf-script-test-prologue.c + $(call rule_mkdir) + $(Q)echo '#include <tests/llvm.h>' > $@ + $(Q)echo 'const char test_llvm__bpf_test_prologue_prog[] =' >> $@ + $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@ + $(Q)echo ';' >> $@ + ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64)) perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o endif
tools/perf/tests/bpf-script-test-prologue.c new file +35 -0
@@ -0,0 +1,35 @@ +/* +* bpf-script-test-prologue.c +* Test BPF prologue +*/ +#ifndef LINUX_VERSION_CODE +# error Need LINUX_VERSION_CODE +# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' +#endif +#define SEC (NAME ) __attribute__((section(NAME), used)) + +#include <uapi/linux/fs.h> + +#define FMODE_READ 0x1 +#define FMODE_WRITE 0x2 + +static void (*bpf_trace_printk)(const char *fmt,int fmt_size, ...) = + (void *)6 ; + +SEC("func=null_lseek file->f_mode offset orig") +int bpf_func__null_lseek (void *ctx,int err,unsigned long f_mode, +unsigned long offset,unsigned long orig) +{ +if (err) +return 0 ; +if (f_mode &FMODE_WRITE ) +return 0 ; +if (offset &1 ) +return 0 ; +if (orig ==SEEK_CUR ) +return 0 ; +return 1 ; +} + +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = LINUX_VERSION_CODE;
tools/perf/tests/bpf.c +72 -17
144 lines not on this page · raw message
tools/perf/tests/llvm.c +5 -0
@@ -22,6 +22,11 @@ struct llvm_testcase { [LLVM_TESTCASE_KBUILD ] = {.source = test_llvm__bpf_test_kbuild_prog, .errmsg ="llvm.kbuild-dir can be fixed" , .tried = false}, + /* Don't output if this one fail. */ + [LLVM_TESTCASE_BPF_PROLOGUE ] = { + .source = test_llvm__bpf_test_prologue_prog, + .errmsg ="failed for unknown reason" , + .tried = false}, {.source = NULL} };
tools/perf/tests/llvm.h +8 -0
@@ -10,10 +10,18 @@ struct test_llvm__bpf_result {extern const char test_llvm__bpf_prog[];extern const char test_llvm__bpf_test_kbuild_prog[]; +extern const char test_llvm__bpf_test_prologue_prog[];enum test_llvm__testcase {LLVM_TESTCASE_BASE ,LLVM_TESTCASE_KBUILD , +/* +* We must put LLVM_TESTCASE_BPF_PROLOGUE after +* LLVM_TESTCASE_KBUILD, so if kbuild test failed, +* don't need to try this one, because it depend on +* kernel header. +*/ +LLVM_TESTCASE_BPF_PROLOGUE ,NR_LLVM_TESTCASES , };void test_llvm__fetch_bpf_obj (void **p_obj_buf,size_t *p_obj_buf_sz,int index);
Signature · 1 line
1.8.3.4
PATCH 23/31 bpf tools: Add helper function for updating bpf maps elements
In reply to Wang NanFrom: He Kuang <[email protected]>
Add helper function bpf_map_update_elem() which calls sys_bpf syscall to update elements in bpf maps.
--- tools/lib/bpf/bpf.c | 14 ++++++++++++++ tools/lib/bpf/bpf.h | 2 ++ 2 files changed, 16 insertions(+)
tools/lib/bpf/bpf.c +14 -0
@@ -83,3 +83,17 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, log_buf[0 ] =0 ;return sys_bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); } + +int bpf_map_update_elem (int fd,void *key,void *value, +u64 flags) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + attr.value =ptr_to_u64 (value); + attr.flags = flags; + +return sys_bpf (BPF_MAP_UPDATE_ELEM , &attr,sizeof (attr)); +}
tools/lib/bpf/bpf.h +2 -0
@@ -20,4 +20,6 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, u32 kern_version, char *log_buf, size_t log_buf_sz); +int bpf_map_update_elem (int fd,void *key,void *value, +u64 flags);#endif
Signature · 1 line
1.8.3.4
PATCH 24/31 bpf tools: Collect map definition in bpf_object
In reply to Wang NanThis patch collects more information from maps sections in BPF object files into 'struct bpf_object', enables later patches access those information (such as the type and size of the map).
In this patch, a new handler 'struct bpf_map' is extracted in parallel with bpf_object and bpf_program. Its iterator and accessor is also created.
--- tools/lib/bpf/libbpf.c | 186 +++++++++++++++++++++++++++++++++---------------- tools/lib/bpf/libbpf.h | 21 ++++++ 2 files changed, 147 insertions(+), 60 deletions(-)
tools/lib/bpf/libbpf.c +126 -60
@@ -109,22 +109,24 @@ struct bpf_program {bpf_program_clear_priv_t clear_priv; }; +struct bpf_map { +int fd; +struct bpf_map_def def; +void *priv; +bpf_map_clear_priv_t clear_priv; +}; +static LIST_HEAD (bpf_objects_list);struct bpf_object {char license[64 ];u32 kern_version; -void *maps_buf; -size_t maps_buf_sz;struct bpf_program *programs;size_t nr_programs; -int *map_fds; -/* -* This field is required because maps_buf will be freed and -* maps_buf_sz will be set to 0 after loaded. -*/ -size_t nr_map_fds; +struct bpf_map *maps; +size_t nr_maps; +bool loaded;/*
@@ -434,21 +436,38 @@ static int bpf_object__init_maps(struct bpf_object *obj, void *data, size_t size) { - if (size == 0) { + size_t nr_maps; +int i; + + nr_maps = size /sizeof (struct bpf_map_def ); +if (!data || !nr_maps) {pr_debug ("%s doesn't need map definition\n" , obj->path);return 0 ; } - obj->maps_buf =malloc (size); -if (!obj->maps_buf) { -pr_warning ("malloc maps failed: %s\n" , obj->path); +pr_debug ("maps in %s: %ld bytes\n" , obj->path, (long )size); + + obj->maps =calloc (1 ,sizeof (obj->maps[0 ]) * nr_maps); +if (!obj->maps) { +pr_warning ("alloc maps for object failed\n" );return -ENOMEM ; } + obj->nr_maps = nr_maps; - obj->maps_buf_sz = size; -memcpy (obj->maps_buf, data, size); -pr_debug ("maps in %s: %ld bytes\n" , obj->path, (long )size); +for (i =0 ; i < nr_maps; i++) { +struct bpf_map_def *def = &obj->maps[i].def; + +/* +* fill all fd with -1 so won't close incorrect +* fd (0, stdin) when failure. +*/ + obj->maps[i].fd =-1 ; + +/* Save map definition into obj->maps */ + *def = *(struct bpf_map_def *)(data + + i *sizeof (struct bpf_map_def )); + }return 0 ; }
@@ -632,37 +651,15 @@ static int bpf_object__create_maps(struct bpf_object *obj) {unsigned int i; -size_t nr_maps; -int *pfd; - - nr_maps = obj->maps_buf_sz /sizeof (struct bpf_map_def ); -if (!obj->maps_buf || !nr_maps) { -pr_debug ("don't need create maps for %s\n" , - obj->path); -return 0 ; - } - - obj->map_fds =malloc (sizeof (int ) * nr_maps); -if (!obj->map_fds) { -pr_warning ("realloc perf_bpf_map_fds failed\n" ); -return -ENOMEM ; - } - obj->nr_map_fds = nr_maps; -/* fill all fd with -1 */ -memset (obj->map_fds,-1 ,sizeof (int ) * nr_maps); +for (i =0 ; i < obj->nr_maps; i++) { +struct bpf_map_def *def = &obj->maps[i].def; +int *pfd = &obj->maps[i].fd; - pfd = obj->map_fds; -for (i =0 ; i < nr_maps; i++) { -struct bpf_map_def def; - - def = *(struct bpf_map_def *)(obj->maps_buf + - i *sizeof (struct bpf_map_def )); - - *pfd =bpf_create_map (def.type, - def.key_size, - def.value_size, - def.max_entries); + *pfd =bpf_create_map (def->type, + def->key_size, + def->value_size, + def->max_entries);if (*pfd <0 ) {size_t j;int err = *pfd;
@@ -670,22 +667,17 @@ bpf_object__create_maps(struct bpf_object *obj)pr_warning ("failed to create map: %s\n" ,strerror (errno));for (j =0 ; j < i; j++) -zclose (obj->map_fds[j]); - obj->nr_map_fds =0 ; -zfree (&obj->map_fds); +zclose (obj->maps[j].fd);return err; }pr_debug ("create map: fd=%d\n" , *pfd); - pfd++; } -zfree (&obj->maps_buf); - obj->maps_buf_sz =0 ;return 0 ; }static int -bpf_program__relocate (struct bpf_program *prog,int *map_fds) +bpf_program__relocate (struct bpf_program *prog,struct bpf_object *obj) {int i;
@@ -705,7 +697,7 @@ bpf_program__relocate(struct bpf_program *prog, int *map_fds)return -ERANGE ; } insns[insn_idx].src_reg =BPF_PSEUDO_MAP_FD ; - insns[insn_idx].imm = map_fds[map_idx]; + insns[insn_idx].imm = obj->maps[map_idx].fd; }zfree (&prog->reloc_desc);
@@ -724,7 +716,7 @@ bpf_object__relocate(struct bpf_object *obj)for (i =0 ; i < obj->nr_programs; i++) { prog = &obj->programs[i]; - err =bpf_program__relocate (prog, obj->map_fds); + err =bpf_program__relocate (prog, obj);if (err) {pr_warning ("failed to relocate '%s'\n" , prog->section_name);
@@ -748,8 +740,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)Elf_Data *data = obj->efile.reloc[i].data;int idx = shdr->sh_info;struct bpf_program *prog; -size_t nr_maps = obj->maps_buf_sz / -sizeof (struct bpf_map_def ); +size_t nr_maps = obj->nr_maps;if (shdr->sh_type !=SHT_REL ) {pr_warning ("internal error at %d\n" , __LINE__);
@@ -986,10 +977,8 @@ int bpf_object__unload(struct bpf_object *obj)if (!obj)return -EINVAL ; -for (i =0 ; i < obj->nr_map_fds; i++) -zclose (obj->map_fds[i]); -zfree (&obj->map_fds); - obj->nr_map_fds =0 ; +for (i =0 ; i < obj->nr_maps; i++) +zclose (obj->maps[i].fd);for (i =0 ; i < obj->nr_programs; i++)bpf_program__unload (&obj->programs[i]);
@@ -1032,7 +1021,15 @@ void bpf_object__close(struct bpf_object *obj)bpf_object__elf_finish (obj);bpf_object__unload (obj); -zfree (&obj->maps_buf); +for (i =0 ; i < obj->nr_maps; i++) { +if (obj->maps[i].clear_priv) + obj->maps[i].clear_priv (&obj->maps[i], + obj->maps[i].priv); + obj->maps[i].priv =NULL ; + obj->maps[i].clear_priv =NULL ; + } +zfree (&obj->maps); + obj->nr_maps =0 ;if (obj->programs && obj->nr_programs) {for (i =0 ; i < obj->nr_programs; i++)
@@ -1179,3 +1176,72 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)return fd; } + +int bpf_map__get_fd (struct bpf_map *map) +{ +if (!map) +return -EINVAL ; + +return map->fd; +} + +int bpf_map__get_def (struct bpf_map *map,struct bpf_map_def *pdef) +{ +if (!map || !pdef) +return -EINVAL ; + + *pdef = map->def; +return 0 ; +} + +int bpf_map__set_private (struct bpf_map *map,void *priv, +bpf_map_clear_priv_t clear_priv) +{ +if (!map) +return -EINVAL ; + +if (map->priv) { +if (map->clear_priv) + map->clear_priv (map, map->priv); + } + + map->priv = priv; + map->clear_priv = clear_priv; +return 0 ; +} + +int bpf_map__get_private (struct bpf_map *map,void **ppriv) +{ +if (!map) +return -EINVAL ; + +if (ppriv) + *ppriv = map->priv; +return 0 ; +} + +struct bpf_map * +bpf_map__next (struct bpf_map *prev,struct bpf_object *obj) +{ +size_t idx; +struct bpf_map *s, *e; + +if (!obj || !obj->maps) +return NULL ; + + s = obj->maps; + e = obj->maps + obj->nr_maps; + +if (prev ==NULL ) +return s; + +if ((prev < s) || (prev >= e)) { +pr_warning ("error: map handler doesn't belong to object\n" ); +return NULL ; + } + + idx = (prev - obj->maps) +1 ; +if (idx >= obj->nr_maps) +return NULL ; +return &obj->maps[idx]; +}
tools/lib/bpf/libbpf.h +21 -0
@@ -102,4 +102,25 @@ struct bpf_map_def { unsigned int max_entries; }; +/* +* There is another 'struct bpf_map' in include/linux/map.h. However, +* it is not a uapi header so no need to consider name confliction. +*/ +struct bpf_map ; + +struct bpf_map * +bpf_map__next (struct bpf_map *map,struct bpf_object *obj); +#define bpf_map__for_each (pos, obj) \ + for ((pos) = bpf_map__next(NULL, (obj)); \ + (pos) != NULL; \ + (pos) = bpf_map__next((pos), (obj))) + +int bpf_map__get_fd (struct bpf_map *map); +int bpf_map__get_def (struct bpf_map *map,struct bpf_map_def *pdef); + +typedef void (*bpf_map_clear_priv_t )(struct bpf_map *,void *); +int bpf_map__set_private (struct bpf_map *map,void *priv, +bpf_map_clear_priv_t clear_priv); +int bpf_map__get_private (struct bpf_map *map,void **ppriv); +#endif
Signature · 1 line
1.8.3.4
PATCH 25/31 bpf tools: Extract and collect map names from BPF object file
In reply to Wang NanThis patch collect name of map in BPF object files and saves them into 'maps' field in 'struct bpf_object'. 'bpf_object__get_map_by_name' is introduced to retrive map fd and definitions through its name.
--- tools/lib/bpf/libbpf.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++-- tools/lib/bpf/libbpf.h | 3 +++ 2 files changed, 64 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +61 -2
@@ -111,6 +111,7 @@ struct bpf_program {struct bpf_map {int fd; +char *name;struct bpf_map_def def;void *priv;bpf_map_clear_priv_t clear_priv;
@@ -471,12 +472,46 @@ bpf_object__init_maps(struct bpf_object *obj, void *data,return 0 ; } +static void +bpf_object__init_maps_name (struct bpf_object *obj,int maps_shndx) +{ +int i; +Elf_Data *symbols = obj->efile.symbols; + +if (!symbols || maps_shndx <0 ) +return ; + +for (i =0 ; i < symbols->d_size /sizeof (GElf_Sym); i++) { +GElf_Sym sym; +size_t map_idx; +const char *map_name; + +if (!gelf_getsym (symbols, i, &sym)) +continue ; +if (sym.st_shndx != maps_shndx) +continue ; + + map_name =elf_strptr (obj->efile.elf, + obj->efile.ehdr.e_shstrndx, + sym.st_name); + map_idx = sym.st_value /sizeof (struct bpf_map_def ); +if (map_idx >= obj->nr_maps) { +pr_warning ("index of map \"%s\" is buggy: %zu > %zu\n" , + map_name, map_idx, obj->nr_maps); +continue ; + } + obj->maps[map_idx].name =strdup (map_name); +pr_debug ("map %zu is \"%s\"\n" , map_idx, + obj->maps[map_idx].name); + } +} +static int bpf_object__elf_collect (struct bpf_object *obj) {Elf *elf = obj->efile.elf;GElf_Ehdr *ep = &obj->efile.ehdr;Elf_Scn *scn =NULL ; -int idx =0 , err =0 ; +int idx =0 , err =0 , maps_shndx =-1 ;/* Elf is corrupted/truncated, avoid calling elf_strptr. */ if (!elf_rawdata (elf_getscn (elf, ep->e_shstrndx),NULL )) {
@@ -526,9 +561,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj) err =bpf_object__init_kversion (obj, data->d_buf, data->d_size); - else if (strcmp(name, "maps") == 0) + else if (strcmp(name, "maps") == 0) { err = bpf_object__init_maps(obj, data->d_buf, data->d_size); + maps_shndx = idx; + } else if (sh.sh_type == SHT_SYMTAB) {if (obj->efile.symbols) { pr_warning("bpf: multiple SYMTAB in %s\n",
@@ -569,6 +606,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)if (err) goto out; } + +if (maps_shndx >=0 ) +bpf_object__init_maps_name (obj, maps_shndx); out:return err; }
@@ -1194,6 +1234,13 @@ int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef)return 0 ; } +const char *bpf_map__get_name (struct bpf_map *map) +{ +if (!map) +return NULL ; +return map->name; +} +int bpf_map__set_private (struct bpf_map *map,void *priv,bpf_map_clear_priv_t clear_priv) {
@@ -1245,3 +1292,15 @@ bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)return NULL ;return &obj->maps[idx]; } + +struct bpf_map * +bpf_object__get_map_by_name (struct bpf_object *obj,const char *name) +{ +struct bpf_map *pos; + +bpf_map__for_each (pos, obj) { +if (strcmp (pos->name, name) ==0 ) +return pos; + } +return NULL ; +}
tools/lib/bpf/libbpf.h +3 -0
@@ -107,6 +107,8 @@ struct bpf_map_def {* it is not a uapi header so no need to consider name confliction. */ struct bpf_map ; +struct bpf_map * +bpf_object__get_map_by_name (struct bpf_object *obj,const char *name);struct bpf_map *bpf_map__next (struct bpf_map *map,struct bpf_object *obj);
@@ -117,6 +119,7 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);int bpf_map__get_fd (struct bpf_map *map);int bpf_map__get_def (struct bpf_map *map,struct bpf_map_def *pdef); +const char *bpf_map__get_name (struct bpf_map *map);typedef void (*bpf_map_clear_priv_t )(struct bpf_map *,void *); int bpf_map__set_private(struct bpf_map *map, void *priv,
Signature · 1 line
1.8.3.4
PATCH 26/31 perf tools: Support perf event alias name
In reply to Wang NanFrom: He Kuang <[email protected]>
This patch adds new bison rules for specifying an alias name to a perf event, which allows cmdline refer to previous defined perf event through its name. With this patch user can give alias name to a perf event using following cmdline:
# perf record -e mypmu=cycles ...
To allow parser refer to existing event selecter, pass event list to 'struct parse_events_evlist'. perf_evlist__find_evsel_by_alias() is introduced to get evsel through its alias.
--- tools/perf/util/evlist.c | 16 ++++++++++++++++ tools/perf/util/evlist.h | 4 ++++ tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 31 ++++++++++++++++++++++++++++--- tools/perf/util/parse-events.h | 5 +++++ tools/perf/util/parse-events.y | 15 ++++++++++++++- 6 files changed, 68 insertions(+), 4 deletions(-)
tools/perf/util/evlist.c +16 -0
@@ -1753,3 +1753,19 @@ void perf_evlist__set_tracking_event(struct perf_evlist *evlist, tracking_evsel->tracking = true; } + +struct perf_evsel * +perf_evlist__find_evsel_by_alias (struct perf_evlist *evlist, +const char *alias) +{ +struct perf_evsel *evsel; + +evlist__for_each (evlist, evsel) { +if (!evsel->alias) +continue ; +if (strcmp (alias, evsel->alias) ==0 ) +return evsel; + } + +return NULL ; +}
tools/perf/util/evlist.h +4 -0
@@ -292,4 +292,8 @@ void perf_evlist__set_tracking_event(struct perf_evlist *evlist, struct perf_evsel *tracking_evsel);void perf_event_attr__set_max_precise_ip (struct perf_event_attr *attr); + +struct perf_evsel * +perf_evlist__find_evsel_by_alias (struct perf_evlist *evlist,const char *alias); +#endif /* __PERF_EVLIST_H */
tools/perf/util/evsel.h +1 -0
@@ -87,6 +87,7 @@ struct perf_evsel {int idx;u32 ids;char *name; +char *alias;double scale;const char *unit;struct event_format *tp_format;
tools/perf/util/parse-events.c +28 -3
@@ -1020,6 +1020,30 @@ int parse_events__modifier_group(struct list_head *list,return parse_events__modifier_event (list, event_mod, true); } +int parse_events__set_event_alias (struct parse_events_evlist *data, +struct list_head *list, +const char *str, +void *loc_alias_) +{ +struct perf_evsel *evsel; +YYLTYPE *loc_alias = loc_alias_; + +if (!str) +return 0 ; + +if (!list_is_singular (list)) { +struct parse_events_error *err = data->error; + + err->idx = loc_alias->first_column; + err->str =strdup ("One alias can be applied to one event only" ); +return -EINVAL ; + } + + evsel = list_first_entry(list, struct perf_evsel, node); + evsel->alias =strdup (str); +return evsel->alias ?0 : -ENOMEM ; +} +void parse_events__set_leader (char *name,struct list_head *list) {struct perf_evsel *leader;
@@ -1373,9 +1397,10 @@ int parse_events(struct perf_evlist *evlist, const char *str, struct parse_events_error *err) {struct parse_events_evlist data = { - .list =LIST_HEAD_INIT (data.list), - .idx = evlist->nr_entries, - .error = err, + .list =LIST_HEAD_INIT (data.list), + .idx = evlist->nr_entries, + .error = err, + .evlist = evlist, };int ret;
tools/perf/util/parse-events.h +5 -0
@@ -96,6 +96,7 @@ struct parse_events_evlist {int idx;int nr_groups;struct parse_events_error *error; +struct perf_evlist *evlist; };struct parse_events_terms {
@@ -168,4 +169,8 @@ extern int is_valid_tracepoint(const char *event_string);int valid_event_mount (const char *eventfs);char *parse_events_formats_error_string (char *additional_terms); +int parse_events__set_event_alias (struct parse_events_evlist *data, +struct list_head *list, +const char *str, +void *loc_alias_);#endif /* __PERF_PARSE_EVENTS_H */
tools/perf/util/parse-events.y +14 -1
@@ -76,6 +76,7 @@ static inc_group_count(struct list_head *list, %type <head> event_bpf_file %type <head> event_def %type <head> event_mod +%type <head> event_alias %type <head> event_name %type <head> event %type <head> events
@@ -192,13 +193,25 @@ event_name PE_MODIFIER_EVENT event_name event_name: -PE_EVENT_NAME event_def +PE_EVENT_NAME event_alias { ABORT_ON(parse_events_name($2, $1)); free($1); $$ = $2; } | +event_alias + +event_alias: +PE_NAME '=' event_def +{ + struct list_head *list = $3; + struct parse_events_evlist *data = _data; + + ABORT_ON(parse_events__set_event_alias(data, list, $1, &@1)); + $$ = list; +} +| event_def event_def: event_pmu |
Signature · 1 line
1.8.3.4
Namhyung Kim
maintainerRe: [PATCH 26/31] perf tools: Support perf event alias name
Hi,
Quoted 7 lines · Wang Nan
From: He Kuang <[email protected]>
This patch adds new bison rules for specifying an alias name to a perf event, which allows cmdline refer to previous defined perf event through its name. With this patch user can give alias name to a perf event using following cmdline:
Quoted 5 lines
# perf record -e mypmu=cycles ...To allow parser refer to existing event selecter, pass event list to 'struct
parse_events_evlist'.perf_evlist__find_evsel_by_alias()is introduced to get evsel through its alias.
What about using event name directly? I guess the alias name is used only to refer an event so it'd be better to use the event name. Anyway we need alias as well when event has no name or name is complex.
Thanks
Namhyung
Quoted 188 lines
Signed-off-by He Kuang <[email protected]>Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>--- tools/perf/util/evlist.c | 16 ++++++++++++++++ tools/perf/util/evlist.h | 4 ++++ tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 31 ++++++++++++++++++++++++++++--- tools/perf/util/parse-events.h | 5 +++++ tools/perf/util/parse-events.y | 15 ++++++++++++++- 6 files changed, 68 insertions(+), 4 deletions(-)tools/perf/util/evlist.c +16
-0@@ -1753,3 +1753,19 @@ void perf_evlist__set_tracking_event(struct perf_evlist *evlist, tracking_evsel->tracking = true; } + +struct perf_evsel * +perf_evlist__find_evsel_by_alias (struct perf_evlist *evlist, +const char *alias) +{ +struct perf_evsel *evsel; + +evlist__for_each (evlist, evsel) { +if (!evsel->alias) +continue ; +if (strcmp (alias, evsel->alias) ==0 ) +return evsel; + } + +return NULL ; +}tools/perf/util/evlist.h +4
-0@@ -292,4 +292,8 @@ void perf_evlist__set_tracking_event(struct perf_evlist *evlist, struct perf_evsel *tracking_evsel);void perf_event_attr__set_max_precise_ip (struct perf_event_attr *attr); + +struct perf_evsel * +perf_evlist__find_evsel_by_alias (struct perf_evlist *evlist,const char *alias); +#endif /* __PERF_EVLIST_H */ tools/perf/util/evsel.h +1
-0@@ -87,6 +87,7 @@ struct perf_evsel {int idx;u32 ids;char *name; +char *alias;double scale;const char *unit;struct event_format *tp_format;tools/perf/util/parse-events.c +28
-3@@ -1020,6 +1020,30 @@ int parse_events__modifier_group(struct list_head *list,return parse_events__modifier_event (list, event_mod, true); } +int parse_events__set_event_alias (struct parse_events_evlist *data, +struct list_head *list, +const char *str, +void *loc_alias_) +{ +struct perf_evsel *evsel; +YYLTYPE *loc_alias = loc_alias_; + +if (!str) +return 0 ; + +if (!list_is_singular (list)) { +struct parse_events_error *err = data->error; + + err->idx = loc_alias->first_column; + err->str =strdup ("One alias can be applied to one event only" ); +return -EINVAL ; + } + + evsel = list_first_entry(list, struct perf_evsel, node); + evsel->alias =strdup (str); +return evsel->alias ?0 : -ENOMEM ; +} +void parse_events__set_leader (char *name,struct list_head *list) {struct perf_evsel *leader;@@ -1373,9 +1397,10 @@ int parse_events(struct perf_evlist *evlist, const char *str, struct parse_events_error *err) {struct parse_events_evlist data = { - .list =LIST_HEAD_INIT (data.list), - .idx = evlist->nr_entries, - .error = err, + .list =LIST_HEAD_INIT (data.list), + .idx = evlist->nr_entries, + .error = err, + .evlist = evlist, };int ret;tools/perf/util/parse-events.h +5
-0@@ -96,6 +96,7 @@ struct parse_events_evlist {int idx;int nr_groups;struct parse_events_error *error; +struct perf_evlist *evlist; };struct parse_events_terms {@@ -168,4 +169,8 @@ extern int is_valid_tracepoint(const char *event_string);int valid_event_mount (const char *eventfs);char *parse_events_formats_error_string (char *additional_terms); +int parse_events__set_event_alias (struct parse_events_evlist *data, +struct list_head *list, +const char *str, +void *loc_alias_);#endif /* __PERF_PARSE_EVENTS_H */ tools/perf/util/parse-events.y +14
-1@@ -76,6 +76,7 @@ static inc_group_count(struct list_head *list, %type <head> event_bpf_file %type <head> event_def %type <head> event_mod +%type <head> event_alias %type <head> event_name %type <head> event %type <head> events@@ -192,13 +193,25 @@ event_name PE_MODIFIER_EVENT event_name event_name: -PE_EVENT_NAME event_def +PE_EVENT_NAME event_alias { ABORT_ON(parse_events_name($2, $1)); free($1); $$ = $2; } | +event_alias + +event_alias: +PE_NAME '=' event_def +{ + struct list_head *list = $3; + struct parse_events_evlist *data = _data; + + ABORT_ON(parse_events__set_event_alias(data, list, $1, &@1)); + $$ = list; +} +| event_def event_def: event_pmu |Signature · 1 line
1.8.3.4
Re: [PATCH 26/31] perf tools: Support perf event alias name
Quoted 10 lines · Namhyung Kim
Hi,
Quoted 7 lines · Wang Nan
From: He Kuang <[email protected]>
This patch adds new bison rules for specifying an alias name to a perf event, which allows cmdline refer to previous defined perf event through its name. With this patch user can give alias name to a perf event using following cmdline:
Quoted 8 lines
Quoted 5 lines
# perf record -e mypmu=cycles ...To allow parser refer to existing event selecter, pass event list to 'struct
parse_events_evlist'.perf_evlist__find_evsel_by_alias()is introduced to get evsel through its alias.What about using event name directly? I guess the alias name is used only to refer an event so it'd be better to use the event name. Anyway we need alias as well when event has no name or name is complex.
It is possible to trigger two perf events with same PMU but different config options:
# perf record -e cycles/period=9999/ -e cycles/period=99999/ -a sleep 1
In this case the name of events are:
cycles/period=9999/ `
cycles/period=99999/
Using it in perf cmdline is painful:
# perf record -e cycles/period=9999/ -e cycles/period=99999/ -e bpf_program.c/myevent=cycles/period=9999//...
Thank you.
Quoted 191 lines
Thanks
NamhyungQuoted 187 lines
Signed-off-by He Kuang <[email protected]>Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Brendan Gregg <[email protected]>Cc Daniel Borkmann <[email protected]>Cc David Ahern <[email protected]>Cc He Kuang <[email protected]>Cc Jiri Olsa <[email protected]>Cc Kaixu Xia <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Zefan Li <[email protected]>--- tools/perf/util/evlist.c | 16 ++++++++++++++++ tools/perf/util/evlist.h | 4 ++++ tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 31 ++++++++++++++++++++++++++++--- tools/perf/util/parse-events.h | 5 +++++ tools/perf/util/parse-events.y | 15 ++++++++++++++- 6 files changed, 68 insertions(+), 4 deletions(-)tools/perf/util/evlist.c +16
-0@@ -1753,3 +1753,19 @@ void perf_evlist__set_tracking_event(struct perf_evlist *evlist, tracking_evsel->tracking = true; } + +struct perf_evsel * +perf_evlist__find_evsel_by_alias (struct perf_evlist *evlist, +const char *alias) +{ +struct perf_evsel *evsel; + +evlist__for_each (evlist, evsel) { +if (!evsel->alias) +continue ; +if (strcmp (alias, evsel->alias) ==0 ) +return evsel; + } + +return NULL ; +}tools/perf/util/evlist.h +4
-0@@ -292,4 +292,8 @@ void perf_evlist__set_tracking_event(struct perf_evlist *evlist, struct perf_evsel *tracking_evsel);void perf_event_attr__set_max_precise_ip (struct perf_event_attr *attr); + +struct perf_evsel * +perf_evlist__find_evsel_by_alias (struct perf_evlist *evlist,const char *alias); +#endif /* __PERF_EVLIST_H */ tools/perf/util/evsel.h +1
-0@@ -87,6 +87,7 @@ struct perf_evsel {int idx;u32 ids;char *name; +char *alias;double scale;const char *unit;struct event_format *tp_format;tools/perf/util/parse-events.c +28
-3@@ -1020,6 +1020,30 @@ int parse_events__modifier_group(struct list_head *list,return parse_events__modifier_event (list, event_mod, true); } +int parse_events__set_event_alias (struct parse_events_evlist *data, +struct list_head *list, +const char *str, +void *loc_alias_) +{ +struct perf_evsel *evsel; +YYLTYPE *loc_alias = loc_alias_; + +if (!str) +return 0 ; + +if (!list_is_singular (list)) { +struct parse_events_error *err = data->error; + + err->idx = loc_alias->first_column; + err->str =strdup ("One alias can be applied to one event only" ); +return -EINVAL ; + } + + evsel = list_first_entry(list, struct perf_evsel, node); + evsel->alias =strdup (str); +return evsel->alias ?0 : -ENOMEM ; +} +void parse_events__set_leader (char *name,struct list_head *list) {struct perf_evsel *leader;@@ -1373,9 +1397,10 @@ int parse_events(struct perf_evlist *evlist, const char *str, struct parse_events_error *err) {struct parse_events_evlist data = { - .list =LIST_HEAD_INIT (data.list), - .idx = evlist->nr_entries, - .error = err, + .list =LIST_HEAD_INIT (data.list), + .idx = evlist->nr_entries, + .error = err, + .evlist = evlist, };int ret;tools/perf/util/parse-events.h +5
-0@@ -96,6 +96,7 @@ struct parse_events_evlist {int idx;int nr_groups;struct parse_events_error *error; +struct perf_evlist *evlist; };struct parse_events_terms {@@ -168,4 +169,8 @@ extern int is_valid_tracepoint(const char *event_string);int valid_event_mount (const char *eventfs);char *parse_events_formats_error_string (char *additional_terms); +int parse_events__set_event_alias (struct parse_events_evlist *data, +struct list_head *list, +const char *str, +void *loc_alias_);#endif /* __PERF_PARSE_EVENTS_H */ tools/perf/util/parse-events.y +14
-1@@ -76,6 +76,7 @@ static inc_group_count(struct list_head *list, %type <head> event_bpf_file %type <head> event_def %type <head> event_mod +%type <head> event_alias %type <head> event_name %type <head> event %type <head> events@@ -192,13 +193,25 @@ event_name PE_MODIFIER_EVENT event_name event_name: -PE_EVENT_NAME event_def +PE_EVENT_NAME event_alias { ABORT_ON(parse_events_name($2, $1)); free($1); $$ = $2; } | +event_alias + +event_alias: +PE_NAME '=' event_def +{ + struct list_head *list = $3; + struct parse_events_evlist *data = _data; + + ABORT_ON(parse_events__set_event_alias(data, list, $1, &@1)); + $$ = list; +} +| event_def event_def: event_pmu |Signature · 1 line
1.8.3.4
Namhyung Kim
maintainerRe: [PATCH 26/31] perf tools: Support perf event alias name
Hi,
Quoted 33 lines · Wangnan (F)
Quoted 14 lines · Namhyung Kim
Hi,
Quoted 10 lines · Wang Nan
From: He Kuang <[email protected]>
This patch adds new bison rules for specifying an alias name to a perf event, which allows cmdline refer to previous defined perf event through its name. With this patch user can give alias name to a perf event using following cmdline:
# perf record -e mypmu=cycles ...Quoted 7 lines
Quoted 3 lines
To allow parser refer to existing event selecter, pass event list to 'struct
parse_events_evlist'.perf_evlist__find_evsel_by_alias()is introduced to get evsel through its alias.What about using event name directly? I guess the alias name is used only to refer an event so it'd be better to use the event name. Anyway we need alias as well when event has no name or name is complex.
It is possible to trigger two perf events with same PMU but different config options:
# perf record -e cycles/period=9999/ -e cycles/period=99999/ -a sleep 1In this case the name of events are:
Quoted 7 lines
cycles/period=9999/ `
cycles/period=99999/Using it in perf cmdline is painful:
# perf record -e cycles/period=9999/ -e cycles/period=99999/ -e bpf_program.c/myevent=cycles/period=9999//...
I understand the need of using aliases but I think it's more natural to use event name for simple cases..
Thanks,
Namhyung
Re: [PATCH 26/31] perf tools: Support perf event alias name
Quoted 34 lines · Namhyung Kim
Hi,
Quoted 31 lines · Wangnan (F)
Quoted 10 lines · Namhyung Kim
Hi,
Quoted 7 lines · Wang Nan
From: He Kuang <[email protected]>
This patch adds new bison rules for specifying an alias name to a perf event, which allows cmdline refer to previous defined perf event through its name. With this patch user can give alias name to a perf event using following cmdline:
Quoted 8 lines
Quoted 5 lines
# perf record -e mypmu=cycles ...To allow parser refer to existing event selecter, pass event list to 'struct
parse_events_evlist'.perf_evlist__find_evsel_by_alias()is introduced to get evsel through its alias.What about using event name directly? I guess the alias name is used only to refer an event so it'd be better to use the event name. Anyway we need alias as well when event has no name or name is complex.
It is possible to trigger two perf events with same PMU but different config options:
# perf record -e cycles/period=9999/ -e cycles/period=99999/ -a sleep 1In this case the name of events are:
cycles/period=9999/ `
cycles/period=99999/
Quoted 6 lines
Quoted 4 lines
Using it in perf cmdline is painful:
# perf record -e cycles/period=9999/ -e cycles/period=99999/ -e bpf_program.c/myevent=cycles/period=9999//...I understand the need of using aliases but I think it's more natural to use event name for simple cases..
I will consider this. However, if we allow using event name directly like this:
# perf record -e cycles -e test_pmu.c/myevent=cycles/ ...
Then two '-e' seems redundant, right? Why not directly using:
# perf record -e test_pmu.c/myevent=cycles/ ...
and make perf creates cycles event for test_pmu.c?
We can make syntax like
# perf record -e test_pmu.c/myevent=cycles/ ...
as a syntax sugar of
# perf record -e randomname=cycles -e test_pmu.c/myevent=randomname/ ...
and don't need to find evsel through their names if alias not exist.
So this is a new feature, and worth another patch.
Thought?
Quoted 2 lines
Thanks,
Namhyung
Namhyung Kim
maintainerRe: [PATCH 26/31] perf tools: Support perf event alias name
Quoted 71 lines · Wangnan (F)
Quoted 42 lines · Namhyung Kim
Hi,
Quoted 38 lines · Wangnan (F)
Quoted 16 lines · Namhyung Kim
Hi,
Quoted 12 lines · Wang Nan
From: He Kuang <[email protected]>
This patch adds new bison rules for specifying an alias name to a perf event, which allows cmdline refer to previous defined perf event through
its name. With this patch user can give alias name to a perf event using
following cmdline:# perf record -e mypmu=cycles ...Quoted 7 lines
Quoted 3 lines
To allow parser refer to existing event selecter, pass event list to 'struct
parse_events_evlist'.perf_evlist__find_evsel_by_alias()is introduced to get evsel through its alias.What about using event name directly? I guess the alias name is used only to refer an event so it'd be better to use the event name. Anyway we need alias as well when event has no name or name is complex.
It is possible to trigger two perf events with same PMU but different config options:
# perf record -e cycles/period=9999/ -e cycles/period=99999/ -a sleep 1In this case the name of events are:
cycles/period=9999/ `
cycles/period=99999/Quoted 7 lines
Quoted 4 lines
Using it in perf cmdline is painful:
# perf record -e cycles/period=9999/ -e cycles/period=99999/ -e bpf_program.c/myevent=cycles/period=9999//...I understand the need of using aliases but I think it's more natural to use event name for simple cases..
I will consider this. However, if we allow using event name directly like this:
# perf record -e cycles -e test_pmu.c/myevent=cycles/ ...Then two '-e' seems redundant, right? Why not directly using:
# perf record -e test_pmu.c/myevent=cycles/ ...and make perf creates cycles event for
test_pmu.c?We can make syntax like
# perf record -e test_pmu.c/myevent=cycles/ ...as a syntax sugar of
Quoted 7 lines
# perf record -e randomname=cycles -e test_pmu.c/myevent=randomname/ ...and don't need to find evsel through their names if alias not exist.
So this is a new feature, and worth another patch.
Thought?
Not sure it's worth. It can confuse users IMHO.
Isn't it enough to give them in a single argument?
# perf record -e cycles,test_pmu.c/myevent=cycles/
Thanks,
Namhyung
Re: [PATCH 26/31] perf tools: Support perf event alias name
Quoted 2 lines · Namhyung Kim
Quoted 1 line · Wangnan (F)
[SNIP]
Quoted 24 lines
Quoted 24 lines
Quoted 2 lines
I understand the need of using aliases but I think it's more natural to use event name for simple cases..
I will consider this. However, if we allow using event name directly like this:
# perf record -e cycles -e test_pmu.c/myevent=cycles/ ...Then two '-e' seems redundant, right? Why not directly using:
# perf record -e test_pmu.c/myevent=cycles/ ...and make perf creates cycles event for
test_pmu.c?We can make syntax like
# perf record -e test_pmu.c/myevent=cycles/ ...as a syntax sugar of
# perf record -e randomname=cycles -e test_pmu.c/myevent=randomname/ ...and don't need to find evsel through their names if alias not exist.
Quoted 8 lines
Quoted 3 lines
So this is a new feature, and worth another patch.
Thought?
Not sure it's worth. It can confuse users IMHO.
Isn't it enough to give them in a single argument?
# perf record -e cycles,test_pmu.c/myevent=cycles/
OK. I have put it on my todo-list.
Thank you.
Quoted 2 lines
Thanks,
Namhyung
PATCH 27/31 perf tools: Pass available CPU number to clang compiler
In reply to Wang NanThis patch introduces a new macro "__NR_CPUS__" to perf's embedded clang compiler, which represent the available CPU counters in this system. BPF program can use this macro to create a map with same number of system CPUs. For exmaple:
struct bpf_map_def SEC("maps") pmu_map = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u32),
.max_entries = __NR_CPUS__,
};
--- tools/perf/util/llvm-utils.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
tools/perf/util/llvm-utils.c +18 -6
@@ -11,10 +11,11 @@#include "cache.h" #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \ - "$CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS " \ - "$KERNEL_INC_OPTIONS -Wno-unused-value " \ - "-Wno-pointer-sign -working-directory " \ - "$WORKING_DIR -c \"$CLANG_SOURCE\" -target bpf -O2 -o -" + "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\ + "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \ + "-Wno-unused-value -Wno-pointer-sign " \ + "-working-directory $WORKING_DIR " \ + "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"struct llvm_param llvm_param = { .clang_path ="clang" ,
@@ -326,8 +327,8 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)int llvm__compile_bpf (const char *path,void **p_obj_buf,size_t *p_obj_buf_sz) { -int err; -char clang_path[PATH_MAX ]; +int err, nr_cpus_avail; +char clang_path[PATH_MAX ], nr_cpus_avail_str[64 ];const char *clang_opt = llvm_param.clang_opt;const char *template = llvm_param.clang_bpf_cmd_template;char *kbuild_dir =NULL , *kbuild_include_opts =NULL ;
@@ -354,6 +355,17 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,*/ get_kbuild_opts (&kbuild_dir, &kbuild_include_opts); + nr_cpus_avail =sysconf (_SC_NPROCESSORS_CONF); +if (nr_cpus_avail <=0 ) { +pr_err ( +"WARNING:\tunable to get available CPUs in this system: %s\n" +" \tUse 128 instead.\n" ,strerror (errno)); + nr_cpus_avail =128 ; + } +snprintf (nr_cpus_avail_str,sizeof (nr_cpus_avail_str),"%d" , + nr_cpus_avail); + +force_set_env ("NR_CPUS" , nr_cpus_avail_str);force_set_env ("CLANG_EXEC" , clang_path);force_set_env ("CLANG_OPTIONS" , clang_opt);force_set_env ("KERNEL_INC_OPTIONS" , kbuild_include_opts);
Signature · 1 line
1.8.3.4
PATCH 28/31 perf tools: Add API to config maps in bpf object
In reply to Wang Nanbpf__config_obj() is introduced as a core API to config BPF object after loading. One configuration option of maps is introduced. After this patch BPF object can accept configuration like:
maps.my_pmy.event=evt
Where evt is a predefined event with alias "evt".
--- tools/perf/util/bpf-loader.c | 147 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 39 ++++++++++++ 2 files changed, 186 insertions(+)
tools/perf/util/bpf-loader.c +147 -0
@@ -10,6 +10,7 @@#include <linux/err.h> #include "perf.h" #include "debug.h" +#include "util.h" #include "bpf-loader.h" #include "bpf-prologue.h" #include "llvm-utils.h"
@@ -633,6 +634,139 @@ int bpf__foreach_tev(struct bpf_object *obj,return 0 ; } +struct bpf_map_priv { +struct perf_evsel *evsel; +}; + +static void +bpf_map_priv__clear (struct bpf_map *map__maybe_unused , +void *_priv) +{ +struct bpf_map_priv *priv = _priv; + +free (priv); +} + +static int +bpf__config_obj_map_event (struct bpf_map *map,const char *val, +struct perf_evlist *evlist) +{ +struct bpf_map_priv *priv; +struct perf_evsel *evsel; +struct bpf_map_def def; +const char *map_name; +int err; + + map_name =bpf_map__get_name (map); + + evsel =perf_evlist__find_evsel_by_alias (evlist, val); +if (!evsel) { +pr_debug ("Event '%s' doesn't exist\n" , val); +return -EINVAL ; + } + + err =bpf_map__get_def (map, &def); +if (err) { +pr_debug ("Unable to get map definition from '%s'\n" , + map_name); +return -EINVAL ; + } + +if (def.type !=BPF_MAP_TYPE_PERF_EVENT_ARRAY ) { +pr_debug ("Map %s type is not BPF_MAP_TYPE_PERF_EVENT_ARRAY\n" , + map_name); +return -EINVAL ; + } + + priv =calloc (sizeof (*priv),1 ); +if (!priv) { +pr_debug ("No enough memory to alloc map private\n" ); +return -ENOMEM ; + } + + priv->evsel = evsel; +return bpf_map__set_private (map, priv, bpf_map_priv__clear); +} + +struct bpf_config_map_func { +const char *config_opt; +int (*config_func)(struct bpf_map *,const char *, +struct perf_evlist *); +}; + +struct bpf_config_map_func bpf_config_map_funcs[] = { + {"event" , bpf__config_obj_map_event}, +}; + +static int +bpf__config_obj_map (struct bpf_object *obj, +const char *key, +const char *val, +struct perf_evlist *evlist) +{ +/* key is "maps.<mapname>.<config opt>" */ +char *map_name =strdup (key +sizeof ("maps." ) -1 ); +struct bpf_map *map; +int err = -ENOENT ; +char *map_opt; +size_t i; + +if (!map_name) +return -ENOMEM ; + + map_opt =strchr (map_name,'.' ); +if (!map_opt) { +pr_debug ("ERROR: Invalid map config: %s\n" , map_name); + goto out; + } + + *map_opt++ ='\0' ; +if (*map_opt =='\0' ) { +pr_debug ("ERROR: Invalid map option: %s\n" , key); + goto out; + } + + map =bpf_object__get_map_by_name (obj, map_name); +if (!map) { +pr_debug ("ERROR: Map %s doesn't exist\n" , map_name); + goto out; + } + +for (i =0 ; i <ARRAY_SIZE (bpf_config_map_funcs); i++) { +struct bpf_config_map_func *func = &bpf_config_map_funcs[i]; + +if (strcmp (map_opt, func->config_opt) ==0 ) { + err = func->config_func (map, val, evlist); + goto out; + } + } + +pr_debug ("ERROR: invalid config option '%s' for maps\n" , + map_opt); + err = -ENOENT ; +out: +free (map_name); +return err; +} + +int bpf__config_obj (struct bpf_object *obj, +const char *key, +struct bpf_config_val *val, +struct perf_evlist *evlist) +{ +if (!obj || !key || !val) +return -ENODEV ; + +if (!prefixcmp (key,"maps." )) { +if (val->type !=BPF_CONFIG_VAL_STRING ) { +pr_debug ("ERROR: incorrect value type\n" ); +return -EINVAL ; + } +return bpf__config_obj_map (obj, key, val->string, evlist); + } +return -ENODEV ; +} +#define bpf__strerror_head (err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\
@@ -675,3 +809,16 @@ int bpf__strerror_load(struct bpf_object *obj __maybe_unused,bpf__strerror_end (buf, size);return 0 ; } + +int bpf__strerror_config_obj (struct bpf_object *obj__maybe_unused , +const char *key,struct bpf_config_val *val, +struct perf_evlist *evlist__maybe_unused , +int err,char *buf,size_t size) +{ +bpf__strerror_head (err, buf, size); + bpf__strerror_entry(ENODEV, "Invalid config option: '%s'", key) + bpf__strerror_entry(ENOENT, "Config target in '%s' is invalid", key) + bpf__strerror_entry(EINVAL, "Invalid config value %s", val) +bpf__strerror_end (buf, size); +return 0 ; +}
tools/perf/util/bpf-loader.h +39 -0
@@ -9,6 +9,7 @@#include <linux/err.h> #include <string.h> #include "probe-event.h" +#include "evlist.h" #include "debug.h" struct bpf_object ;
@@ -17,6 +18,17 @@ struct bpf_object;typedef int (*bpf_prog_iter_callback_t )(struct probe_trace_event *tev,int fd,void *arg); +struct bpf_config_val { +enum { +BPF_CONFIG_VAL_STRING , +BPF_CONFIG_VAL_NUM , + } type; +union { +const char *string; +unsigned long long num; + }; +}; +#ifdef HAVE_LIBBPF_SUPPORT struct bpf_object *bpf__prepare_load (const char *filename,bool source); struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
@@ -34,6 +46,13 @@ int bpf__strerror_load(struct bpf_object *obj, int err, char *buf, size_t size);int bpf__foreach_tev (struct bpf_object *obj,bpf_prog_iter_callback_t func,void *arg); + +int bpf__config_obj (struct bpf_object *obj,const char *key, +struct bpf_config_val *val,struct perf_evlist *evlist); +int bpf__strerror_config_obj (struct bpf_object *obj, +const char *key,struct bpf_config_val *val, +struct perf_evlist *evlist, +int err,char *buf,size_t size);#else static inline struct bpf_object * bpf__prepare_load(const char *filename __maybe_unused,
@@ -65,6 +84,15 @@ bpf__foreach_tev(struct bpf_object *obj __maybe_unused, }static inline int +bpf__config_obj (struct bpf_object *obj__maybe_unused , +const char *key__maybe_unused , +struct bpf_config_val *val__maybe_unused , +struct perf_evlist *evlist__maybe_unused ) +{ +return 0 ; +} + +static inline int __bpf_strerror (char *buf,size_t size) {if (!size)
@@ -90,5 +118,16 @@ static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused, {return __bpf_strerror (buf, size); } + +static inline int +bpf__strerror_config_obj (struct bpf_object *obj__maybe_unused , +const char *key__maybe_unused , +struct bpf_config_val *val__maybe_unused , +struct perf_evlist *evlist__maybe_unused , +int err__maybe_unused , +char *buf,size_t size) +{ +return __bpf_strerror (buf, size); +}#endif #endif
Signature · 1 line
1.8.3.4
PATCH 29/31 perf tools: Add API to apply config to BPF map
In reply to Wang Nanbpf__apply_config() is introduced as the core CPI to apply config options to all BPF objects. This patch also does the real work for setting maps events for BPF_MAP_TYPE_PERF_EVENT_ARRAY maps by inserting file descriptions of a evsel into the BPF map.
This patch is required because we are unable to set all BPF config during parsing. Events in BPF_MAP_TYPE_PERF_EVENT_ARRAY maps is an
example: during parsing, fds of events is not ready yet.
--- tools/perf/util/bpf-loader.c | 109 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 15 ++++++ 2 files changed, 124 insertions(+)
tools/perf/util/bpf-loader.c +109 -0
@@ -7,6 +7,7 @@#include <linux/bpf.h> #include <bpf/libbpf.h> +#include <bpf/bpf.h> #include <linux/err.h> #include "perf.h" #include "debug.h"
@@ -767,6 +768,107 @@ int bpf__config_obj(struct bpf_object *obj,return -ENODEV ; } +static int +bpf__apply_config_map (struct bpf_map *map) +{ +struct bpf_map_priv *priv; +struct bpf_map_def def; +const char *name; +int err, map_fd; + + name =bpf_map__get_name (map); + err =bpf_map__get_private (map, (void **)&priv); +if (err) { +pr_debug ("ERROR: failed to get private field from map %s\n" , + name); +return err; + } +if (!priv) { +pr_debug ("INFO: nothing to config for map %s\n" , name); +return 0 ; + } + + map_fd =bpf_map__get_fd (map); +if (map_fd <0 ) { +pr_debug ("ERROR: failed to get fd from map %s\n" , name); +return map_fd; + } + + err =bpf_map__get_def (map, &def); +if (err) { +pr_debug ("ERROR: failed to retrive map def from map %s\n" , + name); +return err; + } + +if (priv->evsel) { +struct xyarray *xy = priv->evsel->fd; +unsigned int cpus, i; + +if (!xy) { +pr_debug ("ERROR: event is not ready for map %s\n" , name); +return -EINVAL ; + } + +if (xy->row_size / xy->entry_size !=1 ) { +pr_debug ("ERROR: Dimension of target event is incorrect for map %s\n" , + name); +return -EINVAL ; + } + + cpus = xy->entries / (xy->row_size / xy->entry_size); +if (cpus > def.max_entries) { +pr_debug ("ERROR: map %s needs to be enlarge to %d for its event\n" , + name, cpus); +return -EINVAL ; + }else if (cpus < def.max_entries) +pr_debug ("WARNING: map %s has more entries than required\n" , + name); + +for (i =0 ; i < cpus; i++) { +int *evt_fd =xyarray__entry (xy, i,0 ); + + err =bpf_map_update_elem (map_fd, &i, evt_fd, +BPF_ANY ); + +if (err) { +pr_debug ("ERROR: failed to insert fd %d to %s[%d]\n" , + *evt_fd, name, i); +return -errno; + } + } + } +return 0 ; +} + +static int +bpf__apply_config_object (struct bpf_object *obj) +{ +struct bpf_map *map; +int err; + +bpf_map__for_each (map, obj) { + err =bpf__apply_config_map (map); +if (err) +return err; + } +return 0 ; +} + +int bpf__apply_config (void ) +{ +struct bpf_object *obj, *tmp; +int err; + +bpf_object__for_each_safe (obj, tmp) { + err =bpf__apply_config_object (obj); +if (err) +return err; + } + +return 0 ; +} +#define bpf__strerror_head (err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\
@@ -822,3 +924,10 @@ int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,bpf__strerror_end (buf, size);return 0 ; } + +int bpf__strerror_apply_config (int err,char *buf,size_t size) +{ +bpf__strerror_head (err, buf, size); +bpf__strerror_end (buf, size); +return 0 ; +}
tools/perf/util/bpf-loader.h +15 -0
@@ -53,6 +53,8 @@ int bpf__strerror_config_obj(struct bpf_object *obj, const char *key, struct bpf_config_val *val, struct perf_evlist *evlist, int err, char *buf, size_t size); +int bpf__apply_config (void ); +int bpf__strerror_apply_config (int err,char *buf,size_t size);#else static inline struct bpf_object * bpf__prepare_load(const char *filename __maybe_unused,
@@ -93,6 +95,12 @@ bpf__config_obj(struct bpf_object *obj __maybe_unused, }static inline int +bpf__apply_config (void ) +{ +return 0 ; +} + +static inline int __bpf_strerror (char *buf,size_t size) {if (!size)
@@ -129,5 +137,12 @@ bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, {return __bpf_strerror (buf, size); } + +static inline int +bpf__strerror_apply_config (int err__maybe_unused , +char *buf,size_t size) +{ +return __bpf_strerror (buf, size); +}#endif #endif
Signature · 1 line
1.8.3.4
PATCH 30/31 perf record: Apply config to BPF objects before recording
In reply to Wang NanFrom: He Kuang <[email protected]>
In perf record, before start recording, call bpf__apply_config() to turn on all BPF config options.
--- tools/perf/builtin-record.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/perf/builtin-record.c +10 -0
@@ -32,6 +32,7 @@#include "util/parse-branch-options.h" #include "util/parse-regs-options.h" #include "util/llvm-utils.h" +#include "util/bpf-loader.h" #include <unistd.h> #include <sched.h>
@@ -524,6 +525,15 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) goto out_child; } + err =bpf__apply_config (); +if (err) { +char errbuf[BUFSIZ ]; + +bpf__strerror_apply_config (err, errbuf,sizeof (errbuf)); +pr_warning ("WARNING: Apply config to BPF failed: %s\n" , + errbuf); + } +/* * Normally perf_session__new would do this, but it doesn't have the * evlist.
Signature · 1 line
1.8.3.4
PATCH 31/31 perf tools: Enable BPF object configure syntax
In reply to Wang NanThis patch adds the final step for BPF map configuration. A new syntax is appended into parser so user can config BPF objects through '/' '/' enclosed config terms.
After this patch, BPF programs for perf are finally able to utilize bpf_perf_event_read() introduced in commit 35578d7984003097af2b1e3 (bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter) by following way:
===== BPF program bpf_program.c =====
struct bpf_map_def SEC("maps") pmu_map = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u32),
.max_entries = __NR_CPUS__,
};
SEC("func_write=sys_write")
int func_write(void *ctx)
{
unsigned long long val;
char fmt[] = "sys_write: pmu=%llu\n";
val = bpf_perf_event_read(&pmu_map, bpf_get_smp_processor_id());
bpf_trace_printk(fmt, sizeof(fmt), val);
return 0;}
SEC("func_write_return=sys_write%return")
int func_write_return(void *ctx)
{
unsigned long long val = 0;
char fmt[] = "sys_write_return: pmu=%llu\n";
val = bpf_perf_event_read(&pmu_map, bpf_get_smp_processor_id());
bpf_trace_printk(fmt, sizeof(fmt), val);
return 0;}
===== cmdline =====
# echo "" > /sys/kernel/debug/tracing/trace
# perf record -e evt=cycles/period=0x7fffffffffffffff/ \
-e bpf_program.c/maps.pmu_map.event=evt/
-a ls
# cat /sys/kernel/debug/tracing/trace | grep ls
ls-3363 [003] d... 75475.056190: : sys_write: pmu=3961415
ls-3363 [003] dN.. 75475.056212: : sys_write_return: pmu=4051390
ls-3363 [003] d... 75475.056216: : sys_write: pmu=4065447
ls-3363 [003] dN.. 75475.056227: : sys_write_return: pmu=4109760
ls-3363 [003] d... 75475.056230: : sys_write: pmu=4120776
ls-3363 [003] dN.. 75475.056245: : sys_write_return: pmu=4178441
...
# perf report --stdio
Error:
The perf.data file has no samples!Where, setting period of cycles Set a very large value to period of cycles event because we want to use this event as a counter only, don't need sampling.
--- tools/perf/util/parse-events.c | 71 ++++++++++++++++++++++++++++++++++++++++-- tools/perf/util/parse-events.h | 3 +- tools/perf/util/parse-events.y | 19 ++++++++--- 3 files changed, 85 insertions(+), 8 deletions(-)
tools/perf/util/parse-events.c +68 -3
@@ -624,17 +624,79 @@ errout:return err; } +static int +parse_events_config_bpf (struct parse_events_evlist *data, +struct bpf_object *obj, +struct list_head *head_config) +{ +struct parse_events_term *term; + +if (!head_config ||list_empty (head_config)) +return 0 ; + +list_for_each_entry (term, head_config, list) { +struct bpf_config_val val; +char errbuf[BUFSIZ ]; +int err; + +if (term->type_term !=PARSE_EVENTS__TERM_TYPE_USER ) { +snprintf (errbuf,sizeof (errbuf), +"Invalid config term for BPF object" ); + errbuf[BUFSIZ -1 ] ='\0' ; + + data->error->idx = term->err_term; + data->error->str =strdup (errbuf); +return -EINVAL ; + } + +switch (term->type_val) { +case PARSE_EVENTS__TERM_TYPE_NUM : + val.type =BPF_CONFIG_VAL_NUM ; + val.num = (unsigned long long )term->val.num; +break ; +case PARSE_EVENTS__TERM_TYPE_STR : + val.type =BPF_CONFIG_VAL_STRING ; + val.string = term->val.str; +break ; +default : + data->error->idx = term->err_val; + data->error->str =strdup ("Invalid config value" ); +return -EINVAL ; + } + + err =bpf__config_obj (obj, term->config, &val, data->evlist); +if (err) { +bpf__strerror_config_obj (obj, term->config, &val, + data->evlist, err, errbuf, +sizeof (errbuf)); + data->error->help =strdup ( +"Hint:\tValid config term:\n" +" \tmaps.<mapname>.event\n" +" \t(add -v to see detail)" ); + data->error->str =strdup (errbuf); +if (err == -EINVAL ) + data->error->idx = term->err_val; +else + data->error->idx = term->err_term; +return err; + } + } +return 0 ; + +} +int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list,char *bpf_file_name, -bool source) +bool source, +struct list_head *head_config) {struct bpf_object *obj; +int err; obj =bpf__prepare_load (bpf_file_name, source);if (IS_ERR (obj) || !obj) {char errbuf[BUFSIZ ]; -int err; err = obj ?PTR_ERR (obj) : -EINVAL ;
@@ -651,7 +713,10 @@ int parse_events_load_bpf(struct parse_events_evlist *data,return err; } -return parse_events_load_bpf_obj (data, list, obj); + err =parse_events_load_bpf_obj (data, list, obj); +if (err) +return err; +return parse_events_config_bpf (data, obj, head_config); } static int
tools/perf/util/parse-events.h +2 -1
@@ -125,7 +125,8 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx,int parse_events_load_bpf (struct parse_events_evlist *data,struct list_head *list,char *bpf_file_name, -bool source); +bool source, +struct list_head *head_config);/* Provide this function for perf test */ struct bpf_object ; int parse_events_load_bpf_obj(struct parse_events_evlist *data,
tools/perf/util/parse-events.y +15 -4
@@ -64,6 +64,7 @@ static inc_group_count(struct list_head *list, %type <str> PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT %type <num> value_sym %type <head> event_config +%type <head> event_bpf_config %type <term> event_term %type <head> event_pmu %type <head> event_legacy_symbol
@@ -468,27 +469,37 @@ PE_RAW } event_bpf_file: -PE_BPF_OBJECT +PE_BPF_OBJECT event_bpf_config { struct parse_events_evlist *data = _data; struct parse_events_error *error = data->error; struct list_head *list; ALLOC_LIST(list); - ABORT_ON(parse_events_load_bpf(data, list, $1, false)); + ABORT_ON(parse_events_load_bpf(data, list, $1, false, $2)); $$ = list; } | -PE_BPF_SOURCE +PE_BPF_SOURCE event_bpf_config { struct parse_events_evlist *data = _data; struct list_head *list; ALLOC_LIST(list); - ABORT_ON(parse_events_load_bpf(data, list, $1, true)); + ABORT_ON(parse_events_load_bpf(data, list, $1, true, $2)); $$ = list; } +event_bpf_config: +'/' event_config '/' +{ + $$ = $2; +} +| +{ + $$ = NULL; +} + start_terms: event_config { struct parse_events_terms *data = _data;
Signature · 1 line
1.8.3.4
Hello,
Quoted 11 lines · Wang Nan
Hi Arnaldo,
I know you don't have enough time to review my code. I send
this patchset to let you and other know what we are working on.In this new patchset, we create a new perf cmdline syntax so
perf users are able to pass perf events created by perf to BPF maps, which makesbpf_perf_event_read()usable. Compare with out previous solution[1] which embedded 'structperf_event_attr' to "maps" section, this solution is easier to use.
Quoted 3 lines
If you or anyone have any different views on this solution,
please let us know so we can stop our further development base on it as soon as possible.
Thanks for working on this. I've only reviewed the first part and will continue to review it tomorrow (or later). :)
For patch 01 - 08,
Thanks,
Namhyung
Quoted 173 lines
Thank you.
[1] http://lkml.kernel.org/r/[email protected]
The following changes since commit
31eb4360546b4bd890f349db01295a173c09b0fb:perf hists browser: Add 'm' key for context menu display (2015-10-12 23:29:14 -0300)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pi3orama/linux.gittags/perf-ebpf-for-acme-20151014for you to fetch changes up to
6df036cb7d42a2e0ebf312e127b02425bd57bc55:perf tools: Enable BPF object configure syntax (2015-10-14 10:09:17 +0000)
----------------------------------------------------------------
EBPF support for perf- Rebase to newest perf/core
- Bugfix: kprobe events not remove if
bpf__probe()failure occur after it creates some kprobe points successfully.- Bugfix: when multiple BPF functions reside in one BPF object, the last BPF program would be attached to all kprobe events. This bug is introduced by removal of dummy event placeholder.
- New function: support BPF program reading counter through
bpf_perf_event_read()by adding new syntax and support code in event selector. The new BPF object configuration mechanism can be extended to support BPF data output.In this patchset, following BPF function can be used to measure cycles a kernel function cost: ===== BPF program bpf_program.c ===== struct bpf_map_def SEC("maps") pmu_map = { .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, .key_size = sizeof(int), .value_size = sizeof(u32), .max_entries = __NR_CPUS__, }; SEC("func_write=sys_write") int func_write(void *ctx) { unsigned long long val; char fmt[] = "sys_write: pmu=%llu\n"; val = bpf_perf_event_read(&pmu_map, bpf_get_smp_processor_id()); bpf_trace_printk(fmt, sizeof(fmt), val); return 0; } SEC("func_write_return=sys_write%return") int func_write_return(void *ctx) { unsigned long long val = 0; char fmt[] = "sys_write_return: pmu=%llu\n"; val = bpf_perf_event_read(&pmu_map, bpf_get_smp_processor_id()); bpf_trace_printk(fmt, sizeof(fmt), val); return 0; } With cmdline like this: ===== cmdline ===== # echo "" > /sys/kernel/debug/tracing/trace # perf record -e evt=cycles/period=0x7fffffffffffffff/ \ -e bpf_program.c/maps.pmu_map.event=evt/ -a ls # cat /sys/kernel/debug/tracing/trace | grep ls ls-3363 [003] d... 75475.056190: : sys_write: pmu=3961415 ls-3363 [003] dN.. 75475.056212: : sys_write_return: pmu=4051390 ls-3363 [003] d... 75475.056216: : sys_write: pmu=4065447 ls-3363 [003] dN.. 75475.056227: : sys_write_return: pmu=4109760 ls-3363 [003] d... 75475.056230: : sys_write: pmu=4120776 ls-3363 [003] dN.. 75475.056245: : sys_write_return: pmu=4178441 ... # perf report --stdio Error: The perf.data file has no samples!Signed-off-by Wang Nan <[email protected]>----------------------------------------------------------------
He Kuang (5):
perf tools: Add prologue for BPF programs for fetching arguments perf record: Support custom vmlinux path
bpf tools: Add helper function for updating bpf maps elements perf tools: Support perf event alias name
perf record: Apply config to BPF objects before recordingWang Nan (26):
perf tools: Make perf depend on libbpf
perf ebpf: Add the libbpf glue
perf tools: Enable passing bpf object file to --event
perf record, bpf: Create probe points for BPF programs
perf record: Load eBPF object into kernel
perf tools: Collectperf_evselin BPF object files
perf tools: Attach eBPF program to perf event
perf record: Add clang options for compiling BPF scripts
perf tools: Compile scriptlets to BPF objects when passing '.c' to --event perf test: Enforce LLVM test for BPF test
perf test: Add 'perf test BPF'
perf probe: Reset args and nargs forprobe_trace_eventwhen failure bpf tools: Load a program with different instances using preprocessor perf tools: AddBPF_PROLOGUEconfig options for further patches perf tools: Compiledwarf-regs.cifCONFIG_BPF_PROLOGUEis on perf tools: Generate prologue for BPF programs
perf tools: Use same BPF program if arguments are identical perf tools: Allow BPF program attach to uprobe events
perf test: Enforce LLVM test, add kbuild test
perf test: Test BPF prologue
bpf tools: Collect map definition inbpf_object
bpf tools: Extract and collect map names from BPF object file perf tools: Pass available CPU number to clang compiler
perf tools: Add API to config maps in bpf object
perf tools: Add API to apply config to BPF map
perf tools: Enable BPF object configure syntaxtools/build/Makefile.feature | 6 +- tools/lib/bpf/bpf.c | 14 + tools/lib/bpf/bpf.h | 2 + tools/lib/bpf/libbpf.c | 392 +++++++++--- tools/lib/bpf/libbpf.h | 46 ++ tools/perf/MANIFEST | 3 + tools/perf/Makefile.perf | 21 +- tools/perf/arch/x86/util/Build | 1 + tools/perf/builtin-record.c | 21 + tools/perf/config/Makefile | 31 +- tools/perf/perf.c | 2 + tools/perf/tests/Build | 24 +- tools/perf/tests/bpf-script-example.c | 48 ++ tools/perf/tests/bpf-script-test-kbuild.c | 21 + tools/perf/tests/bpf-script-test-prologue.c | 35 ++ tools/perf/tests/bpf.c | 227 +++++++ tools/perf/tests/builtin-test.c | 10 + tools/perf/tests/llvm.c | 210 ++++++- tools/perf/tests/llvm.h | 29 + tools/perf/tests/make | 4 +- tools/perf/tests/tests.h | 5 + tools/perf/util/Build | 2 + tools/perf/util/bpf-loader.c | 933 ++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 148 +++++ tools/perf/util/bpf-prologue.c | 443 +++++++++++++ tools/perf/util/bpf-prologue.h | 34 + tools/perf/util/evlist.c | 16 + tools/perf/util/evlist.h | 4 + tools/perf/util/evsel.c | 17 + tools/perf/util/evsel.h | 2 + tools/perf/util/llvm-utils.c | 24 +- tools/perf/util/parse-events.c | 221 ++++++- tools/perf/util/parse-events.h | 15 + tools/perf/util/parse-events.l | 6 + tools/perf/util/parse-events.y | 55 +- tools/perf/util/probe-finder.c | 4 + 36 files changed, 2968 insertions(+), 108 deletions(-) create mode 100644 tools/perf/tests/bpf-script-example.c create mode 100644 tools/perf/tests/bpf-script-test-kbuild.c create mode 100644 tools/perf/tests/bpf-script-test-prologue.c create mode 100644 tools/perf/tests/bpf.c create mode 100644 tools/perf/tests/llvm.h create mode 100644 tools/perf/util/bpf-loader.c create mode 100644 tools/perf/util/bpf-loader.h create mode 100644 tools/perf/util/bpf-prologue.c create mode 100644 tools/perf/util/bpf-prologue.hSignature · 1 line
1.8.3.4