Export cpuacct CPU usage in total cores over the sampled period.

Docker-DCO-1.1-Signed-off-by: Victor Marmol <vmarmol@google.com> (github: vmarmol)
This commit is contained in:
Victor Marmol 2014-05-05 23:56:53 +00:00
parent 512bf6cd45
commit 741e47e6a4

View file

@ -36,7 +36,7 @@ func (s *cpuacctGroup) Remove(d *data) error {
func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) {
var (
startCpu, lastCpu, startSystem, lastSystem float64
startCpu, lastCpu, startSystem, lastSystem, startUsage, lastUsage float64
percentage float64
paramData = make(map[string]float64)
)
@ -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
}