On Thu, Dec 19, 2019 at 10:13 AM Yonghong Song <yhs@xxxxxx> wrote: > > > > On 12/18/19 11:06 PM, Andrii Nakryiko wrote: > > Convert one of BCC tools (runqslower [0]) to BPF CO-RE + libbpf. It matches > > its BCC-based counterpart 1-to-1, supporting all the same parameters and > > functionality. > > > > runqslower tool utilizes BPF skeleton, auto-generated from BPF object file, > > as well as memory-mapped interface to global (read-only, in this case) data. > > Its makefile also ensures auto-generation of "relocatable" vmlinux.h, which is > > necessary for BTF-typed raw tracepoints with direct memory access. > > > > [0] https://github.com/iovisor/bcc/blob/11bf5d02c895df9646c117c713082eb192825293/tools/runqslower.py > > > > Signed-off-by: Andrii Nakryiko <andriin@xxxxxx> > > --- > > tools/lib/bpf/tools/runqslower/.gitignore | 2 + > > tools/lib/bpf/tools/runqslower/Makefile | 60 ++++++ > > .../lib/bpf/tools/runqslower/runqslower.bpf.c | 101 ++++++++++ > > tools/lib/bpf/tools/runqslower/runqslower.c | 187 ++++++++++++++++++ > > tools/lib/bpf/tools/runqslower/runqslower.h | 13 ++ > > 5 files changed, 363 insertions(+) > > create mode 100644 tools/lib/bpf/tools/runqslower/.gitignore > > create mode 100644 tools/lib/bpf/tools/runqslower/Makefile > > create mode 100644 tools/lib/bpf/tools/runqslower/runqslower.bpf.c > > create mode 100644 tools/lib/bpf/tools/runqslower/runqslower.c > > create mode 100644 tools/lib/bpf/tools/runqslower/runqslower.h > > > > diff --git a/tools/lib/bpf/tools/runqslower/.gitignore b/tools/lib/bpf/tools/runqslower/.gitignore > > new file mode 100644 > > index 000000000000..404942cc9371 > > --- /dev/null > > +++ b/tools/lib/bpf/tools/runqslower/.gitignore > > @@ -0,0 +1,2 @@ > > +/.output > > +/runqslower > > diff --git a/tools/lib/bpf/tools/runqslower/Makefile b/tools/lib/bpf/tools/runqslower/Makefile > > new file mode 100644 > > index 000000000000..b87b1f9fe9da > > --- /dev/null > > +++ b/tools/lib/bpf/tools/runqslower/Makefile > > @@ -0,0 +1,60 @@ > > +# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) > > +CLANG := clang > > +LLC := llc > > +LLVM_STRIP := llvm-strip > > +BPFTOOL := bpftool > > Maybe it is better to use in-tree bpftool? This will ensure we use the > one shipped together with the source which should have needed functionality. I can do that as well, though I tried to keep it close enough to how libbpf+CO-RE applications are going to be built in the wild, so tried to minimize amount of in-kernel source tree assumptions. Ideally both bpftool is installed and libbpf is available as a dynamic library in the system. But surely I can add bpftool auto-build and use that one. > > > +LIBBPF_SRC := ../.. > > +CFLAGS := -g -Wall > > + > > +# Try to detect best kernel BTF source > > +KERNEL_REL := $(shell uname -r) > > +ifneq ("$(wildcard /sys/kenerl/btf/vmlinux)","") > > +VMLINUX_BTF := /sys/kernel/btf/vmlinux > > +else ifneq ("$(wildcard /boot/vmlinux-$(KERNEL_REL))","") > > +VMLINUX_BTF := /boot/vmlinux-$(KERNEL_REL) > > +else > > +$(error "Can't detect kernel BTF, use VMLINUX_BTF to specify it explicitly") > > +endif > > + > > +out := .output > > +abs_out := $(abspath $(out)) > > +libbpf_src := $(abspath $(LIBBPF_SRC)) > > + > > +.DELETE_ON_ERROR: > > + > > +.PHONY: all > > +all: runqslower > > + > > +.PHONY: clean > > +clean: > > + rm -rf $(out) runqslower > > + > > +runqslower: $(out)/runqslower.o $(out)/libbpf.a > > + $(CC) $(CFLAGS) -lelf -lz $^ -o $@ > > + > > +$(out)/vmlinux.h: $(VMLINUX_BTF) | $(out) > > + $(BPFTOOL) btf dump file $(VMLINUX_BTF) format core > $@ > > + > > +$(out)/libbpf.a: | $(out) > > + cd $(out) && \ > > + $(MAKE) -C $(libbpf_src) OUTPUT=$(abs_out)/ $(abs_out)/libbpf.a > > + > > +$(out)/runqslower.o: runqslower.h $(out)/runqslower.skel.h \ > > + $(out)/runqslower.bpf.o > > + > > +$(out)/runqslower.bpf.o: $(out)/vmlinux.h runqslower.h > > + > > +$(out)/%.skel.h: $(out)/%.bpf.o > > + $(BPFTOOL) gen skeleton $< > $@ > > + > > +$(out)/%.bpf.o: %.bpf.c | $(out) > > + $(CLANG) -g -O2 -target bpf -I$(out) -I$(LIBBPF_SRC) \ > > + -c $(filter %.c,$^) -o $@ && \ > > + $(LLVM_STRIP) -g $@ > > + > > +$(out)/%.o: %.c | $(out) > > + $(CC) $(CFLAGS) -I$(LIBBPF_SRC) -I$(out) -c $(filter %.c,$^) -o $@ > > + > > +$(out): > > + mkdir -p $(out) > > + > [...]