命中与错过使用4路关联缓存

时间:2012-04-10 20:21:40

标签: caching memory-management

问题:

Given a computer ,where were made the following memory accesses 

(from left to right) : 

5 ,10 ,2 ,34 ,18 ,4 ,22 ,21 ,11 ,2

* Decide if we have a HIT or MISS when dealing with a 4-way associative mapping ,
  when the total size of the cache is 32 blocks of 2 bytes !

* When you're done , write the final map of the cache 

我的回答:

集合的大小为4,因此:

(块数)/(路数)= 32/4 = 8

然后我们有一个缓存,有8个单元格,从0到7(请纠正我,如果我错了!!?

现在:5:(4,5)→5/2 = 2→2%8 = 2→单元格2→未命中

10:(10,11)→10/2 = 5→5%8 = 5→单元格5→错过

2:(2,3)→2/2 = 1→1%8 = 1→单元格1→未命中

34:(34,35)→34/2 = 17→17%8 = 1→单元格1→未命中

18:(18,19)→18/2 = 9→9%8 = 1→单元格1→未命中

4:细胞2中的HIT

22:(22,23)→22/2 = 11→11%8 = 3→单元格3→错过

21:(20,21)→21/2 = 10→10%8 = 2→单元格2→错过

11:细胞5中的HIT

2:单元格1中的HIT

现在,缓存的最终地图是:

0: empty
1: (2,3) (34,35) (18,19)
2: (4,5) (20,21)
3: (22,23)
4: empty
5: (10,11)
6: empty
7: empty
  1. 我的回答是否正确?

  2. 我对缓存的地图有误吗?

  3. 我很感激你的帮助....我的考试很快就会开始:)

    谢谢,

    罗恩

1 个答案:

答案 0 :(得分:0)

一个简单的Python程序(忽略替换,因为没有)表示你是正确的

from collections import defaultdict

d = defaultdict(list)

for item in (5 ,10 ,2 ,34 ,18 ,4 ,22 ,21 ,11 ,2):
    value = item // 2 * 2, item // 2 * 2 + 1
    cell = item // 2 % 8
    if value in d[cell]:
        print "HIT", cell
    else:
        d[cell].append(value)
        print "MISS", cell

for i in range(8):
    print i, d[i]

-

MISS 2
MISS 5
MISS 1
MISS 1
MISS 1
HIT 2
MISS 3
MISS 2
HIT 5
HIT 1

0 []
1 [(2, 3), (34, 35), (18, 19)]
2 [(4, 5), (20, 21)]
3 [(22, 23)]
4 []
5 [(10, 11)]
6 []
7 []