Skip to content

Commit a76d5bb

Browse files
committed
fix(guest): guard cpu/iowait counter wrap, clarify cpuAndIOWaitUsage doc
1 parent 8de4a32 commit a76d5bb

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

internal/guest/metrics_linux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func readCPUStat() (cpuStat, error) {
5555

5656
// cpuAndIOWaitUsage samples /proc/stat twice 100ms apart and returns both
5757
// the CPU usage ratio and the iowait ratio (both 0.0–1.0).
58+
// cpuRatio is (total - idle) / total where idle excludes iowait, so cpuRatio
59+
// already includes iowait time; iowaitRatio is the iowait fraction of total time.
5860
func cpuAndIOWaitUsage() (cpuRatio, iowaitRatio float64, err error) {
5961
s1, err := readCPUStat()
6062
if err != nil {
@@ -65,6 +67,9 @@ func cpuAndIOWaitUsage() (cpuRatio, iowaitRatio float64, err error) {
6567
if err != nil {
6668
return
6769
}
70+
if s2.total < s1.total || s2.idle < s1.idle || s2.iowait < s1.iowait {
71+
return 0, 0, nil
72+
}
6873
total := float64(s2.total - s1.total)
6974
if total == 0 {
7075
return

0 commit comments

Comments
 (0)