From 81d72b50102d1ee01fe366b85123d6b54cf2f292 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 13 Jan 2024 19:36:25 +0800 Subject: [PATCH] chore: make cpu usage more smooth (#3842) --- core/load/adaptiveshedder.go | 2 +- core/stat/internal/cpu_linux.go | 9 +++++++-- tools/goctl/pkg/parser/api/parser/filter.go | 2 +- tools/goctl/pkg/parser/api/parser/parser.go | 2 +- tools/goctl/pkg/parser/api/scanner/scanner.go | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/load/adaptiveshedder.go b/core/load/adaptiveshedder.go index f925ec4d..4d1b7f1d 100644 --- a/core/load/adaptiveshedder.go +++ b/core/load/adaptiveshedder.go @@ -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() diff --git a/core/stat/internal/cpu_linux.go b/core/stat/internal/cpu_linux.go index 6d117317..1d686ce0 100644 --- a/core/stat/internal/cpu_linux.go +++ b/core/stat/internal/cpu_linux.go @@ -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 } diff --git a/tools/goctl/pkg/parser/api/parser/filter.go b/tools/goctl/pkg/parser/api/parser/filter.go index 322260a0..fd0ce0ae 100644 --- a/tools/goctl/pkg/parser/api/parser/filter.go +++ b/tools/goctl/pkg/parser/api/parser/filter.go @@ -25,7 +25,7 @@ func (b *filterBuilder) check(nodes ...*ast.TokenNode) { func (b *filterBuilder) checkNodeWithPrefix(prefix string, nodes ...*ast.TokenNode) { 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 { b.errorManager.add(ast.DuplicateStmtError(node.Pos(), "duplicate "+b.checkExprName)) } else { diff --git a/tools/goctl/pkg/parser/api/parser/parser.go b/tools/goctl/pkg/parser/api/parser/parser.go index 5cbc2943..77bcd2db 100644 --- a/tools/goctl/pkg/parser/api/parser/parser.go +++ b/tools/goctl/pkg/parser/api/parser/parser.go @@ -385,7 +385,7 @@ func (p *Parser) parsePathExpr() *ast.PathExpr { } 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 } if p.notExpectPeekTokenGotComment(p.curTokenNode().PeekFirstLeadingComment(), token.COLON, token.IDENT, token.INT) { diff --git a/tools/goctl/pkg/parser/api/scanner/scanner.go b/tools/goctl/pkg/parser/api/scanner/scanner.go index 0b86a79d..7aac0056 100644 --- a/tools/goctl/pkg/parser/api/scanner/scanner.go +++ b/tools/goctl/pkg/parser/api/scanner/scanner.go @@ -651,7 +651,7 @@ func NewScanner(filename string, src interface{}) (*Scanner, 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) if err != nil { return nil, err