Skip to content

Commit 43aa44f

Browse files
corherekolyshkin
authored andcommitted
libct/intelrdt: elide parsing mountinfo
The intelrdt package only needs to parse mountinfo to find the mount point of the resctrl filesystem. Users are generally going to mount the resctrl filesystem to the pre-created /sys/fs/resctrl directory, so there is a common case where mountinfo parsing is not required. Optimize for the common case with a fast path which checks both for the existence of the /sys/fs/resctrl directory and whether the resctrl filesystem was mounted to that path using a single statfs syscall. Signed-off-by: Cory Snider <[email protected]> (cherry picked from commit c156bde) Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent b431460 commit 43aa44f

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

libcontainer/intelrdt/intelrdt.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func featuresInit() {
224224
})
225225
}
226226

227-
// Return the mount point path of Intel RDT "resource control" filesystem.
227+
// findIntelRdtMountpointDir returns the mount point of the Intel RDT "resource control" filesystem.
228228
func findIntelRdtMountpointDir() (string, error) {
229229
mi, err := mountinfo.GetMounts(func(m *mountinfo.Info) (bool, bool) {
230230
// similar to mountinfo.FSTypeFilter but stops after the first match
@@ -250,24 +250,32 @@ var (
250250
rootOnce sync.Once
251251
)
252252

253+
// The kernel creates this (empty) directory if resctrl is supported by the
254+
// hardware and kernel. The user is responsible for mounting the resctrl
255+
// filesystem, and they could mount it somewhere else if they wanted to.
256+
const defaultResctrlMountpoint = "/sys/fs/resctrl"
257+
253258
// Root returns the Intel RDT "resource control" filesystem mount point.
254259
func Root() (string, error) {
255260
rootOnce.Do(func() {
256-
// If resctrl is available, kernel creates this directory.
257-
if unix.Access("/sys/fs/resctrl", unix.F_OK) != nil {
258-
intelRdtRootErr = errNotFound
261+
// Does this system support resctrl?
262+
var statfs unix.Statfs_t
263+
if err := unix.Statfs(defaultResctrlMountpoint, &statfs); err != nil {
264+
if errors.Is(err, unix.ENOENT) {
265+
err = errNotFound
266+
}
267+
intelRdtRootErr = err
259268
return
260269
}
261270

262-
// NB: ideally, we could just do statfs and RDTGROUP_SUPER_MAGIC check, but
263-
// we have to parse mountinfo since we're also interested in mount options.
264-
root, err := findIntelRdtMountpointDir()
265-
if err != nil {
266-
intelRdtRootErr = err
271+
// Has the resctrl fs been mounted to the default mount point?
272+
if statfs.Type == unix.RDTGROUP_SUPER_MAGIC {
273+
intelRdtRoot = defaultResctrlMountpoint
267274
return
268275
}
269276

270-
intelRdtRoot = root
277+
// The resctrl fs could have been mounted somewhere nonstandard.
278+
intelRdtRoot, intelRdtRootErr = findIntelRdtMountpointDir()
271279
})
272280

273281
return intelRdtRoot, intelRdtRootErr

0 commit comments

Comments
 (0)