Skip to content

Commit db63953

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 <[email protected]>
1 parent 08f0468 commit db63953

File tree

3 files changed

+57
-52
lines changed

3 files changed

+57
-52
lines changed

libcontainer/factory_linux.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,13 @@ 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+
intelrdt.InitIntelRdt()
152+
if !intelrdt.IsCATEnabled() && !intelrdt.IsMBAEnabled() {
153+
l.NewIntelRdtManager = nil
154+
} else {
155+
l.NewIntelRdtManager = func(config *configs.Config, id string, path string) intelrdt.Manager {
156+
return intelrdt.NewManager(config, id, path)
157+
}
153158
}
154159
return nil
155160
}
@@ -272,7 +277,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
272277
newgidmapPath: l.NewgidmapPath,
273278
cgroupManager: l.NewCgroupsManager(config.Cgroups, nil),
274279
}
275-
if intelrdt.IsCATEnabled() || intelrdt.IsMBAEnabled() {
280+
if l.NewIntelRdtManager != nil {
276281
c.intelRdtManager = l.NewIntelRdtManager(config, id, "")
277282
}
278283
c.state = &stoppedState{c: c}
@@ -314,13 +319,13 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
314319
root: containerRoot,
315320
created: state.Created,
316321
}
322+
if l.NewIntelRdtManager != nil {
323+
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
324+
}
317325
c.state = &loadedState{c: c}
318326
if err := c.refreshState(); err != nil {
319327
return nil, err
320328
}
321-
if intelrdt.IsCATEnabled() || intelrdt.IsMBAEnabled() {
322-
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
323-
}
324329
return c, nil
325330
}
326331

libcontainer/intelrdt/intelrdt.go

Lines changed: 46 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 InitIntelRdt()
210+
func InitIntelRdt() {
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

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)