Skip to content

Commit

Permalink
cgroupv1/fs/cpuset/getMount: avoid parsing mountinfo
Browse files Browse the repository at this point in the history
It is much faster to traverse up a directory tree, comparing the dev_t
field of stat(2) until we hit another device.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Jun 9, 2020
1 parent a455619 commit 3f555f0
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions libcontainer/cgroups/fs/cpuset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"syscall"

"github.com/moby/sys/mountinfo"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
"github.com/opencontainers/runc/libcontainer/configs"
Expand Down Expand Up @@ -47,24 +47,30 @@ func (s *CpusetGroup) GetStats(path string, stats *cgroups.Stats) error {

// Get the source mount point of directory passed in as argument.
func getMount(dir string) (string, error) {
mi, err := mountinfo.GetMounts(mountinfo.ParentsFilter(dir))
dir, err := filepath.Abs(dir)
if err != nil {
return "", err
}
if len(mi) < 1 {
return "", errors.Errorf("Can't find mount point of %s", dir)
st, err := os.Lstat(dir)
if err != nil {
return "", err
}

// find the longest mount point
var idx, maxlen int
for i := range mi {
if len(mi[i].Mountpoint) > maxlen {
maxlen = len(mi[i].Mountpoint)
idx = i
if !st.IsDir() {
return "", &os.PathError{Op: "getMount", Path: dir, Err: syscall.ENOTDIR}
}
for dir != "/" {
parent := filepath.Dir(dir)
pst, err := os.Lstat(parent)
if err != nil {
return "", err
}
if st.Sys().(*syscall.Stat_t).Dev != pst.Sys().(*syscall.Stat_t).Dev {
return dir, nil
}
dir = parent
}

return mi[idx].Mountpoint, nil
return dir, nil
}

func (s *CpusetGroup) ApplyDir(dir string, cgroup *configs.Cgroup, pid int) error {
Expand All @@ -75,7 +81,7 @@ func (s *CpusetGroup) ApplyDir(dir string, cgroup *configs.Cgroup, pid int) erro
}
root, err := getMount(dir)
if err != nil {
return err
return errors.Wrap(err, "CpusetCgroup.Apply")
}
root = filepath.Dir(root)
// 'ensureParent' start with parent because we don't want to
Expand Down

0 comments on commit 3f555f0

Please sign in to comment.