|
|
@ -4,6 +4,7 @@ import (
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/iox"
|
|
|
|
"github.com/zeromicro/go-zero/core/iox"
|
|
|
@ -22,8 +23,10 @@ var (
|
|
|
|
cores uint64
|
|
|
|
cores uint64
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var initonce sync.Once
|
|
|
|
|
|
|
|
|
|
|
|
// if /proc not present, ignore the cpu calculation, like wsl linux
|
|
|
|
// if /proc not present, ignore the cpu calculation, like wsl linux
|
|
|
|
func init() {
|
|
|
|
func initialize() {
|
|
|
|
cpus, err := cpuSets()
|
|
|
|
cpus, err := cpuSets()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
logx.Error(err)
|
|
|
@ -69,6 +72,7 @@ func init() {
|
|
|
|
|
|
|
|
|
|
|
|
// RefreshCpu refreshes cpu usage and returns.
|
|
|
|
// RefreshCpu refreshes cpu usage and returns.
|
|
|
|
func RefreshCpu() uint64 {
|
|
|
|
func RefreshCpu() uint64 {
|
|
|
|
|
|
|
|
initonce.Do(initialize)
|
|
|
|
total, err := totalCpuUsage()
|
|
|
|
total, err := totalCpuUsage()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
return 0
|
|
|
|