Getting the cache line size from Zig

Is there currently a mechanism in the standard library to get the cache line size?

On linux you can use the sysfs. The size is in /sys/devices/system/node/node*/cpu*/cache/index*/size.
On x86 you can run the CPUID instruction.
I am not aware of anything in the zig library that gets the size. The most relevant is std.posix.linux.PERF.COUNT.HW that can measure cache misses.


Gotcha, thanks. Is the cache line size runtime-known only / can the same CPU model have different cache line sizes? I’m asking for alignment purposes, if that matters.

Yes there CPU models may have submodels with different cache sizes.

There is also the syconf API.
Run getconf -a, the parameters are LEVEL?_[ID]CACHE_SIZE

Okay, thanks.

Solution: guess cache line size, my guess is 64.

1 Like