Skip to content

Commit afced5c

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. Signed-off-by: Xiaochen Shen <[email protected]>
1 parent 9067950 commit afced5c

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

libcontainer/intelrdt/intelrdt.go

Lines changed: 45 additions & 40 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,54 +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-
}
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
208+
}
205209

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-
}
210+
// 2. Check if Intel RDT "resource control" filesystem is mounted
211+
// The user guarantees to mount the filesystem
212+
if !isIntelRdtMounted() {
213+
return
214+
}
211215

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
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+
}
219224
}
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 {
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
228229
mbaEnabled = true
230+
} else if flagsSet.MBA {
231+
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "MB")); err == nil {
232+
mbaEnabled = true
233+
}
229234
}
230-
}
231235

232-
if flagsSet.MBMTotal || flagsSet.MBMLocal || flagsSet.CMT {
233-
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3_MON")); err == nil {
234-
enabledMonFeatures, err = getMonFeatures(intelRdtRoot)
235-
if err != nil {
236-
return
237-
}
238-
if enabledMonFeatures.mbmTotalBytes || enabledMonFeatures.mbmLocalBytes {
239-
mbmEnabled = true
240-
}
241-
if enabledMonFeatures.llcOccupancy {
242-
cmtEnabled = true
236+
if flagsSet.MBMTotal || flagsSet.MBMLocal || flagsSet.CMT {
237+
if _, err := os.Stat(filepath.Join(intelRdtRoot, "info", "L3_MON")); err == nil {
238+
enabledMonFeatures, err = getMonFeatures(intelRdtRoot)
239+
if err != nil {
240+
return
241+
}
242+
if enabledMonFeatures.mbmTotalBytes || enabledMonFeatures.mbmLocalBytes {
243+
mbmEnabled = true
244+
}
245+
if enabledMonFeatures.llcOccupancy {
246+
cmtEnabled = true
247+
}
243248
}
244249
}
245-
}
250+
})
246251
}
247252

248253
// Return the mount point path of Intel RDT "resource control" filesysem

utils_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
5656
}
5757
}
5858

59+
intelrdt.InitIntelRdt()
5960
intelRdtManager := libcontainer.IntelRdtFs
6061
if !intelrdt.IsCatEnabled() && !intelrdt.IsMbaEnabled() {
6162
intelRdtManager = nil

0 commit comments

Comments
 (0)