11package intelrdt
22
33import (
4- "bufio"
54 "bytes"
65 "errors"
76 "fmt"
@@ -153,9 +152,13 @@ type Manager struct {
153152 path string
154153}
155154
156- // NewManager returns a new instance of Manager, or nil, if the Intel RDT
157- // functionality is not available from hardware or not enabled in the kernel.
155+ // NewManager returns a new instance of Manager, or nil if the Intel RDT
156+ // functionality is not specified in the config, available from hardware or
157+ // enabled in the kernel.
158158func NewManager (config * configs.Config , id string , path string ) * Manager {
159+ if config .IntelRdt == nil {
160+ return nil
161+ }
159162 if _ , err := Root (); err != nil {
160163 // Intel RDT is not available.
161164 return nil
@@ -182,8 +185,6 @@ var (
182185 catEnabled bool
183186 // The flag to indicate if Intel RDT/MBA is enabled
184187 mbaEnabled bool
185- // The flag to indicate if Intel RDT/MBA Software Controller is enabled
186- mbaScEnabled bool
187188
188189 // For Intel RDT initialization
189190 initOnce sync.Once
@@ -201,50 +202,33 @@ func featuresInit() {
201202 return
202203 }
203204
204- // 2. Check if hardware and kernel support Intel RDT sub-features.
205- flagsSet , err := parseCpuInfoFile ("/proc/cpuinfo" )
206- if err != nil {
207- return
208- }
209-
210- // 3. Double check if Intel RDT sub-features are available in
211- // "resource control" filesystem. Intel RDT sub-features can be
205+ // 2. Check if Intel RDT sub-features are available in "resource
206+ // control" filesystem. Intel RDT sub-features can be
212207 // selectively disabled or enabled by kernel command line
213208 // (e.g., rdt=!l3cat,mba) in 4.14 and newer kernel
214- if flagsSet .CAT {
215- if _ , err := os .Stat (filepath .Join (root , "info" , "L3" )); err == nil {
216- catEnabled = true
217- }
209+ if _ , err := os .Stat (filepath .Join (root , "info" , "L3" )); err == nil {
210+ catEnabled = true
218211 }
219- if mbaScEnabled {
220- // We confirm MBA Software Controller is enabled in step 2,
221- // MBA should be enabled because MBA Software Controller
222- // depends on MBA
212+ if _ , err := os .Stat (filepath .Join (root , "info" , "MB" )); err == nil {
223213 mbaEnabled = true
224- } else if flagsSet .MBA {
225- if _ , err := os .Stat (filepath .Join (root , "info" , "MB" )); err == nil {
226- mbaEnabled = true
227- }
228214 }
229- if flagsSet .MBMTotal || flagsSet .MBMLocal || flagsSet .CMT {
230- if _ , err := os .Stat (filepath .Join (root , "info" , "L3_MON" )); err != nil {
231- return
232- }
233- enabledMonFeatures , err = getMonFeatures (root )
234- if err != nil {
235- return
236- }
237- if enabledMonFeatures .mbmTotalBytes || enabledMonFeatures .mbmLocalBytes {
238- mbmEnabled = true
239- }
240- if enabledMonFeatures .llcOccupancy {
241- cmtEnabled = true
242- }
215+ if _ , err := os .Stat (filepath .Join (root , "info" , "L3_MON" )); err != nil {
216+ return
217+ }
218+ enabledMonFeatures , err = getMonFeatures (root )
219+ if err != nil {
220+ return
221+ }
222+ if enabledMonFeatures .mbmTotalBytes || enabledMonFeatures .mbmLocalBytes {
223+ mbmEnabled = true
224+ }
225+ if enabledMonFeatures .llcOccupancy {
226+ cmtEnabled = true
243227 }
244228 })
245229}
246230
247- // Return the mount point path of Intel RDT "resource control" filesystem.
231+ // findIntelRdtMountpointDir returns the mount point of the Intel RDT "resource control" filesystem.
248232func findIntelRdtMountpointDir () (string , error ) {
249233 mi , err := mountinfo .GetMounts (func (m * mountinfo.Info ) (bool , bool ) {
250234 // similar to mountinfo.FSTypeFilter but stops after the first match
@@ -260,11 +244,6 @@ func findIntelRdtMountpointDir() (string, error) {
260244 return "" , errNotFound
261245 }
262246
263- // Check if MBA Software Controller is enabled through mount option "-o mba_MBps"
264- if strings .Contains ("," + mi [0 ].VFSOptions + "," , ",mba_MBps," ) {
265- mbaScEnabled = true
266- }
267-
268247 return mi [0 ].Mountpoint , nil
269248}
270249
@@ -275,81 +254,37 @@ var (
275254 rootOnce sync.Once
276255)
277256
257+ // The kernel creates this (empty) directory if resctrl is supported by the
258+ // hardware and kernel. The user is responsible for mounting the resctrl
259+ // filesystem, and they could mount it somewhere else if they wanted to.
260+ const defaultResctrlMountpoint = "/sys/fs/resctrl"
261+
278262// Root returns the Intel RDT "resource control" filesystem mount point.
279263func Root () (string , error ) {
280264 rootOnce .Do (func () {
281- // If resctrl is available, kernel creates this directory.
282- if unix .Access ("/sys/fs/resctrl" , unix .F_OK ) != nil {
283- intelRdtRootErr = errNotFound
265+ // Does this system support resctrl?
266+ var statfs unix.Statfs_t
267+ if err := unix .Statfs (defaultResctrlMountpoint , & statfs ); err != nil {
268+ if errors .Is (err , unix .ENOENT ) {
269+ err = errNotFound
270+ }
271+ intelRdtRootErr = err
284272 return
285273 }
286274
287- // NB: ideally, we could just do statfs and RDTGROUP_SUPER_MAGIC check, but
288- // we have to parse mountinfo since we're also interested in mount options.
289- root , err := findIntelRdtMountpointDir ()
290- if err != nil {
291- intelRdtRootErr = err
275+ // Has the resctrl fs been mounted to the default mount point?
276+ if statfs .Type == unix .RDTGROUP_SUPER_MAGIC {
277+ intelRdtRoot = defaultResctrlMountpoint
292278 return
293279 }
294280
295- intelRdtRoot = root
281+ // The resctrl fs could have been mounted somewhere nonstandard.
282+ intelRdtRoot , intelRdtRootErr = findIntelRdtMountpointDir ()
296283 })
297284
298285 return intelRdtRoot , intelRdtRootErr
299286}
300287
301- type cpuInfoFlags struct {
302- CAT bool // Cache Allocation Technology
303- MBA bool // Memory Bandwidth Allocation
304-
305- // Memory Bandwidth Monitoring related.
306- MBMTotal bool
307- MBMLocal bool
308-
309- CMT bool // Cache Monitoring Technology
310- }
311-
312- func parseCpuInfoFile (path string ) (cpuInfoFlags , error ) {
313- infoFlags := cpuInfoFlags {}
314-
315- f , err := os .Open (path )
316- if err != nil {
317- return infoFlags , err
318- }
319- defer f .Close ()
320-
321- s := bufio .NewScanner (f )
322- for s .Scan () {
323- line := s .Text ()
324-
325- // Search "cat_l3" and "mba" flags in first "flags" line
326- if strings .HasPrefix (line , "flags" ) {
327- flags := strings .Split (line , " " )
328- // "cat_l3" flag for CAT and "mba" flag for MBA
329- for _ , flag := range flags {
330- switch flag {
331- case "cat_l3" :
332- infoFlags .CAT = true
333- case "mba" :
334- infoFlags .MBA = true
335- case "cqm_mbm_total" :
336- infoFlags .MBMTotal = true
337- case "cqm_mbm_local" :
338- infoFlags .MBMLocal = true
339- case "cqm_occup_llc" :
340- infoFlags .CMT = true
341- }
342- }
343- return infoFlags , nil
344- }
345- }
346- if err := s .Err (); err != nil {
347- return infoFlags , err
348- }
349-
350- return infoFlags , nil
351- }
352-
353288// Gets a single uint64 value from the specified file.
354289func getIntelRdtParamUint (path , file string ) (uint64 , error ) {
355290 fileName := filepath .Join (path , file )
@@ -493,12 +428,6 @@ func IsMBAEnabled() bool {
493428 return mbaEnabled
494429}
495430
496- // Check if Intel RDT/MBA Software Controller is enabled
497- func IsMBAScEnabled () bool {
498- featuresInit ()
499- return mbaScEnabled
500- }
501-
502431// Get the path of the clos group in "resource control" filesystem that the container belongs to
503432func (m * Manager ) getIntelRdtPath () (string , error ) {
504433 rootPath , err := Root ()
@@ -554,7 +483,7 @@ func (m *Manager) Destroy() error {
554483 // Don't remove resctrl group if closid has been explicitly specified. The
555484 // group is likely externally managed, i.e. by some other entity than us.
556485 // There are probably other containers/tasks sharing the same group.
557- if m .config .IntelRdt == nil || m .config .IntelRdt .ClosID == "" {
486+ if m .config .IntelRdt != nil && m .config .IntelRdt .ClosID == "" {
558487 m .mu .Lock ()
559488 defer m .mu .Unlock ()
560489 if err := os .RemoveAll (m .GetPath ()); err != nil {
0 commit comments