说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C11-MemHierarchy/summary.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
11.7. Summary
本章探讨了计算机存储设备的特征,以及它们在访问延迟、存储容量、传输延迟和成本等关键指标上的权衡。由于各种设备体现了截然不同的设计与性能权衡,它们自然地形成了一个存储器层次结构,按照容量和访问时间排列。在层次结构的顶端,CPU 缓存和主存等主存储设备能快速地直接向 CPU 提供数据,但容量有限。在层次结构的下层,固态硬盘和硬盘等辅助存储设备以牺牲性能为代价,提供高密度的大容量存储。
This chapter explored the characteristics of computer storage devices and their trade-offs with respect to key measures like access latency, storage capacity, transfer latency, and cost. Because devices embody many disparate design and performance trade-offs, they naturally form a memory hierarchy, which arranges them according to their capacity and access time. At the top of the hierarchy, primary storage devices like CPU caches and main memory quickly provide data directly to the CPU, but their capacity is limited. Lower in the hierarchy, secondary storage devices like solid-state drives and hard disks offer dense bulk storage at the cost of performance.
由于现代系统既需要大容量又需要良好的性能,系统设计者用多种形式的存储来构建计算机。关键在于,系统必须管理每一块数据存放在哪个存储设备上。系统的目标是把正在使用的数据存放在较快的存储设备中,而把不常用的数据转移到较慢的存储设备上。
Because modern systems require both high capacity and good performance, system designers build computers with multiple forms of storage. Crucially, the system must manage which storage device holds any particular chunk of data. Systems aim to store data that’s being actively used in faster storage devices, and they relegate infrequently used data to slower storage devices.
为了确定哪些数据正在被使用,系统依赖于称为局部性的程序数据访问模式。程序表现出两种重要的局部性:
To determine which data is being used, systems rely on program data access patterns known as locality. Programs exhibit two important types of locality:
- 时间局部性:程序倾向于在一段时间内反复访问同一数据。
- Temporal Locality: Programs tend to access the same data repeatedly over time.
- 空间局部性:程序倾向于访问与之前访问过的数据相邻的数据。
- Spatial Locality: Programs tend to access data that is nearby other, previously accessed data.
局部性是 CPU 缓存的基础,CPU 缓存在 CPU 芯片上的快速存储中存放主存的一个小子集。当程序试图访问主存时,CPU 首先在缓存中查找数据;如果在那里找到了,就可以免去一趟代价更高的主存访问。
Locality serves as the basis for CPU caches, which store a small subset of main memory in fast storage directly on the CPU chip. When a program attempts to access main memory, the CPU first checks for the data in the cache; if it finds the data there, it can avoid the more costly trip to main memory.
当程序发出读或写内存的请求时,它提供想要访问的内存位置的地址。CPU 缓存把内存地址中的位分成三段,用来识别缓存行存放的是主存的哪个子集:
When a program issues a request to read or write memory, it provides the address of the memory location that it wants to access. CPU caches use three sections of the bits in a memory address to identify which subset of main memory a cache line stores:
- 地址中间的索引位把地址映射到缓存中的一个存储位置。
- The middle index bits of an address map the address to a storage location in the cache.
- 高位的标记位唯一地标识该缓存位置存放的是哪个内存子集。
- The high-order tag bits uniquely identify which subset of memory the cache location stores.
- 低位的偏移位确定程序想访问所存数据中的哪些字节。
- The low-order offset bits identify which bytes of stored data the program wants to access.
最后,本章演示了如何用 Cachegrind 工具对运行中的程序进行缓存性能剖析。Cachegrind 模拟程序与缓存层次结构之间的交互,并收集程序使用缓存的统计信息(例如命中率和未命中率)。
Finally, this chapter concluded by demonstrating how the Cachegrind tool can enable cache performance profiling for a running program. Cachegrind simulates a program’s interaction with the cache hierarchy and collects statistics about a program’s use of the cache (for example, the hit and miss rates).