Skip to content

Commit 325a74d

Browse files
committed
libcontainer/intelrdt: rm init() from intelrdt.go
Use sync.Once to init Intel RDT when needed for a small speedup to operations which do not require Intel RDT. Simplify IntelRdtManager initialization in LinuxFactory. Signed-off-by: Xiaochen Shen <xiaochen.shen@intel.com>
1 parent 544048b commit 325a74d

File tree

5 files changed

+61
-52
lines changed

5 files changed

+61
-52
lines changed

libcontainer/factory_linux.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,12 @@ func RootlessCgroupfs(l *LinuxFactory) error {
148148
// containers that use the Intel RDT "resource control" filesystem to
149149
// create and manage Intel RDT resources (e.g., L3 cache, memory bandwidth).
150150
func IntelRdtFs(l *LinuxFactory) error {
151-
l.NewIntelRdtManager = func(config *configs.Config, id string, path string) intelrdt.Manager {
152-
return intelrdt.NewManager(config, id, path)
151+
if !intelrdt.IsCATEnabled() && !intelrdt.IsMBAEnabled() {
152+
l.NewIntelRdtManager = nil
153+
} else {
154+
l.NewIntelRdtManager = func(config *configs.Config, id string, path string) intelrdt.Manager {
155+
return intelrdt.NewManager(config, id, path)
156+
}
153157
}
154158
return nil
155159
}
@@ -272,7 +276,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
272276
newgidmapPath: l.NewgidmapPath,
273277
cgroupManager: l.NewCgroupsManager(config.Cgroups, nil),
274278
}
275-
if intelrdt.IsCATEnabled() || intelrdt.IsMBAEnabled() {
279+
if l.NewIntelRdtManager != nil {
276280
c.intelRdtManager = l.NewIntelRdtManager(config, id, "")
277281
}
278282
c.state = &stoppedState{c: c}
@@ -314,13 +318,13 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
314318
root: containerRoot,
315319
created: state.Created,
316320
}
321+
if l.NewIntelRdtManager != nil {
322+
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
323+
}
317324
c.state = &loadedState{c: c}
318325
if err := c.refreshState(); err != nil {
319326
return nil, err
320327
}
321-
if intelrdt.IsCATEnabled() || intelrdt.IsMBAEnabled() {
322-
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
323-
}
324328
return c, nil
325329
}
326330

libcontainer/intelrdt/cmt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var (
66

77
// Check if Intel RDT/CMT is enabled.
88
func IsCMTEnabled() bool {
9+
featuresInit()
910
return cmtEnabled
1011
}
1112

libcontainer/intelrdt/intelrdt.go

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ var (
195195
mbaEnabled bool
196196
// The flag to indicate if Intel RDT/MBA Software Controller is enabled
197197
mbaScEnabled bool
198+
199+
// For Intel RDT initialization
200+
initOnce sync.Once
198201
)
199202

200203
type intelRdtData struct {
@@ -203,55 +206,56 @@ type intelRdtData struct {
203206
pid int
204207
}
205208

206-
// Check if Intel RDT sub-features are enabled in init()
207-
func init() {
208-
// 1. Check if hardware and kernel support Intel RDT sub-features
209-
flagsSet, err := parseCpuInfoFile("/proc/cpuinfo")
210-
if err != nil {
211-
return
212-
}
213-
214-
// 2. Check if Intel RDT "resource control" filesystem is mounted
215-
// The user guarantees to mount the filesystem
216-
if !isIntelRdtMounted() {
217-
return
218-
}
219-
220-
// 3. Double check if Intel RDT sub-features are available in
221-
// "resource control" filesystem. Intel RDT sub-features can be
222-
// selectively disabled or enabled by kernel command line
223-
// (e.g., rdt=!l3cat,mba) in 4.14 and newer kernel
224-
if flagsSet.CAT {
225-
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3")); err == nil {
226-
catEnabled = true
227-
}
228-
}
229-
if mbaScEnabled {
230-
// We confirm MBA Software Controller is enabled in step 2,
231-
// MBA should be enabled because MBA Software Controller
232-
// depends on MBA
233-
mbaEnabled = true
234-
} else if flagsSet.MBA {
235-
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "MB")); err == nil {
236-
mbaEnabled = true
209+
// Check if Intel RDT sub-features are enabled in featuresInit()
210+
func featuresInit() {
211+
initOnce.Do(func() {
212+
// 1. Check if hardware and kernel support Intel RDT sub-features
213+
flagsSet, err := parseCpuInfoFile("/proc/cpuinfo")
214+
if err != nil {
215+
return
237216
}
238-
}
239217

240-
if flagsSet.MBMTotal || flagsSet.MBMLocal || flagsSet.CMT {
241-
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3_MON")); err != nil {
218+
// 2. Check if Intel RDT "resource control" filesystem is mounted
219+
// The user guarantees to mount the filesystem
220+
if !isIntelRdtMounted() {
242221
return
243222
}
244-
enabledMonFeatures, err = getMonFeatures(intelRdtRoot)
245-
if err != nil {
246-
return
223+
224+
// 3. Double check if Intel RDT sub-features are available in
225+
// "resource control" filesystem. Intel RDT sub-features can be
226+
// selectively disabled or enabled by kernel command line
227+
// (e.g., rdt=!l3cat,mba) in 4.14 and newer kernel
228+
if flagsSet.CAT {
229+
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3")); err == nil {
230+
catEnabled = true
231+
}
247232
}
248-
if enabledMonFeatures.mbmTotalBytes || enabledMonFeatures.mbmLocalBytes {
249-
mbmEnabled = true
233+
if mbaScEnabled {
234+
// We confirm MBA Software Controller is enabled in step 2,
235+
// MBA should be enabled because MBA Software Controller
236+
// depends on MBA
237+
mbaEnabled = true
238+
} else if flagsSet.MBA {
239+
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "MB")); err == nil {
240+
mbaEnabled = true
241+
}
250242
}
251-
if enabledMonFeatures.llcOccupancy {
252-
cmtEnabled = true
243+
if flagsSet.MBMTotal || flagsSet.MBMLocal || flagsSet.CMT {
244+
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3_MON")); err != nil {
245+
return
246+
}
247+
enabledMonFeatures, err = getMonFeatures(intelRdtRoot)
248+
if err != nil {
249+
return
250+
}
251+
if enabledMonFeatures.mbmTotalBytes || enabledMonFeatures.mbmLocalBytes {
252+
mbmEnabled = true
253+
}
254+
if enabledMonFeatures.llcOccupancy {
255+
cmtEnabled = true
256+
}
253257
}
254-
}
258+
})
255259
}
256260

257261
// Return the mount point path of Intel RDT "resource control" filesysem
@@ -525,16 +529,19 @@ func WriteIntelRdtTasks(dir string, pid int) error {
525529

526530
// Check if Intel RDT/CAT is enabled
527531
func IsCATEnabled() bool {
532+
featuresInit()
528533
return catEnabled
529534
}
530535

531536
// Check if Intel RDT/MBA is enabled
532537
func IsMBAEnabled() bool {
538+
featuresInit()
533539
return mbaEnabled
534540
}
535541

536542
// Check if Intel RDT/MBA Software Controller is enabled
537543
func IsMBAScEnabled() bool {
544+
featuresInit()
538545
return mbaScEnabled
539546
}
540547

libcontainer/intelrdt/mbm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var (
99

1010
// Check if Intel RDT/MBM is enabled.
1111
func IsMBMEnabled() bool {
12+
featuresInit()
1213
return mbmEnabled
1314
}
1415

utils_linux.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/opencontainers/runc/libcontainer"
1414
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
1515
"github.com/opencontainers/runc/libcontainer/configs"
16-
"github.com/opencontainers/runc/libcontainer/intelrdt"
1716
"github.com/opencontainers/runc/libcontainer/specconv"
1817
"github.com/opencontainers/runc/libcontainer/utils"
1918
"github.com/opencontainers/runtime-spec/specs-go"
@@ -57,9 +56,6 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
5756
}
5857

5958
intelRdtManager := libcontainer.IntelRdtFs
60-
if !intelrdt.IsCATEnabled() && !intelrdt.IsMBAEnabled() {
61-
intelRdtManager = nil
62-
}
6359

6460
// We resolve the paths for {newuidmap,newgidmap} from the context of runc,
6561
// to avoid doing a path lookup in the nsexec context. TODO: The binary

0 commit comments

Comments
 (0)