Skip to contentSkip to conversation

Introduce perf-stat -b for BPF programs

RFC Superseded Song Liu 3 participants 23 messages Nov 19, 2020 – Nov 25, 2020 perf events

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Song Liu

author
#0 lore raw

cover Introduce perf-stat -b for BPF programs

This set introduces perf-stat -b option to count events for BPF programs. This is similar to bpftool-prog-profile. But perf-stat makes it much more flexible.

Sending as RFC because I would like comments on some key design choices:
1. We are using BPF skeletons here, which is by far the easiest way to write and ship BPF programs. However, this requires bpftool, which makes building perf slower.
2. A Makefile is added to tools/perf/util/bpf_skel/ to build bpftool, and BPF skeletons. This keeps main perf Makefiles simple. But we may not like it for some reason?

Some known limitations (or work to be done):
1. Only counting events for one BPF program at a time.
2. Need extra logic in target__validate().

Song Liu (2):
perf: support build BPF skeletons with perf
perf-stat: enable counting events for BPF programs

 tools/build/Makefile.feature                  |   3 +-
 tools/perf/Makefile.config                    |   8 +
 tools/perf/Makefile.perf                      |  15 +-
 tools/perf/builtin-stat.c                     |  59 ++++-
 tools/perf/util/Build                         |   1 +
 tools/perf/util/bpf_counter.c                 | 215 ++++++++++++++++++
 tools/perf/util/bpf_counter.h                 |  71 ++++++
 tools/perf/util/bpf_skel/.gitignore           |   3 +
 tools/perf/util/bpf_skel/Makefile             |  71 ++++++
 .../util/bpf_skel/bpf_prog_profiler.bpf.c     |  96 ++++++++
 tools/perf/util/bpf_skel/dummy.bpf.c          |  19 ++
 tools/perf/util/evsel.c                       |  10 +
 tools/perf/util/evsel.h                       |   5 +
 tools/perf/util/target.h                      |   6 +
 14 files changed, 571 insertions(+), 11 deletions(-)
 create mode 100644 tools/perf/util/bpf_counter.c
 create mode 100644 tools/perf/util/bpf_counter.h
 create mode 100644 tools/perf/util/bpf_skel/.gitignore
 create mode 100644 tools/perf/util/bpf_skel/Makefile
 create mode 100644 tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c
 create mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c

--
2.24.1

author
#1 lore raw

RFC 1/2 perf: support build BPF skeletons with perf

BPF programs are useful in perf to profile BPF programs. BPF skeleton is by far the easiest way to write BPF tools. Enable building BPF skeletons in util/bpf_skel. A dummy bpf skeleton is added. More bpf skeletons will be added for different use cases.

---
 tools/build/Makefile.feature         |  3 +-
 tools/perf/Makefile.config           |  7 +++
 tools/perf/Makefile.perf             | 15 +++++-
 tools/perf/util/bpf_skel/.gitignore  |  3 ++
 tools/perf/util/bpf_skel/Makefile    | 71 ++++++++++++++++++++++++++++
 tools/perf/util/bpf_skel/dummy.bpf.c | 19 ++++++++
 6 files changed, 115 insertions(+), 3 deletions(-)
 create mode 100644 tools/perf/util/bpf_skel/.gitignore
 create mode 100644 tools/perf/util/bpf_skel/Makefile
 create mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c
tools/build/Makefile.feature +2 -1
index 97cbfb31b7625..95a58b5564218 100644
@@ -70,7 +70,8 @@ FEATURE_TESTS_BASIC :=                  \
         libaio				\
         libzstd				\
         disassembler-four-args		\
-        file-handle
+        file-handle			\
+        clang-bpf-co-re
 
 # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
 # of all feature tests
tools/perf/Makefile.config +7 -0
index ce8516e4de34f..1c2c0f4badd85 100644
@@ -619,6 +619,13 @@ ifndef NO_LIBBPF
     msg := $(warning BPF API too old. Please install recent kernel headers. BPF support in 'perf record' is disabled.)
     NO_LIBBPF := 1
   endif
+
+  ifndef NO_BPF_SKEL
+    ifeq ($(feature-clang-bpf-co-re), 1)
+      BUILD_BPF_SKEL := 1
+      $(call detected,CONFIG_PERF_BPF_SKEL)
+    endif
+  endif
 endif
 
 dwarf-post-unwind := 1
tools/perf/Makefile.perf +13 -2
index 7ce3f2e8b9c74..9a9fc71e2ffa4 100644
@@ -126,6 +126,8 @@ include ../scripts/utilities.mak
 #
 # Define NO_LIBDEBUGINFOD if you do not want support debuginfod
 #
+# Define NO_BPF_SKEL if you do not want build BPF skeletons
+#
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
@@ -735,7 +737,8 @@ prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioc
 	$(x86_arch_prctl_code_array) \
 	$(rename_flags_array) \
 	$(arch_errno_name_array) \
-	$(sync_file_range_arrays)
+	$(sync_file_range_arrays) \
+	bpf-skel
 
 $(OUTPUT)%.o: %.c prepare FORCE
 	$(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
@@ -1008,7 +1011,15 @@ config-clean:
 python-clean:
 	$(python-clean)
 
+ifdef BUILD_BPF_SKEL
+bpf-skel:
+	$(Q)$(MAKE) -C util/bpf_skel
+else
+bpf-skel:
+endif
+
 clean:: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean $(LIBPERF)-clean config-clean fixdep-clean python-clean
+	$(call descend,util/bpf_skel,clean)
 	$(call QUIET_CLEAN, core-objs)  $(RM) $(LIBPERF_A) $(OUTPUT)perf-archive $(OUTPUT)perf-with-kcore $(LANG_BINDINGS)
 	$(Q)find $(if $(OUTPUT),$(OUTPUT),.) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
 	$(Q)$(RM) $(OUTPUT).config-detected
@@ -1071,7 +1082,7 @@ endif
 
 FORCE:
 
-.PHONY: all install clean config-clean strip install-gtk
+.PHONY: all install clean config-clean strip install-gtk bpf-skel
 .PHONY: shell_compatibility_test please_set_SHELL_PATH_to_a_more_modern_shell
 .PHONY: $(GIT-HEAD-PHONY) TAGS tags cscope FORCE prepare
 .PHONY: libtraceevent_plugins archheaders
tools/perf/util/bpf_skel/.gitignore new file +3 -0
new file mode 100644
index 0000000000000..5263e9e6c5d83
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+.tmp
+*.skel.h
\ No newline at end of file
tools/perf/util/bpf_skel/Makefile new file +71 -0

71 lines not on this page · raw message

tools/perf/util/bpf_skel/dummy.bpf.c new file +19 -0
new file mode 100644
index 0000000000000..085fcee1f52cf
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+// Copyright (c) 2020 Facebook
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(key_size, sizeof(u32));
+	__uint(value_size, sizeof(u64));
+} counts SEC(".maps");
+
+SEC("fentry/dummy")
+int BPF_PROG(fentry_dummy)
+{
+	return 0;
+}
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
Signature · 1 line
2.24.1
#2 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

Quoted 25 lines

# FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list

 # of all feature tests
tools/perf/Makefile.config +7 -0
index ce8516e4de34f..1c2c0f4badd85 100644
@@ -619,6 +619,13 @@ ifndef NO_LIBBPF
     msg := $(warning BPF API too old. Please install recent kernel headers. BPF support in 'perf record' is disabled.)
     NO_LIBBPF := 1
   endif
+
+  ifndef NO_BPF_SKEL
+    ifeq ($(feature-clang-bpf-co-re), 1)
+      BUILD_BPF_SKEL := 1
+      $(call detected,CONFIG_PERF_BPF_SKEL)
+    endif
+  endif
 endif
 
 dwarf-post-unwind := 1
tools/perf/Makefile.perf +0 -0
index 7ce3f2e8b9c74..9a9fc71e2ffa4 100644

tools/perf/Makefile.perf include ../scripts/utilities.mak · line 129

Quoted 4 lines
tools/perf/Makefile.perf +1 -0
@@ -126,6 +126,8 @@ include ../scripts/utilities.mak
 #
 # Define NO_LIBDEBUGINFOD if you do not want support debuginfod
 #
+# Define NO_BPF_SKEL if you do not want build BPF skeletons

we will need to make this the other way round and disable it by default before we figure out al lthe dependencies, my build's failing on:

[jolsa@krava perf]$ make JOBS=1
BUILD: Doing 'make -j1' parallel build
CC /home/jolsa/kernel/linux-perf/tools/perf/util/bpf_skel/.tmp/prog.o
CLANG /home/jolsa/kernel/linux-perf/tools/perf/util/bpf_skel/.tmp/pid_iter.bpf.o
In file included from skeleton/pid_iter.bpf.c:4:
In file included from /home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_helpers.h:11:
/home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_helper_defs.h:3560:57: warning: declaration of 'struct bpf_redir_neigh' will not be visible outside of this function [-Wvisibility] static long (*bpf_redirect_neigh)(__u32 ifindex, struct bpf_redir_neigh *params, int plen, __u64 flags) = (void *) 152;
^
skeleton/pid_iter.bpf.c:35:48: error: no member named 'id' in 'struct bpf_link'
return BPF_CORE_READ((struct bpf_link *)ent, id);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_core_read.h:339:18: note: expanded from macro 'BPF_CORE_READ'
___type((src), a, ##__VA_ARGS__) __r; \
...

jirka

author
#3 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

Quoted 42 lines · Jiri Olsa

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

Quoted 25 lines

# FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list

# of all feature tests
tools/perf/Makefile.config +7 -0
index ce8516e4de34f..1c2c0f4badd85 100644
@@ -619,6 +619,13 @@ ifndef NO_LIBBPF
    msg := $(warning BPF API too old. Please install recent kernel headers. BPF support in 'perf record' is disabled.)
    NO_LIBBPF := 1
  endif
+
+  ifndef NO_BPF_SKEL
+    ifeq ($(feature-clang-bpf-co-re), 1)
+      BUILD_BPF_SKEL := 1
+      $(call detected,CONFIG_PERF_BPF_SKEL)
+    endif
+  endif

endif

dwarf-post-unwind := 1

tools/perf/Makefile.perf +0 -0
index 7ce3f2e8b9c74..9a9fc71e2ffa4 100644

tools/perf/Makefile.perf include ../scripts/utilities.mak · line 129

Quoted 4 lines
tools/perf/Makefile.perf +1 -0
@@ -126,6 +126,8 @@ include ../scripts/utilities.mak
 #
 # Define NO_LIBDEBUGINFOD if you do not want support debuginfod
 #
+# Define NO_BPF_SKEL if you do not want build BPF skeletons

we will need to make this the other way round and disable it by default before we figure out al lthe dependencies, my build's failing on:

Quoted 15 lines

[jolsa@krava perf]$ make JOBS=1
BUILD: Doing 'make -j1' parallel build
CC /home/jolsa/kernel/linux-perf/tools/perf/util/bpf_skel/.tmp/prog.o
CLANG /home/jolsa/kernel/linux-perf/tools/perf/util/bpf_skel/.tmp/pid_iter.bpf.o
In file included from skeleton/pid_iter.bpf.c:4:
In file included from /home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_helpers.h:11:
/home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_helper_defs.h:3560:57: warning: declaration of 'struct bpf_redir_neigh' will not be visible outside of this function [-Wvisibility] static long (*bpf_redirect_neigh)(__u32 ifindex, struct bpf_redir_neigh *params, int plen, __u64 flags) = (void *) 152;
^
skeleton/pid_iter.bpf.c:35:48: error: no member named 'id' in 'struct bpf_link'
return BPF_CORE_READ((struct bpf_link *)ent, id);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_core_read.h:339:18: note: expanded from macro 'BPF_CORE_READ'
___type((src), a, ##__VA_ARGS__) __r; \
...

I guess this was caused by older vmlinux.h. Could you please try build vmlinux first?

Thanks,
Song

#4 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

Quoted 58 lines · Song Liu
Quoted 42 lines · Jiri Olsa

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

Quoted 25 lines

# FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list

# of all feature tests
tools/perf/Makefile.config +7 -0
index ce8516e4de34f..1c2c0f4badd85 100644
@@ -619,6 +619,13 @@ ifndef NO_LIBBPF
    msg := $(warning BPF API too old. Please install recent kernel headers. BPF support in 'perf record' is disabled.)
    NO_LIBBPF := 1
  endif
+
+  ifndef NO_BPF_SKEL
+    ifeq ($(feature-clang-bpf-co-re), 1)
+      BUILD_BPF_SKEL := 1
+      $(call detected,CONFIG_PERF_BPF_SKEL)
+    endif
+  endif

endif

dwarf-post-unwind := 1

tools/perf/Makefile.perf +0 -0
index 7ce3f2e8b9c74..9a9fc71e2ffa4 100644

tools/perf/Makefile.perf include ../scripts/utilities.mak · line 129

Quoted 4 lines
tools/perf/Makefile.perf +1 -0
@@ -126,6 +126,8 @@ include ../scripts/utilities.mak
 #
 # Define NO_LIBDEBUGINFOD if you do not want support debuginfod
 #
+# Define NO_BPF_SKEL if you do not want build BPF skeletons

we will need to make this the other way round and disable it by default before we figure out al lthe dependencies, my build's failing on:

Quoted 15 lines

[jolsa@krava perf]$ make JOBS=1
BUILD: Doing 'make -j1' parallel build
CC /home/jolsa/kernel/linux-perf/tools/perf/util/bpf_skel/.tmp/prog.o
CLANG /home/jolsa/kernel/linux-perf/tools/perf/util/bpf_skel/.tmp/pid_iter.bpf.o
In file included from skeleton/pid_iter.bpf.c:4:
In file included from /home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_helpers.h:11:
/home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_helper_defs.h:3560:57: warning: declaration of 'struct bpf_redir_neigh' will not be visible outside of this function [-Wvisibility] static long (*bpf_redirect_neigh)(__u32 ifindex, struct bpf_redir_neigh *params, int plen, __u64 flags) = (void *) 152;
^
skeleton/pid_iter.bpf.c:35:48: error: no member named 'id' in 'struct bpf_link'
return BPF_CORE_READ((struct bpf_link *)ent, id);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/home/jolsa/kernel/linux-perf/tools/lib/bpf/bpf_core_read.h:339:18: note: expanded from macro 'BPF_CORE_READ'
___type((src), a, ##__VA_ARGS__) __r; \
...

Quoted 2 lines

I guess this was caused by older vmlinux.h. Could you please try build vmlinux first?

yes, that helped, but I think that's the one of the reasons we need to make this to be built in conditionaly

thanks,
jirka

#5 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

In reply to Song Liu

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

Quoted 35 lines
tools/perf/util/bpf_skel/.gitignore new file +3 -0
new file mode 100644
index 0000000000000..5263e9e6c5d83
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+.tmp
+*.skel.h
\ No newline at end of file
tools/perf/util/bpf_skel/Makefile new file +19 -0
new file mode 100644
index 0000000000000..853bece088f4b
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+SKEL_OUTPUT := $(abspath .)
+TMP_OUTPUT := $(abspath .tmp)
+CLANG ?= clang
+LLC ?= llc
+LLVM_STRIP ?= llvm-strip
+DEFAULT_BPFTOOL := $(TMP_OUTPUT)/sbin/bpftool
+BPFTOOL ?= $(DEFAULT_BPFTOOL)
+LIBBPF_SRC := $(abspath ../../../lib/bpf)
+BPFOBJ := $(TMP_OUTPUT)/libbpf.a
+BPF_INCLUDE := $(TMP_OUTPUT)
+INCLUDES := -I$(TMP_OUTPUT) -I$(BPF_INCLUDE) -I$(abspath ../../../lib)        \
+       -I$(abspath ../../../include/uapi)
+CFLAGS := -g -Wall
+
+# Try to detect best kernel BTF source
+KERNEL_REL := $(shell uname -r)
+VMLINUX_BTF_PATHS := /sys/kernel/btf/vmlinux /boot/vmlinux-$(KERNEL_REL)
+VMLINUX_BTF_PATH := $(or $(VMLINUX_BTF),$(firstword			       \

tools/perf/util/bpf_skel/Makefile line 29

Quoted 10 lines
tools/perf/util/bpf_skel/Makefile new file +10 -0
+					  $(wildcard $(VMLINUX_BTF_PATHS))))
+ifeq ($(V),1)
+Q =
+msg =
+else
+Q = @
+msg = @printf '  %-8s %s%s\n' "$(1)" "$(notdir $(2))" "$(if $(3), $(3))";
+MAKEFLAGS += --no-print-directory
+submake_extras := feature_display=0
+endif

some of the above should already be defined in the base Makefile.perf we should use that, I think it'd fit better in the Makefile.perf itself

jirka

author
#6 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

Quoted 48 lines · Jiri Olsa

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

Quoted 40 lines
tools/perf/util/bpf_skel/.gitignore new file +3 -0
new file mode 100644
index 0000000000000..5263e9e6c5d83
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+.tmp
+*.skel.h
\ No newline at end of file
tools/perf/util/bpf_skel/Makefile new file +24 -0
new file mode 100644
index 0000000000000..853bece088f4b
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+SKEL_OUTPUT := $(abspath .)
+TMP_OUTPUT := $(abspath .tmp)
+CLANG ?= clang
+LLC ?= llc
+LLVM_STRIP ?= llvm-strip
+DEFAULT_BPFTOOL := $(TMP_OUTPUT)/sbin/bpftool
+BPFTOOL ?= $(DEFAULT_BPFTOOL)
+LIBBPF_SRC := $(abspath ../../../lib/bpf)
+BPFOBJ := $(TMP_OUTPUT)/libbpf.a
+BPF_INCLUDE := $(TMP_OUTPUT)
+INCLUDES := -I$(TMP_OUTPUT) -I$(BPF_INCLUDE) -I$(abspath ../../../lib)        \
+       -I$(abspath ../../../include/uapi)
+CFLAGS := -g -Wall
+
+# Try to detect best kernel BTF source
+KERNEL_REL := $(shell uname -r)
+VMLINUX_BTF_PATHS := /sys/kernel/btf/vmlinux /boot/vmlinux-$(KERNEL_REL)
+VMLINUX_BTF_PATH := $(or $(VMLINUX_BTF),$(firstword			       \
+					  $(wildcard $(VMLINUX_BTF_PATHS))))
+ifeq ($(V),1)
+Q =
+msg =
+else
Quoted 8 lines

tools/perf/util/bpf_skel/Makefile line 29

Quoted 5 lines
tools/perf/util/bpf_skel/Makefile new file +5 -0
+Q = @
+msg = @printf '  %-8s %s%s\n' "$(1)" "$(notdir $(2))" "$(if $(3), $(3))";
+MAKEFLAGS += --no-print-directory
+submake_extras := feature_display=0
+endif

some of the above should already be defined in the base Makefile.perf we should use that, I think it'd fit better in the Makefile.perf itself

Let me try that. OTOH, I think Makefile.perf is a little too big (1k+ lines)..

Thanks,
Song

#7 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

In reply to Song Liu
Quoted 4 lines · Song Liu

BPF programs are useful in perf to profile BPF programs. BPF skeleton is by far the easiest way to write BPF tools. Enable building BPF skeletons in util/bpf_skel. A dummy bpf skeleton is added. More bpf skeletons will be added for different use cases.

I was just in a place adding bpf program to perf as well, so this will save me some time ;-) thanks!

jirka

Quoted 209 lines
---
 tools/build/Makefile.feature         |  3 +-
 tools/perf/Makefile.config           |  7 +++
 tools/perf/Makefile.perf             | 15 +++++-
 tools/perf/util/bpf_skel/.gitignore  |  3 ++
 tools/perf/util/bpf_skel/Makefile    | 71 ++++++++++++++++++++++++++++
 tools/perf/util/bpf_skel/dummy.bpf.c | 19 ++++++++
 6 files changed, 115 insertions(+), 3 deletions(-)
 create mode 100644 tools/perf/util/bpf_skel/.gitignore
 create mode 100644 tools/perf/util/bpf_skel/Makefile
 create mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c
tools/build/Makefile.feature +2 -1
index 97cbfb31b7625..95a58b5564218 100644
@@ -70,7 +70,8 @@ FEATURE_TESTS_BASIC :=                  \
         libaio				\
         libzstd				\
         disassembler-four-args		\
-        file-handle
+        file-handle			\
+        clang-bpf-co-re
 
 # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
 # of all feature tests
tools/perf/Makefile.config +7 -0
index ce8516e4de34f..1c2c0f4badd85 100644
@@ -619,6 +619,13 @@ ifndef NO_LIBBPF
     msg := $(warning BPF API too old. Please install recent kernel headers. BPF support in 'perf record' is disabled.)
     NO_LIBBPF := 1
   endif
+
+  ifndef NO_BPF_SKEL
+    ifeq ($(feature-clang-bpf-co-re), 1)
+      BUILD_BPF_SKEL := 1
+      $(call detected,CONFIG_PERF_BPF_SKEL)
+    endif
+  endif
 endif
 
 dwarf-post-unwind := 1
tools/perf/Makefile.perf +13 -2
index 7ce3f2e8b9c74..9a9fc71e2ffa4 100644
@@ -126,6 +126,8 @@ include ../scripts/utilities.mak
 #
 # Define NO_LIBDEBUGINFOD if you do not want support debuginfod
 #
+# Define NO_BPF_SKEL if you do not want build BPF skeletons
+#
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
@@ -735,7 +737,8 @@ prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioc
 	$(x86_arch_prctl_code_array) \
 	$(rename_flags_array) \
 	$(arch_errno_name_array) \
-	$(sync_file_range_arrays)
+	$(sync_file_range_arrays) \
+	bpf-skel
 
 $(OUTPUT)%.o: %.c prepare FORCE
 	$(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
@@ -1008,7 +1011,15 @@ config-clean:
 python-clean:
 	$(python-clean)
 
+ifdef BUILD_BPF_SKEL
+bpf-skel:
+	$(Q)$(MAKE) -C util/bpf_skel
+else
+bpf-skel:
+endif
+
 clean:: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean $(LIBPERF)-clean config-clean fixdep-clean python-clean
+	$(call descend,util/bpf_skel,clean)
 	$(call QUIET_CLEAN, core-objs)  $(RM) $(LIBPERF_A) $(OUTPUT)perf-archive $(OUTPUT)perf-with-kcore $(LANG_BINDINGS)
 	$(Q)find $(if $(OUTPUT),$(OUTPUT),.) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
 	$(Q)$(RM) $(OUTPUT).config-detected
@@ -1071,7 +1082,7 @@ endif
 
 FORCE:
 
-.PHONY: all install clean config-clean strip install-gtk
+.PHONY: all install clean config-clean strip install-gtk bpf-skel
 .PHONY: shell_compatibility_test please_set_SHELL_PATH_to_a_more_modern_shell
 .PHONY: $(GIT-HEAD-PHONY) TAGS tags cscope FORCE prepare
 .PHONY: libtraceevent_plugins archheaders
tools/perf/util/bpf_skel/.gitignore new file +3 -0
new file mode 100644
index 0000000000000..5263e9e6c5d83
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+.tmp
+*.skel.h
\ No newline at end of file
tools/perf/util/bpf_skel/Makefile new file +71 -0
new file mode 100644
index 0000000000000..853bece088f4b
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+SKEL_OUTPUT := $(abspath .)
+TMP_OUTPUT := $(abspath .tmp)
+CLANG ?= clang
+LLC ?= llc
+LLVM_STRIP ?= llvm-strip
+DEFAULT_BPFTOOL := $(TMP_OUTPUT)/sbin/bpftool
+BPFTOOL ?= $(DEFAULT_BPFTOOL)
+LIBBPF_SRC := $(abspath ../../../lib/bpf)
+BPFOBJ := $(TMP_OUTPUT)/libbpf.a
+BPF_INCLUDE := $(TMP_OUTPUT)
+INCLUDES := -I$(TMP_OUTPUT) -I$(BPF_INCLUDE) -I$(abspath ../../../lib)        \
+       -I$(abspath ../../../include/uapi)
+CFLAGS := -g -Wall
+
+# Try to detect best kernel BTF source
+KERNEL_REL := $(shell uname -r)
+VMLINUX_BTF_PATHS := /sys/kernel/btf/vmlinux /boot/vmlinux-$(KERNEL_REL)
+VMLINUX_BTF_PATH := $(or $(VMLINUX_BTF),$(firstword			       \
+					  $(wildcard $(VMLINUX_BTF_PATHS))))
+ifeq ($(V),1)
+Q =
+msg =
+else
+Q = @
+msg = @printf '  %-8s %s%s\n' "$(1)" "$(notdir $(2))" "$(if $(3), $(3))";
+MAKEFLAGS += --no-print-directory
+submake_extras := feature_display=0
+endif
+
+SKELETONS := $(SKEL_OUTPUT)/dummy.skel.h
+
+.DELETE_ON_ERROR:
+
+.PHONY: all clean
+all: $(SKELETONS)
+
+clean:
+	$(call msg,CLEAN,bpf_skel)
+	$(Q)rm -rf $(TMP_OUTPUT) $(SKELETONS)
+
+$(SKEL_OUTPUT)/%.skel.h: $(TMP_OUTPUT)/%.bpf.o | $(BPFTOOL)
+	$(call msg,GEN-SKEL,$@)
+	$(Q)$(BPFTOOL) gen skeleton $< > $@
+
+$(TMP_OUTPUT)/%.bpf.o: %.bpf.c $(TMP_OUTPUT)/vmlinux.h $(BPFOBJ) | $(TMP_OUTPUT)
+	$(call msg,BPF,$@)
+	$(Q)$(CLANG) -g -O2 -target bpf $(INCLUDES)			      \
+		 -c $(filter %.c,$^) -o $@ &&				      \
+	$(LLVM_STRIP) -g $@
+
+$(TMP_OUTPUT):
+	$(call msg,MKDIR,$@)
+	$(Q)mkdir -p $(TMP_OUTPUT)
+
+$(TMP_OUTPUT)/vmlinux.h: $(VMLINUX_BTF_PATH) | $(TMP_OUTPUT) $(BPFTOOL)
+	$(call msg,GEN,$@)
+	$(Q)if [ ! -e "$(VMLINUX_BTF_PATH)" ] ; then \
+		echo "Couldn't find kernel BTF; set VMLINUX_BTF to"	       \
+			"specify its location." >&2;			       \
+		exit 1;\
+	fi
+	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF_PATH) format c > $@
+
+$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(TMP_OUTPUT)
+	$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC)			       \
+		    OUTPUT=$(abspath $(dir $@))/ $(abspath $@)
+
+$(DEFAULT_BPFTOOL): | $(TMP_OUTPUT)
+	$(Q)$(MAKE) $(submake_extras) -C ../../../bpf/bpftool		      \
+		    prefix= OUTPUT=$(TMP_OUTPUT)/ DESTDIR=$(TMP_OUTPUT) install
tools/perf/util/bpf_skel/dummy.bpf.c new file +19 -0
new file mode 100644
index 0000000000000..085fcee1f52cf
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+// Copyright (c) 2020 Facebook
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(key_size, sizeof(u32));
+	__uint(value_size, sizeof(u64));
+} counts SEC(".maps");
+
+SEC("fentry/dummy")
+int BPF_PROG(fentry_dummy)
+{
+	return 0;
+}
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
Signature · 1 line
2.24.1
author
#8 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

Quoted 12 lines · Jiri Olsa
Quoted 4 lines · Song Liu

BPF programs are useful in perf to profile BPF programs. BPF skeleton is by far the easiest way to write BPF tools. Enable building BPF skeletons in util/bpf_skel. A dummy bpf skeleton is added. More bpf skeletons will be added for different use cases.

I was just in a place adding bpf program to perf as well, so this will save me some time ;-) thanks!

I'd love to learn about your plan. Maybe we have some similar ideas, and could collaborate on them.

Thanks,
Song

#9 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

Quoted 10 lines · Song Liu
Quoted 10 lines · Jiri Olsa
Quoted 4 lines · Song Liu

BPF programs are useful in perf to profile BPF programs. BPF skeleton is by far the easiest way to write BPF tools. Enable building BPF skeletons in util/bpf_skel. A dummy bpf skeleton is added. More bpf skeletons will be added for different use cases.

Quoted 5 lines
Quoted 2 lines

I was just in a place adding bpf program to perf as well, so this will save me some time ;-) thanks!

I'd love to learn about your plan. Maybe we have some similar ideas, and could collaborate on them.

the plan was to use skeleton as you did, because I agree it's the best way to include bpf program in perf

I'm now using your patch and adding my bpf program on top of that ;-)

thanks,
jirka

#10 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

In reply to Song Liu

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

Quoted 11 lines
tools/perf/util/bpf_skel/Makefile new file +11 -0
+
+$(TMP_OUTPUT):
+	$(call msg,MKDIR,$@)
+	$(Q)mkdir -p $(TMP_OUTPUT)
+
+$(TMP_OUTPUT)/vmlinux.h: $(VMLINUX_BTF_PATH) | $(TMP_OUTPUT) $(BPFTOOL)
+	$(call msg,GEN,$@)
+	$(Q)if [ ! -e "$(VMLINUX_BTF_PATH)" ] ; then \
+		echo "Couldn't find kernel BTF; set VMLINUX_BTF to"	       \
+			"specify its location." >&2;			       \
+		exit 1;\

tools/perf/util/bpf_skel/Makefile line 71

Quoted 10 lines
tools/perf/util/bpf_skel/Makefile new file +10 -0
+	fi
+	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF_PATH) format c > $@
+
+$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(TMP_OUTPUT)
+	$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC)			       \
+		    OUTPUT=$(abspath $(dir $@))/ $(abspath $@)
+
+$(DEFAULT_BPFTOOL): | $(TMP_OUTPUT)
+	$(Q)$(MAKE) $(submake_extras) -C ../../../bpf/bpftool		      \
+		    prefix= OUTPUT=$(TMP_OUTPUT)/ DESTDIR=$(TMP_OUTPUT) install

could we build just the bootstrap version of bpftool? should be enough for skeleton and vmlinux.h dump, no?

thanks,
jirka

author
#11 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

Quoted 24 lines · Jiri Olsa

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

Quoted 16 lines
tools/perf/util/bpf_skel/Makefile new file +16 -0
+
+$(TMP_OUTPUT):
+	$(call msg,MKDIR,$@)
+	$(Q)mkdir -p $(TMP_OUTPUT)
+
+$(TMP_OUTPUT)/vmlinux.h: $(VMLINUX_BTF_PATH) | $(TMP_OUTPUT) $(BPFTOOL)
+	$(call msg,GEN,$@)
+	$(Q)if [ ! -e "$(VMLINUX_BTF_PATH)" ] ; then \
+		echo "Couldn't find kernel BTF; set VMLINUX_BTF to"	       \
+			"specify its location." >&2;			       \
+		exit 1;\
+	fi
+	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF_PATH) format c > $@
+
+$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(TMP_OUTPUT)
+	$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC)			       \
Quoted 9 lines

tools/perf/util/bpf_skel/Makefile line 71

Quoted 5 lines
tools/perf/util/bpf_skel/Makefile new file +5 -0
+		    OUTPUT=$(abspath $(dir $@))/ $(abspath $@)
+
+$(DEFAULT_BPFTOOL): | $(TMP_OUTPUT)
+	$(Q)$(MAKE) $(submake_extras) -C ../../../bpf/bpftool		      \
+		    prefix= OUTPUT=$(TMP_OUTPUT)/ DESTDIR=$(TMP_OUTPUT) install

could we build just the bootstrap version of bpftool? should be enough for skeleton and vmlinux.h dump, no?

Yeah, that should work. I thought about it, but didn't try that hard.

Song

#12 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

In reply to Song Liu

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

tools/perf/util/bpf_skel/Makefile line 56

Quoted 13 lines
tools/perf/util/bpf_skel/Makefile new file +13 -0
+	$(Q)$(BPFTOOL) gen skeleton $< > $@
+
+$(TMP_OUTPUT)/%.bpf.o: %.bpf.c $(TMP_OUTPUT)/vmlinux.h $(BPFOBJ) | $(TMP_OUTPUT)
+	$(call msg,BPF,$@)
+	$(Q)$(CLANG) -g -O2 -target bpf $(INCLUDES)			      \
+		 -c $(filter %.c,$^) -o $@ &&				      \
+	$(LLVM_STRIP) -g $@
+
+$(TMP_OUTPUT):
+	$(call msg,MKDIR,$@)
+	$(Q)mkdir -p $(TMP_OUTPUT)
+
+$(TMP_OUTPUT)/vmlinux.h: $(VMLINUX_BTF_PATH) | $(TMP_OUTPUT) $(BPFTOOL)

please add support to specify VMLINUX_H as it is in selftests or bpftool, we will need it in out building setup

thanks,
jirka

Quoted 43 lines
tools/perf/util/bpf_skel/Makefile new file +15 -0
+	$(call msg,GEN,$@)
+	$(Q)if [ ! -e "$(VMLINUX_BTF_PATH)" ] ; then \
+		echo "Couldn't find kernel BTF; set VMLINUX_BTF to"	       \
+			"specify its location." >&2;			       \
+		exit 1;\
+	fi
+	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF_PATH) format c > $@
+
+$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(TMP_OUTPUT)
+	$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC)			       \
+		    OUTPUT=$(abspath $(dir $@))/ $(abspath $@)
+
+$(DEFAULT_BPFTOOL): | $(TMP_OUTPUT)
+	$(Q)$(MAKE) $(submake_extras) -C ../../../bpf/bpftool		      \
+		    prefix= OUTPUT=$(TMP_OUTPUT)/ DESTDIR=$(TMP_OUTPUT) install
tools/perf/util/bpf_skel/dummy.bpf.c new file +19 -0
new file mode 100644
index 0000000000000..085fcee1f52cf
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+// Copyright (c) 2020 Facebook
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(key_size, sizeof(u32));
+	__uint(value_size, sizeof(u64));
+} counts SEC(".maps");
+
+SEC("fentry/dummy")
+int BPF_PROG(fentry_dummy)
+{
+	return 0;
+}
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
Signature · 1 line
2.24.1
author
#13 lore raw

Re: [RFC 1/2] perf: support build BPF skeletons with perf

Quoted 25 lines · Jiri Olsa

On Wed, Nov 18, 2020 at 08:50:45PM -0800, Song Liu wrote:

SNIP

tools/perf/util/bpf_skel/Makefile line 56

Quoted 13 lines
tools/perf/util/bpf_skel/Makefile new file +13 -0
+	$(Q)$(BPFTOOL) gen skeleton $< > $@
+
+$(TMP_OUTPUT)/%.bpf.o: %.bpf.c $(TMP_OUTPUT)/vmlinux.h $(BPFOBJ) | $(TMP_OUTPUT)
+	$(call msg,BPF,$@)
+	$(Q)$(CLANG) -g -O2 -target bpf $(INCLUDES)			      \
+		 -c $(filter %.c,$^) -o $@ &&				      \
+	$(LLVM_STRIP) -g $@
+
+$(TMP_OUTPUT):
+	$(call msg,MKDIR,$@)
+	$(Q)mkdir -p $(TMP_OUTPUT)
+
+$(TMP_OUTPUT)/vmlinux.h: $(VMLINUX_BTF_PATH) | $(TMP_OUTPUT) $(BPFTOOL)

please add support to specify VMLINUX_H as it is in selftests or bpftool, we will need it in out building setup

Quoted 2 lines

thanks,
jirka

Let me try that.

Thanks,
Song

author
#14 lore raw

RFC 2/2 perf-stat: enable counting events for BPF programs

In reply to Song Liu

Introduce perf-stat -b option, which counts events for BPF programs, like:

[root@localhost ~]# ~/perf stat -e ref-cycles,cycles -b 254 -I 1000

     1.487903822            115,200      ref-cycles
     1.487903822             86,012      cycles
     2.489147029             80,560      ref-cycles
     2.489147029             73,784      cycles
     3.490341825             60,720      ref-cycles
     3.490341825             37,797      cycles
     4.491540887             37,120      ref-cycles
     4.491540887             31,963      cycles

The example above counts cycles and ref-cycles of BPF program of id 254. This is similar to bpftool-prog-profile command, but more flexible.

perf-stat -b creates per-cpu perf_event and loads fentry/fexit BPF programs (monitor-progs) to the target BPF program (target-prog). The monitor-progs read perf_event before and after the target-prog, and aggregate the difference in a BPF map. Then the user space reads data from these maps.

A new struct bpf_counter is introduced to provide common interface that uses BPF programs/maps to count perf events.

---
 tools/perf/Makefile.config                    |   1 +
 tools/perf/builtin-stat.c                     |  59 ++++-
 tools/perf/util/Build                         |   1 +
 tools/perf/util/bpf_counter.c                 | 215 ++++++++++++++++++
 tools/perf/util/bpf_counter.h                 |  71 ++++++
 tools/perf/util/bpf_skel/Makefile             |   2 +-
 .../util/bpf_skel/bpf_prog_profiler.bpf.c     |  96 ++++++++
 tools/perf/util/evsel.c                       |  10 +
 tools/perf/util/evsel.h                       |   5 +
 tools/perf/util/target.h                      |   6 +
 10 files changed, 457 insertions(+), 9 deletions(-)
 create mode 100644 tools/perf/util/bpf_counter.c
 create mode 100644 tools/perf/util/bpf_counter.h
 create mode 100644 tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c
tools/perf/Makefile.config +1 -0
index 1c2c0f4badd85..60bcaa5af4d4c 100644
@@ -624,6 +624,7 @@ ifndef NO_LIBBPF
     ifeq ($(feature-clang-bpf-co-re), 1)
       BUILD_BPF_SKEL := 1
       $(call detected,CONFIG_PERF_BPF_SKEL)
+      CFLAGS += -DBUILD_BPF_SKEL
     endif
   endif
 endif
tools/perf/builtin-stat.c +51 -8

126 lines not on this page · raw message

tools/perf/util/Build +1 -0
index e2563d0154eb6..188521f343470 100644
@@ -135,6 +135,7 @@ perf-y += clockid.o
 
 perf-$(CONFIG_LIBBPF) += bpf-loader.o
 perf-$(CONFIG_LIBBPF) += bpf_map.o
+perf-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
 perf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o
 perf-$(CONFIG_LIBELF) += symbol-elf.o
 perf-$(CONFIG_LIBELF) += probe-file.o
tools/perf/util/bpf_counter.c new file +215 -0

215 lines not on this page · raw message

tools/perf/util/bpf_counter.h new file +71 -0

71 lines not on this page · raw message

tools/perf/util/bpf_skel/Makefile +1 -1
index 853bece088f4b..9e942f280c713 100644
@@ -28,7 +28,7 @@ MAKEFLAGS += --no-print-directory
 submake_extras := feature_display=0
 endif
 
-SKELETONS := $(SKEL_OUTPUT)/dummy.skel.h
+SKELETONS := $(SKEL_OUTPUT)/dummy.skel.h $(SKEL_OUTPUT)/bpf_prog_profiler.skel.h
 
 .DELETE_ON_ERROR:
 
tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +96 -0

96 lines not on this page · raw message

tools/perf/util/evsel.c +10 -0
index 1cad6051d8b08..6376b8db58e09 100644
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 #include <perf/evsel.h>
 #include "asm/bug.h"
+#include "bpf_counter.h"
 #include "callchain.h"
 #include "cgroup.h"
 #include "counts.h"
@@ -51,6 +52,10 @@
 #include <internal/lib.h>
 
 #include <linux/ctype.h>
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+#include <bpf/btf.h>
+#include "rlimit.h"
 
 struct perf_missing_features perf_missing_features;
 
@@ -1365,6 +1370,7 @@ void evsel__exit(struct evsel *evsel)
 {
 	assert(list_empty(&evsel->core.node));
 	assert(evsel->evlist == NULL);
+	bpf_counter__destroy(evsel);
 	evsel__free_counts(evsel);
 	perf_evsel__free_fd(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
@@ -1770,6 +1776,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		evsel->core.attr.sample_id_all = 0;
 
 	display_attr(&evsel->core.attr);
+	if (evsel->bpf_counter.skel)
+		evsel->core.attr.inherit = 0;
 
 	for (cpu = start_cpu; cpu < end_cpu; cpu++) {
 
@@ -1788,6 +1796,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 			FD(evsel, cpu, thread) = fd;
 
+			bpf_counter__install_pe(evsel, cpu, fd);
+
 			if (unlikely(test_attr__enabled)) {
 				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
 						fd, group_fd, flags);
tools/perf/util/evsel.h +5 -0
index 79a860d8e3eef..3a44f7b25726c 100644
@@ -10,6 +10,7 @@
 #include <internal/evsel.h>
 #include <perf/evsel.h>
 #include "symbol_conf.h"
+#include "bpf_counter.h"
 #include <internal/cpumap.h>
 
 struct bpf_object;
@@ -17,6 +18,8 @@ struct cgroup;
 struct perf_counts;
 struct perf_stat_evsel;
 union perf_event;
+struct bpf_counter;
+struct target;
 
 typedef int (evsel__sb_cb_t)(union perf_event *event, void *data);
 
@@ -127,6 +130,7 @@ struct evsel {
 	 * See also evsel__has_callchain().
 	 */
 	__u64			synth_sample_type;
+	struct bpf_counter	bpf_counter;
 };
 
 struct perf_missing_features {
@@ -423,4 +427,5 @@ static inline bool evsel__is_dummy_event(struct evsel *evsel)
 struct perf_env *evsel__env(struct evsel *evsel);
 
 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
+
 #endif /* __PERF_EVSEL_H */
tools/perf/util/target.h +6 -0
index 6ef01a83b24e9..cdaa7510f918b 100644
@@ -10,6 +10,7 @@ struct target {
 	const char   *tid;
 	const char   *cpu_list;
 	const char   *uid_str;
+	const char   *bpf_prog_id;
 	uid_t	     uid;
 	bool	     system_wide;
 	bool	     uses_mmap;
@@ -59,6 +60,11 @@ static inline bool target__has_cpu(struct target *target)
 	return target->system_wide || target->cpu_list;
 }
 
+static inline bool target__has_bpf(struct target *target)
+{
+	return target->bpf_prog_id;
+}
+
 static inline bool target__none(struct target *target)
 {
 	return !target__has_task(target) && !target__has_cpu(target);
Signature · 1 line
2.24.1
#15 lore raw

Re: [RFC 2/2] perf-stat: enable counting events for BPF programs

Quoted 21 lines · Song Liu

Introduce perf-stat -b option, which counts events for BPF programs, like:

[root@localhost ~]# ~/perf stat -e ref-cycles,cycles -b 254 -I 1000

     1.487903822            115,200      ref-cycles
     1.487903822             86,012      cycles
     2.489147029             80,560      ref-cycles
     2.489147029             73,784      cycles
     3.490341825             60,720      ref-cycles
     3.490341825             37,797      cycles
     4.491540887             37,120      ref-cycles
     4.491540887             31,963      cycles

The example above counts cycles and ref-cycles of BPF program of id 254. This is similar to bpftool-prog-profile command, but more flexible.

perf-stat -b creates per-cpu perf_event and loads fentry/fexit BPF programs (monitor-progs) to the target BPF program (target-prog). The monitor-progs read perf_event before and after the target-prog, and aggregate the difference in a BPF map. Then the user space reads data from these maps.

Quoted 4 lines

A new struct bpf_counter is introduced to provide common interface that uses BPF programs/maps to count perf events.

I still need to review this deeply, but so far I'm getting this error:

	# ./perf stat -b 40
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: Can't get the 0th fd from program fentry_XXX: only -1 instances
	libbpf: prog 'fentry_XXX': can't attach before loaded
	libbpf: failed to auto-attach program 'fentry_XXX': -22

jirka

author
#16 lore raw

Re: [RFC 2/2] perf-stat: enable counting events for BPF programs

Quoted 3 lines · Jiri Olsa

[...]

Quoted 39 lines

I still need to review this deeply, but so far I'm getting this error:

	# ./perf stat -b 40
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
Quoted 8 lines
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: Can't get the 0th fd from program fentry_XXX: only -1 instances
	libbpf: prog 'fentry_XXX': can't attach before loaded
	libbpf: failed to auto-attach program 'fentry_XXX': -22

I cannot reproduce this. Is 40 a valid BPF program ID? Could you please share more information about it with "bpftool prog show id 40"?

Thanks,
Song

#17 lore raw

Re: [RFC 2/2] perf-stat: enable counting events for BPF programs

Quoted 54 lines · Song Liu
Quoted 3 lines · Jiri Olsa

[...]

Quoted 39 lines

I still need to review this deeply, but so far I'm getting this error:

	# ./perf stat -b 40
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
Quoted 8 lines
	libbpf: elf: skipping unrecognized data section(9) .eh_frame
	libbpf: elf: skipping relo section(15) .rel.eh_frame for section(9) .eh_frame
	libbpf: XXX is not found in vmlinux BTF
	libbpf: failed to load object 'bpf_prog_profiler_bpf'
	libbpf: failed to load BPF skeleton 'bpf_prog_profiler_bpf': -2
	libbpf: Can't get the 0th fd from program fentry_XXX: only -1 instances
	libbpf: prog 'fentry_XXX': can't attach before loaded
	libbpf: failed to auto-attach program 'fentry_XXX': -22
Quoted 2 lines

I cannot reproduce this. Is 40 a valid BPF program ID? Could you please share more information about it with "bpftool prog show id 40"?

it was bpftrace kfunc program, I'll try again and get back with more details if there's still the problem

jirka

#18 lore raw

Re: [RFC 2/2] perf-stat: enable counting events for BPF programs

In reply to Song Liu

On Wed, Nov 18, 2020 at 08:50:46PM -0800, Song Liu wrote:

SNIP

Quoted 6 lines
tools/perf/util/bpf_counter.c new file +6 -0
+static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu,
+					    int fd)
+{
+	struct bpf_prog_profiler_bpf *skel = evsel->bpf_counter.skel;
+
+	return bpf_map_update_elem(bpf_map__fd(skel->maps.events),

tools/perf/util/bpf_counter.c line 174

Quoted 10 lines
tools/perf/util/bpf_counter.c new file +10 -0
+				   &cpu, &fd, BPF_ANY);
+}
+
+struct bpf_counter_ops bpf_program_profiler_ops = {
+	.load       = bpf_program_profiler__load,
+	.enable	    = bpf_program_profiler__enable,
+	.read       = bpf_program_profiler__read,
+	.destroy    = bpf_program_profiler__destroy,
+	.install_pe = bpf_program_profiler__install_pe,
+};

hum, what's the point of this ops? you plan some other ops? we could just define stat callbacks right?

Quoted 12 lines
tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +12 -0
+SEC("fentry/XXX")
+int BPF_PROG(fentry_XXX)
+{
+	u32 key = bpf_get_smp_processor_id();
+	struct bpf_perf_event_value reading;
+	struct bpf_perf_event_value *ptr;
+	u32 zero = 0;
+	long err;
+
+	/* look up before reading, to reduce error */
+	ptr = bpf_map_lookup_elem(&fentry_readings, &zero);
+	if (!ptr)

tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c line 53

Quoted 10 lines
tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +10 -0
+		return 0;
+
+	err = bpf_perf_event_read_value(&events, key, &reading,
+					sizeof(reading));
+	if (err)
+		return 0;
+
+	*ptr = reading;
+	return 0;
+}

so currently it's one extra bpf program for each event, but it seems bad for multiple events stat.. could we just have one program that would read and process all events?

thanks,
jirka

author
#19 lore raw

Re: [RFC 2/2] perf-stat: enable counting events for BPF programs

Quoted 19 lines · Jiri Olsa

On Wed, Nov 18, 2020 at 08:50:46PM -0800, Song Liu wrote:

SNIP

Quoted 11 lines
tools/perf/util/bpf_counter.c new file +11 -0
+static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu,
+					    int fd)
+{
+	struct bpf_prog_profiler_bpf *skel = evsel->bpf_counter.skel;
+
+	return bpf_map_update_elem(bpf_map__fd(skel->maps.events),
+				   &cpu, &fd, BPF_ANY);
+}
+
+struct bpf_counter_ops bpf_program_profiler_ops = {
+	.load       = bpf_program_profiler__load,
Quoted 8 lines

tools/perf/util/bpf_counter.c line 174

Quoted 5 lines
tools/perf/util/bpf_counter.c new file +5 -0
+	.enable	    = bpf_program_profiler__enable,
+	.read       = bpf_program_profiler__read,
+	.destroy    = bpf_program_profiler__destroy,
+	.install_pe = bpf_program_profiler__install_pe,
+};

hum, what's the point of this ops? you plan some other ops? we could just define stat callbacks right?

I do have other ideas using BPF program to aggregate perf event counts. This ops provides common interface for different BPF extensions to evsel.

Quoted 19 lines
Quoted 19 lines
tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +18 -0
+SEC("fentry/XXX")
+int BPF_PROG(fentry_XXX)
+{
+	u32 key = bpf_get_smp_processor_id();
+	struct bpf_perf_event_value reading;
+	struct bpf_perf_event_value *ptr;
+	u32 zero = 0;
+	long err;
+
+	/* look up before reading, to reduce error */
+	ptr = bpf_map_lookup_elem(&fentry_readings, &zero);
+	if (!ptr)
+		return 0;
+
+	err = bpf_perf_event_read_value(&events, key, &reading,
+					sizeof(reading));
+	if (err)
+		return 0;
Quoted 8 lines

tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c line 53

Quoted 4 lines
tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +4 -0
+
+	*ptr = reading;
+	return 0;
+}

so currently it's one extra bpf program for each event, but it seems bad for multiple events stat.. could we just have one program that would read and process all events?

Multiple fentry programs should not be too expensive. Current design extends evsel, so it is a cleaner implementation. We can evaluate the difference of these two designs by comparing this with "bpftool prog profile".

Thanks,
Song

author
#20 lore raw

Re: [RFC 2/2] perf-stat: enable counting events for BPF programs

Quoted 23 lines · Song Liu
Quoted 20 lines · Jiri Olsa

On Wed, Nov 18, 2020 at 08:50:46PM -0800, Song Liu wrote:

SNIP

Quoted 11 lines
tools/perf/util/bpf_counter.c new file +11 -0
+static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu,
+					    int fd)
+{
+	struct bpf_prog_profiler_bpf *skel = evsel->bpf_counter.skel;
+
+	return bpf_map_update_elem(bpf_map__fd(skel->maps.events),
+				   &cpu, &fd, BPF_ANY);
+}
+
+struct bpf_counter_ops bpf_program_profiler_ops = {
+	.load       = bpf_program_profiler__load,
Quoted 8 lines
Quoted 8 lines

tools/perf/util/bpf_counter.c line 174

Quoted 5 lines
tools/perf/util/bpf_counter.c new file +5 -0
+	.enable	    = bpf_program_profiler__enable,
+	.read       = bpf_program_profiler__read,
+	.destroy    = bpf_program_profiler__destroy,
+	.install_pe = bpf_program_profiler__install_pe,
+};

hum, what's the point of this ops? you plan some other ops? we could just define stat callbacks right?

Which callbacks do you mean here? I would like to try that as well.

Thanks,
Song

Quoted 40 lines

I do have other ideas using BPF program to aggregate perf event counts. This ops provides common interface for different BPF extensions to evsel.

Quoted 19 lines
Quoted 19 lines
tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +18 -0
+SEC("fentry/XXX")
+int BPF_PROG(fentry_XXX)
+{
+	u32 key = bpf_get_smp_processor_id();
+	struct bpf_perf_event_value reading;
+	struct bpf_perf_event_value *ptr;
+	u32 zero = 0;
+	long err;
+
+	/* look up before reading, to reduce error */
+	ptr = bpf_map_lookup_elem(&fentry_readings, &zero);
+	if (!ptr)
+		return 0;
+
+	err = bpf_perf_event_read_value(&events, key, &reading,
+					sizeof(reading));
+	if (err)
+		return 0;
Quoted 8 lines

tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c line 53

Quoted 4 lines
tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +4 -0
+
+	*ptr = reading;
+	return 0;
+}

so currently it's one extra bpf program for each event, but it seems bad for multiple events stat.. could we just have one program that would read and process all events?

Multiple fentry programs should not be too expensive. Current design extends evsel, so it is a cleaner implementation. We can evaluate the difference of these two designs by comparing this with "bpftool prog profile".

Thanks,
Song

#21 lore raw

Re: [RFC 2/2] perf-stat: enable counting events for BPF programs

Quoted 32 lines · Song Liu
Quoted 23 lines · Song Liu
Quoted 20 lines · Jiri Olsa

On Wed, Nov 18, 2020 at 08:50:46PM -0800, Song Liu wrote:

SNIP

Quoted 11 lines
tools/perf/util/bpf_counter.c new file +11 -0
+static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu,
+					    int fd)
+{
+	struct bpf_prog_profiler_bpf *skel = evsel->bpf_counter.skel;
+
+	return bpf_map_update_elem(bpf_map__fd(skel->maps.events),
+				   &cpu, &fd, BPF_ANY);
+}
+
+struct bpf_counter_ops bpf_program_profiler_ops = {
+	.load       = bpf_program_profiler__load,
Quoted 8 lines
Quoted 8 lines

tools/perf/util/bpf_counter.c line 174

Quoted 5 lines
tools/perf/util/bpf_counter.c new file +5 -0
+	.enable	    = bpf_program_profiler__enable,
+	.read       = bpf_program_profiler__read,
+	.destroy    = bpf_program_profiler__destroy,
+	.install_pe = bpf_program_profiler__install_pe,
+};

hum, what's the point of this ops? you plan some other ops? we could just define stat callbacks right?

Quoted 2 lines

Which callbacks do you mean here? I would like to try that as well.

I meant just to drop that ops struct and have load/enable/read.. functions called from stat code

jirka

#22 lore raw
In reply to Song Liu

Em Wed, Nov 18, 2020 at 08:50:44PM -0800, Song Liu escreveu:

Quoted 11 lines

This set introduces perf-stat -b option to count events for BPF programs. This is similar to bpftool-prog-profile. But perf-stat makes it much more flexible.

Sending as RFC because I would like comments on some key design choices:
1. We are using BPF skeletons here, which is by far the easiest way to write and ship BPF programs. However, this requires bpftool, which makes building perf slower.
2. A Makefile is added to tools/perf/util/bpf_skel/ to build bpftool, and BPF skeletons. This keeps main perf Makefiles simple. But we may not like it for some reason?

I'll review it in detail, but before that: thanks a lot for working on this! Looks super cool from a first quick look. :-)

- Arnaldo

Quoted 33 lines

Some known limitations (or work to be done):
1. Only counting events for one BPF program at a time.
2. Need extra logic in target__validate().

Song Liu (2):
perf: support build BPF skeletons with perf
perf-stat: enable counting events for BPF programs

 tools/build/Makefile.feature                  |   3 +-
 tools/perf/Makefile.config                    |   8 +
 tools/perf/Makefile.perf                      |  15 +-
 tools/perf/builtin-stat.c                     |  59 ++++-
 tools/perf/util/Build                         |   1 +
 tools/perf/util/bpf_counter.c                 | 215 ++++++++++++++++++
 tools/perf/util/bpf_counter.h                 |  71 ++++++
 tools/perf/util/bpf_skel/.gitignore           |   3 +
 tools/perf/util/bpf_skel/Makefile             |  71 ++++++
 .../util/bpf_skel/bpf_prog_profiler.bpf.c     |  96 ++++++++
 tools/perf/util/bpf_skel/dummy.bpf.c          |  19 ++
 tools/perf/util/evsel.c                       |  10 +
 tools/perf/util/evsel.h                       |   5 +
 tools/perf/util/target.h                      |   6 +
 14 files changed, 571 insertions(+), 11 deletions(-)
 create mode 100644 tools/perf/util/bpf_counter.c
 create mode 100644 tools/perf/util/bpf_counter.h
 create mode 100644 tools/perf/util/bpf_skel/.gitignore
 create mode 100644 tools/perf/util/bpf_skel/Makefile
 create mode 100644 tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c
 create mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c

--
2.24.1

Signature · 1 line
- Arnaldo