# CPU 缓存

CPU 缓存是处理器核心内部或近旁的一块小而快的存储器，保存着主存中近期使用过的数据和指令的副本，使重复访问不必再走一趟慢得多的 DRAM 之路。现代 x86 与 Arm 处理器将缓存组织为 L1、L2、L3 的层级结构，在容量与速度之间取舍：每一级都比上一级更大、更慢，最后一级由各核心共享，而前几级为每个核心私有。<sup>[1](https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/cache-allocation-technology-white-paper.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 层级 | L1 最小、最快，位置最靠近核心；L2（中级缓存，MLC）大出许多倍，延迟也更高；L3（末级缓存，LLC）最大、最慢，由所有核心共享。<sup>[1](https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/cache-allocation-technology-white-paper.pdf)</sup> |
| 缓存行 | 数据以固定大小的块在缓存与内存之间搬移；在 Intel 64 处理器上，缓存行为 64 字节，从内存读取时以 64 字节边界开始。<sup>[2](https://cdrdv2-public.intel.com/812380/252046-sdm-change-document.pdf)</sup> |
| 典型时序 | L1 命中约 1 个时钟周期，L2 约 3 至 8 个周期，未命中到主存的代价约为 25 至 100 个周期；L1 的未命中率为 3% 至 10%，L2 低于 1%。<sup>[3](https://www.cs.umd.edu/users/meesh/webpages/cmu-csapp/lectures/csapp-lectures/ics/class14.pdf)</sup> |
| Sandy Bridge 实例 | 32 KB 八路组相联 L1 数据缓存，每核心 256 KB 八路组相联统一 L2，以及一个包容式共享 LLC；最佳情况下的加载延迟为 4 个周期（L1）、12 个周期（L2）、26 至 31 个周期（LLC）。<sup>[4](https://cdrdv2-public.intel.com/821614/356477-Optimization-Reference-Manual-V2-050.pdf)</sup> |
| 一致性 | Intel 核心上所有缓存层级之间的数据一致性由 MESI 协议维护；组件之间的系统级一致性则使用 Arm 的 AMBA 4 ACE 等协议。<sup>[4](https://cdrdv2-public.intel.com/821614/356477-Optimization-Reference-Manual-V2-050.pdf)</sup><sup> • </sup><sup>[5](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/extended-system-coherency---part-1---cache-coherency-fundamentals?CommentId=bb092c1f-c2d5-4966-ab77-9139a130ca0c%3FCommentId%3Dbb092c1f-c2d5-4966-ab77-9139a130ca0c)</sup> |
| 为何重要 | 片上缓存访问的功耗显著低于访问外部 DRAM，而在大型服务器上，一次 L3 访问的耗时可达一次 L2 访问的 10 倍甚至更多。<sup>[5](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/extended-system-coherency---part-1---cache-coherency-fundamentals?CommentId=bb092c1f-c2d5-4966-ab77-9139a130ca0c%3FCommentId%3Dbb092c1f-c2d5-4966-ab77-9139a130ca0c)</sup><sup> • </sup><sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/hardware-and-software-approach-for-using-numa-systems.html)</sup> |

## 缓存层级

Intel 的 Xeon 处理器包含三级缓存。L1 缓存最小但最快，位置最靠近核心。L2 缓存又称中级缓存（MLC），比 L1 大出许多倍，但带宽与低延迟都及不上 L1。L3 缓存即末级缓存（LLC），最大也最慢，容量可比 MLC 大出几个数量级；每个物理核心拥有私有的 L1 和 L2 缓存，而 LLC 为共享缓存，所有核心均可完全访问。<sup>[1](https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/cache-allocation-technology-white-paper.pdf)</sup> 当一次请求在核心的 L1 和 L2 中均未命中时，会继续前往 LLC；若在 LLC 中仍未命中，则由内存提供服务。<sup>[1](https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/cache-allocation-technology-white-paper.pdf)</sup>

**分离式与统一式层级。** 第一级通常分为数据缓存与指令缓存。在 Nehalem 与 Westmere 的 Xeon 产品上，L1 分为预解码指令区和数据区两部分，L2 是每核心私有的统一数据与指令缓存，L3 则是封装内所有核心共享的包容式统一缓存。<sup>[2](https://cdrdv2-public.intel.com/812380/252046-sdm-change-document.pdf)</sup> 在 Sandy Bridge 上，启用超线程时 L1 数据缓存可能由两个逻辑处理器共享，所有核心通过环形总线连接到共享的 LLC。<sup>[4](https://cdrdv2-public.intel.com/821614/356477-Optimization-Reference-Manual-V2-050.pdf)</sup>

**具体容量随代际而变。** 第 11 代 [Intel Core](https://www.edgepedia.org/zh/intel-core) 桌面处理器每核心拥有 48 KB 的 L1 数据缓存和 32 KB 的 L1 指令缓存，每核心 512 KB 八路组相联 L2，以及每核心最高 2 MB、十六路组相联的共享 LLC，处理器图形也使用该 LLC。<sup>[7](https://cdrdv2-public.intel.com/634648/634648-004.pdf)</sup> Intel Core 处理器（Series 3）的 P 核更进一步，采用两级数据侧设计：48 KB L0 加 192 KB L1，均为十二路组相联，另有 64 KB 十六路组相联指令缓存，以及每核心 3 MB 的 L2。<sup>[8](https://edc.intel.com/content/www/de/de/design/platforms/core-processor-series-3-datasheet-volume-1-of-2/001/p-core-lp-e-core-level-0-level-1-and-level-2-caches/)</sup>

## 缓存的组织方式

缓存利用程序中的两种局部性。时间局部性是指在短时间内重复使用一小份工作数据集；空间局部性是指访问彼此邻近的地址。缓存以若干字节为块（称为缓存行）保存数据，使一次取数能服务邻近的访问，并且一行会一直保留到空间被需要时才被替换。<sup>[9](https://www.cs.cornell.edu/~bindel/nmds/00-Background/02-Performance.html)</sup>

内存地址被划分为三个字段：标记、索引和块内偏移。在直接映射缓存中，每个地址恰好映射到一个表项，由地址低位决定；在 n 路组相联缓存中，一个地址可进入某一组内的 n 个位置之一，发生替换时，处理器会采用最近最少使用（LRU）之类的策略来选择被替换的行。<sup>[9](https://www.cs.cornell.edu/~bindel/nmds/00-Background/02-Performance.html)</sup> 全相联缓存中，一个块可以放在任意位置，每次访问都需搜索所有表项，这种结构由内容可寻址存储器构建，体积大、发热高、速度慢，因此只适用于小容量缓存。<sup>[10](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup> 直接映射与全相联设计分别是组相联在一路和一组情形下的特例。<sup>[11](https://www.cs.cornell.edu/~tomf/notes/cps104/cache)</sup>

**写操作。** 有两种策略管理存储操作。写直达（write-through）将每次存储立即送往主存；写回（write-back）只更新缓存，推迟对内存的写入。写回避免了许多代价高昂的内存写操作，因而被广泛采用，但每个表项需要一个脏位：一次存储会置位该位，替换时脏行被写回内存，而干净行则直接丢弃。<sup>[10](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup> 写分配（write-allocate）策略在存储未命中时把被写入的块调入缓存，其假设是被写入的位置很可能很快会被再次读取。<sup>[10](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup>

## 性能

缓存性能由命中率、命中代价和未命中代价来量化，平均访问时间由下式给出

\[ t_{\text{avg}} = t_{\text{hit}} + r_{\text{miss}} \times t_{\text{miss}} \]

其中 \( t_{\text{hit}} \) 为缓存访问时间，\( r_{\text{miss}} \) 为未命中的访问比例，\( t_{\text{miss}} \) 为从下一级满足一次未命中所需的时间。<sup>[12](https://www.cs.cornell.edu/courses/cs3410/2024fa/notes/caches.html)</sup> 典型数值为：L1 命中 1 个时钟周期，L2 为 3 至 8 个周期，到达主存需 25 至 100 个周期。<sup>[3](https://www.cs.umd.edu/users/meesh/webpages/cmu-csapp/lectures/csapp-lectures/ics/class14.pdf)</sup> 一份已发表的总结列出：256 KB 的 L2 约为 4 纳秒、1 TB/秒，8 MB 或更大的 L3 比 L2 慢约 10 倍，但带宽超过 400 GB/秒。<sup>[13](https://www.intel.com/content/www/us/en/developer/articles/technical/memory-performance-in-a-nutshell.html)</sup>

未命中分为三个经典类别。强制性（冷）未命中发生在某一行首次被加载时；容量未命中是因为工作集超出缓存容量，无论缓存如何组织都无法避免；冲突未命中是因为过多行争用同一组，尽管其他位置仍有空余。<sup>[14](https://www.cs.umd.edu/~meesh/411/website/proj01/cache/cache.pdf)</sup><sup> • </sup><sup>[12](https://www.cs.cornell.edu/courses/cs3410/2024fa/notes/caches.html)</sup> 在大型多路系统中，距离本身也带来代价：一次 L3 访问的耗时可达一次 L2 访问的 10 倍甚至更多，因为请求要在总线的各连接点之间跳转，而每个核心与 L3 缓存之间的交换速率至多每秒几十 GB。<sup>[6](https://www.intel.com/content/www/us/en/developer/articles/technical/hardware-and-software-approach-for-using-numa-systems.html)</sup>

## 包容式、非包容式与共享缓存

在 Intel Xeon v3 处理器上，LLC 是包容式的：它包含低级缓存中存储的所有数据，因此当 LLC 中的某一行被替换时，该行也必须在各核心的缓存中失效。<sup>[1](https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/cache-allocation-technology-white-paper.pdf)</sup> Sandy Bridge 的 LLC 同样包容其上的所有层级，每条 LLC 行记录哪些核心可能在其 L1 和 L2 中持有该行，从而可以直接到持有该行的核心缓存中查找来解决一次未命中。<sup>[4](https://cdrdv2-public.intel.com/821614/356477-Optimization-Reference-Manual-V2-050.pdf)</sup>

**转向非包容式 LLC。** Intel 在 Xeon Scalable 系列中扭转了这一平衡：中级缓存从每核心 256 KB 增至 1 MB，而共享 LLC 从每核心包容式的 2.5 MB 缩减为非包容式的 1.375 MB。更大的 MLC 提高了命中率，降低了有效内存延迟，也减轻了对网状互连和 LLC 的压力；发生未命中时，从内存取回的缓存行只放入请求核心的 MLC，而监听过滤器在缓存行未分配进 LLC 时跟踪其位置。<sup>[15](https://www.intel.com/content/www/us/en/support/articles/000027820/processors/intel-xeon-processors.html)</sup> Arm 的 CoreLink CCN-508 采取混合方式：其分布式 L3 对数据行基本是排他式的，对代码行则是伪包容式的，监听过滤器将监听广播转换为定向监听，从而抑制监听流量的平方级增长。<sup>[16](https://developer.arm.com/documentation/100020/0001/functional-description/about-the-functions/fully-coherent-home-node?lang=en)</sup>

## 缓存一致性

一致性确保系统中的所有处理器或总线主设备看到一致的内存视图。在 Intel 核心上，MESI 协议维护所有缓存层级之间的数据一致性。<sup>[4](https://cdrdv2-public.intel.com/821614/356477-Optimization-Reference-Manual-V2-050.pdf)</sup> 若无硬件支持，软件必须自行管理缓存：在读取被外部代理（如 DMA 网络接口）更新过的数据之前，先将脏行清理（刷新）回 DRAM，并使过期行失效。<sup>[5](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/extended-system-coherency---part-1---cache-coherency-fundamentals?CommentId=bb092c1f-c2d5-4966-ab77-9139a130ca0c%3FCommentId%3Dbb092c1f-c2d5-4966-ab77-9139a130ca0c)</sup> Arm 的基准测试发现，一个处理每个数据包头的网络应用可能把超过三分之一的 CPU 周期花在这类缓存维护上，这促使人们通过 2011 年发布的 AMBA 4 ACE 规范将硬件一致性扩展到整个系统；后来的 AMBA 5 CHI 协议支持超过 12 个 CPU 的一致性枢纽。<sup>[5](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/extended-system-coherency---part-1---cache-coherency-fundamentals?CommentId=bb092c1f-c2d5-4966-ab77-9139a130ca0c%3FCommentId%3Dbb092c1f-c2d5-4966-ab77-9139a130ca0c)</sup><sup> • </sup><sup>[17](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/5-things-you-might-not-know-about-amba-5-chi)</sup>

**伪共享。** 当多个 CPU 缓存中持有的同一条缓存行包含被不同线程使用的数据时，一致性流量可能占据主导。在 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)的经典例子中，一个频繁修改的引用计数和一个从不修改的名称字段共享同一行，于是每当另一个 CPU 增加引用计数时，读取名称的每个 CPU 都必须重新加载整行。有害的伪共享需要满足：有一个被多个 CPU 共享的数据项，且并发访问中至少有一次写操作；它在现实中曾导致内核的性能回退。<sup>[18](https://docs.kernel.org/kernel-hacking/false-sharing.html)</sup>

## 安全：缓存时序侧信道

由于命中快、未命中慢，缓存的分配与替换模式可以通过时序来测量，从而泄露另一上下文活动的信息。基于推测的变体进一步扩展了这一点：在尚未确定结果的分支之后进行推测性内存读取，会分配缓存表项，其地址取决于攻击者无法直接读取的数据，而测量这些分配即可揭示数据；这一类包括 Spectre 系列攻击。<sup>[19](https://developer.arm.com/-/media/files/pdf/cache_speculation_side-channels.pdf)</sup> 缓存时序攻击已经攻破了包括 DES、AES 和 Camellia 在内的分组密码软件实现，NIST 指出，针对 AES 的缓存时序攻击值得高度关注，因为它们有可能被远程实施，这也是在现代处理器中加入 AES 指令的关键动机之一。<sup>[20](https://csrc.nist.gov/csrc/media/events/physical-security-testing-workshop/documents/papers/physecpaper19.pdf)</sup><sup> • </sup><sup>[21](https://csrc.nist.gov/News/2022/aes-draft-fips-197-update-available-for-comment)</sup>

## 软件技术

程序员可以安排数据和循环以提高命中率。循环分块重排计算顺序，使载入缓存的数据在被替换之前得到复用，从而实现时间局部性；将数据按 64 字节缓存行粒度对齐，可避免跨越两行的拆分加载；减小工作集大小则能同时降低缓存和数据 TLB 的未命中开销。<sup>[22](https://www.intel.com/content/www/us/en/developer/articles/technical/loop-optimizations-where-blocks-are-required.html)</sup><sup> • </sup><sup>[23](https://www.intel.com/content/www/us/en/docs/vtune-profiler/user-guide/2026-1/cpu-metrics-reference.html)</sup> 硬件也能提供帮助：Intel 的缓存分配技术（Cache Allocation Technology）让软件控制哪些核心可以向共享 LLC 的某些部分分配数据，旨在提升 Xeon 处理器上的实时性能。<sup>[1](https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/cache-allocation-technology-white-paper.pdf)</sup>

## References

1. [Improving Real-Time Performance by Utilizing Cache Allocation Technology, Intel white paper (April 2015)](https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/cache-allocation-technology-white-paper.pdf)
2. [Intel 64 and IA-32 Architectures Software Developer's Manual, Documentation Changes (cache structure and 64-byte lines)](https://cdrdv2-public.intel.com/812380/252046-sdm-change-document.pdf)
3. [Cache Performance Metrics, CMU 15-213 lecture notes](https://www.cs.umd.edu/users/meesh/webpages/cmu-csapp/lectures/csapp-lectures/ics/class14.pdf)
4. [Intel 64 and IA-32 Architectures Optimization Reference Manual, Sandy Bridge microarchitecture](https://cdrdv2-public.intel.com/821614/356477-Optimization-Reference-Manual-V2-050.pdf)
5. [Extended System Coherency, Part 1: Cache Coherency Fundamentals, Arm Community (Neil Parris, 2013)](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/extended-system-coherency---part-1---cache-coherency-fundamentals?CommentId=bb092c1f-c2d5-4966-ab77-9139a130ca0c%3FCommentId%3Dbb092c1f-c2d5-4966-ab77-9139a130ca0c)
6. [Hardware and Software Approach for Using NUMA Systems, Intel](https://www.intel.com/content/www/us/en/developer/articles/technical/hardware-and-software-approach-for-using-numa-systems.html)
7. [11th Generation Intel Core Processor Desktop Datasheet](https://cdrdv2-public.intel.com/634648/634648-004.pdf)
8. [Intel Core Processor (Series 3), P-core and LP E-core Level 0, Level 1 and Level 2 Caches, Intel datasheet](https://edc.intel.com/content/www/de/de/design/platforms/core-processor-series-3-datasheet-volume-1-of-2/001/p-core-lp-e-core-level-0-level-1-and-level-2-caches/)
9. [Performance Basics, Numerical Methods for Data Science, Cornell (David Bindel)](https://www.cs.cornell.edu/~bindel/nmds/00-Background/02-Performance.html)
10. [Caches, CS 3410 Spring 2026, Cornell](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)
11. [Cache notes, Thomas Finley, Cornell (2000)](https://www.cs.cornell.edu/~tomf/notes/cps104/cache)
12. [Caches, CS 3410 Fall 2024, Cornell](https://www.cs.cornell.edu/courses/cs3410/2024fa/notes/caches.html)
13. [Memory Performance in a Nutshell, Intel](https://www.intel.com/content/www/us/en/developer/articles/technical/memory-performance-in-a-nutshell.html)
14. [Introduction of Cache Memory, CMU 15-411 project notes](https://www.cs.umd.edu/~meesh/411/website/proj01/cache/cache.pdf)
15. [What Is the Difference in Cache Memory Between CPUs for Intel Xeon Scalable Processors?, Intel support article](https://www.intel.com/content/www/us/en/support/articles/000027820/processors/intel-xeon-processors.html)
16. [Arm CoreLink CCN-508 Cache Coherent Network Technical Reference Manual, Fully-coherent Home Node](https://developer.arm.com/documentation/100020/0001/functional-description/about-the-functions/fully-coherent-home-node?lang=en)
17. [5 things you might not know about AMBA 5 CHI, Arm Community](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/5-things-you-might-not-know-about-amba-5-chi)
18. [False Sharing, The Linux Kernel documentation](https://docs.kernel.org/kernel-hacking/false-sharing.html)
19. [Cache Speculation Side-channels white paper, Richard Grisenthwaite, Arm (January 2018)](https://developer.arm.com/-/media/files/pdf/cache_speculation_side-channels.pdf)
20. [Side-Channel Attacks: Ten Years After Its Publication and the Impacts on Cryptographic Module Security Testing, NIST workshop paper](https://csrc.nist.gov/csrc/media/events/physical-security-testing-workshop/documents/papers/physecpaper19.pdf)
21. [Proposal to Update FIPS 197, The Advanced Encryption Standard, NIST CSRC](https://csrc.nist.gov/News/2022/aes-draft-fips-197-update-available-for-comment)
22. [Loop Optimizations Where Blocks are Required, Intel](https://www.intel.com/content/www/us/en/developer/articles/technical/loop-optimizations-where-blocks-are-required.html)
23. [CPU Metrics Reference, Intel VTune Profiler user guide](https://www.intel.com/content/www/us/en/docs/vtune-profiler/user-guide/2026-1/cpu-metrics-reference.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Computer architecture theory › Memory hierarchy and caching

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/cpu-cache

English original: https://www.edgepedia.org/cpu-cache
