Skip to content

Commit

Permalink
updateMapMetric: close maps on errors
Browse files Browse the repository at this point in the history
[ upstream commit 0ffc6c6 ]

create updateMapMetric() function to update the metrics for a single
map. This allows to use defer() to do proper cleanup in case of an error.

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Jun 28, 2023
1 parent 18c14fd commit 8e059b2
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions pkg/observer/observer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,38 +47,43 @@ func (s *statValue) DeepCopyMapValue() bpf.MapValue {
return v
}

func (k *Observer) startUpdateMapMetrics() {
update := func() {
for _, m := range sensors.AllMaps {
pin := filepath.Join(option.Config.MapDir, m.Name)
pinStats := pin + "_stats"
func updateMapMetric(name string) {
pin := filepath.Join(option.Config.MapDir, name)
pinStats := pin + "_stats"

mapLinkStats, err := bpf.OpenMap(pinStats)
if err != nil {
continue
}
mapLink, err := bpf.OpenMap(pin)
if err != nil {
continue
}
mapLinkStats, err := bpf.OpenMap(pinStats)
if err != nil {
return
}
defer mapLinkStats.Close()
mapLink, err := bpf.OpenMap(pin)
if err != nil {
return
}
defer mapLink.Close()

zeroKey := &statKey{}
value, err := mapLinkStats.Lookup(zeroKey)
if err != nil {
continue
}
zeroKey := &statKey{}
value, err := mapLinkStats.Lookup(zeroKey)
if err != nil {
return
}

v, ok := value.DeepCopyMapValue().(*statValue)
if !ok {
continue
}
sum := int64(0)
for cpu := int(0); cpu < runtime.NumCPU(); cpu++ {
sum += v.Value[cpu]
}
mapmetrics.MapSizeSet(m.Name, int(mapLink.MapInfo.MaxEntries), float64(sum))
mapLink.Close()
mapLinkStats.Close()
v, ok := value.DeepCopyMapValue().(*statValue)
if !ok {
return
}

sum := int64(0)
for cpu := int(0); cpu < runtime.NumCPU(); cpu++ {
sum += v.Value[cpu]
}
mapmetrics.MapSizeSet(name, int(mapLink.MapInfo.MaxEntries), float64(sum))
}

func (k *Observer) startUpdateMapMetrics() {
update := func() {
for _, m := range sensors.AllMaps {
updateMapMetric(m.Name)
}
}

Expand Down

0 comments on commit 8e059b2

Please sign in to comment.