gz / rust-cpuid

cpuid library in rust.
https://docs.rs/raw-cpuid/
MIT License
153 stars 45 forks source link

Wrong cache sizes ? #192

Open Sparkenstein opened 1 week ago

Sparkenstein commented 1 week ago

Sorry if I don't understand this correctly, as I am not well versed with rust, but from example I am getting cache sizes as:

L1 Instruction-Cache: (32 KiB, 8-way associativity, direct-mapped)
L1 Data-Cache: (32 KiB, 8-way associativity, direct-mapped)
L2 Unified-Cache: (1024 KiB, 8-way associativity, direct-mapped)
L3 Unified-Cache: (96 MiB, 16-way associativity, direct-mapped)

So far so good, but I am using AMD Ryzen 7800X3D 8-Core Processor. Official spec and windows system monitor both report L2 cache size as 512KB.

The example says the calculation of cache is :

let size = cache.associativity()
                    * cache.physical_line_partitions()
                    * cache.coherency_line_size()
                    * cache.sets();

But its associativity is 8 way, so do I have to multiply size with associativity again? (that gives 8MB which is correct cache size). that also works with L1 because L1 data + Instruction is given as 32 so (32x8)x2 gives 512.

If that's the case, then why L3 is reported correctly? Is this a bug? Following are the cache objects for L2 and L3.

CacheParameter {
    cache_type: Unified,
    level: 3,
    is_self_initializing: true,
    is_fully_associative: false,
    max_cores_for_cache: 16,
    max_cores_for_package: 1,
    coherency_line_size: 64,
    physical_line_partitions: 1,
    associativity: 16,
    sets: 98304,
    is_write_back_invalidate: true,
    is_inclusive: false,
    has_complex_indexing: false,
}
CacheParameter {
    cache_type: Unified,
    level: 2,
    is_self_initializing: true,
    is_fully_associative: false,
    max_cores_for_cache: 2,
    max_cores_for_package: 1,
    coherency_line_size: 64,
    physical_line_partitions: 1,
    associativity: 8,
    sets: 2048,
    is_write_back_invalidate: false,
    is_inclusive: true,
    has_complex_indexing: false,
}
gz commented 1 week ago

Note that the example uses get_cache_info which has limited support on AMD. To get cache sizes reliably on AMD you might want to use other functions like https://docs.rs/raw-cpuid/latest/raw_cpuid/struct.CpuId.html#method.get_l1_cache_and_tlb_info and https://docs.rs/raw-cpuid/latest/raw_cpuid/struct.CpuId.html#method.get_l2_l3_cache_and_tlb_info

Umio-Yasuno commented 1 week ago

So far so good, but I am using AMD Ryzen 7800X3D 8-Core Processor. Official spec and windows system monitor both report L2 cache size as 512KB.

L1 cache?
7800X3D has the L2 cache of 8MB (1MB per core).

https://www.amd.com/en/products/processors/desktops/ryzen/7000-series/amd-ryzen-7-7800x3d.html

The total cache count can be calculated as <max threads> / max_cores_for_cache for AMD Ryzen.
If you want the total cache size, use <cache size> * (<max threads> / max_cores_for_cache) to calculate it.