Skip to contentSkip to conversation

Introduce perf-stat -b for BPF programs

PATCH In review Song Liu 2 participants 7 messages Dec 2, 2020 – Dec 4, 2020

Subsystems (3)
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.

Changes RFC v2 => PATCH v1:
1. Support counting on multiple BPF programs.
2. Add BPF handling to target__validate().
3. Improve Makefile. (Jiri)

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

     (Jiri)

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

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

 tools/bpf/bpftool/Makefile                    |   2 +
 tools/build/Makefile.feature                  |   4 +-
 tools/perf/Makefile.config                    |  10 +
 tools/perf/Makefile.perf                      |  46 ++-
 tools/perf/builtin-stat.c                     |  70 ++++-
 tools/perf/util/Build                         |   1 +
 tools/perf/util/bpf_counter.c                 | 281 ++++++++++++++++++
 tools/perf/util/bpf_counter.h                 |  73 +++++
 tools/perf/util/bpf_skel/.gitignore           |   3 +
 .../util/bpf_skel/bpf_prog_profiler.bpf.c     |  96 ++++++
 tools/perf/util/evsel.c                       |  11 +
 tools/perf/util/evsel.h                       |   6 +
 tools/perf/util/stat-display.c                |   4 +-
 tools/perf/util/target.c                      |  34 ++-
 tools/perf/util/target.h                      |  10 +
 tools/scripts/Makefile.include                |   1 +
 16 files changed, 636 insertions(+), 16 deletions(-)
 create mode 100644 tools/perf/util/bpf_counter.c
 create mode 100644 tools/perf/util/bpf_counter.h
 create mode 100644 tools/perf/util/bpf_skel/.gitignore
 create mode 100644 tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c

--
2.24.1

author
#1 lore raw

PATCH 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         |  4 ++-
 tools/perf/Makefile.config           | 10 ++++++
 tools/perf/Makefile.perf             | 46 ++++++++++++++++++++++++++--
 tools/perf/util/bpf_skel/.gitignore  |  3 ++
 tools/perf/util/bpf_skel/dummy.bpf.c | 19 ++++++++++++
 tools/scripts/Makefile.include       |  1 +
 7 files changed, 82 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 +3 -1
index 97cbfb31b7625..4eeeabbaa2947 100644
@@ -99,7 +99,9 @@ FEATURE_TESTS_EXTRA :=                  \
          clang                          \
          libbpf                         \
          libpfm4                        \
-         libdebuginfod
+         libdebuginfod			\
+	 clang-bpf-co-re
+
 
 FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
 
tools/perf/Makefile.config +10 -0
index ce8516e4de34f..c8ff82b4fc1b2 100644
@@ -621,6 +621,16 @@ ifndef NO_LIBBPF
   endif
 endif
 
+$(call feature_check,clang-bpf-co-re)
+ifeq ($(feature-clang-bpf-co-re), 0)
+  undefine BUILD_BPF_SKEL
+endif
+
+ifdef BUILD_BPF_SKEL
+    $(call detected,CONFIG_PERF_BPF_SKEL)
+    CFLAGS += -DBUILD_BPF_SKEL
+endif
+
 dwarf-post-unwind := 1
 dwarf-post-unwind-text := BUG
 
tools/perf/Makefile.perf +44 -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: [PATCH v1 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         |  4 ++-
 tools/perf/Makefile.config           | 10 ++++++
 tools/perf/Makefile.perf             | 46 ++++++++++++++++++++++++++--
 tools/perf/util/bpf_skel/.gitignore  |  3 ++
 tools/perf/util/bpf_skel/dummy.bpf.c | 19 ++++++++++++
 tools/scripts/Makefile.include       |  1 +
 7 files changed, 82 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..4eeeabbaa2947 100644

tools/build/Makefile.feature FEATURE_TESTS_EXTRA := \ · line 103

Quoted 6 lines
tools/build/Makefile.feature +2 -1
@@ -99,7 +99,9 @@ FEATURE_TESTS_EXTRA :=                  \
          clang                          \
          libbpf                         \
          libpfm4                        \
-         libdebuginfod
+         libdebuginfod			\
+	 clang-bpf-co-re

do not use tabs in here

Quoted 9 lines
tools/build/Makefile.feature +1 -0
+
 
 FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
 
tools/perf/Makefile.config +0 -0
index ce8516e4de34f..c8ff82b4fc1b2 100644

tools/perf/Makefile.config ifndef NO_LIBBPF · line 626

Quoted 6 lines
tools/perf/Makefile.config +3 -0
@@ -621,6 +621,16 @@ ifndef NO_LIBBPF
   endif
 endif
 
+$(call feature_check,clang-bpf-co-re)
+ifeq ($(feature-clang-bpf-co-re), 0)
+  undefine BUILD_BPF_SKEL

we compile with 'make BUILD_BPF_SKEL=1' so we should fail here with the standard 'please install..' message we use

Quoted 24 lines
tools/perf/Makefile.config +7 -0
+endif
+
+ifdef BUILD_BPF_SKEL
+    $(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..f46f0cd012b2d 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 LD += $(EXTRA_LDFLAGS) · line 183

Quoted 4 lines
tools/perf/Makefile.perf +1 -0
@@ -178,6 +180,8 @@ LD += $(EXTRA_LDFLAGS)
 HOSTCC  ?= gcc
 HOSTLD  ?= ld
 HOSTAR  ?= ar
+CLANG ?= clang

please keep the indent with other ?= when possible

Quoted 15 lines
tools/perf/Makefile.perf +3 -1
+LLVM_STRIP ?= llvm-strip
 
 PKG_CONFIG = $(CROSS_COMPILE)pkg-config
 LLVM_CONFIG ?= llvm-config
@@ -735,7 +739,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) $@

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

Quoted 6 lines
tools/perf/Makefile.perf +2 -1
@@ -1008,7 +1013,44 @@ config-clean:
 python-clean:
 	$(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)

hum, actualy we use $(OUTPUT) and if it's not defined we build directly in the tree, this should follow the same

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

Quoted 7 lines
tools/perf/Makefile.perf +7 -0
+SKELETONS := $(SKEL_OUT)/dummy.skel.h
+
+ifdef BUILD_BPF_SKEL
+BPFTOOL := $(SKEL_TMP_OUT)/bpftool-bootstrap
+LIBBPF_SRC := $(abspath ../lib/bpf)
+BPF_INCLUDE := -I$(SKEL_TMP_OUT)/..
+submake_extras := feature_display=0

there's no need for the variable, is there?
also why do we want to hide it?

thanks,
jirka

author
#3 lore raw

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

Quoted 12 lines · Jiri Olsa
Quoted 7 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 4 lines
Quoted 4 lines

index 97cbfb31b7625..4eeeabbaa2947 100644

tools/build/Makefile.feature +0 -0
@@ -99,7 +99,9 @@ FEATURE_TESTS_EXTRA :=                  \
Quoted 8 lines

tools/build/Makefile.feature FEATURE_TESTS_EXTRA := \ · line 103

Quoted 6 lines
tools/build/Makefile.feature +2 -1
         clang                          \
         libbpf                         \
         libpfm4                        \
-         libdebuginfod
+         libdebuginfod			\
+	 clang-bpf-co-re

do not use tabs in here

Will fix.

Quoted 13 lines
Quoted 13 lines
tools/build/Makefile.feature +1 -0
+
 
 FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
 
tools/perf/Makefile.config +0 -0
index ce8516e4de34f..c8ff82b4fc1b2 100644
@@ -621,6 +621,16 @@ ifndef NO_LIBBPF
  endif

endif

Quoted 6 lines

tools/perf/Makefile.config ifndef NO_LIBBPF · line 626

Quoted 3 lines

+$(call feature_check,clang-bpf-co-re)

tools/perf/Makefile.config +2 -0
+ifeq ($(feature-clang-bpf-co-re), 0)
+  undefine BUILD_BPF_SKEL

we compile with 'make BUILD_BPF_SKEL=1' so we should fail here with the standard 'please install..' message we use

Yeah, that makes sense. Will fix it in the next version.

Quoted 22 lines
Quoted 22 lines
tools/perf/Makefile.config +7 -0
+endif
+
+ifdef BUILD_BPF_SKEL
+    $(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..f46f0cd012b2d 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 9 lines
Quoted 7 lines
tools/perf/Makefile.perf +0 -0
 # As per kernel Makefile, avoid funny character set dependencies

unexport LC_ALL

tools/perf/Makefile.perf +0 -0
@@ -178,6 +180,8 @@ LD += $(EXTRA_LDFLAGS)
HOSTCC  ?= gcc
HOSTLD  ?= ld
HOSTAR  ?= ar

+CLANG ?= clang

please keep the indent with other ?= when possible

Will fix.

Quoted 19 lines
Quoted 19 lines

+LLVM_STRIP ?= llvm-strip

PKG_CONFIG = $(CROSS_COMPILE)pkg-config
LLVM_CONFIG ?= llvm-config

tools/perf/Makefile.perf +2 -1
@@ -735,7 +739,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) $@
tools/perf/Makefile.perf +0 -0
@@ -1008,7 +1013,44 @@ config-clean:

python-clean:

	$(python-clean)
Quoted 6 lines

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

Quoted 3 lines
tools/perf/Makefile.perf +2 -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)

hum, actualy we use $(OUTPUT) and if it's not defined we build directly in the tree, this should follow the same

Good catch! I tested with OUTPUT in earlier version, missed it later on. Will fix it in the next version.

Quoted 10 lines

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

Quoted 7 lines
tools/perf/Makefile.perf +7 -0
+SKELETONS := $(SKEL_OUT)/dummy.skel.h
+
+ifdef BUILD_BPF_SKEL
+BPFTOOL := $(SKEL_TMP_OUT)/bpftool-bootstrap
+LIBBPF_SRC := $(abspath ../lib/bpf)
+BPF_INCLUDE := -I$(SKEL_TMP_OUT)/..
+submake_extras := feature_display=0

there's no need for the variable, is there?
also why do we want to hide it?

I guess no need to hide it. I copied this pattern from bpftool. Let me remove it in v2.

Thanks,
Song

author
#4 lore raw

PATCH 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                     |  70 ++++-
 tools/perf/util/Build                         |   1 +
 tools/perf/util/bpf_counter.c                 | 281 ++++++++++++++++++
 tools/perf/util/bpf_counter.h                 |  73 +++++
 .../util/bpf_skel/bpf_prog_profiler.bpf.c     |  96 ++++++
 tools/perf/util/bpf_skel/dummy.bpf.c          |  19 --
 tools/perf/util/evsel.c                       |  11 +
 tools/perf/util/evsel.h                       |   6 +
 tools/perf/util/stat-display.c                |   4 +-
 tools/perf/util/target.c                      |  34 ++-
 tools/perf/util/target.h                      |  10 +
 12 files changed, 574 insertions(+), 33 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
 delete mode 100644 tools/perf/util/bpf_skel/dummy.bpf.c
tools/perf/Makefile.perf +1 -1
index f46f0cd012b2d..8a7085fb6a7fd 100644
@@ -1015,7 +1015,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)/bpf_prog_profiler.skel.h
 
 ifdef BUILD_BPF_SKEL
 BPFTOOL := $(SKEL_TMP_OUT)/bpftool-bootstrap
tools/perf/builtin-stat.c +59 -11

159 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 +281 -0

281 lines not on this page · raw message

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

73 lines not on this page · raw message

tools/perf/util/bpf_skel/bpf_prog_profiler.bpf.c new file +96 -0

96 lines not on this page · raw message

tools/perf/util/bpf_skel/dummy.bpf.c deleted +0 -19
deleted file mode 100644
index 492a43a851deb..0000000000000
@@ -1,19 +0,0 @@
-// 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/perf/util/evsel.c +11 -0
index 1cad6051d8b08..c6a50467076f8 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;
 
@@ -247,6 +252,7 @@ void evsel__init(struct evsel *evsel,
 	evsel->bpf_obj	   = NULL;
 	evsel->bpf_fd	   = -1;
 	INIT_LIST_HEAD(&evsel->config_terms);
+	INIT_LIST_HEAD(&evsel->bpf_counter_list);
 	perf_evsel__object.init(evsel);
 	evsel->sample_size = __evsel__sample_size(attr->sample_type);
 	evsel__calc_id_pos(evsel);
@@ -1365,6 +1371,7 @@ void evsel__exit(struct evsel *evsel)
 {
 	assert(list_empty(&evsel->core.node));
 	assert(evsel->evlist == NULL);
+	bpf_counter__destroy(evsel);
 	evsel__free_counts(evsel);
 	perf_evsel__free_fd(&evsel->core);
 	perf_evsel__free_id(&evsel->core);
@@ -1770,6 +1777,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 		evsel->core.attr.sample_id_all = 0;
 
 	display_attr(&evsel->core.attr);
+	if (!list_empty(&evsel->bpf_counter_list))
+		evsel->core.attr.inherit = 0;
 
 	for (cpu = start_cpu; cpu < end_cpu; cpu++) {
 
@@ -1788,6 +1797,8 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 
 			FD(evsel, cpu, thread) = fd;
 
+			bpf_counter__install_pe(evsel, cpu, fd);
+
 			if (unlikely(test_attr__enabled)) {
 				test_attr__open(&evsel->core.attr, pid, cpus->map[cpu],
 						fd, group_fd, flags);
tools/perf/util/evsel.h +6 -0
index 79a860d8e3eef..1731fba702bf4 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_ops;
+struct target;
 
 typedef int (evsel__sb_cb_t)(union perf_event *event, void *data);
 
@@ -127,6 +130,8 @@ struct evsel {
 	 * See also evsel__has_callchain().
 	 */
 	__u64			synth_sample_type;
+	struct list_head	bpf_counter_list;
+	struct bpf_counter_ops	*bpf_counter_ops;
 };
 
 struct perf_missing_features {
@@ -423,4 +428,5 @@ static inline bool evsel__is_dummy_event(struct evsel *evsel)
 struct perf_env *evsel__env(struct evsel *evsel);
 
 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
+
 #endif /* __PERF_EVSEL_H */
tools/perf/util/stat-display.c +3 -1
index 4b57c0c076323..1536cfe0e50b4 100644
@@ -1029,7 +1029,9 @@ static void print_header(struct perf_stat_config *config,
 	if (!config->csv_output) {
 		fprintf(output, "\n");
 		fprintf(output, " Performance counter stats for ");
-		if (_target->system_wide)
+		if (_target->bpf_str)
+			fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
+		else if (_target->system_wide)
 			fprintf(output, "\'system wide");
 		else if (_target->cpu_list)
 			fprintf(output, "\'CPU(s) %s", _target->cpu_list);
tools/perf/util/target.c +33 -1
index a3db13dea937c..0f383418e3df5 100644
@@ -56,6 +56,34 @@ enum target_errno target__validate(struct target *target)
 			ret = TARGET_ERRNO__UID_OVERRIDE_SYSTEM;
 	}
 
+	/* BPF and CPU are mutually exclusive */
+	if (target->bpf_str && target->cpu_list) {
+		target->cpu_list = NULL;
+		if (ret == TARGET_ERRNO__SUCCESS)
+			ret = TARGET_ERRNO__BPF_OVERRIDE_CPU;
+	}
+
+	/* BPF and PID/TID are mutually exclusive */
+	if (target->bpf_str && target->tid) {
+		target->tid = NULL;
+		if (ret == TARGET_ERRNO__SUCCESS)
+			ret = TARGET_ERRNO__BPF_OVERRIDE_PID;
+	}
+
+	/* BPF and UID are mutually exclusive */
+	if (target->bpf_str && target->uid_str) {
+		target->uid_str = NULL;
+		if (ret == TARGET_ERRNO__SUCCESS)
+			ret = TARGET_ERRNO__BPF_OVERRIDE_UID;
+	}
+
+	/* BPF and THREADS are mutually exclusive */
+	if (target->bpf_str && target->per_thread) {
+		target->per_thread = false;
+		if (ret == TARGET_ERRNO__SUCCESS)
+			ret = TARGET_ERRNO__BPF_OVERRIDE_THREAD;
+	}
+
 	/* THREAD and SYSTEM/CPU are mutually exclusive */
 	if (target->per_thread && (target->system_wide || target->cpu_list)) {
 		target->per_thread = false;
@@ -109,6 +137,10 @@ static const char *target__error_str[] = {
 	"PID/TID switch overriding SYSTEM",
 	"UID switch overriding SYSTEM",
 	"SYSTEM/CPU switch overriding PER-THREAD",
+	"BPF switch overriding CPU",
+	"BPF switch overriding PID/TID",
+	"BPF switch overriding UID",
+	"BPF switch overriding THREAD",
 	"Invalid User: %s",
 	"Problems obtaining information for user %s",
 };
@@ -134,7 +166,7 @@ int target__strerror(struct target *target, int errnum,
 
 	switch (errnum) {
 	case TARGET_ERRNO__PID_OVERRIDE_CPU ...
-	     TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD:
+	     TARGET_ERRNO__BPF_OVERRIDE_THREAD:
 		snprintf(buf, buflen, "%s", msg);
 		break;
 
tools/perf/util/target.h +10 -0
index 6ef01a83b24e9..f132c6c2eef81 100644
@@ -10,6 +10,7 @@ struct target {
 	const char   *tid;
 	const char   *cpu_list;
 	const char   *uid_str;
+	const char   *bpf_str;
 	uid_t	     uid;
 	bool	     system_wide;
 	bool	     uses_mmap;
@@ -36,6 +37,10 @@ enum target_errno {
 	TARGET_ERRNO__PID_OVERRIDE_SYSTEM,
 	TARGET_ERRNO__UID_OVERRIDE_SYSTEM,
 	TARGET_ERRNO__SYSTEM_OVERRIDE_THREAD,
+	TARGET_ERRNO__BPF_OVERRIDE_CPU,
+	TARGET_ERRNO__BPF_OVERRIDE_PID,
+	TARGET_ERRNO__BPF_OVERRIDE_UID,
+	TARGET_ERRNO__BPF_OVERRIDE_THREAD,
 
 	/* for target__parse_uid() */
 	TARGET_ERRNO__INVALID_UID,
@@ -59,6 +64,11 @@ static inline bool target__has_cpu(struct target *target)
 	return target->system_wide || target->cpu_list;
 }
 
+static inline bool target__has_bpf(struct target *target)
+{
+	return target->bpf_str;
+}
+
 static inline bool target__none(struct target *target)
 {
 	return !target__has_task(target) && !target__has_cpu(target);
Signature · 1 line
2.24.1
#5 lore raw

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

Quoted 11 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

hi,
I'm getting compile fail with gcc 10:

builtin-stat.c: In function ‘cmd_stat’:
builtin-stat.c:2429:7: error: declaration of ‘err’ shadows a previous local [-Werror=shadow]
2429 | int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
| ^~~
builtin-stat.c:2104:33: note: shadowed declaration is here
2104 | int status = -EINVAL, run_idx, err;
| ^~~

jirka

author
#6 lore raw

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

Quoted 20 lines · Jiri Olsa
Quoted 11 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

hi,
I'm getting compile fail with gcc 10:

Quoted 7 lines

builtin-stat.c: In function ‘cmd_stat’:
builtin-stat.c:2429:7: error: declaration of ‘err’ shadows a previous local [-Werror=shadow]
2429 | int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
| ^~~
builtin-stat.c:2104:33: note: shadowed declaration is here
2104 | int status = -EINVAL, run_idx, err;
| ^~~

gcc 9 didn't complain about it. Let me fix it in the next version.

Thanks,
Song