From 0d4b691b4f147fccec768c2b496d9d856becb737 Mon Sep 17 00:00:00 2001 From: Christopher Harrington Date: Tue, 8 Nov 2022 09:40:41 -0600 Subject: [PATCH 1/2] Implement test for Darwin icache/dcache Check that the struct members for the L1 cache match the values provided by the OS sysctl utility --- os_darwin_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 os_darwin_test.go diff --git a/os_darwin_test.go b/os_darwin_test.go new file mode 100644 index 0000000..7462816 --- /dev/null +++ b/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) + } +} From 798fc34d0e4c7b20006bf2d236a926ec02e2d259 Mon Sep 17 00:00:00 2001 From: Christopher Harrington Date: Tue, 8 Nov 2022 09:45:34 -0600 Subject: [PATCH 2/2] Use dcache instead of icache for L1D --- os_darwin_arm64.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os_darwin_arm64.go b/os_darwin_arm64.go index d91d021..84b1acd 100644 --- a/os_darwin_arm64.go +++ b/os_darwin_arm64.go @@ -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")