Skip to content

Commit

Permalink
pkg/sensors: export BPF maps memlock to tracing policy lists
Browse files Browse the repository at this point in the history
Retrieve memlock from each map loaded by a sensor.

Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
  • Loading branch information
mtardy committed Oct 8, 2024
1 parent 1a1a1e9 commit 2426956
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/sensors/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func (cm *collectionMap) listPolicies() []*tetragon.TracingPolicyStatus {

for _, sens := range col.sensors {
pol.Sensors = append(pol.Sensors, sens.GetName())
pol.KernelMemoryBytes += uint64(sens.TotalMemlock())
}

ret = append(ret, &pol)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sensors/delayed_sensor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type TestDelayedSensor struct {
ch chan struct{}
}

func (tds TestDelayedSensor) TotalMemlock() int {
return 0
}

func (tds *TestDelayedSensor) GetName() string {
return tds.name
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/sensors/sensors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"

"github.com/cilium/tetragon/pkg/bpf"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/policyfilter"
"github.com/cilium/tetragon/pkg/sensors/program"
Expand Down Expand Up @@ -75,6 +76,9 @@ type SensorIface interface {
Load(bpfDir string) error
Unload() error
Destroy()
// TotalMemlock is the total amount of memlock bytes for BPF maps used by
// the sensor's programs.
TotalMemlock() int
}

func (s *Sensor) GetName() string {
Expand All @@ -85,6 +89,23 @@ func (s *Sensor) IsLoaded() bool {
return s.Loaded
}

func (s Sensor) TotalMemlock() int {
uniqueMap := map[int]bpf.ExtendedMapInfo{}
for _, p := range s.Progs {
for id, info := range p.LoadedMapsInfo {
// we could first check that it exist then write but all maps with
// same ID on the kernel should share the same info
uniqueMap[id] = info
}
}

var total int
for _, info := range uniqueMap {
total += info.Memlock
}
return total
}

// SensorHook is the function signature for an optional function
// that can be called during sensor unloading and removing.
type SensorHook func() error
Expand Down

0 comments on commit 2426956

Please sign in to comment.