ebpf icon indicating copy to clipboard operation
ebpf copied to clipboard

btf: support marshaling Type

Open lmb opened this issue 4 years ago • 2 comments

This is a first cut at adding a btf.Builder which allows turning Type into BTF that the kernel understands. The code can round-trip vmlinux BTF and then load the result into the kernel, which is a good indication that we covered most of the bases.

As it stands, the builder is not exported or used in the package, since there is currently no way to add strings necessary for LineInfo to the BTF. This should become a lot easier once #606 lands.

Best reviewed per commit.

btf: add btfInt

Replace intEncoding with a type. This allows adding setters.

btf: add Array.Index

The kernel refuses to load an array type without an index type, so probably
btf.rst is outdated.

btf: move typeDeque to separate file

Split typeDeque into a separate file to reduce the amount of code in types.go.

btf: add stringTableBuilder

A stringTableBuilder helps build a BTF-style string table, which delimits
individual strings with a 0 byte. Each unique string is only added to the
table once.

btf: replace Type.walk with walkType

Type.walk takes a *typeDeque as argument. Go escape analysis doesn't see through
interface boundaries and therefore always allocates such pointers on the heap.
This forces typeDeque to always be heap allocated, which makes traversal more
expensive than it has to be.

Replace the interface function with walkType, which essentially contains a big
type switch.

btf: add builder

Allow building BTF wire format from scratch. We can compare a full vmlinux build to
just re-encoding raw type data as we currently do in Spec.marshal:

    name                    time/op
    BuildVmlinux/builder-4  77.9ms ± 2%
    BuildVmlinux/native-4   23.3ms ± 2%

    name                    alloc/op
    BuildVmlinux/builder-4  33.7MB ± 0%
    BuildVmlinux/native-4   20.1MB ± 0%

    name                    allocs/op
    BuildVmlinux/builder-4    473k ± 0%
    BuildVmlinux/native-4     394k ± 0%

A full rebuild is about 3x slower and uses 1.6x the memory. We can't use the builder
to replace Spec.marshal yet, since we don't have a good way to include LineInfos
in the generated BTF. This will become easier once LineInfos are stored in
Instruction metadata.

Updates #616

lmb avatar Apr 25 '22 13:04 lmb

As always, needs some more work!

lmb avatar Apr 26 '22 10:04 lmb

This works now!

lmb avatar Apr 27 '22 16:04 lmb