gopkg/system/cpu.go

55 lines
1.0 KiB
Go

// Package system...
//
// Description : system...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-12 5:48 下午
package system
import (
"time"
"github.com/shirou/gopsutil/cpu"
)
// GetCPUInfo 获取CPU信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:49 下午 2021/10/12
func GetCPUInfo() (*CPUDetail, error) {
var (
cpuInfoList []cpu.InfoStat
cpuTimeList []cpu.TimesStat
percentList []float64
err error
baseInfo cpu.InfoStat
)
if cpuInfoList, err = cpu.Info(); nil != err {
return nil, err
}
if len(cpuInfoList) > 0 {
baseInfo = cpuInfoList[0]
}
if cpuTimeList, err = cpu.Times(true); nil != err {
return nil, err
}
// 使用率
percentList, _ = cpu.Percent(time.Second, true)
logicalCnt, _ := cpu.Counts(true)
physicalCnt, _ := cpu.Counts(false)
return &CPUDetail{
BaseInfo: baseInfo,
Time: cpuTimeList,
Percent: percentList,
CoreCntInfo: CPUCoreCnt{
Logical: logicalCnt,
Physical: physicalCnt,
},
}, nil
}