If you are just starting to learn how multi-core CPUs, caching, cache coherency, and memory works, it may seem a little bit confusing at first. With that in mind, today’s SuperUser Q&A post has answers to a curious reader’s question.
如果您刚刚开始学习多核CPU,缓存,缓存一致性和内存的工作原理,一开始似乎有些混乱。 考虑到这一点,今天的SuperUser Q&A帖子回答了一个好奇的读者的问题。
Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.
今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。
问题 (The Question)
SuperUser reader CarmeloS wants to know when a CPU’s cache is flushed back to main memory:
超级用户阅读器CarmeloS想知道何时将CPU的缓存刷新回主内存:
If I have a CPU with two cores and each core has its own L1 cache, is it possible that Core1 and Core2 both cache the same part of memory at the same time? If it is possible, what will the value of main memory be if both Core1 and Core2 have edited their values in cache?
如果我有一个带有两个核心的CPU,并且每个核心都有自己的L1缓存,那么Core1和Core2是否都可以同时缓存同一部分内存? 如果有可能,如果Core1和Core2都在缓存中编辑了它们的值,那么主存储器的值将是什么?
When is a CPU’s cache flushed back to main memory?
什么时候将CPU的缓存刷新回主内存?
答案 (The Answer)
SuperUser contributors David Schwartz, sleske, and Kimberly W have the answer for us. First up, David Schwartz:
超级用户贡献者David Schwartz,sleske和Kimberly W为我们找到了答案。 首先,David Schwartz:
If I have a CPU with two cores and each core has its own L1 cache, is it possible that Core1 and Core2 both cache the same part of memory at the same time?
如果我有一个带有两个核心的CPU,并且每个核心都有自己的L1缓存,那么Core1和Core2是否都可以同时缓存同一部分内存?
Yes, performance would be terrible if this was not the case. Consider two threads running the same code. You want that code in both L1 caches.
是的,如果不是这种情况,性能将很糟糕。 考虑两个运行相同代码的线程。 您希望两个L1缓存中都有该代码。
If it is possible, what will the value of main memory be if both Core1 and Core2 have edited their values in cache?
如果有可能,如果Core1和Core2都在缓存中编辑了它们的值,那么主存储器的值将是什么?
The old value will be in main memory, which will not matter since neither core will read it. Before ejecting a modified value from cache, it must be written to memory. Typically, some variant of the MESI protocol is used. In the traditional implementation of MESI, if a value is modified in one cache, it cannot be present at all in any other cache at that same level.
旧值将位于主存储器中,这无关紧要,因为两个内核都不会读取它。 从缓存弹出修改后的值之前,必须将其写入内存。 通常,使用MESI协议的某些变体。 在MESI的传统实现中,如果在一个缓存中修改了一个值,则该值不能在同一级别的任何其他缓存中存在。
Followed by the answer from sleske:
随后是sleske的回答:
Yes, having two caches cache the same memory region can happen and is actually a problem that occurs a lot in practice. There are various solutions, for example:
是的,可能会发生两个缓存都缓存同一内存区域的情况,实际上这实际上是一个经常发生的问题。 有多种解决方案,例如:
- The two caches can communicate to make sure they do not disagree 这两个缓存可以通信以确保它们不会不同意
- You can have some sort of supervisor which monitors all caches and updates them accordingly 您可以有某种主管来监视所有缓存并相应地更新它们
- Each processor monitors the memory areas that it has cached, and when it detects a write, it throws out its (now invalid) cache 每个处理器都会监视已缓存的内存区域,并且在检测到写操作时会丢弃其(现在无效)的缓存
The problem is called cache coherency and the Wikipedia article on the topic has a nice overview of the problem and possible solutions.
该问题称为缓存一致性,有关该主题的Wikipedia文章对问题及其可能的解决方案进行了很好的概述。
And our final answer from Kimberly W:
我们对金伯利W的最终回答是:
To answer the question in your post’s title, it depends on what the caching protocol is. If it is write-back, the cache will only be flushed back to main memory when the cache controller has no choice but to put a new cache block in already occupied space. The block that previously occupied the space is removed and its value is written back to main memory.
要回答帖子标题中的问题,这取决于什么是缓存协议。 如果是回写,则仅当缓存控制器别无选择,只能将新的缓存块放入已占用的空间时,才将缓存刷新回主内存。 先前占用该空间的块将被删除,并将其值写回到主存储器。
The other protocol is write-through. In that case, anytime the cache block is written on level n, the corresponding block on level n+1 is updated. It is similar in concept to filling out a form with carbon paper underneath; whatever you write on top is copied on the sheet below. This is slower because it obviously involves more writing operations, but the values between caches are more consistent. In the write-back scheme, only the highest level cache would have the most up-to-date value for a particular memory block.
另一种协议是直写。 在那种情况下,无论何时将缓存块写入第n级,都会更新第n + 1级的相应块。 这在概念上类似于在下面用复写纸填写表格。 您在顶部写的所有内容都会复制到下面的工作表中。 这比较慢,因为它显然涉及更多的写入操作,但是缓存之间的值更一致。 在回写方案中,只有*别的缓存才具有特定存储块的最新值。
Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.
有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程 。
Image Credit: Lemsipmatt (Flickr)
图片来源: Lemsipmatt(Flickr)
翻译自: https://www.howtogeek.com/295222/when-is-a-cpus-cache-flushed-back-to-main-memory/