forked from DataDog/datadog-agent
/
container_common.go
34 lines (28 loc) · 997 Bytes
/
container_common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package checks
import (
"time"
)
func calculateCtrPct(cur, prev float64, sys2, sys1 uint64, numCPU int, current, before time.Time) float32 {
// -1 is returned if a cgroup file is missing or the `ContainerCPUStats` object is nil.
// In these situations, return -1 so that the metric is skipped on the backend.
if cur == -1 || prev == -1 {
return -1
}
diff := current.Sub(before).Seconds()
if before.IsZero() || diff < 0 {
return 0
}
// Prevent uint underflows
if prev > cur || sys1 > sys2 {
return 0
}
cpuDelta := cur - prev
// If we have system usage values then we need to calculate against those.
// XXX: Right now this only applies to ECS collection. Note that the inclusion of CPUs is
// necessary because the value gets normalized against the CPU limit, which also accounts for CPUs.
if sys1 >= 0 && sys2 > 0 && sys2 != sys1 {
sysDelta := float64(sys2 - sys1)
return float32((cpuDelta / sysDelta) * float64(numCPU) * 100)
}
return float32(cpuDelta / diff)
}