cache释疑

来源:互联网 发布:linux挂载文件夹到mnt 编辑:程序博客网 时间:2024/06/06 01:23

几个基本概念

先来介绍几个基本的概念:

cache line:若干个word组成的block,通常是由2^n个word组成

set:由若干个cache line组成

way:和组成set的cache line number数目相同

在使用set和way时,物理地址会被分解成:

tag        set_index     offset

下面是一个cache实例:

Each row in this diagram is a set. For a 4-way associative cache each set contains 4 cache lines. Each cache line consists of a "tag" and a "data" field. There is also a "valid" bit, which is not shown.

The tag portion of the request address is compared to all of the tag fields in the selected set. If one the tag fields is a match the the corresponding data field is selected - it contains the requested data. If there is no match, the cache controller must go to the lower-level store for the requested data.

Whether the requested line comes from the local store or the lower-level store, the offset bits of the request address drive a multiplexer to select the desired data.

映射关系

memory和cache的映射关系有三种:

1、直接映射

2、组相联映射

3、全相联映射


直接映射

memory会根据cache的大小,分割成memory size/cache size个主存块,每个主存块中的line和cache中的cache line是一一对应的。从下图可以看出,由于cache只能映射某一个memory block的一个cache line,很可能导致cache空间不能被充分利用,也容易产生冲突。

327.gif

组相联映射

组相连映射是直接映射和全相联映射的折中方案,主存和Cache都分组,主存中一个组内的块数与Cache中的分组数相同,组间采用直接映射,组内采用全相联映射(cache组内),也就是说,将Cache分成u组,每组v块,主存块存放到哪个组是固定的,至于存到该组哪一块则是灵活的。具体结构如下图所示:

329.gif

主存中的各块与Cache的组号之间有固定的映射关系,但可自由映射到对应Cache组中的任何一块。例如,主存中的第0块、第8块……均映射于Cache的第0组,但可映射到Cache第0组中的第0块或第1块;主存的第1块、第9块……均映射于Cache的第1组,但可映射到Cache第1组中的第2块或第3块。


全相联映射

这种方式种主存的任何一个 line可以映射到cache的任何一个cache line,这种方式cache可以得到充分利用,不容易产生冲突,但是电路设计复杂,需要遍历整个cache,确认memory line是否已经加载到cache中,效率低。

328.gif


部分内容以及截图来自http://share.onlinesjtu.com/mod/tab/view.php?id=204

https://www.d.umn.edu/~gshute/arch/cache-addressing.xhtml

原创粉丝点击