Skip to content

Commit b856cd3

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 0cc43aa commit b856cd3

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

libcontainer/factory_linux.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,16 @@ 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.IntelRdtManager{
153-
Config: config,
154-
Id: id,
155-
Path: 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.IntelRdtManager{
157+
Config: config,
158+
Id: id,
159+
Path: path,
160+
}
156161
}
157162
}
158163
return nil
@@ -276,7 +281,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
276281
newgidmapPath: l.NewgidmapPath,
277282
cgroupManager: l.NewCgroupsManager(config.Cgroups, nil),
278283
}
279-
if intelrdt.IsCATEnabled() || intelrdt.IsMBAEnabled() {
284+
if l.NewIntelRdtManager != nil {
280285
c.intelRdtManager = l.NewIntelRdtManager(config, id, "")
281286
}
282287
c.state = &stoppedState{c: c}
@@ -318,13 +323,13 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
318323
root: containerRoot,
319324
created: state.Created,
320325
}
326+
if l.NewIntelRdtManager != nil {
327+
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
328+
}
321329
c.state = &loadedState{c: c}
322330
if err := c.refreshState(); err != nil {
323331
return nil, err
324332
}
325-
if intelrdt.IsCATEnabled() || intelrdt.IsMBAEnabled() {
326-
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
327-
}
328333
return c, nil
329334
}
330335

libcontainer/intelrdt/intelrdt.go

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ var (
187187
mbaEnabled bool
188188
// The flag to indicate if Intel RDT/MBA Software Controller is enabled
189189
mbaScEnabled bool
190+
191+
// For Intel RDT initialization
192+
initOnce sync.Once
190193
)
191194

192195
type intelRdtData struct {
@@ -195,55 +198,56 @@ type intelRdtData struct {
195198
pid int
196199
}
197200

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

232-
if flagsSet.MBMTotal || flagsSet.MBMLocal || flagsSet.CMT {
233-
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3_MON")); err != nil {
210+
// 2. Check if Intel RDT "resource control" filesystem is mounted
211+
// The user guarantees to mount the filesystem
212+
if !isIntelRdtMounted() {
234213
return
235214
}
236-
enabledMonFeatures, err = getMonFeatures(intelRdtRoot)
237-
if err != nil {
238-
return
215+
216+
// 3. Double check if Intel RDT sub-features are available in
217+
// "resource control" filesystem. Intel RDT sub-features can be
218+
// selectively disabled or enabled by kernel command line
219+
// (e.g., rdt=!l3cat,mba) in 4.14 and newer kernel
220+
if flagsSet.CAT {
221+
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3")); err == nil {
222+
catEnabled = true
223+
}
239224
}
240-
if enabledMonFeatures.mbmTotalBytes || enabledMonFeatures.mbmLocalBytes {
241-
mbmEnabled = true
225+
if mbaScEnabled {
226+
// We confirm MBA Software Controller is enabled in step 2,
227+
// MBA should be enabled because MBA Software Controller
228+
// depends on MBA
229+
mbaEnabled = true
230+
} else if flagsSet.MBA {
231+
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "MB")); err == nil {
232+
mbaEnabled = true
233+
}
242234
}
243-
if enabledMonFeatures.llcOccupancy {
244-
cmtEnabled = true
235+
if flagsSet.MBMTotal || flagsSet.MBMLocal || flagsSet.CMT {
236+
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3_MON")); err != nil {
237+
return
238+
}
239+
enabledMonFeatures, err = getMonFeatures(intelRdtRoot)
240+
if err != nil {
241+
return
242+
}
243+
if enabledMonFeatures.mbmTotalBytes || enabledMonFeatures.mbmLocalBytes {
244+
mbmEnabled = true
245+
}
246+
if enabledMonFeatures.llcOccupancy {
247+
cmtEnabled = true
248+
}
245249
}
246-
}
250+
})
247251
}
248252

249253
// 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)