chore: make cpu usage more smooth (#3842)

master
Kevin Wan 11 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. // 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
} }

@ -25,7 +25,7 @@ func (b *filterBuilder) check(nodes ...*ast.TokenNode) {
func (b *filterBuilder) checkNodeWithPrefix(prefix string, nodes ...*ast.TokenNode) { func (b *filterBuilder) checkNodeWithPrefix(prefix string, nodes ...*ast.TokenNode) {
for _, node := range nodes { for _, node := range nodes {
joinText:=fmt.Sprintf("%s/%s",prefix,node.Token.Text) joinText := fmt.Sprintf("%s/%s", prefix, node.Token.Text)
if _, ok := b.m[joinText]; ok { if _, ok := b.m[joinText]; ok {
b.errorManager.add(ast.DuplicateStmtError(node.Pos(), "duplicate "+b.checkExprName)) b.errorManager.add(ast.DuplicateStmtError(node.Pos(), "duplicate "+b.checkExprName))
} else { } else {

@ -385,7 +385,7 @@ func (p *Parser) parsePathExpr() *ast.PathExpr {
} }
values = append(values, p.curTok) values = append(values, p.curTok)
if p.peekTokenIs(token.LPAREN, token.Returns, token.AT_DOC, token.AT_HANDLER, token.SEMICOLON, token.RBRACE){ if p.peekTokenIs(token.LPAREN, token.Returns, token.AT_DOC, token.AT_HANDLER, token.SEMICOLON, token.RBRACE) {
break break
} }
if p.notExpectPeekTokenGotComment(p.curTokenNode().PeekFirstLeadingComment(), token.COLON, token.IDENT, token.INT) { if p.notExpectPeekTokenGotComment(p.curTokenNode().PeekFirstLeadingComment(), token.COLON, token.IDENT, token.INT) {

@ -651,7 +651,7 @@ func NewScanner(filename string, src interface{}) (*Scanner, error) {
} }
func readData(filename string, src interface{}) ([]byte, error) { func readData(filename string, src interface{}) ([]byte, error) {
if strings.HasSuffix(filename, ".api") &&pathx.FileExists(filename){ if strings.HasSuffix(filename, ".api") && pathx.FileExists(filename) {
data, err := os.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err

Loading…
Cancel
Save