Skip to content

Commit

Permalink
Memory optimizations
Browse files Browse the repository at this point in the history
Do not cache large objects like the BTF and ksyms. Reload them when
needed.

Signed-off-by: Anastasios Papagiannis <[email protected]>
  • Loading branch information
tpapagian committed Sep 19, 2024
1 parent c35738c commit 1c61893
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 4 additions & 0 deletions pkg/bpf/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ func HasMissedStatsKprobeMulti() bool {
}

func LogFeatures() string {
// once we have detected all features, flush the BTF spec
// we cache all values so calling again a Has* function will
// not load the BTF again
defer ebtf.FlushKernelSpec()
return fmt.Sprintf("override_return: %t, buildid: %t, kprobe_multi: %t, uprobe_multi %t, fmodret: %t, fmodret_syscall: %t, signal: %t, large: %t, link_pin: %t, lsm: %t, missed_stats_kprobe_multi: %t, missed_stats_kprobe: %t",
HasOverrideHelper(), HasBuildId(), HasKprobeMulti(), HasUprobeMulti(),
HasModifyReturn(), HasModifyReturnSyscall(), HasSignalHelper(), HasProgramLargeSize(),
Expand Down
12 changes: 1 addition & 11 deletions pkg/ksyms/ksyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@ import (
"sort"
"strconv"
"strings"
"sync"

"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/option"

lru "github.com/hashicorp/golang-lru/v2"
)

var (
kernelSymbols *Ksyms
setKernelSymbols sync.Once
)

func KernelSymbols() (*Ksyms, error) {
var err error
setKernelSymbols.Do(func() {
kernelSymbols, err = NewKsyms(option.Config.ProcFS)
})
return kernelSymbols, err
return NewKsyms(option.Config.ProcFS)
}

type ksym struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ func doLoadProgram(
return nil, fmt.Errorf("opening collection '%s' failed: %w", load.Name, err)
}
defer coll.Close()
// cleanup the BTF once we have loaded the program
defer btf.FlushKernelSpec()

err = installTailCalls(bpfDir, spec, coll, load)
if err != nil {
Expand Down Expand Up @@ -911,6 +913,9 @@ func doLoadProgram(

load.Prog = prog

// now we don't need the BTF anymore
load.KernelTypes = nil

// Copy the loaded collection before it's destroyed
if KeepCollection {
return copyLoadedCollection(coll)
Expand Down

0 comments on commit 1c61893

Please sign in to comment.