I'm trying to get an accurate description of the data cache hierarchy of the current CPU on Linux: not just the size of individual L1/L2/L3 (and possibly L4) data caches, but also the way they are split or shared across cores.
For instance, on my CPU (AMD Ryzen Threadripper 3970X), each core has its own 32 KB of L1 data cache and 512 KB of L2 cache, however the L3 cache is shared across cores within a core complex (CCX). In other words, there are 8 distinct L3 caches, each of 16 MB.
The "Cache" section of this screenshot of CPU-Z on Windows is basically what I'm trying to find out:
I have no problem getting these information on Windows with GetLogicalProcessorInformation().
However, on Linux, it appears that sysconf() only gives me either the per-core cache size for L1 and L2 data caches (_SC_LEVEL1_DCACHE_SIZE and _SC_LEVEL2_DCACHE_SIZE), or the total L3 cache size (_SC_LEVEL3_CACHE_SIZE).
EDIT: lstopo's output under VMWare. The virtual machine has 8 cores. L1 and L2 cache information are fine but L3 cache size does not appear to be correct:


sysconf(). How to make sense of them if we don't know if caches are shared or not?cpuidinstruction yourself on that ISA, and maybe even embed some per-model cache layout details. IDK how much detail the various CPUID leafs like sandpile.org/x86/cpuid.htm#level_0000_0004h can represent.lstopoon Linux on bare metal? (e.g. boot a live USB). Your bogus result could be the VM's fault so we should rule that out. Unsurprisingly it works as expected on my i7-6700k desktop, showing all 4 cores in the same package sharing an L3 cache. But Intel Sandybridge-family is the most widely used and not recently changed series of x86 CPUs.lstopois also available for Windows.lstopouse thecpuidinstruction (and maybe the ACPISRATtable).cpuidis relatively easy to use but Intel and AMD differ a lot in this aspect.hwloc(to whichlstopobelongs) has an API interface that you can use to get the cache topology both on Windows and Linux.