From 210c19999fbca15d5540588e1361917204d0dd7a Mon Sep 17 00:00:00 2001 From: Florian Lehner Date: Mon, 6 Nov 2023 13:47:06 +0100 Subject: [PATCH] replace sync.Map with custom struct with sync.RWMutex Signed-off-by: Florian Lehner --- metric/system/cgroup/reader.go | 8 +++++++- metric/system/cgroup/util.go | 31 +++++++++++++++++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/metric/system/cgroup/reader.go b/metric/system/cgroup/reader.go index 5fbd840ba9..f9bb6bb86c 100644 --- a/metric/system/cgroup/reader.go +++ b/metric/system/cgroup/reader.go @@ -85,6 +85,11 @@ type pathListWithTime struct { pathList PathList } +type pathCache struct { + sync.RWMutex + cache map[string]pathListWithTime +} + // Reader reads cgroup metrics and limits. type Reader struct { // Mountpoint of the root filesystem. Defaults to / if not set. This can be @@ -95,7 +100,7 @@ type Reader struct { cgroupMountpoints Mountpoints // Mountpoints for each subsystem (e.g. cpu, cpuacct, memory, blkio). // Cache to map known v2 cgroup controllerPaths to pathListWithTime. - v2ControllerPathCache sync.Map + v2ControllerPathCache pathCache } // ReaderOptions holds options for NewReaderOptions. @@ -146,6 +151,7 @@ func NewReaderOptions(opts ReaderOptions) (*Reader, error) { ignoreRootCgroups: opts.IgnoreRootCgroups, cgroupsHierarchyOverride: opts.CgroupsHierarchyOverride, cgroupMountpoints: mountpoints, + v2ControllerPathCache: pathCache{cache: make(map[string]pathListWithTime)}, }, nil } diff --git a/metric/system/cgroup/util.go b/metric/system/cgroup/util.go index 8a9329bdb9..95942b7a83 100644 --- a/metric/system/cgroup/util.go +++ b/metric/system/cgroup/util.go @@ -278,20 +278,21 @@ the container as /sys/fs/cgroup/unified and start the system module with the hos } // Check if there is an entry for controllerPath already cached. - if tmp, ok := r.v2ControllerPathCache.Load(controllerPath); ok { - cacheEntry, ok := tmp.(pathListWithTime) - if ok { - // 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 - } + r.v2ControllerPathCache.Lock() + cacheEntry, ok := r.v2ControllerPathCache.cache[controllerPath] + if ok { + // If the cached entry for controllerPath is not older than 5 minutes, + // return the cached entry. + if time.Since(cacheEntry.added) < 5*time.Minute { + cPaths.V2 = cacheEntry.pathList.V2 + r.v2ControllerPathCache.Unlock() + 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) } + // Consider the existing entry for controllerPath invalid, as it is + // older than 5 minutes. + delete(r.v2ControllerPathCache.cache, controllerPath) + r.v2ControllerPathCache.Unlock() cgpaths, err := os.ReadDir(controllerPath) if err != nil { @@ -305,10 +306,12 @@ 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, pathListWithTime{ + r.v2ControllerPathCache.Lock() + r.v2ControllerPathCache.cache[controllerPath] = pathListWithTime{ added: time.Now(), pathList: cPaths, - }) + } + r.v2ControllerPathCache.Unlock() // cgroup v1 } else { subsystems := strings.Split(fields[1], ",")