diff --git a/pkg/sensors/collection.go b/pkg/sensors/collection.go index 979517b618e..2697367b20c 100644 --- a/pkg/sensors/collection.go +++ b/pkg/sensors/collection.go @@ -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) diff --git a/pkg/sensors/delayed_sensor_test.go b/pkg/sensors/delayed_sensor_test.go index eddb8821505..a8311c9ee05 100644 --- a/pkg/sensors/delayed_sensor_test.go +++ b/pkg/sensors/delayed_sensor_test.go @@ -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 } diff --git a/pkg/sensors/sensors.go b/pkg/sensors/sensors.go index 56b1917b999..749a90d9c61 100644 --- a/pkg/sensors/sensors.go +++ b/pkg/sensors/sensors.go @@ -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" @@ -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 { @@ -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