mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
chore: make cpu usage more smooth (#3842)
This commit is contained in:
parent
7ba8adfc74
commit
81d72b5010
@ -136,7 +136,7 @@ func (as *adaptiveShedder) addFlying(delta int64) {
|
|||||||
// update avgFlying when the request is finished.
|
// update avgFlying when the request is finished.
|
||||||
// this strategy makes avgFlying have a little bit lag against flying, and smoother.
|
// 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 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.
|
// it makes the service to serve as more requests as possible.
|
||||||
if delta < 0 {
|
if delta < 0 {
|
||||||
as.avgFlyingLock.Lock()
|
as.avgFlyingLock.Lock()
|
||||||
|
@ -14,6 +14,8 @@ import (
|
|||||||
const (
|
const (
|
||||||
cpuTicks = 100
|
cpuTicks = 100
|
||||||
cpuFields = 8
|
cpuFields = 8
|
||||||
|
cpuMax = 1000
|
||||||
|
statDir = "/proc/stat"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -81,7 +83,10 @@ func RefreshCpu() uint64 {
|
|||||||
cpuDelta := total - preTotal
|
cpuDelta := total - preTotal
|
||||||
systemDelta := system - preSystem
|
systemDelta := system - preSystem
|
||||||
if cpuDelta > 0 && systemDelta > 0 {
|
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
|
preSystem = system
|
||||||
preTotal = total
|
preTotal = total
|
||||||
@ -117,7 +122,7 @@ func cpuSets() ([]uint64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func systemCpuUsage() (uint64, error) {
|
func systemCpuUsage() (uint64, error) {
|
||||||
lines, err := iox.ReadTextLines("/proc/stat", iox.WithoutBlank())
|
lines, err := iox.ReadTextLines(statDir, iox.WithoutBlank())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user