chore: refactor stat (#2299)

This commit is contained in:
Kevin Wan 2022-08-25 23:37:32 +08:00 committed by GitHub
parent 90cdd61efc
commit 36678f9023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,10 +21,9 @@ var (
preTotal uint64
quota float64
cores uint64
initOnce sync.Once
)
var initonce sync.Once
// if /proc not present, ignore the cpu calculation, like wsl linux
func initialize() {
cpus, err := cpuSets()
@ -72,11 +71,13 @@ func initialize() {
// RefreshCpu refreshes cpu usage and returns.
func RefreshCpu() uint64 {
initonce.Do(initialize)
initOnce.Do(initialize)
total, err := totalCpuUsage()
if err != nil {
return 0
}
system, err := systemCpuUsage()
if err != nil {
return 0