Skip to content

Commit 47c09d6

Browse files
liu-song-6borkmann
authored andcommitted
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. Signed-off-by: Song Liu <songliubraving@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Quentin Monnet <quentin@isovalent.com> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20200309173218.2739965-2-songliubraving@fb.com
1 parent 7b4b73b commit 47c09d6

File tree

5 files changed

+608
-1
lines changed

5 files changed

+608
-1
lines changed

tools/bpf/bpftool/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ LIBS = $(LIBBPF) -lelf -lz
5959

6060
INSTALL ?= install
6161
RM ?= rm -f
62+
CLANG ?= clang
6263

6364
FEATURE_USER = .bpftool
6465
FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib
@@ -110,6 +111,22 @@ SRCS += $(BFD_SRCS)
110111
endif
111112

112113
OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
114+
_OBJS = $(filter-out $(OUTPUT)prog.o,$(OBJS)) $(OUTPUT)_prog.o
115+
116+
$(OUTPUT)_prog.o: prog.c
117+
$(QUIET_CC)$(COMPILE.c) -MMD -DBPFTOOL_WITHOUT_SKELETONS -o $@ $<
118+
119+
$(OUTPUT)_bpftool: $(_OBJS) $(LIBBPF)
120+
$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(_OBJS) $(LIBS)
121+
122+
skeleton/profiler.bpf.o: skeleton/profiler.bpf.c
123+
$(QUIET_CLANG)$(CLANG) -I$(srctree)/tools/lib -g -O2 -target bpf -c $< -o $@
124+
125+
profiler.skel.h: $(OUTPUT)_bpftool skeleton/profiler.bpf.o
126+
$(QUIET_GEN)$(OUTPUT)./_bpftool gen skeleton skeleton/profiler.bpf.o > $@
127+
128+
$(OUTPUT)prog.o: prog.c profiler.skel.h
129+
$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
113130

114131
$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
115132
$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
@@ -125,6 +142,7 @@ $(OUTPUT)%.o: %.c
125142
clean: $(LIBBPF)-clean
126143
$(call QUIET_CLEAN, bpftool)
127144
$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
145+
$(Q)$(RM) -- $(OUTPUT)_bpftool profiler.skel.h skeleton/profiler.bpf.o
128146
$(Q)$(RM) -r -- $(OUTPUT)libbpf/
129147
$(call QUIET_CLEAN, core-gen)
130148
$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool

0 commit comments

Comments
 (0)