Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

btf: reduce unnecessary copies when parsing BTF #623

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions internal/btf/btf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package btf

import (
"bufio"
"bytes"
"debug/elf"
"encoding/binary"
Expand Down Expand Up @@ -165,7 +166,11 @@ func loadSpecFromELF(file *internal.SafeELFFile) (*Spec, error) {
return nil, err
}

spec, err := loadRawSpec(btfSection.Open(), file.ByteOrder, sectionSizes, vars)
if btfSection.ReaderAt == nil {
return nil, fmt.Errorf("compressed BTF is not supported")
}

spec, err := loadRawSpec(btfSection.ReaderAt, file.ByteOrder, sectionSizes, vars)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -298,7 +303,7 @@ func (spec *Spec) splitExtInfos(info *extInfo) error {
return nil
}

func loadRawSpec(btf io.Reader, bo binary.ByteOrder, sectionSizes map[string]uint32, variableOffsets map[variable]uint32) (*Spec, error) {
func loadRawSpec(btf io.ReaderAt, bo binary.ByteOrder, sectionSizes map[string]uint32, variableOffsets map[variable]uint32) (*Spec, error) {
rawTypes, rawStrings, err := parseBTF(btf, bo)
if err != nil {
return nil, err
Expand Down Expand Up @@ -427,11 +432,13 @@ func parseBTFHeader(r io.Reader, bo binary.ByteOrder) (*btfHeader, error) {
}

func guessRawBTFByteOrder(r io.ReaderAt) binary.ByteOrder {
buf := new(bufio.Reader)
for _, bo := range []binary.ByteOrder{
binary.LittleEndian,
binary.BigEndian,
} {
if _, err := parseBTFHeader(io.NewSectionReader(r, 0, math.MaxInt64), bo); err == nil {
buf.Reset(io.NewSectionReader(r, 0, math.MaxInt64))
if _, err := parseBTFHeader(buf, bo); err == nil {
return bo
}
}
Expand All @@ -441,25 +448,20 @@ func guessRawBTFByteOrder(r io.ReaderAt) binary.ByteOrder {

// parseBTF reads a .BTF section into memory and parses it into a list of
// raw types and a string table.
func parseBTF(btf io.Reader, bo binary.ByteOrder) ([]rawType, stringTable, error) {
rawBTF, err := io.ReadAll(btf)
if err != nil {
return nil, nil, fmt.Errorf("can't read BTF: %v", err)
}
rd := bytes.NewReader(rawBTF)

header, err := parseBTFHeader(rd, bo)
func parseBTF(btf io.ReaderAt, bo binary.ByteOrder) ([]rawType, stringTable, error) {
buf := internal.NewBufferedSectionReader(btf, 0, math.MaxInt64)
header, err := parseBTFHeader(buf, bo)
if err != nil {
return nil, nil, fmt.Errorf("parsing .BTF header: %v", err)
}

buf := io.NewSectionReader(rd, header.stringStart(), int64(header.StringLen))
buf.Reset(io.NewSectionReader(btf, header.stringStart(), int64(header.StringLen)))
rawStrings, err := readStringTable(buf)
if err != nil {
return nil, nil, fmt.Errorf("can't read type names: %w", err)
}

buf = io.NewSectionReader(rd, header.typeStart(), int64(header.TypeLen))
buf.Reset(io.NewSectionReader(btf, header.typeStart(), int64(header.TypeLen)))
rawTypes, err := readTypes(buf, bo)
if err != nil {
return nil, nil, fmt.Errorf("can't read types: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func TestTypeByName(t *testing.T) {

func BenchmarkParseVmlinux(b *testing.B) {
rd := readVMLinux(b)
b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
Expand Down
2 changes: 1 addition & 1 deletion internal/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// end up being read completely anyway.
//
// Use instead of the r.Seek() + io.LimitReader() pattern.
func NewBufferedSectionReader(ra io.ReaderAt, off, n int64) io.Reader {
func NewBufferedSectionReader(ra io.ReaderAt, off, n int64) *bufio.Reader {
// Clamp the size of the buffer to one page to avoid slurping large parts
// of a file into memory. bufio.NewReader uses a hardcoded default buffer
// of 4096. Allow arches with larger pages to allocate more, but don't
Expand Down