Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: prevent negative counter from iowait decrease #18835

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/18835.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
metrics: Fixed a bug where CPU counters could report errors for negative values
```
10 changes: 5 additions & 5 deletions client/hoststats/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (h *HostCpuStatsCalculator) Calculate(times cpu.TimesStat) (idle float64, u
currentIdle := times.Idle
currentUser := times.User
currentSystem := times.System
currentTotal := times.Total()
currentTotal := times.Total() // this is Idle + currentBusy
currentBusy := times.User + times.System + times.Nice + times.Iowait + times.Irq +
times.Softirq + times.Steal + times.Guest + times.GuestNice

Expand All @@ -284,16 +284,16 @@ func (h *HostCpuStatsCalculator) Calculate(times cpu.TimesStat) (idle float64, u
total = ((currentBusy - h.prevBusy) / deltaTotal) * 100

// Protect against any invalid values
if math.IsNaN(idle) || math.IsInf(idle, 0) {
if math.IsNaN(idle) || math.IsInf(idle, 0) || idle < 0.0 {
idle = 100.0
}
if math.IsNaN(user) || math.IsInf(user, 0) {
if math.IsNaN(user) || math.IsInf(user, 0) || user < 0.0 {
user = 0.0
}
if math.IsNaN(system) || math.IsInf(system, 0) {
if math.IsNaN(system) || math.IsInf(system, 0) || system < 0.0 {
system = 0.0
}
if math.IsNaN(total) || math.IsInf(total, 0) {
if math.IsNaN(total) || math.IsInf(total, 0) || total < 0.0 {
total = 0.0
}

Expand Down
39 changes: 29 additions & 10 deletions client/hoststats/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/shirou/gopsutil/v3/cpu"
"github.com/shoenig/test/must"
)

func TestHostCpuStatsCalculator_Nan(t *testing.T) {
Expand All @@ -20,16 +21,34 @@ func TestHostCpuStatsCalculator_Nan(t *testing.T) {
calculator.Calculate(times)
idle, user, system, total := calculator.Calculate(times)

if idle != 100.0 {
t.Errorf("idle: Expected: %f, Got %f", 100.0, idle)
}
if user != 0.0 {
t.Errorf("user: Expected: %f, Got %f", 0.0, user)
}
if system != 0.0 {
t.Errorf("system: Expected: %f, Got %f", 0.0, system)
must.Eq(t, 100.0, idle, must.Sprint("unexpected idle stats"))
must.Eq(t, 0.0, user, must.Sprint("unexpected user stats"))
must.Eq(t, 0.0, system, must.Sprint("unexpected system stats"))
must.Eq(t, 0.0, total, must.Sprint("unexpected total stats"))
}

func TestHostCpuStatsCalculator_DecreasedIOWait(t *testing.T) {
times := cpu.TimesStat{
CPU: "cpu0",
User: 20000,
Nice: 100,
System: 9000,
Idle: 370000,
Iowait: 700,
}
if total != 0.0 {
t.Errorf("total: Expected: %f, Got %f", 0.0, total)

calculator := NewHostCpuStatsCalculator()
calculator.Calculate(times)

times = cpu.TimesStat{
CPU: "cpu0",
User: 20000,
Nice: 100,
System: 9000,
Idle: 380000,
Iowait: 600,
}

_, _, _, total := calculator.Calculate(times)
must.GreaterEq(t, 0.0, total, must.Sprint("total must never be negative"))
}