From 16b8d77f9ef5a06b90d1507fff1f9c21fbbcd0cb Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 12 Jul 2022 10:55:39 +0200 Subject: utils: call MaybeMoveToSubCgroup once memoize its result and use it for subsequent calls. Signed-off-by: Giuseppe Scrivano --- utils/utils.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 7cf28fda5..aa1c6a958 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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 { - unifiedMode, err := cgroups.IsCgroup2UnifiedMode() - if err != nil { - return err - } - if !unifiedMode { - return nil - } - cgroup, err := GetOwnCgroup() - if err != nil { - return err - } - if cgroup == "/" { - return MoveUnderCgroupSubtree("init") - } - return nil + maybeMoveToSubCgroupSync.Do(func() { + unifiedMode, err := cgroups.IsCgroup2UnifiedMode() + if err != nil { + maybeMoveToSubCgroupSyncErr = err + return + } + if !unifiedMode { + maybeMoveToSubCgroupSyncErr = nil + return + } + cgroup, err := GetOwnCgroup() + if err != nil { + maybeMoveToSubCgroupSyncErr = err + return + } + if cgroup == "/" { + maybeMoveToSubCgroupSyncErr = MoveUnderCgroupSubtree("init") + } + }) + return maybeMoveToSubCgroupSyncErr } -- cgit v1.2.3-54-g00ecf