Warm tip: This article is reproduced from serverfault.com, please click

caching-计算CPU缓存命中

(caching - Calculating CPU cache hits)

发布于 2020-11-21 17:14:06

我正在努力了解什么是用于计算缓存命中的过程/公式。因此,例如,如果我们有一个包含16个条目的主存储器和一个4个条目的高速缓存,并且CPU加载了内存地址:0、1、2、8、9、2',那么我该如何计算命中数a)如果高速缓存是直接映射的,并且b)2路关联?

Questioner
user3755632
Viewed
11
pio 2020-11-30 21:38:31

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

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

Cache 0 --> can hold 0,4,8,12 of the main memory entries.
Cache 1 --> can hold 1,5,9,13 of the main memory entries.
Cache 2 --> can hold 2,6,10,14 of the main memory entries.
Cache 3 --> can hold 3,7,11,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.

所以我们有1次命中和5次未中,命中率为1 /(5 + 1)= 1/6 = 16%

b)对于2种关联方式,你需要为内存中的每个条目分配2个条目到缓存中。因此,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 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%