Parse directly in FindCgroupMountpointDir
Unify it with FindCgroupMountpoint, and add comments why we should to do this. Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
This commit is contained in:
parent
0f85e4e1a3
commit
bc67941c72
|
@ -21,6 +21,9 @@ const cgroupNamePrefix = "name="
|
||||||
|
|
||||||
// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
|
// https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
|
||||||
func FindCgroupMountpoint(subsystem string) (string, error) {
|
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")
|
f, err := os.Open("/proc/self/mountinfo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -69,16 +72,23 @@ func FindCgroupMountpointAndSource(subsystem string) (string, string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindCgroupMountpointDir() (string, error) {
|
func FindCgroupMountpointDir() (string, error) {
|
||||||
mounts, err := mount.GetMounts()
|
f, err := os.Open("/proc/self/mountinfo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
for _, mount := range mounts {
|
scanner := bufio.NewScanner(f)
|
||||||
if mount.Fstype == "cgroup" {
|
for scanner.Scan() {
|
||||||
return filepath.Dir(mount.Mountpoint), nil
|
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")
|
return "", NewNotFoundError("cgroup")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue