bpftool: introduce prog profile
Subsystems (4)
v3 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.
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 (3):
bpftool: introduce "prog profile" command
bpftool: Documentation for bpftool prog profile
bpftool: bash completion for "bpftool prog profile"
.../bpftool/Documentation/bpftool-prog.rst | 19 + tools/bpf/bpftool/Makefile | 18 + tools/bpf/bpftool/bash-completion/bpftool | 45 +- tools/bpf/bpftool/prog.c | 432 +++++++++++++++++- tools/bpf/bpftool/skeleton/profiler.bpf.c | 171 +++++++ tools/bpf/bpftool/skeleton/profiler.h | 47 ++ tools/scripts/Makefile.include | 1 + 7 files changed, 731 insertions(+), 2 deletions(-) create mode 100644 tools/bpf/bpftool/skeleton/profiler.bpf.c create mode 100644 tools/bpf/bpftool/skeleton/profiler.h
--
2.17.1
PATCH v3 1/3 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 | 432 +++++++++++++++++++++- tools/bpf/bpftool/skeleton/profiler.bpf.c | 171 +++++++++ tools/bpf/bpftool/skeleton/profiler.h | 47 +++ tools/scripts/Makefile.include | 1 + 5 files changed, 668 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 +431 -1
477 lines not on this page · raw message
tools/bpf/bpftool/skeleton/profiler.bpf.c new file +171 -0
171 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 v3 bpf-next 1/3] bpftool: introduce "prog profile" command
2020-03-03 11:55 UTC-0800 ~ Song Liu <[email protected]>
Quoted 28 lines
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_misses4228 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.Signed-off-by Song Liu <[email protected]>---
Hi Song, thank you for all the changes! Just found a couple more things below, sorry I missed them on the v2.
[...]
Quoted 2 lines
tools/bpf/bpftool/prog.c +0
-0
[...]
Quoted 21 lines
tools/bpf/bpftool/prog.c +21
-0+static int profile_open_perf_events (struct profiler_bpf *obj) +{ +unsigned int cpu, m; +int map_fd, pmu_fd; + + profile_perf_events =calloc ( +sizeof (int ), obj->rodata->num_cpu * obj->rodata->num_metric); +if (!profile_perf_events) { +p_err ("failed to allocate memory for perf_event array: %s" , +strerror (errno)); +return -1 ; + } + map_fd =bpf_map__fd (obj->maps.events); +if (map_fd <0 ) { +p_err ("failed to get fd for events map" ); +return -1 ; + } + +for (m =0 ; m <ARRAY_SIZE (metrics); m++) { +if (!metrics[m].selected) +continue ;
tools/bpf/bpftool/prog.c static int do_loadall(int argc, char **argv) · line 1834
Quoted 10 lines
tools/bpf/bpftool/prog.c +10
-0+for (cpu =0 ; cpu < obj->rodata->num_cpu; cpu++) { + pmu_fd =syscall (__NR_perf_event_open, &metrics[m].attr, +-1 /*pid*/ , cpu,-1 /*group_fd*/ ,0 ); +if (pmu_fd <0 || +bpf_map_update_elem (map_fd, &profile_perf_event_cnt, + &pmu_fd,BPF_ANY ) || +ioctl (pmu_fd,PERF_EVENT_IOC_ENABLE ,0 )) { +p_err ("failed to create event %s on cpu %d" , + metrics[m].name, cpu); + goto err;
You can probably simply return here...
tools/bpf/bpftool/prog.c static int do_loadall(int argc, char **argv) · line 1842
Quoted 8 lines
tools/bpf/bpftool/prog.c +8
-0+ } + profile_perf_events[profile_perf_event_cnt++] = pmu_fd; + } + } +return 0 ; +err: +profile_close_perf_events (profile_obj); +return -1 ;
... and remove the "err:" label here, because if I understand correctly the only call to profile_open_perf_events() is in do_profile() (below), and if it fails, "profile_close_perf_events(profile_obj);" is called there.
Quoted 1 line
+}
[...]
Quoted 2 lines
tools/bpf/bpftool/prog.c +2
-0+static int do_profile (int argc,char **argv) +{
[...]
tools/bpf/bpftool/prog.c static int do_loadall(int argc, char **argv) · line 1941
Quoted 3 lines
tools/bpf/bpftool/prog.c +3
-0+ err =profile_open_perf_events (profile_obj); +if (err) { +p_err ("failed to open perf events" );
Also do you think we can remove this p_err() (or move the text in profile_open_perf_events() error messages)? This is because profile_open_perf_events() already prints something if it fails, and error messages work best with JSON if there is just one at a time.
Would also apply to profile_target_name() a few lines above.
Quoted 22 lines
tools/bpf/bpftool/prog.c +22
-0+ goto out; + } + + err =profiler_bpf__attach (profile_obj); +if (err) { +p_err ("failed to attach profile_obj" ); + goto out; + } +signal (SIGINT , int_exit); + +sleep (duration); +profile_print_and_cleanup (); +return 0 ; + +out: +profile_close_perf_events (profile_obj); +if (profile_obj) +profiler_bpf__destroy (profile_obj); +close (profile_tgt_fd); +free (profile_tgt_name); +return err; +}
PATCH v3 2/3 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
Re: [PATCH v3 bpf-next 2/3] bpftool: Documentation for bpftool prog profile
2020-03-03 11:55 UTC-0800 ~ Song Liu <[email protected]>
Quoted 3 lines
Add documentation for the new bpftool prog profile command.
Signed-off-by Song Liu <[email protected]>
Thanks for the changes!
PATCH v3 3/3 bpftool: 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_map_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
Re: [PATCH v3 bpf-next 3/3] bpftool: bash completion for "bpftool prog profile"
2020-03-03 11:55 UTC-0800 ~ Song Liu <[email protected]>
Quoted 26 lines
Add bash completion for "bpftool prog profile" command.
Signed-off-by Song Liu <[email protected]>--- tools/bpf/bpftool/bash-completion/bpftool | 45 ++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)tools/bpf/bpftool/bash-completion/bpftool +4
-0@@ -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)
tools/bpf/bpftool/bash-completion/bpftool _bpftool() · line 514
Quoted 10 lines
tools/bpf/bpftool/bash-completion/bpftool +10
-0+ COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) ) + return 0 + ;; + 4) + case $prev in + id) + _bpftool_get_prog_ids + ;; + name) + _bpftool_get_map_names
s/map/prog/
Quoted 19 lines
tools/bpf/bpftool/bash-completion/bpftool +19
-0+ ;; + 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
tools/bpf/bpftool/bash-completion/bpftool _bpftool() · line 543
Quoted 10 lines
tools/bpf/bpftool/bash-completion/bpftool +10
-0+ ;; + esac + return 0 + ;; + *) + COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) ) + return 0 + ;; + esac + ;;
You have a mix of tabs and spaces for indent on this chunk, could you please fix it?
Other than this and the map|prog thing above, completion looks good to me, thanks a lot!
Quoted 13 lines
tools/bpf/bpftool/bash-completion/bpftool +1
-1run) 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 ;;