Skip to content

Commit

Permalink
btf: guess the number of raw types
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux authored and lmb committed Apr 23, 2022
1 parent 2c93040 commit 31c4e1c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func parseBTF(btf io.ReaderAt, bo binary.ByteOrder) ([]rawType, stringTable, err
}

buf.Reset(io.NewSectionReader(btf, header.typeStart(), int64(header.TypeLen)))
rawTypes, err := readTypes(buf, bo)
rawTypes, err := readTypes(buf, bo, header.TypeLen)
if err != nil {
return nil, nil, fmt.Errorf("can't read types: %w", err)
}
Expand Down
13 changes: 8 additions & 5 deletions internal/btf/btf_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,14 @@ type btfParam struct {
Type TypeID
}

func readTypes(r io.Reader, bo binary.ByteOrder) ([]rawType, error) {
var (
header btfType
types []rawType
)
func readTypes(r io.Reader, bo binary.ByteOrder, typeLen uint32) ([]rawType, error) {
var header btfType
// because of the interleaving between types and struct members it is difficult to
// precompute the numbers of raw types this will parse
// this "guess" is a good first estimation
sizeOfbtfType := uintptr(binary.Size(btfType{}))
tyMaxCount := uintptr(typeLen) / sizeOfbtfType / 2
types := make([]rawType, 0, tyMaxCount)

for id := TypeID(1); ; id++ {
if err := binary.Read(r, bo, &header); err == io.EOF {
Expand Down

0 comments on commit 31c4e1c

Please sign in to comment.