From 7296dc171238291f4fc5336bbb2fba3d5cb70109 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Wed, 31 Mar 2021 17:05:00 +0300 Subject: [PATCH] libcontainer/intelrdt: refactor clos path handling Simplify the code and make path a property of the container (via intelRdtManager). Signed-off-by: Markus Lehtonen --- libcontainer/container_linux.go | 7 ++--- libcontainer/intelrdt/intelrdt.go | 45 +++++++++---------------------- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index 19a82bbf6..39932c751 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -1902,9 +1902,10 @@ func (c *linuxContainer) currentState() (*State, error) { startTime, _ = c.initProcess.startTime() externalDescriptors = c.initProcess.externalDescriptors() } - intelRdtPath, err := intelrdt.GetIntelRdtPath(c.ID()) - if err != nil { - intelRdtPath = "" + + intelRdtPath := "" + if c.intelRdtManager != nil { + intelRdtPath = c.intelRdtManager.GetPath() } state := &State{ BaseState: BaseState{ diff --git a/libcontainer/intelrdt/intelrdt.go b/libcontainer/intelrdt/intelrdt.go index 9c32aabd3..2b62af104 100644 --- a/libcontainer/intelrdt/intelrdt.go +++ b/libcontainer/intelrdt/intelrdt.go @@ -207,7 +207,6 @@ var ( type intelRdtData struct { root string config *configs.Config - pid int } // Check if Intel RDT sub-features are enabled in featuresInit() @@ -405,18 +404,6 @@ func writeFile(dir, file, data string) error { return nil } -func getIntelRdtData(c *configs.Config, pid int) (*intelRdtData, error) { - rootPath, err := getIntelRdtRoot() - if err != nil { - return nil, err - } - return &intelRdtData{ - root: rootPath, - config: c, - pid: pid, - }, nil -} - // Get the read-only L3 cache information func getL3CacheInfo() (*L3CacheInfo, error) { l3CacheInfo := &L3CacheInfo{} @@ -532,14 +519,13 @@ func IsMBAScEnabled() bool { } // Get the 'container_id' path in Intel RDT "resource control" filesystem -func GetIntelRdtPath(id string) (string, error) { +func (m *intelRdtManager) getIntelRdtPath() (string, error) { rootPath, err := getIntelRdtRoot() if err != nil { return "", err } - path := filepath.Join(rootPath, id) - return path, nil + return filepath.Join(rootPath, m.id), nil } // Applies Intel RDT configuration to the process with the specified pid @@ -548,16 +534,21 @@ func (m *intelRdtManager) Apply(pid int) (err error) { if m.config.IntelRdt == nil { return nil } - d, err := getIntelRdtData(m.config, pid) + + path, err := m.getIntelRdtPath() if err != nil { return err } m.mu.Lock() defer m.mu.Unlock() - path, err := d.join(m.id) - if err != nil { - return err + + if err := os.MkdirAll(path, 0o755); err != nil { + return newLastCmdError(err) + } + + if err := WriteIntelRdtTasks(path, pid); err != nil { + return newLastCmdError(err) } m.path = path @@ -579,7 +570,7 @@ func (m *intelRdtManager) Destroy() error { // restore the object later func (m *intelRdtManager) GetPath() string { if m.path == "" { - m.path, _ = GetIntelRdtPath(m.id) + m.path, _ = m.getIntelRdtPath() } return m.path } @@ -747,18 +738,6 @@ func (m *intelRdtManager) Set(container *configs.Config) error { return nil } -func (raw *intelRdtData) join(id string) (string, error) { - path := filepath.Join(raw.root, id) - if err := os.MkdirAll(path, 0o755); err != nil { - return "", newLastCmdError(err) - } - - if err := WriteIntelRdtTasks(path, raw.pid); err != nil { - return "", err - } - return path, nil -} - func newLastCmdError(err error) error { status, err1 := getLastCmdStatus() if err1 == nil {