utils: call MaybeMoveToSubCgroup once

memoize its result and use it for subsequent calls.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2022-07-12 10:55:39 +02:00
parent 4b547a72ab
commit 16b8d77f9e
No known key found for this signature in database
GPG Key ID: 67E38F7A8BA21772
1 changed files with 25 additions and 15 deletions

View File

@ -191,22 +191,32 @@ func MovePauseProcessToScope(pausePidPath string) {
} }
} }
var (
maybeMoveToSubCgroupSync sync.Once
maybeMoveToSubCgroupSyncErr error
)
// MaybeMoveToSubCgroup moves the current process in a sub cgroup when // MaybeMoveToSubCgroup moves the current process in a sub cgroup when
// it is running in the root cgroup on a system that uses cgroupv2. // it is running in the root cgroup on a system that uses cgroupv2.
func MaybeMoveToSubCgroup() error { func MaybeMoveToSubCgroup() error {
unifiedMode, err := cgroups.IsCgroup2UnifiedMode() maybeMoveToSubCgroupSync.Do(func() {
if err != nil { unifiedMode, err := cgroups.IsCgroup2UnifiedMode()
return err if err != nil {
} maybeMoveToSubCgroupSyncErr = err
if !unifiedMode { return
return nil }
} if !unifiedMode {
cgroup, err := GetOwnCgroup() maybeMoveToSubCgroupSyncErr = nil
if err != nil { return
return err }
} cgroup, err := GetOwnCgroup()
if cgroup == "/" { if err != nil {
return MoveUnderCgroupSubtree("init") maybeMoveToSubCgroupSyncErr = err
} return
return nil }
if cgroup == "/" {
maybeMoveToSubCgroupSyncErr = MoveUnderCgroupSubtree("init")
}
})
return maybeMoveToSubCgroupSyncErr
} }