Skip to content

Commit

Permalink
replace sync.Map with custom struct with sync.RWMutex
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <[email protected]>
  • Loading branch information
florianl committed Nov 6, 2023
1 parent 9cb7cc2 commit 210c199
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
8 changes: 7 additions & 1 deletion metric/system/cgroup/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down
31 changes: 17 additions & 14 deletions metric/system/cgroup/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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], ",")
Expand Down

0 comments on commit 210c199

Please sign in to comment.