mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
libct/intelrdt: wrap Root in sync.Once
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>
This commit is contained in:
@@ -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()
|
||||
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 intelRdtRoot != "" {
|
||||
return intelRdtRoot, nil
|
||||
}
|
||||
intelRdtRoot = root
|
||||
})
|
||||
|
||||
f, err := os.Open("/proc/self/mountinfo")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
root, err := findIntelRdtMountpointDir(f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(root); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
intelRdtRoot = root
|
||||
return intelRdtRoot, nil
|
||||
return intelRdtRoot, intelRdtRootErr
|
||||
}
|
||||
|
||||
type cpuInfoFlags struct {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user