缓存的 N-way 可以通过getconf
获取
LEVEL1_ICACHE_SIZE 32768
LEVEL1_ICACHE_ASSOC 8
LEVEL1_ICACHE_LINESIZE 64
LEVEL1_DCACHE_SIZE 32768
LEVEL1_DCACHE_ASSOC 8
LEVEL1_DCACHE_LINESIZE 64
LEVEL2_CACHE_SIZE 262144
LEVEL2_CACHE_ASSOC 8
LEVEL2_CACHE_LINESIZE 64
LEVEL3_CACHE_SIZE 3145728
LEVEL3_CACHE_ASSOC 12
LEVEL3_CACHE_LINESIZE 64
LEVEL1有32K缓存,cache line长度为64字节,8-way cache
那么缓存被分为 32K/64/8 = 64个set,set号是从000000
到111111
,即26=64
内存按照cache line的大小被分为对应的chunk号,每个chunk号的最后6位对应set号。
chunk 0 对应cache set 0
chunk 1 对应 cache set 1
…
chunk 63 对应 cache set 63
chunk 64 对应 cache set 0
chunk 65 对应 cache set 1
…
chunk N 根据具体策略存储在 cache set N 的 8条cache line中的一条
也就是说每 64 (cache line) * 64 (set) = 4KB,内存chunk 开始存储在与4KB前的内存映射一样的 cache set
如果程序进行以4096的约数或倍数为步数的访问,程序性能可能特别差