Skip to content

Commit

Permalink
Initialize CPU stat code only if used (zeromicro#2020)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Quintana <josh@highwaybenefits.com>
  • Loading branch information
joshq00 and Josh Quintana authored Aug 25, 2022
1 parent 28166de commit 90cdd61
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/stat/internal/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
"time"

"github.com/zeromicro/go-zero/core/iox"
Expand All @@ -22,8 +23,10 @@ var (
cores uint64
)

var initonce sync.Once

// if /proc not present, ignore the cpu calculation, like wsl linux
func init() {
func initialize() {
cpus, err := cpuSets()
if err != nil {
logx.Error(err)
Expand Down Expand Up @@ -69,6 +72,7 @@ func init() {

// RefreshCpu refreshes cpu usage and returns.
func RefreshCpu() uint64 {
initonce.Do(initialize)
total, err := totalCpuUsage()
if err != nil {
return 0
Expand Down

0 comments on commit 90cdd61

Please sign in to comment.