bpftool: introduce prog profile
Subsystems (4)
v6 cover bpftool: introduce prog profile
This set introduces bpftool prog profile command, which uses hardware counters to profile BPF programs.
This command attaches fentry/fexit programs to a target program. These two programs read hardware counters before and after the target program and calculate the difference.
Changes v5 => v6:
1. Use new header bpf_tracing.h (Yonghong).
Changes v4 => v5:
1. Adjust perf_event_attr for the events (Jiri).
Changes v3 => v4:
1. Simplify err handling in profile_open_perf_events() (Quentin);
2. Remove redundant p_err() (Quentin);
3. Replace tab with space in bash-completion; (Quentin);
4. Fix typo _bpftool_get_map_names => _bpftool_get_prog_names (Quentin).
Changes v2 => v3:
1. Change order of arguments (Quentin), as:
bpftool prog profile PROG [duration DURATION] METRICs
2. Add bash-completion for bpftool prog profile (Quentin);
3. Fix build of selftests (Yonghong);
4. Better handling of bpf_map_lookup_elem() returns (Yonghong);
5. Improve clean up logic of do_profile() (Yonghong);
6. Other smaller fixes/cleanups.
Changes RFC => v2:
1. Use new bpf_program__set_attach_target() API;
2. Update output format to be perf-stat like (Alexei);
3. Incorporate skeleton generation into Makefile;
4. Make DURATION optional and Allow Ctrl-C (Alexei);
5. Add calcated values "insn per cycle" and "LLC misses per million isns".
Song Liu (4):
bpftool: introduce "prog profile" command
bpftool: Documentation for bpftool prog profile
ybpftool: bash completion for "bpftool prog profile"
bpftool: fix typo in bash-completion
.../bpftool/Documentation/bpftool-prog.rst | 19 + tools/bpf/bpftool/Makefile | 18 + tools/bpf/bpftool/bash-completion/bpftool | 47 +- tools/bpf/bpftool/prog.c | 424 +++++++++++++++++- tools/bpf/bpftool/skeleton/profiler.bpf.c | 119 +++++ tools/bpf/bpftool/skeleton/profiler.h | 47 ++ tools/scripts/Makefile.include | 1 + 7 files changed, 672 insertions(+), 3 deletions(-) create mode 100644 tools/bpf/bpftool/skeleton/profiler.bpf.c create mode 100644 tools/bpf/bpftool/skeleton/profiler.h
--
2.17.1
PATCH v6 1/4 bpftool: introduce "prog profile" command
With fentry/fexit programs, it is possible to profile BPF program with hardware counters. Introduce bpftool "prog profile", which measures key metrics of a BPF program.
bpftool prog profile command creates per-cpu perf events. Then it attaches fentry/fexit programs to the target BPF program. The fentry program saves perf event value to a map. The fexit program reads the perf event again, and calculates the difference, which is the instructions/cycles used by the target program.
Example input and output:
./bpftool prog profile id 337 duration 3 cycles instructions llc_misses
4228 run_cnt
3403698 cycles (84.08%)
3525294 instructions # 1.04 insn per cycle (84.05%)
13 llc_misses # 3.69 LLC misses per million isns (83.50%)This command measures cycles and instructions for BPF program with id 337 for 3 seconds. The program has triggered 4228 times. The rest of the output is similar to perf-stat. In this example, the counters were only counting ~84% of the time because of time multiplexing of perf counters.
Note that, this approach measures cycles and instructions in very small increments. So the fentry/fexit programs introduce noticeable errors to the measurement results.
The fentry/fexit programs are generated with BPF skeletons. Therefore, we build bpftool twice. The first time _bpftool is built without skeletons. Then, _bpftool is used to generate the skeletons. The second time, bpftool is built with skeletons.
--- tools/bpf/bpftool/Makefile | 18 + tools/bpf/bpftool/prog.c | 424 +++++++++++++++++++++- tools/bpf/bpftool/skeleton/profiler.bpf.c | 119 ++++++ tools/bpf/bpftool/skeleton/profiler.h | 47 +++ tools/scripts/Makefile.include | 1 + 5 files changed, 608 insertions(+), 1 deletion(-) create mode 100644 tools/bpf/bpftool/skeleton/profiler.bpf.c create mode 100644 tools/bpf/bpftool/skeleton/profiler.h
tools/bpf/bpftool/Makefile +18 -0
@@ -59,6 +59,7 @@ LIBS = $(LIBBPF) -lelf -lz INSTALL ?= install RM ?= rm -f +CLANG ?= clang FEATURE_USER = .bpftool FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib
@@ -110,6 +111,22 @@ SRCS += $(BFD_SRCS) endif OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o +_OBJS = $(filter-out $(OUTPUT)prog.o,$(OBJS)) $(OUTPUT)_prog.o + +$(OUTPUT)_prog.o: prog.c + $(QUIET_CC)$(COMPILE.c) -MMD -DBPFTOOL_WITHOUT_SKELETONS -o $@ $< + +$(OUTPUT)_bpftool: $(_OBJS) $(LIBBPF) + $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(_OBJS) $(LIBS) + +skeleton/profiler.bpf.o: skeleton/profiler.bpf.c + $(QUIET_CLANG)$(CLANG) -I$(srctree)/tools/lib -g -O2 -target bpf -c $< -o $@ + +profiler.skel.h: $(OUTPUT)_bpftool skeleton/profiler.bpf.o + $(QUIET_GEN)$(OUTPUT)./_bpftool gen skeleton skeleton/profiler.bpf.o > $@ + +$(OUTPUT)prog.o: prog.c profiler.skel.h + $(QUIET_CC)$(COMPILE.c) -MMD -o $@ $< $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c $(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
@@ -125,6 +142,7 @@ $(OUTPUT)%.o: %.c clean: $(LIBBPF)-clean $(call QUIET_CLEAN, bpftool) $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d + $(Q)$(RM) -- $(OUTPUT)_bpftool profiler.skel.h skeleton/profiler.bpf.o $(Q)$(RM) -r -- $(OUTPUT)libbpf/ $(call QUIET_CLEAN, core-gen) $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
tools/bpf/bpftool/prog.c +423 -1
469 lines not on this page · raw message
tools/bpf/bpftool/skeleton/profiler.bpf.c new file +119 -0
119 lines not on this page · raw message
tools/bpf/bpftool/skeleton/profiler.h new file +47 -0
47 lines not on this page · raw message
tools/scripts/Makefile.include +1 -0
@@ -106,6 +106,7 @@ ifneq ($(silent),1) ifneq ($(V),1) QUIET_CC = @echo ' CC '$@; QUIET_CC_FPIC = @echo ' CC FPIC '$@; + QUIET_CLANG = @echo ' CLANG '$@; QUIET_AR = @echo ' AR '$@; QUIET_LINK = @echo ' LINK '$@; QUIET_MKDIR = @echo ' MKDIR '$@;
Signature · 1 line
2.17.1
Re: [PATCH v6 bpf-next 1/4] bpftool: introduce "prog profile" command
Quoted 28 lines · Song Liu
With fentry/fexit programs, it is possible to profile BPF program with hardware counters. Introduce bpftool "prog profile", which measures key metrics of a BPF program.
bpftool prog profile command creates per-cpu perf events. Then it attaches fentry/fexit programs to the target BPF program. The fentry program saves perf event value to a map. The fexit program reads the perf event again, and calculates the difference, which is the instructions/cycles used by the target program.
Example input and output:
./bpftool prog profile id 337 duration 3 cycles instructions llc_misses 4228 run_cnt 3403698 cycles (84.08%) 3525294 instructions # 1.04 insn per cycle (84.05%) 13 llc_misses # 3.69 LLC misses per million isns (83.50%)This command measures cycles and instructions for BPF program with id 337 for 3 seconds. The program has triggered 4228 times. The rest of the output is similar to perf-stat. In this example, the counters were only counting ~84% of the time because of time multiplexing of perf counters.
Note that, this approach measures cycles and instructions in very small increments. So the fentry/fexit programs introduce noticeable errors to the measurement results.
Quoted 7 lines
The fentry/fexit programs are generated with BPF skeletons. Therefore, we build bpftool twice. The first time
_bpftoolis built without skeletons. Then,_bpftoolis used to generate the skeletons. The second time, bpftool is built with skeletons.Reviewed-by Quentin Monnet <[email protected]>Signed-off-by Song Liu <[email protected]>
PATCH v6 2/4 bpftool: Documentation for bpftool prog profile
In reply to Song LiuAdd documentation for the new bpftool prog profile command.
--- .../bpftool/Documentation/bpftool-prog.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
tools/bpf/bpftool/Documentation/bpftool-prog.rst +19 -0
@@ -30,6 +30,7 @@ PROG COMMANDS | **bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*] | **bpftool** **prog tracelog** | **bpftool** **prog run** *PROG* **data_in** *FILE* [**data_out** *FILE* [**data_size_out** *L*]] [**ctx_in** *FILE* [**ctx_out** *FILE* [**ctx_size_out** *M*]]] [**repeat** *N*] +| **bpftool** **prog profile** *PROG* [**duration** *DURATION*] *METRICs* | **bpftool** **prog help** | | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
@@ -48,6 +49,9 @@ PROG COMMANDS | *ATTACH_TYPE* := { | **msg_verdict** | **stream_verdict** | **stream_parser** | **flow_dissector** | } +| *METRIC* := { +| **cycles** | **instructions** | **l1d_loads** | **llc_misses** +| } DESCRIPTION
@@ -189,6 +193,12 @@ DESCRIPTION not all of them can take the **ctx_in**/**ctx_out** arguments. bpftool does not perform checks on program types. + **bpftool prog profile** *PROG* [**duration** *DURATION*] *METRICs* + Profile *METRICs* for bpf program *PROG* for *DURATION* + seconds or until user hits Ctrl-C. *DURATION* is optional. + If *DURATION* is not specified, the profiling will run up to + UINT_MAX seconds. + **bpftool prog help** Print short help message.
@@ -311,6 +321,15 @@ EXAMPLES **# rm /sys/fs/bpf/xdp1** +| +| **# bpftool prog profile id 337 duration 10 cycles instructions llc_misses** + +:: + 51397 run_cnt + 40176203 cycles (83.05%) + 42518139 instructions # 1.06 insns per cycle (83.39%) + 123 llc_misses # 2.89 LLC misses per million insns (83.15%) + SEE ALSO ======== **bpf**\ (2),
Signature · 1 line
2.17.1
PATCH v6 3/4 ybpftool: bash completion for "bpftool prog profile"
In reply to Song LiuAdd bash completion for "bpftool prog profile" command.
--- tools/bpf/bpftool/bash-completion/bpftool | 45 ++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)
tools/bpf/bpftool/bash-completion/bpftool +44 -1
@@ -337,6 +337,7 @@ _bpftool() local PROG_TYPE='id pinned tag name' local MAP_TYPE='id pinned name' + local METRIC_TYPE='cycles instructions l1d_loads llc_misses' case $command in show|list) [[ $prev != "$command" ]] && return 0
@@ -498,6 +499,48 @@ _bpftool() tracelog) return 0 ;; + profile) + case $cword in + 3) + COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) ) + return 0 + ;; + 4) + case $prev in + id) + _bpftool_get_prog_ids + ;; + name) + _bpftool_get_prog_names + ;; + pinned) + _filedir + ;; + esac + return 0 + ;; + 5) + COMPREPLY=( $( compgen -W "$METRIC_TYPE duration" -- "$cur" ) ) + return 0 + ;; + 6) + case $prev in + duration) + return 0 + ;; + *) + COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) ) + return 0 + ;; + esac + return 0 + ;; + *) + COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) ) + return 0 + ;; + esac + ;; run) if [[ ${#words[@]} -lt 5 ]]; then _filedir
@@ -525,7 +568,7 @@ _bpftool() *) [[ $prev == $object ]] && \ COMPREPLY=( $( compgen -W 'dump help pin attach detach \ - load loadall show list tracelog run' -- "$cur" ) ) + load loadall show list tracelog run profile' -- "$cur" ) ) ;; esac ;;
Signature · 1 line
2.17.1
PATCH v6 4/4 bpftool: fix typo in bash-completion
In reply to Song Liu_bpftool_get_map_names => _bpftool_get_prog_names for prog-attach|detach.
--- tools/bpf/bpftool/bash-completion/bpftool | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/bash-completion/bpftool +1 -1
@@ -389,7 +389,7 @@ _bpftool() _bpftool_get_prog_ids ;; name) - _bpftool_get_map_names + _bpftool_get_prog_names ;; pinned) _filedir
Signature · 1 line
2.17.1
Daniel Borkmann
maintainerQuoted 35 lines · Song Liu
This set introduces bpftool prog profile command, which uses hardware counters to profile BPF programs.
This command attaches fentry/fexit programs to a target program. These two programs read hardware counters before and after the target program and calculate the difference.
Changes v5 => v6:
1. Use new headerbpf_tracing.h(Yonghong).Changes v4 => v5:
1. Adjustperf_event_attrfor the events (Jiri).Changes v3 => v4:
1. Simplify err handling inprofile_open_perf_events()(Quentin);
2. Remove redundantp_err()(Quentin);
3. Replace tab with space in bash-completion; (Quentin);
4. Fix typo_bpftool_get_map_names=>_bpftool_get_prog_names(Quentin).Changes v2 => v3:
1. Change order of arguments (Quentin), as:
bpftool prog profile PROG [duration DURATION] METRICs
2. Add bash-completion for bpftool prog profile (Quentin);
3. Fix build of selftests (Yonghong);
4. Better handling ofbpf_map_lookup_elem()returns (Yonghong);
5. Improve clean up logic ofdo_profile()(Yonghong);
6. Other smaller fixes/cleanups.Changes RFC => v2:
1. Use newbpf_program__set_attach_target()API;
2. Update output format to be perf-stat like (Alexei);
3. Incorporate skeleton generation intoMakefile;
4. Make DURATION optional and Allow Ctrl-C (Alexei);
5. Add calcated values "insn per cycle" and "LLC misses per million isns".
Quoted 4 lines
Song Liu (4):
bpftool: introduce "prog profile" command
bpftool: Documentation for bpftool prog profile
ybpftool: bash completion for "bpftool prog profile"
Applied and fixed subject typo above, thanks.
Please follow-up with a feature probe to support older clang/llvm versions.
Quoted 1 line
bpftool: fix typo in bash-completion