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

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

问题描述

我正在学习系统架构课程,但我无法理解直接映射缓存的工作原理.

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?

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

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).

非常感谢.

推荐答案

好的.那么我们先来了解一下CPU是如何与缓存交互的.

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

内存分为三层(广义上​​)——cache(一般由SRAM芯片构成)、main memory(一般由SRAM芯片构成)、main memory(一般由DRAM 芯片)和 storage(通常是磁性的,如硬盘).每当 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 连续编号的主内存位置映射到编号为 0 到 9 的 10 个缓存位置,那么内存位置 n 的缓存位置可以是 n%10.所以 21 对应于 1,37 对应于 7,以此类推.这就成为了索引.

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天全站免登陆