Skip to content

Commit

Permalink
libct/intelrdt: wrap Root in sync.Once
Browse files Browse the repository at this point in the history
In case resctrl filesystem can not be found in /proc/self/mountinfo
(which is pretty common on non-server or non-x86 hardware), subsequent
calls to Root() will result in parsing it again and again.

Use sync.Once to avoid it. Make unit tests call it so that Root() won't
actually parse mountinfo in tests.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Dec 3, 2021
1 parent 45c31f9 commit 02e961b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
41 changes: 18 additions & 23 deletions libcontainer/intelrdt/intelrdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,35 +276,30 @@ func findIntelRdtMountpointDir(f io.Reader) (string, error) {

// For Root() use only.
var (
intelRdtRoot string
rootMu sync.Mutex
intelRdtRoot string
intelRdtRootErr error
rootOnce sync.Once
)

// Root returns the Intel RDT "resource control" filesystem mount point.
func Root() (string, error) {
rootMu.Lock()
defer rootMu.Unlock()

if intelRdtRoot != "" {
return intelRdtRoot, nil
}

f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return "", err
}
root, err := findIntelRdtMountpointDir(f)
f.Close()
if err != nil {
return "", err
}
rootOnce.Do(func() {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
intelRdtRootErr = err
return
}
root, err := findIntelRdtMountpointDir(f)
f.Close()
if err != nil {
intelRdtRootErr = err
return
}

if _, err := os.Stat(root); err != nil {
return "", err
}
intelRdtRoot = root
})

intelRdtRoot = root
return intelRdtRoot, nil
return intelRdtRoot, intelRdtRootErr
}

type cpuInfoFlags struct {
Expand Down
5 changes: 5 additions & 0 deletions libcontainer/intelrdt/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ func NewIntelRdtTestUtil(t *testing.T) *intelRdtTestUtil {
config := &configs.Config{
IntelRdt: &configs.IntelRdt{},
}

// Assign fake intelRtdRoot value, returned by Root().
intelRdtRoot = t.TempDir()
// Make sure Root() won't even try to parse mountinfo.
rootOnce.Do(func() {})

testIntelRdtPath := filepath.Join(intelRdtRoot, "resctrl")

// Ensure the full mock Intel RDT "resource control" filesystem path exists
Expand Down

0 comments on commit 02e961b

Please sign in to comment.