Skip to content

Commit 0281bd0

Browse files
authored
Check for NaN value in usage (#106)
When calculating the `relativeCPU` we divide `selfCPU` by `allCPU` if both values are zero we end up with `NaN`. Any mathematical operation with a `NaN` value results in a `NaN` value. So we must correct this to ensure later on we can serialize the value.
1 parent 6151571 commit 0281bd0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

usage/usage.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,13 @@ func poll(pollingInterval time.Duration) error {
157157
float64(allStat.CPUStatAll.System-lastAllStat.CPUStatAll.System) +
158158
float64(allStat.CPUStatAll.Idle-lastAllStat.CPUStatAll.Idle)
159159

160-
relativeCPU := selfCPU / allCPU
160+
var relativeCPU float64 = 0.0
161+
if allCPU > 0.0 {
162+
// If allCPU is `0.0` we would get a `NaN` value which would
163+
// result in all subsequent operations involving `relativeCPU`
164+
// also resulting in a `NaN` value.
165+
relativeCPU = selfCPU / allCPU
166+
}
161167
peakRelativeCPU = math.Max(peakRelativeCPU, relativeCPU)
162168

163169
coresUsed := relativeCPU * float64(len(allStat.CPUStats))

0 commit comments

Comments
 (0)