[cpu] Fix #599 cap percent values returned by Percent() between 0 and 100
See https://github.com/shirou/gopsutil/issues/599#issuecomment-491942842 for a repoduction case
This commit is contained in:
parent
2ac72f1fa1
commit
946c9ce6ea
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -112,7 +113,7 @@ func calculateBusy(t1, t2 TimesStat) float64 {
|
||||||
if t2All <= t1All {
|
if t2All <= t1All {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return (t2Busy - t1Busy) / (t2All - t1All) * 100
|
return math.Min(100, math.Max(0, (t2Busy-t1Busy)/(t2All-t1All)*100))
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateAllBusy(t1, t2 []TimesStat) ([]float64, error) {
|
func calculateAllBusy(t1, t2 []TimesStat) ([]float64, error) {
|
||||||
|
|
Loading…
Reference in New Issue