mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-10 21:53:57 +08:00
libct/intelrdt: use sync.OnceFunc and sync.OnceValues
Switch from sync.Once to sync.OnceFunc and sync.OnceValues. Keep Root a function (rather than a variable) because godoc renders function doc better than a variable doc. Switch to using internal function root internally. Modify tests accordingly (and simplify NewIntelRdtTestUtil to fakeRoot). Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
@@ -162,7 +162,7 @@ func NewManager(config *configs.Config, id, path string) *Manager {
|
||||
return nil
|
||||
}
|
||||
|
||||
rootPath, err := Root()
|
||||
rootPath, err := root()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -197,18 +197,14 @@ var (
|
||||
// The flag to indicate if Intel RDT/MBA is enabled
|
||||
mbaEnabled bool
|
||||
|
||||
// For Intel RDT initialization
|
||||
initOnce sync.Once
|
||||
|
||||
errNotFound = errors.New("Intel RDT not available")
|
||||
)
|
||||
|
||||
// Check if Intel RDT sub-features are enabled in featuresInit()
|
||||
func featuresInit() {
|
||||
initOnce.Do(func() {
|
||||
var featuresInit = sync.OnceFunc(func() {
|
||||
// 1. Check if Intel RDT "resource control" filesystem is available.
|
||||
// The user guarantees to mount the filesystem.
|
||||
root, err := Root()
|
||||
root, err := root()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -236,8 +232,7 @@ func featuresInit() {
|
||||
if enabledMonFeatures.llcOccupancy {
|
||||
cmtEnabled = true
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// findIntelRdtMountpointDir returns the mount point of the Intel RDT "resource control" filesystem.
|
||||
func findIntelRdtMountpointDir() (string, error) {
|
||||
@@ -258,13 +253,6 @@ func findIntelRdtMountpointDir() (string, error) {
|
||||
return mi[0].Mountpoint, nil
|
||||
}
|
||||
|
||||
// For Root() use only.
|
||||
var (
|
||||
intelRdtRoot string
|
||||
intelRdtRootErr error
|
||||
rootOnce sync.Once
|
||||
)
|
||||
|
||||
// The kernel creates this (empty) directory if resctrl is supported by the
|
||||
// hardware and kernel. The user is responsible for mounting the resctrl
|
||||
// filesystem, and they could mount it somewhere else if they wanted to.
|
||||
@@ -272,29 +260,27 @@ const defaultResctrlMountpoint = "/sys/fs/resctrl"
|
||||
|
||||
// Root returns the Intel RDT "resource control" filesystem mount point.
|
||||
func Root() (string, error) {
|
||||
rootOnce.Do(func() {
|
||||
return root()
|
||||
}
|
||||
|
||||
var root = sync.OnceValues(func() (string, error) {
|
||||
// Does this system support resctrl?
|
||||
var statfs unix.Statfs_t
|
||||
if err := unix.Statfs(defaultResctrlMountpoint, &statfs); err != nil {
|
||||
if errors.Is(err, unix.ENOENT) {
|
||||
err = errNotFound
|
||||
}
|
||||
intelRdtRootErr = err
|
||||
return
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Has the resctrl fs been mounted to the default mount point?
|
||||
if statfs.Type == unix.RDTGROUP_SUPER_MAGIC {
|
||||
intelRdtRoot = defaultResctrlMountpoint
|
||||
return
|
||||
return defaultResctrlMountpoint, nil
|
||||
}
|
||||
|
||||
// The resctrl fs could have been mounted somewhere nonstandard.
|
||||
intelRdtRoot, intelRdtRootErr = findIntelRdtMountpointDir()
|
||||
})
|
||||
|
||||
return intelRdtRoot, intelRdtRootErr
|
||||
}
|
||||
return findIntelRdtMountpointDir()
|
||||
})
|
||||
|
||||
// Gets a single uint64 value from the specified file.
|
||||
func getIntelRdtParamUint(path, file string) (uint64, error) {
|
||||
@@ -325,7 +311,7 @@ func getIntelRdtParamString(path, file string) (string, error) {
|
||||
func getL3CacheInfo() (*L3CacheInfo, error) {
|
||||
l3CacheInfo := &L3CacheInfo{}
|
||||
|
||||
rootPath, err := Root()
|
||||
rootPath, err := root()
|
||||
if err != nil {
|
||||
return l3CacheInfo, err
|
||||
}
|
||||
@@ -355,7 +341,7 @@ func getL3CacheInfo() (*L3CacheInfo, error) {
|
||||
func getMemBwInfo() (*MemBwInfo, error) {
|
||||
memBwInfo := &MemBwInfo{}
|
||||
|
||||
rootPath, err := Root()
|
||||
rootPath, err := root()
|
||||
if err != nil {
|
||||
return memBwInfo, err
|
||||
}
|
||||
@@ -388,7 +374,7 @@ func getMemBwInfo() (*MemBwInfo, error) {
|
||||
|
||||
// Get diagnostics for last filesystem operation error from file info/last_cmd_status
|
||||
func getLastCmdStatus() (string, error) {
|
||||
rootPath, err := Root()
|
||||
rootPath, err := root()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -419,7 +405,7 @@ func WriteIntelRdtTasks(dir string, pid int) error {
|
||||
|
||||
// IsEnabled checks if Intel RDT is enabled.
|
||||
func IsEnabled() bool {
|
||||
fsroot, err := Root()
|
||||
fsroot, err := root()
|
||||
return err == nil && fsroot != ""
|
||||
}
|
||||
|
||||
@@ -538,7 +524,7 @@ func (m *Manager) GetStats() (*Stats, error) {
|
||||
defer m.mu.Unlock()
|
||||
stats := newStats()
|
||||
|
||||
rootPath, err := Root()
|
||||
rootPath, err := root()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -105,18 +105,20 @@ func TestIntelRdtSet(t *testing.T) {
|
||||
|
||||
for _, tc := range tcs {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
helper := NewIntelRdtTestUtil(t)
|
||||
helper.config.IntelRdt = tc.config
|
||||
intelRdtRoot := fakeRoot(t)
|
||||
config := &configs.Config{
|
||||
IntelRdt: tc.config,
|
||||
}
|
||||
|
||||
intelrdt := &Manager{
|
||||
config: helper.config,
|
||||
path: helper.IntelRdtPath,
|
||||
config: config,
|
||||
path: intelRdtRoot,
|
||||
}
|
||||
if err := intelrdt.Set(helper.config); err != nil {
|
||||
if err := intelrdt.Set(config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tmpStrings, err := getIntelRdtParamString(helper.IntelRdtPath, "schemata")
|
||||
tmpStrings, err := getIntelRdtParamString(intelRdtRoot, "schemata")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse file 'schemata' - %s", err)
|
||||
}
|
||||
@@ -186,7 +188,7 @@ func TestApply(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
NewIntelRdtTestUtil(t)
|
||||
intelRdtRoot := fakeRoot(t)
|
||||
id := "abcd-1234"
|
||||
closPath := filepath.Join(intelRdtRoot, id)
|
||||
if tt.config.ClosID != "" {
|
||||
@@ -288,8 +290,7 @@ func TestDestroy(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
NewIntelRdtTestUtil(t)
|
||||
|
||||
intelRdtRoot := fakeRoot(t)
|
||||
id := "abcd-1234"
|
||||
closPath := filepath.Join(intelRdtRoot, id)
|
||||
if tt.config.ClosID != "" {
|
||||
|
||||
@@ -8,35 +8,25 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/opencontainers/runc/libcontainer/configs"
|
||||
)
|
||||
|
||||
type intelRdtTestUtil struct {
|
||||
config *configs.Config
|
||||
|
||||
// Path to the mock Intel RDT "resource control" filesystem directory
|
||||
IntelRdtPath string
|
||||
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
// Creates a new test util
|
||||
func NewIntelRdtTestUtil(t *testing.T) *intelRdtTestUtil {
|
||||
config := &configs.Config{
|
||||
IntelRdt: &configs.IntelRdt{},
|
||||
}
|
||||
|
||||
// fakeRoot creates a new fake root for tests and returns its path.
|
||||
// Once this is called, Root() returns the same path.
|
||||
func fakeRoot(t *testing.T) string {
|
||||
// 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
|
||||
if err := os.MkdirAll(testIntelRdtPath, 0o755); err != nil {
|
||||
intelRdtRoot := filepath.Join(t.TempDir(), "resctrl")
|
||||
if err := os.MkdirAll(intelRdtRoot, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &intelRdtTestUtil{config: config, IntelRdtPath: testIntelRdtPath, t: t}
|
||||
|
||||
origRoot := root
|
||||
t.Cleanup(func() {
|
||||
root = origRoot
|
||||
})
|
||||
|
||||
root = func() (string, error) {
|
||||
return intelRdtRoot, nil
|
||||
}
|
||||
|
||||
return intelRdtRoot
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user