diff options
author | Giuseppe Scrivano <gscrivan@redhat.com> | 2022-07-12 10:55:39 +0200 |
---|---|---|
committer | Matthew Heon <matthew.heon@pm.me> | 2022-07-26 13:08:06 -0400 |
commit | 3b37095b2b91f6e67db6195af685ce88e7a2519a (patch) | |
tree | 1658b8da054f1ca948cf980c9f41cda7a4fe46bc | |
parent | edfe80027144629f1881d02285f478f95fd27b56 (diff) | |
download | podman-3b37095b2b91f6e67db6195af685ce88e7a2519a.tar.gz podman-3b37095b2b91f6e67db6195af685ce88e7a2519a.tar.bz2 podman-3b37095b2b91f6e67db6195af685ce88e7a2519a.zip |
utils: call MaybeMoveToSubCgroup once
memoize its result and use it for subsequent calls.
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
-rw-r--r-- | utils/utils.go | 40 |
1 files 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 } |