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

Memory improvements to loadRawSpec #634

Merged
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
2 changes: 1 addition & 1 deletion internal/btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,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
35 changes: 31 additions & 4 deletions internal/btf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,20 +776,47 @@ func (dq *typeDeque) all() []*Type {
return types
}

// countFixups takes a list of raw btf types and returns the count of fixups that
// will be required when building the graph of Types.
func countFixups(rawTypes []rawType) int {
fixupCount := 0
for _, raw := range rawTypes {
switch raw.Kind() {
case kindStruct, kindUnion:
members := raw.data.([]btfMember)
fixupCount += len(members)

case kindFuncProto:
rawparams := raw.data.([]btfParam)
fixupCount += len(rawparams) + 1

case kindDatasec:
btfVars := raw.data.([]btfVarSecinfo)
fixupCount += len(btfVars)

default:
fixupCount++
}
}
return fixupCount
}

// inflateRawTypes takes a list of raw btf types linked via type IDs, and turns
// it into a graph of Types connected via pointers.
//
// Returns a map of named types (so, where NameOff is non-zero) and a slice of types
// indexed by TypeID. Since BTF ignores compilation units, multiple types may share
// the same name. A Type may form a cyclic graph by pointing at itself.
func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type, namedTypes map[essentialName][]Type, err error) {
func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) ([]Type, map[essentialName][]Type, error) {
type fixupDef struct {
id TypeID
expectedKind btfKind
typ *Type
}

var fixups []fixupDef
fixupsCount := countFixups(rawTypes)
fixups := make([]fixupDef, 0, fixupsCount)

fixup := func(id TypeID, expectedKind btfKind, typ *Type) {
fixups = append(fixups, fixupDef{id, expectedKind, typ})
}
Expand Down Expand Up @@ -819,9 +846,9 @@ func inflateRawTypes(rawTypes []rawType, rawStrings stringTable) (types []Type,
return members, nil
}

types = make([]Type, 0, len(rawTypes))
types := make([]Type, 0, len(rawTypes)+1)
types = append(types, (*Void)(nil))
namedTypes = make(map[essentialName][]Type)
namedTypes := make(map[essentialName][]Type)

for i, raw := range rawTypes {
var (
Expand Down