Skip to content

Commit

Permalink
fix GetCpuInfo/TestOS_Darwin_GetCpuInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
kakuilan committed Dec 14, 2024
1 parent 134673c commit ffd5609
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
23 changes: 6 additions & 17 deletions os_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ func (ko *LkkOS) GetCpuInfo() *CpuInfo {
Threads: 0,
}

//打印CPU信息
_, cpuInfos, _ := ko.System("sysctl machdep.cpu")
println("cpuInfos: ", cpuInfos)

//调用系统命令获取,例如
//$ sysctl machdep.cpu
//machdep.cpu.cores_per_package: 10
Expand All @@ -189,25 +185,18 @@ func (ko *LkkOS) GetCpuInfo() *CpuInfo {
//machdep.cpu.feature_bits: 151121000215084031
//machdep.cpu.family: 6

//打印CPU信息
//cpuInfos, _ := unix.Sysctl("machdep.cpu")
//println("cpuInfos: ", cpuInfos)

res.Model, _ = unix.Sysctl("machdep.cpu.brand_string")
res.Vendor, _ = unix.Sysctl("machdep.cpu.vendor")
res.Vendor, _ = unix.Sysctl("machdep.cpu.vendor") //有可能为空

cacheSize, _ := unix.SysctlUint32("machdep.cpu.cache.size")
cacheSize, _ := unix.SysctlUint32("machdep.cpu.cache.size") //有可能为空
cpus, _ := unix.SysctlUint32("hw.physicalcpu")
cores, _ := unix.SysctlUint32("machdep.cpu.core_count")
threads, _ := unix.SysctlUint32("machdep.cpu.thread_count")

if cpus == 0 {
infos := ko.getIOInfos()
if len(infos) > 0 {
infoStr := string(infos)
tmpStr := trim(KStr.GetEquationValue(infoStr, "AppleARMCPU"), "<", ">", `"`, `'`)
if tmpStr != "" {
cpus = uint32(KConv.ToInt(tmpStr))
}
}
}

res.Cache = uint(cacheSize)
res.Cpus = uint(cpus)
res.Cores = uint(cores)
Expand Down
2 changes: 1 addition & 1 deletion os_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func TestOS_Darwin_GetCpuInfo(t *testing.T) {
res := KOS.GetCpuInfo()
KDbug.DumpPrint(res)
assert.NotNil(t, res)
assert.NotEmpty(t, res.Vendor)
assert.NotEmpty(t, res.Model)
assert.NotEmpty(t, res.Threads)
}

func BenchmarkOS_Darwin_GetCpuInfo(b *testing.B) {
Expand Down

0 comments on commit ffd5609

Please sign in to comment.