Merge pull request #5631 from vmarmol/cpuacct-usage

Export cpuacct CPU usage in total cores over the sampled period.
This commit is contained in:
Michael Crosby 2014-05-06 11:47:55 -07:00
commit a65772368c

View file

@ -36,7 +36,7 @@ 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)
) )
@ -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
} }