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