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,9 +36,9 @@ func (s *cpuacctGroup) Remove(d *data) error {
func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) { func (s *cpuacctGroup) Stats(d *data) (map[string]float64, error) {
var ( var (
startCpu, lastCpu, startSystem, lastSystem float64 startCpu, lastCpu, startSystem, lastSystem, startUsage, lastUsage float64
percentage float64 percentage float64
paramData = make(map[string]float64) paramData = make(map[string]float64)
) )
path, err := d.path("cpuacct") path, err := d.path("cpuacct")
if startCpu, err = s.getCpuUsage(d, path); err != nil { 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 { if startSystem, err = s.getSystemCpuUsage(d); err != nil {
return nil, err return nil, err
} }
startUsageTime := time.Now()
if startUsage, err = getCgroupParamFloat64(path, "cpuacct.usage"); err != nil {
return nil, err
}
// sample for 100ms // sample for 100ms
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
if lastCpu, err = s.getCpuUsage(d, path); err != nil { 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 { if lastSystem, err = s.getSystemCpuUsage(d); err != nil {
return nil, err return nil, err
} }
usageSampleDuration := time.Since(startUsageTime)
if lastUsage, err = getCgroupParamFloat64(path, "cpuacct.usage"); err != nil {
return nil, err
}
var ( var (
deltaProc = lastCpu - startCpu deltaProc = lastCpu - startCpu
deltaSystem = lastSystem - startSystem deltaSystem = lastSystem - startSystem
deltaUsage = lastUsage - startUsage
) )
if deltaSystem > 0.0 { if deltaSystem > 0.0 {
percentage = ((deltaProc / deltaSystem) * clockTicks) * cpuCount 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 // NOTE: a percentage over 100% is valid for POSIX because that means the
// processes is using multiple cores // processes is using multiple cores
paramData["percentage"] = percentage 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 return paramData, nil
} }