直接映射的缓存如何工作? [英] How does direct mapped cache work?

查看:76
本文介绍了直接映射的缓存如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习系统体系结构课程,但在理解直接映射缓存的工作方式时遇到了麻烦。

I am taking a System Architecture course and I have trouble understanding how a direct mapped cache works.

我在多个地方进行了考察,他们用不同的方式进行了解释。

I have looked in several places and they explain it in a different manner which gets me even more confused.

我无法理解的是标签和索引是什么,以及如何选择它们?

What I cannot understand is what is the Tag and Index, and how are they selected?

我的演讲的解释是:
地址分为两部分
索引(例如15位),用于直接寻址(32k)RAM
地址的其余部分,标签被存储并与传入标签进行比较。

The explanation from my lecture is: "Address divided is into two parts index (e.g 15 bits) used to address (32k) RAMs directly Rest of address, tag is stored and compared with incoming tag. "

该标签来自何处?它不能是RAM中内存位置的完整地址,因为它使直接映射的缓存无效(与完全关联的缓存相比)。

Where does that tag come from? It cannot be the full address of the memory location in RAM since it renders direct mapped cache useless (when compared with the fully associative cache).

非常感谢。 / p>

Thank you very much.

推荐答案

好的。因此,让我们首先了解CPU如何与缓存进行交互。

Okay. So let's first understand how the CPU interacts with the cache.

有三层内存(广义上讲)-缓存(通常由组成) SRAM 芯片,主存储器(通常由 DRAM 芯片制成)和存储(通常是磁性的,如硬盘)。每当CPU需要来自某个特定位置的任何数据时,它都会首先搜索高速缓存以查看其是否在那里。就内存层次而言,高速缓存内存最接近CPU,因此其访问时间最短(且成本最高),因此,如果可以在此处找到要查找的数据CPU,则它构成命中,而数据从那里获得供CPU使用。如果不存在,则必须先将数据从主存储器移动到缓存中,然后CPU才能访问该数据(CPU通常仅与缓存进行交互),这会造成时间损失。

There are three layers of memory (broadly speaking) - cache (generally made of SRAM chips), main memory (generally made of DRAM chips), and storage (generally magnetic, like hard disks). Whenever CPU needs any data from some particular location, it first searches the cache to see if it is there. Cache memory lies closest to the CPU in terms of memory hierarchy, hence its access time is the least (and cost is the highest), so if the data CPU is looking for can be found there, it constitutes a 'hit', and data is obtained from there for use by CPU. If it is not there, then the data has to be moved from the main memory to the cache before it can be accessed by the CPU (CPU generally interacts only with the cache), that incurs a time penalty.

因此,要找出缓存中是否存在数据,可以应用各种算法。一种是这种直接映射的缓存方法。为简单起见,我们假设有一个内存系统,其中有10个可用的缓存存储位置(编号为0到9)和40个主存储位置可用(编号为0到39)。这张图片概括起来:

So to find out whether the data is there or not in the cache, various algorithms are applied. One is this direct mapped cache method. For simplicity, let's assume a memory system where there are 10 cache memory locations available (numbered 0 to 9), and 40 main memory locations available (numbered 0 to 39). This picture sums it up:

有40个主内存位置可用,但高速缓存中最多只能容纳10个。因此,现在,通过某种方式,来自CPU的传入请求需要重定向到缓存位置。那有两个问题:

There are 40 main memory locations available, but only upto 10 can be accommodated in the cache. So now, by some means, the incoming request from CPU needs to be redirected to a cache location. That has two problems:


  1. 如何重定向?具体来说,如何以一种可预测的方式来实现该目标,而该方式不会随时间变化?

  1. How to redirect? Specifically, how to do it in a predictable way which will not change over time?

如果缓存位置已经填充了一些数据,则传入请求CPU必须确定需要数据的地址与该数据存储在该位置的地址是否相同。

If the cache location is already filled up with some data, the incoming request from CPU has to identify whether the address from which it requires the data is same as the address whose data is stored in that location.

在我们的简单示例中,我们可以通过简单的逻辑进行重定向。假定我们必须将40个主内存位置(从0到39依次编号)映射到10个编号为0到9的缓存位置,则内存位置 n 的缓存位置可以为 n%10 。因此21对应于1,37对应于7,依此类推。这就是 index

In our simple example, we can redirect by a simple logic. Given that we have to map 40 main memory locations numbered serially from 0 to 39 to 10 cache locations numbered 0 to 9, the cache location for a memory location n can be n%10. So 21 corresponds to 1, 37 corresponds to 7, etc. That becomes the index.

但是37、17、7都对应于7 。为了区分它们,请使用标签。因此,就像索引是 n%10 一样,标签是 int(n / 10)。因此,现在37、17、7将具有相同的索引7,但具有不同的标签,如3、1、0等。也就是说,映射可以完全由标签和索引这两个数据指定。

But 37, 17, 7 all correspond to 7. So to differentiate between them, comes the tag. So just like index is n%10, tag is int(n/10). So now 37, 17, 7 will have the same index 7, but different tags like 3, 1, 0, etc. That is, the mapping can be completely specified by the two data - tag and index.

现在,如果请求地址29的请求,它将转换为标记2和索引9。索引对应于缓存位置编号,因此缓存位置编号。将查询9以查看它是否包含任何数据,如果包含,则关联的标签是否为2。如果是,则是CPU命中,并且将立即从该位置获取数据。如果为空或标记不是2,则表示它包含对应于其他某个内存地址的数据,而不是29(尽管它具有相同的索引,这意味着它包含来自地址的数据,例如9、19, 39等)。因此这是CPU丢失,而数据来自位置编号。必须将主存储器中的29加载到位置29的高速缓存中(并且标记更改为2,并删除之前的任何数据),之后它将由CPU提取。

So now if a request comes for address location 29, that will translate to a tag of 2 and index of 9. Index corresponds to cache location number, so cache location no. 9 will be queried to see if it contains any data, and if so, if the associated tag is 2. If yes, it's a CPU hit and the data will be fetched from that location immediately. If it is empty, or the tag is not 2, it means that it contains the data corresponding to some other memory address and not 29 (although it will have the same index, which means it contains a data from address like 9, 19, 39, etc.). So it is a CPU miss, and data from location no. 29 in main memory will have to be loaded into the cache at location 29 (and the tag changed to 2, and deleting any data which was there before), after which it will be fetched by CPU.

这篇关于直接映射的缓存如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆