chore: make cpu usage more smooth (#3842)

master
Kevin Wan 10 months ago committed by GitHub
parent 7ba8adfc74
commit 81d72b5010
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -136,7 +136,7 @@ func (as *adaptiveShedder) addFlying(delta int64) {
// update avgFlying when the request is finished.
// this strategy makes avgFlying have a little bit lag against flying, and smoother.
// when the flying requests increase rapidly, avgFlying increase slower, accept more requests.
// when the flying requests drop rapidly, avgFlying drop slower, accept less requests.
// when the flying requests drop rapidly, avgFlying drop slower, accept fewer requests.
// it makes the service to serve as more requests as possible.
if delta < 0 {
as.avgFlyingLock.Lock()

@ -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
}

Loading…
Cancel
Save