From 741e47e6a45f11491bc26788a9ec347ce3303503 Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Mon, 5 May 2014 23:56:53 +0000 Subject: [PATCH] Export cpuacct CPU usage in total cores over the sampled period. Docker-DCO-1.1-Signed-off-by: Victor Marmol (github: vmarmol) --- cgroups/fs/cpuacct.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cgroups/fs/cpuacct.go b/cgroups/fs/cpuacct.go index 4ea2b1f..892b5ab 100644 --- a/cgroups/fs/cpuacct.go +++ b/cgroups/fs/cpuacct.go @@ -36,9 +36,9 @@ func (s *cpuacctGroup) Remove(d *data) error { func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { var ( - startCpu, lastCpu, startSystem, lastSystem float64 - percentage float64 - paramData = make(map[string]float64) + startCpu, lastCpu, startSystem, lastSystem, startUsage, lastUsage float64 + percentage float64 + paramData = make(map[string]float64) ) path, err := d.path("cpuacct") if startCpu, err = s.getCpuUsage(d, path); err != nil { @@ -47,6 +47,10 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { if startSystem, err = s.getSystemCpuUsage(d); err != nil { return nil, err } + startUsageTime := time.Now() + if startUsage, err = getCgroupParamFloat64(path, "cpuacct.usage"); err != nil { + return nil, err + } // sample for 100ms time.Sleep(100 * time.Millisecond) if lastCpu, err = s.getCpuUsage(d, path); err != nil { @@ -55,10 +59,15 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { if lastSystem, err = s.getSystemCpuUsage(d); err != nil { return nil, err } + usageSampleDuration := time.Since(startUsageTime) + if lastUsage, err = getCgroupParamFloat64(path, "cpuacct.usage"); err != nil { + return nil, err + } var ( deltaProc = lastCpu - startCpu deltaSystem = lastSystem - startSystem + deltaUsage = lastUsage - startUsage ) if deltaSystem > 0.0 { percentage = ((deltaProc / deltaSystem) * clockTicks) * cpuCount @@ -66,6 +75,9 @@ func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { // NOTE: a percentage over 100% is valid for POSIX because that means the // processes is using multiple cores paramData["percentage"] = percentage + + // Delta usage is in nanoseconds of CPU time so get the usage (in cores) over the sample time. + paramData["usage"] = deltaUsage / float64(usageSampleDuration.Nanoseconds()) return paramData, nil }