Skip to content

Commit

Permalink
libct/cg/sd/v2: move fsMgr init to NewUnifiedManager
Browse files Browse the repository at this point in the history
Many operations require fsMgr, so let's create it right in
NewUnifiedManager and reuse.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Sep 21, 2021
1 parent aaa281c commit 7407df9
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions libcontainer/cgroups/systemd/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ type unifiedManager struct {
mu sync.Mutex
cgroups *configs.Cgroup
// path is like "/sys/fs/cgroup/user.slice/user-1001.slice/session-1.scope"
path string
dbus *dbusConnManager
path string
dbus *dbusConnManager
fsMgr cgroups.Manager
}

func NewUnifiedManager(config *configs.Cgroup, path string) (cgroups.Manager, error) {
Expand All @@ -35,6 +36,11 @@ func NewUnifiedManager(config *configs.Cgroup, path string) (cgroups.Manager, er
if err := m.initPath(); err != nil {
return nil, err
}
var err error
m.fsMgr, err = fs2.NewManager(config, m.path)
if err != nil {
return nil, err
}

return m, nil
}
Expand Down Expand Up @@ -361,16 +367,8 @@ func (m *unifiedManager) initPath() error {
return nil
}

func (m *unifiedManager) fsManager() (cgroups.Manager, error) {
return fs2.NewManager(m.cgroups, m.path)
}

func (m *unifiedManager) Freeze(state configs.FreezerState) error {
fsMgr, err := m.fsManager()
if err != nil {
return err
}
return fsMgr.Freeze(state)
return m.fsMgr.Freeze(state)
}

func (m *unifiedManager) GetPids() ([]int, error) {
Expand All @@ -382,11 +380,7 @@ func (m *unifiedManager) GetAllPids() ([]int, error) {
}

func (m *unifiedManager) GetStats() (*cgroups.Stats, error) {
fsMgr, err := m.fsManager()
if err != nil {
return nil, err
}
return fsMgr.GetStats()
return m.fsMgr.GetStats()
}

func (m *unifiedManager) Set(r *configs.Resources) error {
Expand All @@ -399,11 +393,7 @@ func (m *unifiedManager) Set(r *configs.Resources) error {
return fmt.Errorf("unable to set unit properties: %w", err)
}

fsMgr, err := m.fsManager()
if err != nil {
return err
}
return fsMgr.Set(r)
return m.fsMgr.Set(r)
}

func (m *unifiedManager) GetPaths() map[string]string {
Expand All @@ -417,21 +407,13 @@ func (m *unifiedManager) GetCgroups() (*configs.Cgroup, error) {
}

func (m *unifiedManager) GetFreezerState() (configs.FreezerState, error) {
fsMgr, err := m.fsManager()
if err != nil {
return configs.Undefined, err
}
return fsMgr.GetFreezerState()
return m.fsMgr.GetFreezerState()
}

func (m *unifiedManager) Exists() bool {
return cgroups.PathExists(m.path)
}

func (m *unifiedManager) OOMKillCount() (uint64, error) {
fsMgr, err := m.fsManager()
if err != nil {
return 0, err
}
return fsMgr.OOMKillCount()
return m.fsMgr.OOMKillCount()
}

0 comments on commit 7407df9

Please sign in to comment.