-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #947 from mackerelio/iowait-can-be-reset
Avoid iowait percentage's overflow when counter is reset
- Loading branch information
Showing
3 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package util | ||
|
||
// Calculate incremental difference of uint64 counters that can be reset. | ||
// Avoiding overflow, return current value if it is smaller than previous one. | ||
func DiffResettableCounter(current, previous uint64) uint64 { | ||
if current < previous { | ||
// counter has been reset | ||
return current | ||
} | ||
return current - previous | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestDiffResettableCounter(t *testing.T) { | ||
cases := []struct { | ||
inCurrent uint64 | ||
inPrevious uint64 | ||
want uint64 | ||
}{ | ||
{100, 30, 70}, | ||
{20, 50, 20}, // counter has been reset | ||
} | ||
for _, tt := range cases { | ||
t.Run(fmt.Sprintf("%d - %d", tt.inCurrent, tt.inPrevious), func(t *testing.T) { | ||
got := DiffResettableCounter(tt.inCurrent, tt.inPrevious) | ||
if got != tt.want { | ||
t.Errorf("want=%d, got=%d", tt.want, got) | ||
} | ||
}) | ||
} | ||
} |