Skip to content

Commit

Permalink
Darwin L1 Data Cache fix (#122)
Browse files Browse the repository at this point in the history
* Implement test for Darwin icache/dcache

Check that the struct members for the L1 cache match the values provided by the OS sysctl utility

* Use dcache instead of icache for L1D
  • Loading branch information
ironiridis authored Nov 8, 2022
1 parent e32428e commit 1061e99
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion os_darwin_arm64.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 1061e99

Please sign in to comment.