From 3cbe3eb3f693876d6c94c0e4d72d627df05a95ae Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Wed, 10 Sep 2014 17:44:13 -0700 Subject: [PATCH] Cache cgroup root mount location. We calculate this on every cgroup call. It comprised of 30%+ of the CPU usage in cAdvisor. Docker-DCO-1.1-Signed-off-by: Victor Marmol (github: vmarmol) --- cgroups/fs/apply_raw.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/cgroups/fs/apply_raw.go b/cgroups/fs/apply_raw.go index 443dbb698..133241e47 100644 --- a/cgroups/fs/apply_raw.go +++ b/cgroups/fs/apply_raw.go @@ -24,6 +24,23 @@ var ( CgroupProcesses = "cgroup.procs" ) +// The absolute path to the root of the cgroup hierarchies. +var cgroupRoot string + +// TODO(vmarmol): Report error here, we'll probably need to wait for the new API. +func init() { + // we can pick any subsystem to find the root + cpuRoot, err := cgroups.FindCgroupMountpoint("cpu") + if err != nil { + return + } + cgroupRoot = filepath.Dir(cpuRoot) + + if _, err := os.Stat(cgroupRoot); err != nil { + return + } +} + type subsystem interface { // Returns the stats, as 'stats', corresponding to the cgroup under 'path'. GetStats(path string, stats *cgroups.Stats) error @@ -121,15 +138,8 @@ func GetPids(c *cgroups.Cgroup) ([]int, error) { } func getCgroupData(c *cgroups.Cgroup, pid int) (*data, error) { - // we can pick any subsystem to find the root - cgroupRoot, err := cgroups.FindCgroupMountpoint("cpu") - if err != nil { - return nil, err - } - cgroupRoot = filepath.Dir(cgroupRoot) - - if _, err := os.Stat(cgroupRoot); err != nil { - return nil, fmt.Errorf("cgroups fs not found") + if cgroupRoot == "" { + return nil, fmt.Errorf("failed to find the cgroup root") } cgroup := c.Name