bpftool: Introduce "prog profile" command
authorSong Liu <songliubraving@fb.com>
Mon, 9 Mar 2020 17:32:15 +0000 (10:32 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 9 Mar 2020 23:04:07 +0000 (00:04 +0100)
commit47c09d6a9f6794caface4ad50930460b82d7c670
treebc29e8f83ffc3df13df0650e19c884970a6ab406
parent7b4b73bc8a609fa9655022ed24a7a714bc8c2155
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
tools/bpf/bpftool/Makefile
tools/bpf/bpftool/prog.c
tools/bpf/bpftool/skeleton/profiler.bpf.c [new file with mode: 0644]
tools/bpf/bpftool/skeleton/profiler.h [new file with mode: 0644]
tools/scripts/Makefile.include