Skip to contentSkip to conversation

Introduce perf-stat -b for BPF programs

RFC Superseded Song Liu 2 participants 8 messages Dec 1, 2020 – Dec 3, 2020

Subsystems (3)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Song Liu

author
#0 lore raw

v2 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 haven't addressed some known limitations:
1. Only counting events for one BPF program at a time.
2. Need extra logic in target__validate().

Changes RFCv1 => RFCv2:
1. Use bootstrap version of bpftool. (Jiri)
2. Set default to not building bpf skeletons. (Jiri)
3. Remove util/bpf_skel/Makefile, keep all the logic in Makefile.perf.

     (Jiri)

4. Remove dependency to vmlinux.h in the two skeletons. The goal here is to enable building perf without building kernel (vmlinux) first.
Note: I also removed the logic that build vmlinux.h. We can add that back when we have to use it (to access big kernel structures).

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

 tools/bpf/bpftool/Makefile                    |   2 +
 tools/build/Makefile.feature                  |   3 +-
 tools/perf/Makefile.config                    |   9 +
 tools/perf/Makefile.perf                      |  52 ++++-
 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 +
 .../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 +
 tools/scripts/Makefile.include                |   1 +
 15 files changed, 541 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/bpf_prog_profiler.bpf.c
 create mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c

--
2.24.1

author
#1 lore raw

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

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

---
 tools/bpf/bpftool/Makefile           |  2 ++
 tools/build/Makefile.feature         |  3 +-
 tools/perf/Makefile.config           |  9 +++++
 tools/perf/Makefile.perf             | 52 ++++++++++++++++++++++++++--
 tools/perf/util/bpf_skel/.gitignore  |  3 ++
 tools/perf/util/bpf_skel/dummy.bpf.c | 19 ++++++++++
 tools/scripts/Makefile.include       |  1 +
 7 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100644 tools/perf/util/bpf_skel/.gitignore
 create mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c
tools/bpf/bpftool/Makefile +2 -0
index f60e6ad3a1dff..a01407ec78dc5 100644
@@ -120,6 +120,8 @@ endif
 
 BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)
 
+bootstrap: $(BPFTOOL_BOOTSTRAP)
+
 BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)
 OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
 
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 +9 -0
index ce8516e4de34f..cb0cf06e0bb43 100644
@@ -621,6 +621,15 @@ ifndef NO_LIBBPF
   endif
 endif
 
+ifeq ($(feature-clang-bpf-co-re), 0)
+  BUILD_BPF_SKEL := 0
+endif
+
+ifeq ($(BUILD_BPF_SKEL), 1)
+  $(call detected,CONFIG_PERF_BPF_SKEL)
+  CFLAGS += -DBUILD_BPF_SKEL
+endif
+
 dwarf-post-unwind := 1
 dwarf-post-unwind-text := BUG
 
tools/perf/Makefile.perf +50 -2

70 lines not on this page · raw message

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/dummy.bpf.c new file +19 -0
new file mode 100644
index 0000000000000..492a43a851deb
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+// Copyright (c) 2020 Facebook
+#include <linux/bpf.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";
tools/scripts/Makefile.include +1 -0
index a7974638561ca..ed1720dc12194 100644
@@ -126,6 +126,7 @@ ifneq ($(silent),1)
 	QUIET_CLEAN    = @printf '  CLEAN    %s\n' $1;
 	QUIET_INSTALL  = @printf '  INSTALL  %s\n' $1;
 	QUIET_UNINST   = @printf '  UNINST   %s\n' $1;
+	QUIET_GENSKEL  = @printf '  GEN-SKEL   %s\n' $1;
   endif
 endif
 
Signature · 1 line
2.24.1
#2 lore raw

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

Quoted 36 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.

---
 tools/bpf/bpftool/Makefile           |  2 ++
 tools/build/Makefile.feature         |  3 +-
 tools/perf/Makefile.config           |  9 +++++
 tools/perf/Makefile.perf             | 52 ++++++++++++++++++++++++++--
 tools/perf/util/bpf_skel/.gitignore  |  3 ++
 tools/perf/util/bpf_skel/dummy.bpf.c | 19 ++++++++++
 tools/scripts/Makefile.include       |  1 +
 7 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100644 tools/perf/util/bpf_skel/.gitignore
 create mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c
tools/bpf/bpftool/Makefile +2 -0
index f60e6ad3a1dff..a01407ec78dc5 100644
@@ -120,6 +120,8 @@ endif
 
 BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)
 
+bootstrap: $(BPFTOOL_BOOTSTRAP)
+
 BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)
 OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
 
tools/build/Makefile.feature +0 -0
index 97cbfb31b7625..95a58b5564218 100644

tools/build/Makefile.feature FEATURE_TESTS_BASIC := \ · line 74

Quoted 6 lines
tools/build/Makefile.feature +2 -1
@@ -70,7 +70,8 @@ FEATURE_TESTS_BASIC :=                  \
         libaio				\
         libzstd				\
         disassembler-four-args		\
-        file-handle
+        file-handle			\
+        clang-bpf-co-re

this needs to go under FEATURE_TESTS_EXTRA and you need to call
$(call feature_check,clang-bpf-co-re) before checking on $(feature-clang-bpf-co-re)

otherwise the test-all will mark it as detected even if it's not

Quoted 37 lines
tools/build/Makefile.feature +0 -0
 # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
 # of all feature tests
tools/perf/Makefile.config +9 -0
index ce8516e4de34f..cb0cf06e0bb43 100644
@@ -621,6 +621,15 @@ ifndef NO_LIBBPF
   endif
 endif
 
+ifeq ($(feature-clang-bpf-co-re), 0)
+  BUILD_BPF_SKEL := 0
+endif
+
+ifeq ($(BUILD_BPF_SKEL), 1)
+  $(call detected,CONFIG_PERF_BPF_SKEL)
+  CFLAGS += -DBUILD_BPF_SKEL
+endif
+
 dwarf-post-unwind := 1
 dwarf-post-unwind-text := BUG
 
tools/perf/Makefile.perf +2 -0
index 7ce3f2e8b9c74..37b7ffe1db27c 100644
@@ -126,6 +126,8 @@ include ../scripts/utilities.mak
 #
 # Define NO_LIBDEBUGINFOD if you do not want support debuginfod
 #
+# Define BUILD_BPF_SKEL to enable BPF skeletons
+#
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL

tools/perf/Makefile.perf prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioc · line 741

Quoted 6 lines
tools/perf/Makefile.perf +2 -1
@@ -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

I think the 'prepare' target is misused already with other stuff, there's generated bpf_counter.c dependency on util/bpf_skel/bpf_prog_profiler.skel.h in util/.bpf_counter.o.cmd, that should triger the build no?

we have few other dependencies like that for flex/bison generation perhaps we could follow also that

Quoted 5 lines
tools/perf/Makefile.perf +0 -0
 $(OUTPUT)%.o: %.c prepare FORCE
 	$(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
@@ -1008,7 +1011,52 @@ config-clean:
 python-clean:

tools/perf/Makefile.perf config-clean: · line 1020

Quoted 10 lines
tools/perf/Makefile.perf +7 -1
 	$(python-clean)
 
-clean:: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean $(LIBPERF)-clean config-clean fixdep-clean python-clean
+SKEL_OUT := $(abspath util/bpf_skel)
+SKEL_TMP_OUT := $(abspath util/bpf_skel/.tmp)
+SKELETONS := $(SKEL_OUT)/dummy.skel.h
+
+ifdef BUILD_BPF_SKEL
+CLANG ?= clang
+LLVM_STRIP ?= llvm-strip

please move this up where we have similar setup like HOSTCC and others

tools/perf/Makefile.perf config-clean: · line 1024

Quoted 4 lines
tools/perf/Makefile.perf +4 -0
+BPFTOOL_CFLAGS := $(filter-out -D_GNU_SOURCE,$(CFLAGS))
+BPFTOOL := $(SKEL_TMP_OUT)/bpftool-bootstrap
+LIBBPF_SRC := $(abspath ../lib/bpf)
+BPFOBJ := $(SKEL_TMP_OUT)/libbpf.a

we already build libbpf.a, chekc 'LIBBPF = $(BPF_PATH)libbpf.a' above

Quoted 7 lines
tools/perf/Makefile.perf +7 -0
+BPF_INCLUDE := $(SKEL_TMP_OUT)
+submake_extras := feature_display=0
+
+$(SKEL_TMP_OUT):
+	$(Q)$(MKDIR) -p $@
+
+$(BPFTOOL): | $(SKEL_TMP_OUT)

tools/perf/Makefile.perf config-clean: · line 1041

Quoted 10 lines
tools/perf/Makefile.perf +10 -0
+	CFLAGS= $(MAKE) $(submake_extras) -C ../bpf/bpftool \
+		OUTPUT=$(SKEL_TMP_OUT)/ bootstrap
+
+$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(BPFOBJ) | $(SKEL_TMP_OUT)
+	$(call QUIET_CLANG, $@)
+	$(Q)$(CLANG) -g -O2 -target bpf	-c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && \
+	$(LLVM_STRIP) -g $@
+
+$(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL)
+	$(call QUIET_GENSKEL, $@)

is there a reason to use call in here? you could define QUIET_GENSKEL to use $@ and use it the same way as we use the rest of QUIET_* stuf in Makefile.perf

tools/perf/util/bpf_skel/dummy.bpf.c

Quoted 14 lines

new file mode 100644
index 0000000000000..5263e9e6c5d83

tools/perf/util/bpf_skel/.gitignore new file +3 -0
@@ -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/dummy.bpf.c new file +0 -0
new file mode 100644
index 0000000000000..492a43a851deb

hum, what's the reason for dummy skeleton? it just adds time to compilation no?

thanks,
jirka

author
#3 lore raw

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

Quoted 87 lines · Jiri Olsa
Quoted 36 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.

---

tools/bpf/bpftool/Makefile           |  2 ++
tools/build/Makefile.feature         |  3 +-
tools/perf/Makefile.config           |  9 +++++
tools/perf/Makefile.perf             | 52 ++++++++++++++++++++++++++--
tools/perf/util/bpf_skel/.gitignore  |  3 ++
tools/perf/util/bpf_skel/dummy.bpf.c | 19 ++++++++++
tools/scripts/Makefile.include       |  1 +
7 files changed, 86 insertions(+), 3 deletions(-)

create mode 100644 tools/perf/util/bpf_skel/.gitignore create mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c

tools/bpf/bpftool/Makefile +2 -0
index f60e6ad3a1dff..a01407ec78dc5 100644
@@ -120,6 +120,8 @@ endif
 
 BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)
 
+bootstrap: $(BPFTOOL_BOOTSTRAP)
+
 BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)

OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o

tools/build/Makefile.feature +0 -0
index 97cbfb31b7625..95a58b5564218 100644

tools/build/Makefile.feature FEATURE_TESTS_BASIC := \ · line 74

Quoted 6 lines
tools/build/Makefile.feature +2 -1
@@ -70,7 +70,8 @@ FEATURE_TESTS_BASIC :=                  \
        libaio				\
        libzstd				\
        disassembler-four-args		\
-        file-handle
+        file-handle			\
+        clang-bpf-co-re

this needs to go under FEATURE_TESTS_EXTRA and you need to call
$(call feature_check,clang-bpf-co-re) before checking on $(feature-clang-bpf-co-re)

otherwise the test-all will mark it as detected even if it's not

Quoted 34 lines
tools/build/Makefile.feature +0 -0
 # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
 # of all feature tests
tools/perf/Makefile.config +0 -0
index ce8516e4de34f..cb0cf06e0bb43 100644
@@ -621,6 +621,15 @@ ifndef NO_LIBBPF
  endif

endif

tools/perf/Makefile.config +9 -0
+ifeq ($(feature-clang-bpf-co-re), 0)
+  BUILD_BPF_SKEL := 0
+endif
+
+ifeq ($(BUILD_BPF_SKEL), 1)
+  $(call detected,CONFIG_PERF_BPF_SKEL)
+  CFLAGS += -DBUILD_BPF_SKEL
+endif
+

dwarf-post-unwind := 1
dwarf-post-unwind-text := BUG

tools/perf/Makefile.perf +2 -0
index 7ce3f2e8b9c74..37b7ffe1db27c 100644
@@ -126,6 +126,8 @@ include ../scripts/utilities.mak
 #
 # Define NO_LIBDEBUGINFOD if you do not want support debuginfod
 #
+# Define BUILD_BPF_SKEL to enable BPF skeletons
+#
 
Quoted 13 lines

tools/perf/Makefile.perf prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioc · line 741

Quoted 9 lines
tools/perf/Makefile.perf +0 -0
 # As per kernel Makefile, avoid funny character set dependencies

unexport LC_ALL

tools/perf/Makefile.perf +2 -1
@@ -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

I think the 'prepare' target is misused already with other stuff, there's generated bpf_counter.c dependency on util/bpf_skel/bpf_prog_profiler.skel.h in util/.bpf_counter.o.cmd, that should triger the build no?

This doesn't work for me. Once bpf-skel is removed from "prepare", we hit compilation error before util/.bpf_counter.o.cmd is generated.

Quoted 22 lines

we have few other dependencies like that for flex/bison generation perhaps we could follow also that

Quoted 7 lines

$(OUTPUT)%.o: %.c prepare FORCE

	$(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
tools/perf/Makefile.perf +0 -0
@@ -1008,7 +1011,52 @@ config-clean:

python-clean:

	$(python-clean)

tools/perf/Makefile.perf config-clean: · line 1020

Quoted 8 lines
tools/perf/Makefile.perf +7 -1
-clean:: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean $(LIBPERF)-clean config-clean fixdep-clean python-clean
+SKEL_OUT := $(abspath util/bpf_skel)
+SKEL_TMP_OUT := $(abspath util/bpf_skel/.tmp)
+SKELETONS := $(SKEL_OUT)/dummy.skel.h
+
+ifdef BUILD_BPF_SKEL
+CLANG ?= clang
+LLVM_STRIP ?= llvm-strip

please move this up where we have similar setup like HOSTCC and others

Quoted 6 lines

tools/perf/Makefile.perf config-clean: · line 1024

Quoted 4 lines
tools/perf/Makefile.perf +4 -0
+BPFTOOL_CFLAGS := $(filter-out -D_GNU_SOURCE,$(CFLAGS))
+BPFTOOL := $(SKEL_TMP_OUT)/bpftool-bootstrap
+LIBBPF_SRC := $(abspath ../lib/bpf)
+BPFOBJ := $(SKEL_TMP_OUT)/libbpf.a

we already build libbpf.a, chekc 'LIBBPF = $(BPF_PATH)libbpf.a' above

Yeah, this works! Thanks!

Quoted 14 lines
Quoted 14 lines
tools/perf/Makefile.perf +13 -0
+BPF_INCLUDE := $(SKEL_TMP_OUT)
+submake_extras := feature_display=0
+
+$(SKEL_TMP_OUT):
+	$(Q)$(MKDIR) -p $@
+
+$(BPFTOOL): | $(SKEL_TMP_OUT)
+	CFLAGS= $(MAKE) $(submake_extras) -C ../bpf/bpftool \
+		OUTPUT=$(SKEL_TMP_OUT)/ bootstrap
+
+$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(BPFOBJ) | $(SKEL_TMP_OUT)
+	$(call QUIET_CLANG, $@)
+	$(Q)$(CLANG) -g -O2 -target bpf	-c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && \
Quoted 8 lines

tools/perf/Makefile.perf config-clean: · line 1041

Quoted 4 lines
tools/perf/Makefile.perf +4 -0
+	$(LLVM_STRIP) -g $@
+
+$(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL)
+	$(call QUIET_GENSKEL, $@)

is there a reason to use call in here? you could define QUIET_GENSKEL to use $@ and use it the same way as we use the rest of QUIET_* stuf in Makefile.perf

I am not following here. $(CALL QUIET_xx) was used for QUIET_CLEAN and QUIET_INSTALL in Makefile.perf. What is the preferred pattern here?

Quoted 10 lines
Quoted 10 lines

new file mode 100644
index 0000000000000..5263e9e6c5d83

tools/perf/util/bpf_skel/.gitignore new file +3 -0
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+.tmp
+*.skel.h
\ No newline at end of file
Quoted 8 lines

tools/perf/util/bpf_skel/dummy.bpf.c

Quoted 5 lines
tools/perf/util/bpf_skel/dummy.bpf.c new file +0 -0
new file mode 100644
index 0000000000000..492a43a851deb

hum, what's the reason for dummy skeleton? it just adds time to compilation no?

It is mostly to test 1/2 of the set works fine. I guess we can remove it in 2/2?

Thanks,
Song

#4 lore raw

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

On Tue, Dec 01, 2020 at 10:47:59PM +0000, Song Liu wrote:

SNIP

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines

# As per kernel Makefile, avoid funny character set dependencies unexport LC_ALL

tools/perf/Makefile.perf +0 -0
@@ -735,7 +737,8 @@ prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioc
 	$(x86_arch_prctl_code_array) \
 	$(rename_flags_array) \
Quoted 8 lines

tools/perf/Makefile.perf prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders $(drm_ioc · line 741

Quoted 4 lines
tools/perf/Makefile.perf +2 -1
 	$(arch_errno_name_array) \
-	$(sync_file_range_arrays)
+	$(sync_file_range_arrays) \
+	bpf-skel

I think the 'prepare' target is misused already with other stuff, there's generated bpf_counter.c dependency on util/bpf_skel/bpf_prog_profiler.skel.h in util/.bpf_counter.o.cmd, that should triger the build no?

Quoted 2 lines

This doesn't work for me. Once bpf-skel is removed from "prepare", we hit compilation error before util/.bpf_counter.o.cmd is generated.

ok, I'll check on that with your new version

SNIP

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
tools/perf/Makefile.perf +12 -0
+submake_extras := feature_display=0
+
+$(SKEL_TMP_OUT):
+	$(Q)$(MKDIR) -p $@
+
+$(BPFTOOL): | $(SKEL_TMP_OUT)
+	CFLAGS= $(MAKE) $(submake_extras) -C ../bpf/bpftool \
+		OUTPUT=$(SKEL_TMP_OUT)/ bootstrap
+
+$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(BPFOBJ) | $(SKEL_TMP_OUT)
+	$(call QUIET_CLANG, $@)
+	$(Q)$(CLANG) -g -O2 -target bpf	-c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && \
Quoted 8 lines

tools/perf/Makefile.perf config-clean: · line 1041

Quoted 4 lines
tools/perf/Makefile.perf +4 -0
+	$(LLVM_STRIP) -g $@
+
+$(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL)
+	$(call QUIET_GENSKEL, $@)

is there a reason to use call in here? you could define QUIET_GENSKEL to use $@ and use it the same way as we use the rest of QUIET_* stuf in Makefile.perf

Quoted 2 lines

I am not following here. $(CALL QUIET_xx) was used for QUIET_CLEAN and QUIET_INSTALL in Makefile.perf. What is the preferred pattern here?

right it's QUIET_CLEAN and QUIET_INSTALL because they need argument, QUIET_GENSKEL uses $@, which is used directly in other QUIET_* macros

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines

new file mode 100644
index 0000000000000..5263e9e6c5d83

tools/perf/util/bpf_skel/.gitignore new file +3 -0
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+.tmp
+*.skel.h
\ No newline at end of file
Quoted 8 lines

tools/perf/util/bpf_skel/dummy.bpf.c

Quoted 5 lines
tools/perf/util/bpf_skel/dummy.bpf.c new file +0 -0
new file mode 100644
index 0000000000000..492a43a851deb

hum, what's the reason for dummy skeleton? it just adds time to compilation no?

Quoted 2 lines

It is mostly to test 1/2 of the set works fine. I guess we can remove it in 2/2?

so it needs to be there otherwise the compilation fails?

I'll check your new version

thanks,
jirka

author
#5 lore raw

RFC v2 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.perf                      |   2 +-
 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 ++++++
 .../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 +
 9 files changed, 456 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.perf +1 -1
index 37b7ffe1db27c..d926f0c35ed46 100644
@@ -1013,7 +1013,7 @@ python-clean:
 
 SKEL_OUT := $(abspath util/bpf_skel)
 SKEL_TMP_OUT := $(abspath util/bpf_skel/.tmp)
-SKELETONS := $(SKEL_OUT)/dummy.skel.h
+SKELETONS := $(SKEL_OUT)/dummy.skel.h $(SKEL_OUT)/bpf_prog_profiler.skel.h
 
 ifdef BUILD_BPF_SKEL
 CLANG ?= clang
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/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
#6 lore raw
In reply to Song Liu
Quoted 7 lines · Song Liu

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 haven't addressed some known limitations:
1. Only counting events for one BPF program at a time.
2. Need extra logic in target__validate().

hi,
I'm getting this eror:

	  CLANG    /home/jolsa/linux-perf/tools/perf/util/bpf_skel/.tmp/dummy.bpf.o
	util/bpf_skel/dummy.bpf.c:4:10: fatal error: 'bpf/bpf_helpers.h' file not found
	#include <bpf/bpf_helpers.h>
		 ^~~~~~~~~~~~~~~~~~~
	1 error generated.

I added change below to fix it, but not sure it's the best fix

jirka

---

tools/perf/Makefile.perf +2 -2
index d926f0c35ed4..c8f012132d19 100644
@@ -1022,7 +1022,7 @@ BPFTOOL_CFLAGS := $(filter-out -D_GNU_SOURCE,$(CFLAGS))
 BPFTOOL := $(SKEL_TMP_OUT)/bpftool-bootstrap
 LIBBPF_SRC := $(abspath ../lib/bpf)
 BPFOBJ := $(SKEL_TMP_OUT)/libbpf.a
-BPF_INCLUDE := $(SKEL_TMP_OUT)
+BPF_INCLUDE := -I$(LIBBPF_SRC)/..
 submake_extras := feature_display=0
 
 $(SKEL_TMP_OUT):
@@ -1034,7 +1034,7 @@ $(BPFTOOL): | $(SKEL_TMP_OUT)
 
 $(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(BPFOBJ) | $(SKEL_TMP_OUT)
 	$(call QUIET_CLANG, $@)
-	$(Q)$(CLANG) -g -O2 -target bpf	-c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && \
+	$(Q)$(CLANG) -g -O2 -target bpf	$(BPF_INCLUDE) -c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && \
 	$(LLVM_STRIP) -g $@
 
 $(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL)
author
#7 lore raw
Quoted 22 lines · Jiri Olsa
Quoted 7 lines · Song Liu

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 haven't addressed some known limitations:
1. Only counting events for one BPF program at a time.
2. Need extra logic in target__validate().

hi,
I'm getting this eror:

	  CLANG    /home/jolsa/linux-perf/tools/perf/util/bpf_skel/.tmp/dummy.bpf.o
	util/bpf_skel/dummy.bpf.c:4:10: fatal error: 'bpf/bpf_helpers.h' file not found
	#include <bpf/bpf_helpers.h>
		 ^~~~~~~~~~~~~~~~~~~
	1 error generated.
Quoted 3 lines

I added change below to fix it, but not sure it's the best fix

jirka

The fix looks great to me. I will fold it in.

Thanks!
Song

Quoted 26 lines

---

tools/perf/Makefile.perf +0 -0
index d926f0c35ed4..c8f012132d19 100644
@@ -1022,7 +1022,7 @@ BPFTOOL_CFLAGS := $(filter-out -D_GNU_SOURCE,$(CFLAGS))

BPFTOOL := $(SKEL_TMP_OUT)/bpftool-bootstrap
LIBBPF_SRC := $(abspath ../lib/bpf)
BPFOBJ := $(SKEL_TMP_OUT)/libbpf.a

tools/perf/Makefile.perf +1 -1
-BPF_INCLUDE := $(SKEL_TMP_OUT)
+BPF_INCLUDE := -I$(LIBBPF_SRC)/..

submake_extras := feature_display=0

$(SKEL_TMP_OUT):

tools/perf/Makefile.perf +1 -1
@@ -1034,7 +1034,7 @@ $(BPFTOOL): | $(SKEL_TMP_OUT)
 
 $(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(BPFOBJ) | $(SKEL_TMP_OUT)
 	$(call QUIET_CLANG, $@)
-	$(Q)$(CLANG) -g -O2 -target bpf	-c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && \
+	$(Q)$(CLANG) -g -O2 -target bpf	$(BPF_INCLUDE) -c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && \
 	$(LLVM_STRIP) -g $@
 
 $(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL)