perf stat: Enable BPF counter with --for-each-cgroup
Namhyung Kim
maintainerPATCH v5 1/1 perf stat: Enable BPF counter with --for-each-cgroup
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given perf_events and to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.
---
* fix build errors
* update copyright year
* call bperf_cgrp__sync_counters during enable
* change warning when trigger reading failed
tools/perf/Makefile.perf | 17 +- tools/perf/util/Build | 1 + tools/perf/util/bpf_counter.c | 5 + tools/perf/util/bpf_counter_cgroup.c | 307 ++++++++++++++++++++ tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 191 ++++++++++++ tools/perf/util/cgroup.c | 2 + tools/perf/util/cgroup.h | 1 + 7 files changed, 523 insertions(+), 1 deletion(-) create mode 100644 tools/perf/util/bpf_counter_cgroup.c create mode 100644 tools/perf/util/bpf_skel/bperf_cgroup.bpf.c
tools/perf/Makefile.perf +16 -1
@@ -1015,6 +1015,7 @@ SKEL_OUT := $(abspath $(OUTPUT)util/bpf_skel) SKEL_TMP_OUT := $(abspath $(SKEL_OUT)/.tmp) SKELETONS := $(SKEL_OUT)/bpf_prog_profiler.skel.h SKELETONS += $(SKEL_OUT)/bperf_leader.skel.h $(SKEL_OUT)/bperf_follower.skel.h +SKELETONS += $(SKEL_OUT)/bperf_cgroup.skel.h ifdef BUILD_BPF_SKEL BPFTOOL := $(SKEL_TMP_OUT)/bootstrap/bpftool
@@ -1028,7 +1029,21 @@ $(BPFTOOL): | $(SKEL_TMP_OUT) CFLAGS= $(MAKE) -C ../bpf/bpftool \ OUTPUT=$(SKEL_TMP_OUT)/ bootstrap -$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(LIBBPF) | $(SKEL_TMP_OUT) +VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ + $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ + ../../vmlinux \ + /sys/kernel/btf/vmlinux \ + /boot/vmlinux-$(shell uname -r) +VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) + +$(SKEL_OUT)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) +ifeq ($(VMLINUX_H),) + $(QUIET_GEN)$(BPFTOOL) btf dump file $< format c > $@ +else + $(Q)cp "$(VMLINUX_H)" $@ +endif + +$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(LIBBPF) $(SKEL_OUT)/vmlinux.h | $(SKEL_TMP_OUT) $(QUIET_CLANG)$(CLANG) -g -O2 -target bpf -Wall -Werror $(BPF_INCLUDE) \ -c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ && $(LLVM_STRIP) -g $@
tools/perf/util/Build +1 -0
@@ -140,6 +140,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_PERF_BPF_SKEL) += bpf_counter_cgroup.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 +5 -0
@@ -18,6 +18,7 @@#include "evsel.h" #include "evlist.h" #include "target.h" +#include "cgroup.h" #include "cpumap.h" #include "thread_map.h"
@@ -742,6 +743,8 @@ struct bpf_counter_ops bperf_ops = { .destroy = bperf__destroy, }; +extern struct bpf_counter_ops bperf_cgrp_ops; +static inline bool bpf_counter_skip (struct evsel *evsel) { return list_empty(&evsel->bpf_counter_list) &&
@@ -759,6 +762,8 @@ int bpf_counter__load(struct evsel *evsel, struct target *target) {if (target->bpf_str) evsel->bpf_counter_ops = &bpf_program_profiler_ops; +else if (cgrp_event_expanded && target->use_bpf) + evsel->bpf_counter_ops = &bperf_cgrp_ops;else if (target->use_bpf || evsel->bpf_counter ||evsel__match_bpf_counter_events (evsel->name)) evsel->bpf_counter_ops = &bperf_ops;
tools/perf/util/bpf_counter_cgroup.c new file +307 -0
307 lines not on this page · raw message
tools/perf/util/bpf_skel/bperf_cgroup.bpf.c new file +191 -0
191 lines not on this page · raw message
tools/perf/util/cgroup.c +2 -0
@@ -18,6 +18,7 @@#include <regex.h> int nr_cgroups; +bool cgrp_event_expanded;/* used to match cgroup name with patterns */ struct cgroup_name {
@@ -484,6 +485,7 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, } ret =0 ; + cgrp_event_expanded = true; out_err:evlist__delete (orig_list);
tools/perf/util/cgroup.h +1 -0
@@ -17,6 +17,7 @@ struct cgroup { };extern int nr_cgroups;/* number of explicit cgroups defined */ +extern bool cgrp_event_expanded;struct cgroup *cgroup__get (struct cgroup *cgroup);void cgroup__put (struct cgroup *cgroup);
Signature · 1 line
2.32.0.93.g670b81a890-goog
Quoted 12 lines · Namhyung Kim
Recently bperf was added to use BPF to count perf events for various purposes. This is an extension for the approach and targetting to cgroup usages.
Unlike the other bperf, it doesn't share the events with other processes but it'd reduce unnecessary events (and the overhead of multiplexing) for each monitored cgroup within the perf session.
Quoted 8 lines
When --for-each-cgroup is used with --bpf-counters, it will open cgroup-switches event per cpu internally and attach the new BPF program to read given
perf_eventsand to aggregate the results for cgroups. It's only called when task is switched to a task in a different cgroup.Cc Song Liu <[email protected]>Signed-off-by Namhyung Kim <[email protected]>