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 optimizations: remove BTF and kallsyms caches #2937

Merged
merged 2 commits into from
Sep 20, 2024
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
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)
}
Comment on lines -22 to 23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could we add a comment there? it might be a little bit confusing why we had this function in the first place.


type ksym struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sensors/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
cachedbtf "github.com/cilium/tetragon/pkg/btf"
"github.com/cilium/tetragon/pkg/kernels"
"github.com/cilium/tetragon/pkg/logger"
Expand Down Expand Up @@ -124,6 +125,9 @@ func (s *Sensor) Load(bpfDir string) error {
progsAdd(s.Progs)
AllMaps = append(AllMaps, s.Maps...)

// cleanup the BTF once we have loaded all sensor's program
btf.FlushKernelSpec()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😻 perfect!


l.WithField("sensor", s.Name).Infof("Loaded BPF maps and events for sensor successfully")
s.Loaded = true
return nil
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 @@ -911,6 +911,11 @@ func doLoadProgram(

load.Prog = prog

// in KernelTypes, we use a non-standard BTF which is possibly annotated with symbols
// from kernel modules. At this point we don't need that anymore, so we can release
// the memory from it.
load.KernelTypes = nil

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