chore: make cpu usage more smooth (#3842)

This commit is contained in:
Kevin Wan
2024-01-13 19:36:25 +08:00
committed by GitHub
parent 7ba8adfc74
commit 81d72b5010
5 changed files with 11 additions and 6 deletions

View File

@@ -14,6 +14,8 @@ import (
const (
cpuTicks = 100
cpuFields = 8
cpuMax = 1000
statDir = "/proc/stat"
)
var (
@@ -81,7 +83,10 @@ func RefreshCpu() uint64 {
cpuDelta := total - preTotal
systemDelta := system - preSystem
if cpuDelta > 0 && systemDelta > 0 {
usage = uint64(float64(cpuDelta*cores*1e3) / (float64(systemDelta) * quota))
usage = uint64(float64(cpuDelta*cores*cpuMax) / (float64(systemDelta) * quota))
if usage > cpuMax {
usage = cpuMax
}
}
preSystem = system
preTotal = total
@@ -117,7 +122,7 @@ func cpuSets() ([]uint64, error) {
}
func systemCpuUsage() (uint64, error) {
lines, err := iox.ReadTextLines("/proc/stat", iox.WithoutBlank())
lines, err := iox.ReadTextLines(statDir, iox.WithoutBlank())
if err != nil {
return 0, err
}