Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darwin L1 Data Cache fix #122

Merged
merged 2 commits into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion os_darwin_arm64.go
Expand Up @@ -83,7 +83,7 @@ func tryToFillCPUInfoFomSysctl(c *CPUInfo) {
c.Model = sysctlGetInt(0, "machdep.cpu.model")
c.CacheLine = sysctlGetInt64(0, "hw.cachelinesize")
c.Cache.L1I = sysctlGetInt64(-1, "hw.l1icachesize")
c.Cache.L1D = sysctlGetInt64(-1, "hw.l1icachesize")
c.Cache.L1D = sysctlGetInt64(-1, "hw.l1dcachesize")
c.Cache.L2 = sysctlGetInt64(-1, "hw.l2cachesize")
c.Cache.L3 = sysctlGetInt64(-1, "hw.l3cachesize")

Expand Down
44 changes: 44 additions & 0 deletions os_darwin_test.go
@@ -0,0 +1,44 @@
package cpuid

import (
"os/exec"
"strconv"
"strings"
"testing"
)

// Tests that the returned L1 instruction cache value matches the value returned from
// a call to the OS `sysctl` utility. Skips the test if we can't run it.
func TestDarwinL1ICache(t *testing.T) {
out, err := exec.Command("/usr/sbin/sysctl", "-n", "hw.l1icachesize").Output()
if err != nil {
t.Skipf("cannot run sysctl utility: %v", err)
return
}
v, err := strconv.ParseInt(strings.TrimSpace(string(out)), 10, 0)
if err != nil {
t.Skipf("sysctl output %q could not be parsed as int: %v", string(out), err)
return
}
if CPU.Cache.L1I != int(v) {
t.Fatalf("sysctl output %q did not match CPU.Cache.L1I %d", string(out), CPU.Cache.L1I)
}
}

// Tests that the returned L1 data cache value matches the value returned from a call
// to the OS `sysctl` utility. Skips the test if we can't run it.
func TestDarwinL1DCache(t *testing.T) {
out, err := exec.Command("/usr/sbin/sysctl", "-n", "hw.l1dcachesize").Output()
if err != nil {
t.Skipf("cannot run sysctl utility: %v", err)
return
}
v, err := strconv.ParseInt(strings.TrimSpace(string(out)), 10, 0)
if err != nil {
t.Skipf("sysctl output %q could not be parsed as int: %v", string(out), err)
return
}
if CPU.Cache.L1D != int(v) {
t.Fatalf("sysctl output %q did not match CPU.Cache.L1D %d", string(out), CPU.Cache.L1D)
}
}