From bc67941c726007570d092d4449c41019e6a48c7f Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Wed, 9 Sep 2015 09:28:50 +0800 Subject: [PATCH] Parse directly in FindCgroupMountpointDir Unify it with FindCgroupMountpoint, and add comments why we should to do this. Signed-off-by: Qiang Huang --- libcontainer/cgroups/utils.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/libcontainer/cgroups/utils.go b/libcontainer/cgroups/utils.go index 1d0cb502c..fbec948c2 100644 --- a/libcontainer/cgroups/utils.go +++ b/libcontainer/cgroups/utils.go @@ -21,6 +21,9 @@ const cgroupNamePrefix = "name=" // https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt func FindCgroupMountpoint(subsystem string) (string, error) { + // We are not using mount.GetMounts() because it's super-inefficient, + // parsing it directly sped up x10 times because of not using Sscanf. + // It was one of two major performance drawbacks in container start. f, err := os.Open("/proc/self/mountinfo") if err != nil { return "", err @@ -69,16 +72,23 @@ func FindCgroupMountpointAndSource(subsystem string) (string, string, error) { } func FindCgroupMountpointDir() (string, error) { - mounts, err := mount.GetMounts() + f, err := os.Open("/proc/self/mountinfo") if err != nil { return "", err } + defer f.Close() - for _, mount := range mounts { - if mount.Fstype == "cgroup" { - return filepath.Dir(mount.Mountpoint), nil + scanner := bufio.NewScanner(f) + for scanner.Scan() { + txt := scanner.Text() + fields := strings.Split(txt, " ") + if fields[7] == "cgroup" { + return filepath.Dir(fields[4]), nil } } + if err := scanner.Err(); err != nil { + return "", err + } return "", NewNotFoundError("cgroup") }