diff --git a/libcontainer/configs/validate/validator.go b/libcontainer/configs/validate/validator.go index ca2efe84d85..cbbba9a03a2 100644 --- a/libcontainer/configs/validate/validator.go +++ b/libcontainer/configs/validate/validator.go @@ -159,7 +159,7 @@ func (v *ConfigValidator) sysctl(config *configs.Config) error { func (v *ConfigValidator) intelrdt(config *configs.Config) error { if config.IntelRdt != nil { - if !intelrdt.IsIntelRdtEnabled() { + if !intelrdt.IsEnabled() { return fmt.Errorf("intelRdt is specified in config, but Intel RDT feature is not supported or enabled") } if config.IntelRdt.L3CacheSchema == "" { diff --git a/libcontainer/container_linux_test.go b/libcontainer/container_linux_test.go index 263db00feb8..3c9143ab514 100644 --- a/libcontainer/container_linux_test.go +++ b/libcontainer/container_linux_test.go @@ -160,7 +160,7 @@ func TestGetContainerStats(t *testing.T) { if stats.CgroupStats.MemoryStats.Usage.Usage != 1024 { t.Fatalf("expected memory usage 1024 but recevied %d", stats.CgroupStats.MemoryStats.Usage.Usage) } - if intelrdt.IsIntelRdtEnabled() { + if intelrdt.IsEnabled() { if stats.IntelRdtStats == nil { t.Fatal("intel rdt stats are nil") } @@ -231,7 +231,7 @@ func TestGetContainerState(t *testing.T) { if memPath := paths["memory"]; memPath != expectedMemoryPath { t.Fatalf("expected memory path %q but received %q", expectedMemoryPath, memPath) } - if intelrdt.IsIntelRdtEnabled() { + if intelrdt.IsEnabled() { intelRdtPath := state.IntelRdtPath if intelRdtPath == "" { t.Fatal("intel rdt path should not be empty") diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index 7ac6e1dee3a..3395f8c26b0 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -204,7 +204,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err cgroupManager: l.NewCgroupsManager(config.Cgroups, nil), } c.intelRdtManager = nil - if intelrdt.IsIntelRdtEnabled() && c.config.IntelRdt != nil { + if intelrdt.IsEnabled() && c.config.IntelRdt != nil { c.intelRdtManager = l.NewIntelRdtManager(config, id, "") } c.state = &stoppedState{c: c} @@ -245,7 +245,7 @@ func (l *LinuxFactory) Load(id string) (Container, error) { return nil, err } c.intelRdtManager = nil - if intelrdt.IsIntelRdtEnabled() && c.config.IntelRdt != nil { + if intelrdt.IsEnabled() && c.config.IntelRdt != nil { c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath) } return c, nil diff --git a/libcontainer/intelrdt/intelrdt.go b/libcontainer/intelrdt/intelrdt.go index 01db772a30a..667625a10ab 100644 --- a/libcontainer/intelrdt/intelrdt.go +++ b/libcontainer/intelrdt/intelrdt.go @@ -130,7 +130,7 @@ var ( intelRdtRootLock sync.Mutex // The flag to indicate if Intel RDT is supported - isIntelRdtEnabled bool + isEnabled bool ) type intelRdtData struct { @@ -139,6 +139,21 @@ type intelRdtData struct { pid int } +// Check if Intel RDT is enabled in init() +func init() { + // 1. Check if hardware and kernel support Intel RDT/CAT feature + // "cat_l3" flag is set if supported + isFlagSet, err := parseCpuInfoFile("/proc/cpuinfo") + if !isFlagSet || err != nil { + isEnabled = false + return + } + + // 2. Check if Intel RDT "resource control" filesystem is mounted + // The user guarantees to mount the filesystem + isEnabled = isIntelRdtMounted() +} + // Return the mount point path of Intel RDT "resource control" filesysem func findIntelRdtMountpointDir() (string, error) { f, err := os.Open("/proc/self/mountinfo") @@ -369,24 +384,8 @@ func WriteIntelRdtTasks(dir string, pid int) error { } // Check if Intel RDT is enabled -func IsIntelRdtEnabled() bool { - // We have checked the flag before - if isIntelRdtEnabled { - return true - } - - // 1. Check if hardware and kernel support Intel RDT/CAT feature - // "cat_l3" flag is set if supported - isFlagSet, err := parseCpuInfoFile("/proc/cpuinfo") - if !isFlagSet || err != nil { - isIntelRdtEnabled = false - return false - } - - // 2. Check if Intel RDT "resource control" filesystem is mounted - // The user guarantees to mount the filesystem - isIntelRdtEnabled = isIntelRdtMounted() - return isIntelRdtEnabled +func IsEnabled() bool { + return isEnabled } // Get the 'container_id' path in Intel RDT "resource control" filesystem diff --git a/libcontainer/intelrdt/intelrdt_test.go b/libcontainer/intelrdt/intelrdt_test.go index 8e4081640d9..32bd1e4c642 100644 --- a/libcontainer/intelrdt/intelrdt_test.go +++ b/libcontainer/intelrdt/intelrdt_test.go @@ -8,7 +8,7 @@ import ( ) func TestIntelRdtSetL3CacheSchema(t *testing.T) { - if !IsIntelRdtEnabled() { + if !IsEnabled() { return } diff --git a/utils_linux.go b/utils_linux.go index 6da9171c02e..791e52d31d2 100644 --- a/utils_linux.go +++ b/utils_linux.go @@ -43,7 +43,7 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) { return nil, fmt.Errorf("systemd cgroup flag passed, but systemd support for managing cgroups is not available") } } - if intelrdt.IsIntelRdtEnabled() { + if intelrdt.IsEnabled() { intelRdtManager := libcontainer.IntelRdtFs return libcontainer.New(abs, cgroupManager, intelRdtManager, libcontainer.CriuPath(context.GlobalString("criu"))) }