Merge pull request #1830 from weaveworks/1664-correct-cpu-metrics

correct cpu metrics

Fixes #1664.
This commit is contained in:
Matthias Radestock
2016-08-26 11:18:56 +01:00
committed by GitHub
3 changed files with 5 additions and 10 deletions

View File

@@ -378,7 +378,6 @@ func (c *container) cpuPercentMetric(stats []docker.Stats) report.Metric {
}
samples := make([]report.Sample, len(stats)-1)
var max float64
previous := stats[0]
for i, s := range stats[1:] {
// Copies from docker/api/client/stats.go#L205
@@ -386,17 +385,13 @@ func (c *container) cpuPercentMetric(stats []docker.Stats) report.Metric {
systemDelta := float64(s.CPUStats.SystemCPUUsage - previous.CPUStats.SystemCPUUsage)
cpuPercent := 0.0
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * float64(len(s.CPUStats.CPUUsage.PercpuUsage)) * 100.0
cpuPercent = (cpuDelta / systemDelta) * 100.0
}
samples[i].Timestamp = s.Read
samples[i].Value = cpuPercent
available := float64(len(s.CPUStats.CPUUsage.PercpuUsage)) * 100.0
if available >= max {
max = available
}
previous = s
}
return report.MakeMetric(samples).WithMax(max)
return report.MakeMetric(samples).WithMax(100.0)
}
func (c *container) metrics() report.Metrics {

View File

@@ -74,7 +74,7 @@ var GetUptime = func() (time.Duration, error) {
return (time.Duration(d) * 24 * time.Hour) + (time.Duration(h) * time.Hour) + (time.Duration(m) * time.Minute), nil
}
// GetCPUUsagePercent returns the percent cpu usage and max (ie #cpus * 100)
// GetCPUUsagePercent returns the percent cpu usage and max (i.e. 100% or 0 if unavailable)
var GetCPUUsagePercent = func() (float64, float64) {
return 0.0, 0.0
}

View File

@@ -71,7 +71,7 @@ var GetUptime = func() (time.Duration, error) {
var previousStat = linuxproc.CPUStat{}
// GetCPUUsagePercent returns the percent cpu usage and max (ie #cpus * 100)
// GetCPUUsagePercent returns the percent cpu usage and max (i.e. 100% or 0 if unavailable)
var GetCPUUsagePercent = func() (float64, float64) {
stat, err := linuxproc.ReadStat(ProcStat)
if err != nil {
@@ -94,7 +94,7 @@ var GetCPUUsagePercent = func() (float64, float64) {
idled = idle - prevIdle
)
previousStat = currentStat
return float64(totald-idled) * 100. / float64(totald), float64(len(stat.CPUStats)) * 100.
return float64(totald-idled) * 100. / float64(totald), 100.
}
// GetMemoryUsageBytes returns the bytes memory usage and max