Skip to content

Commit

Permalink
Reduce memory footprint of kernel symbols (open-telemetry#194)
Browse files Browse the repository at this point in the history
Reducing the kernel symbols' memory footprint by

    skipping non-text symbols when reading from /proc/kallsyms
    adjusting overcommitted symbols array

Additionally, we pre-allocate a relatively large symbols array to begin with parsing to avoid re-allocations.
  • Loading branch information
rockdaboot authored Oct 18, 2024
1 parent 0ff85af commit 0a6fc45
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 12 additions & 0 deletions libpf/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type SymbolMap struct {
addressToSymbol []Symbol
}

func NewSymbolMap(capacity int) *SymbolMap {
return &SymbolMap{
addressToSymbol: make([]Symbol, 0, capacity),
}
}

// Add a symbol to the map
func (symmap *SymbolMap) Add(s Symbol) {
symmap.addressToSymbol = append(symmap.addressToSymbol, s)
Expand All @@ -51,10 +57,16 @@ func (symmap *SymbolMap) Add(s Symbol) {
// Finalize symbol map by sorting and constructing the nameToSymbol table after
// all symbols are inserted via Add() calls
func (symmap *SymbolMap) Finalize() {
// Adjust the overcommitted capacity
a := make([]Symbol, len(symmap.addressToSymbol))
copy(a, symmap.addressToSymbol)
symmap.addressToSymbol = a

sort.Slice(symmap.addressToSymbol,
func(i, j int) bool {
return symmap.addressToSymbol[i].Address > symmap.addressToSymbol[j].Address
})

symmap.nameToSymbol = make(map[SymbolName]*Symbol, len(symmap.addressToSymbol))
for i, s := range symmap.addressToSymbol {
symmap.nameToSymbol[s.Name] = &symmap.addressToSymbol[i]
Expand Down
11 changes: 9 additions & 2 deletions proc/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func GetKallsyms(kallsymsPath string) (*libpf.SymbolMap, error) {
var address uint64
var symbol string

symmap := libpf.SymbolMap{}
// As an example, the Debian 6.10.11 kernel has ~180k text symbols.
symmap := libpf.NewSymbolMap(200 * 1024)
noSymbols := true

file, err := os.Open(kallsymsPath)
Expand All @@ -52,6 +53,12 @@ func GetKallsyms(kallsymsPath string) (*libpf.SymbolMap, error) {
return nil, fmt.Errorf("unexpected line in kallsyms: '%s'", line)
}

// Skip non-text symbols, see 'man nm'.
// Special case for 'etext', which can be of type `D` (data) in some kernels.
if strings.IndexByte("TtVvWwA", fields[1][0]) == -1 && fields[2] != "_etext" {
continue
}

if address, err = strconv.ParseUint(fields[0], 16, 64); err != nil {
return nil, fmt.Errorf("failed to parse address value: '%s'", fields[0])
}
Expand All @@ -74,7 +81,7 @@ func GetKallsyms(kallsymsPath string) (*libpf.SymbolMap, error) {
"all addresses from kallsyms are zero - check process permissions")
}

return &symmap, nil
return symmap, nil
}

// GetKernelModules returns SymbolMap for kernel modules from /proc/modules.
Expand Down

0 comments on commit 0a6fc45

Please sign in to comment.