Skip to content

Commit

Permalink
metric/system/cgroup: add timestamp to cache for v2 entries
Browse files Browse the repository at this point in the history
Make sure v2 entries do not stay forever in the cache.

Signed-off-by: Florian Lehner <[email protected]>
  • Loading branch information
florianl committed Oct 31, 2023
1 parent bbbc241 commit 80dde66
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
9 changes: 8 additions & 1 deletion metric/system/cgroup/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"
"strings"
"sync"
"time"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-system-metrics/metric/system/cgroup/cgv1"
Expand Down Expand Up @@ -78,6 +79,12 @@ type mount struct {
fullPath string // Absolute path to the cgroup. It's the mountpoint joined with the path.
}

// pathListWithTime combines PathList with a timestamp.
type pathListWithTime struct {
added time.Time
pathList PathList
}

// Reader reads cgroup metrics and limits.
type Reader struct {
// Mountpoint of the root filesystem. Defaults to / if not set. This can be
Expand All @@ -87,7 +94,7 @@ type Reader struct {
cgroupsHierarchyOverride string
cgroupMountpoints Mountpoints // Mountpoints for each subsystem (e.g. cpu, cpuacct, memory, blkio).

// Cache to map known v2 cgroup controllerPaths to PathList.
// Cache to map known v2 cgroup controllerPaths to pathListWithTime.
v2ControllerPathCache sync.Map
}

Expand Down
20 changes: 16 additions & 4 deletions metric/system/cgroup/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
Expand Down Expand Up @@ -276,12 +277,20 @@ the container as /sys/fs/cgroup/unified and start the system module with the hos
controllerPath = r.rootfsMountpoint.ResolveHostFS(filepath.Join("/sys/fs/cgroup/unified", path))
}

// Check if there is an entry for controllerPath already cached.
if tmp, ok := r.v2ControllerPathCache.Load(controllerPath); ok {
pathList, ok := tmp.(PathList)
cacheEntry, ok := tmp.(pathListWithTime)
if ok {
cPaths.V2 = pathList.V2
continue
// If the cached entry for controllerPath is not older than 5 minutes,
// return the cached entry.
if time.Since(cacheEntry.added) < time.Duration(5*time.Minute) {
cPaths.V2 = cacheEntry.pathList.V2
continue
}
}
// Consider the existing entry for controllerPath invalid. The entry can
// (1) not be casted to pathListWithTime or (2) is older than 5 minutes.
r.v2ControllerPathCache.Delete(controllerPath)
}

cgpaths, err := os.ReadDir(controllerPath)
Expand All @@ -296,7 +305,10 @@ the container as /sys/fs/cgroup/unified and start the system module with the hos
cPaths.V2[controllerName] = ControllerPath{ControllerPath: path, FullPath: controllerPath, IsV2: true}
}
}
r.v2ControllerPathCache.Store(controllerPath, cPaths)
r.v2ControllerPathCache.Store(controllerPath, pathListWithTime{
added: time.Now(),
pathList: cPaths,
})
// cgroup v1
} else {
subsystems := strings.Split(fields[1], ",")
Expand Down

0 comments on commit 80dde66

Please sign in to comment.