Caching 计算CPU缓存命中率

Caching 计算CPU缓存命中率,caching,memory,cpu,Caching,Memory,Cpu,我正在努力理解计算缓存命中的过程/公式。因此,例如,如果我们有一个有16个条目的主存和一个有4个条目的高速缓存,CPU加载内存地址:0、1、2、8、9、2',我如何计算命中数a)如果高速缓存是直接映射的,b)双向关联的?假设没有预取器和LRU作为替换机制 a) 对于直接映射缓存,每个内存项只能位于一个缓存项中。 缓存将如下映射(默认情况下假定均匀分布): 重置后,缓存为空 Load from 0 will be missed and will be cached in cache entry 0

我正在努力理解计算缓存命中的过程/公式。因此,例如,如果我们有一个有16个条目的主存和一个有4个条目的高速缓存,CPU加载内存地址:0、1、2、8、9、2',我如何计算命中数a)如果高速缓存是直接映射的,b)双向关联的?

假设没有预取器和LRU作为替换机制

a) 对于直接映射缓存,每个内存项只能位于一个缓存项中。 缓存将如下映射(默认情况下假定均匀分布):

重置后,缓存为空

Load from 0 will be missed and will be cached in cache entry 0.
Load from 1 will be missed and will be cached in cache entry 1.
Load from 2 will be missed and will be cached in cache entry 2.
Load from 8 will be missed and will be cached in cache entry 0 (replaced load 0).
Load from 9 will be missed and will be cached in cache entry 1 (replaced load 1).
load from 2 will be hit and will be taken from cache entry 2.
Load from 0 will be missed and will be cached in cache entry 0.
Load from 1 will be missed and will be cached in cache entry 2.
Load from 2 will be missed and will be cached in cache entry 1.
Load from 8 will be missed and will be cached in cache entry 0 (replaced load 0 because we replace the Least Recently Used).
Load from 9 will be missed and will be cached in cache entry 3.
load from 2 will be hit and will be taken from cache entry 1.
因此我们有1次命中和5次未命中,命中率为1/(5+1)=1/6=16%

b)对于两种关联方式,内存中的每个条目都有两个缓存条目可供访问。 因此,set0(缓存中的条目0,1)将保存所有偶数主内存条目,set1将保存所有奇数条目,因此如果我们对其进行跨距,我们将得到如下结果:

cache 0 (set 0) --> can hold 0,2,4,6,8,10,12,14 of the main memory entries.
cache 1 (set 0) --> can hold 0,2,4,6,8,10,12,14 of the main memory entries.
cache 2 (set 1) --> can hold 1,3,5,7,9,11,13,15 of the main memory entries.
cache 2 (set 0) --> can hold 1,3,5,7,9,11,13,15 of the main memory entries.
重置后,缓存为空

Load from 0 will be missed and will be cached in cache entry 0.
Load from 1 will be missed and will be cached in cache entry 1.
Load from 2 will be missed and will be cached in cache entry 2.
Load from 8 will be missed and will be cached in cache entry 0 (replaced load 0).
Load from 9 will be missed and will be cached in cache entry 1 (replaced load 1).
load from 2 will be hit and will be taken from cache entry 2.
Load from 0 will be missed and will be cached in cache entry 0.
Load from 1 will be missed and will be cached in cache entry 2.
Load from 2 will be missed and will be cached in cache entry 1.
Load from 8 will be missed and will be cached in cache entry 0 (replaced load 0 because we replace the Least Recently Used).
Load from 9 will be missed and will be cached in cache entry 3.
load from 2 will be hit and will be taken from cache entry 1.
在这种情况下,命中率是相同的:1/(5+1)=1/6=16%