Skip to content

Commit

Permalink
fix getProcessCpuStat return value (in percentage) breaks compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunyucheng committed Jul 13, 2023
1 parent 0807185 commit d7f5902
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
2 changes: 2 additions & 0 deletions api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric"
"github.com/alibaba/sentinel-golang/util"
"github.com/pkg/errors"

_ "go.uber.org/automaxprocs"
)

// Initialization func initialize the Sentinel's runtime environment, including:
Expand Down
5 changes: 5 additions & 0 deletions core/system_metric/sys_metric_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package system_metric

import (
"os"
"runtime"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -176,6 +177,10 @@ func retrieveAndUpdateCpuStat() {
return
}

// fix getProcessCpuStat return value (in percentage) breaks compatibility.
cpuNum := runtime.NumCPU()
cpuPercent = cpuPercent / float64(cpuNum) / 100.0

cpuRatioGauge.Set(cpuPercent)

currentCpuUsage.Store(cpuPercent)
Expand Down
42 changes: 27 additions & 15 deletions core/system_metric/sys_metric_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,8 @@ func TestCurrentCpuUsage(t *testing.T) {
assert.True(t, util.Float64Equals(v, cpuUsage))
}

func Test_getProcessCpuStat(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
i := 0
wg.Done()
for i < 10000000000 {
i++
if i == 1000000000 {
i = 0
}
}
}()
wg.Wait()

func TestGetProcessCpuStat(t *testing.T) {
upraiseCpuRate()
got, err := getProcessCpuStat()
if err != nil {
t.Error(err)
Expand All @@ -83,3 +70,28 @@ func Test_getProcessCpuStat(t *testing.T) {
assert.True(t, int(got) > 0)
time.Sleep(time.Millisecond * 200)
}

func TestRetrieveAndUpdateCpuStatReturnValueRange(t *testing.T) {
// Initial cpu retrieval.
retrieveAndUpdateCpuStat()
upraiseCpuRate()
time.Sleep(time.Millisecond * 200)
retrieveAndUpdateCpuStat()
assert.True(t, true, CurrentCpuUsage() < 1.0)
}

func upraiseCpuRate() {
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
i := 0
wg.Done()
for i < 10000000000 {
i++
if i == 1000000000 {
i = 0
}
}
}()
wg.Wait()
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.9.0
github.com/shirou/gopsutil/v3 v3.21.6
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.1
go.uber.org/automaxprocs v1.5.0 // indirect
go.uber.org/multierr v1.5.0
gopkg.in/yaml.v2 v2.3.0
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6J
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
Expand Down Expand Up @@ -270,6 +271,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4=
github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
Expand All @@ -287,6 +291,8 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/automaxprocs v1.5.0 h1:UctqgMvxKU3PHD5qSSlKkOcTAptYykGIpJlEBYNGPvM=
go.uber.org/automaxprocs v1.5.0/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
Expand Down Expand Up @@ -426,6 +432,8 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down

0 comments on commit d7f5902

Please sign in to comment.