c - 高速缓存 Controller 一次从主存到二级高速缓存取多少字节?

标签 c caching memory operating-system

我刚刚阅读了两篇关于这个主题的文章,它们提供的信息不一致,所以我想知道哪一篇是正确的。也许两者都是正确的,但在什么情况下?

first一种说法是我们每次获取一个页面大小

The cache controller is always observing the memory positions being loaded and loading data from several memory positions after the memory position that has just been read.

To give you a real example, if the CPU loaded data stored in the address 1,000, the cache controller will load data from ”n“ addresses after the address 1,000. This number ”n“ is called page; if a given processor is working with 4 KB pages (which is a typical value), it will load data from 4,096 addresses below the current memory position being load (address 1,000 in our example). In following Figure, we illustrate this example.

enter image description here

second一种说法是我们一次获取 sizeof(cache line) + sizeof(prefetcher)

So we can summarize how the memory cache works as:

  1. The CPU asks for instruction/data stored in address “a”.
  2. Since the contents from address “a” aren’t inside the memory cache, the CPU has to fetch it directly from RAM.
  3. The cache controller loads a line (typically 64 bytes) starting at address “a” into the memory cache. This is more data than the CPU requested, so if the program continues to run sequentially (i.e. asks for address a+1) the next instruction/data the CPU will ask will be already loaded in the memory cache.
  4. A circuit called prefetcher loads more data located after this line, i.e. starts loading the contents from address a+64 on into the cache. To give you a real example, Pentium 4 CPUs have a 256-byte prefetcher, so it loads the next 256 bytes after the line already loaded into the cache.

最佳答案

完全依赖于硬件实现。一些实现一次从主内存加载一条线——缓存线的大小在不同的处理器之间变化很大。我见过从 64 字节一直到 256 字节的行大小。基本上,“高速缓存行”的大小意味着当 CPU 从主 RAM 请求内存时,它会一次 n 个字节。所以如果 n 是 64 字节,并且你在 0x1004 加载一个 4 字节整数,MMU 实际上将通过总线发送 64 字节,所有地址从 0x1000 到 0x1040。整个数据 block 将作为一行存储在数据缓存中。

一些 MMU 可以在每个请求中跨总线获取多条缓存线——因此,在具有 64 字节缓存的机器上在地址 0x1000 发出请求实际上会加载从 0x1000 到 0x1100 的四条线。 Some systems让您使用特殊的缓存预取或 DMA 操作码显式执行此操作。

但是,通过您的第一个链接的文章是完全错误的。它混淆了操作系统的大小 memory page带硬件cache line .这些是完全不同的概念。第一个是操作系统将立即分配的虚拟地址空间的最小大小。后者是 CPU 如何与主 RAM 对话的详细信息。

它们相似只是在操作系统的物理内存不足时,它会将一些最近未使用的虚拟内存分页到磁盘;稍后,当您再次使用该内存时,操作系统会将整个页面从磁盘加载回物理 RAM。这与 CPU 从 RAM 加载字节的方式类似(但不相关),这就是为什么《硬件 secret 》的作者感到困惑的原因。

Ulrich Drepper 的论文 What Every Programmer Should Know About Memory 是了解计算机内存以及高速缓存为何如此工作的好地方。 .

关于c - 高速缓存 Controller 一次从主存到二级高速缓存取多少字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9826274/

相关文章:

c - 我将代码从 FreeBSD 移植到 Linux,但它没有提取目标地址

c - 大输出的浮点异常

c - 如何将字符一个接一个地存储在数组中,直到用户输入字符以外的输入,然后在屏幕上重现相同的内容?

c++ - 缓存高效的多线程归并排序

linux - 缓存路由不反射(reflect)路由表

asp.net - 如何缓存 ASP.NET 用户控件?

Java堆内存

c - C语言中#error指令的用途是什么?

在最大堆中分配字节 [] 时出现 Java OutOfMemoryError

memory - Hadoop 名称节点内存使用情况