From 90cdd61efcf06b23427e56c22e6f661a90652de6 Mon Sep 17 00:00:00 2001 From: Josh Quintana Date: Thu, 25 Aug 2022 10:05:29 -0400 Subject: [PATCH] Initialize CPU stat code only if used (#2020) Co-authored-by: Josh Quintana --- core/stat/internal/cpu_linux.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/stat/internal/cpu_linux.go b/core/stat/internal/cpu_linux.go index f9c1729a..5965b096 100644 --- a/core/stat/internal/cpu_linux.go +++ b/core/stat/internal/cpu_linux.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "sync" "time" "github.com/zeromicro/go-zero/core/iox" @@ -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) @@ -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