0. Preface
在该书的前言中有对于整本书的导航:
看完第一章并做了点对应习题后本应顺势进入第二章,但是返回到导航发现附录B排在了第二章前面,故而转到附录B学习,此附录开篇也说能够对Memory Hierarchy有个快速了解,就当对知识的快速复习吧!!!!
B.1 Introduction
开篇便是36个术语:如果对某术语不太理解,希望能多搜搜~
cache | fully associative | write allocate |
---|---|---|
virtual memory | dirty bit | unified cache |
memory stall cycles | block offset | misses per instruction |
direct mapped | write back | block |
valid bit | data cache | locality |
block address | hit time | address trace |
write through | cache miss | set |
instruction cache | page fault | random replacement |
average memory access time | miss rate | index field |
cache hit | n-way set associative | no-write allocate |
page | least recently used | write buffer |
miss penalty | tag field | write stall |
Cache Performance Review
我们直观意义上来说,测量机器性能都是看给定一个任务,机器执行该任务所花费的时间,高速缓冲存储器也不例外:
Memory stall cycles: 处理器停顿等待内存获取的周期数量。
而Memory stall cycles 又依赖 the number of misses (未命中数量) 和 the cost per miss (未命中开销):
当然,上述式子只是一个近似,因为对于read
和wirte
操作来说,Miss rate (未命中率) 和Miss penalty (未命中惩罚) 经常是不同的。
Four Memory Hierarchy Questions
- Where can a block be placed in the upper level? (block placement)
- How is a block found if it is in the upper level? (block identification)
- Which block should be replaced on a miss? (block replacement)
- What happens on a write? (write strategy)
回答这4个问题有助于我们理解在存储器层次结构中不同层级的利弊权衡~
Q1: Where Can a Block be Placed in a Cache?
directed mapped: (不清楚请阅读Directed Mapped Cache
fully associative: (不清楚请阅读Fully Associative Cache
set associative: (不清楚请阅读Set Associative Cache
下图是每种方式的图例:
Q2:How Is a Block Found If It Is in the Cache?
下图为set associative 或 direct-mapped cache 的一个地址:
首先,在 Tag 中必须有 valid bit,该位表明 entry 中是否包含有效地址。
如果对此一点印象都没有,那么请读一读两位大师的另一本书《计算机组成与设计 硬件/软件接口》
Q3:Which Block Should be Replaced on a Cache Miss?
- Random
- Least recently used (LRU)
- First In, First Out (FIFO)
Q4:What Happens on a Write?
对于read
操作而言,如果block hit (块命中),那么块中的数据立即送到处理器中;如果block miss (块未命中),那么也不会有什么损失,仅仅就是浪费了一些时间与能耗。
而write
操作就不一样了,写操作不仅比读操作花费的时间长,而且更复杂。
往 Cache 中写信息有两种方法:
- write through:信息不仅写在cache中,而且写到更低级的存储器中。
- write back:信息仅仅写到cache中;当被写的块被替换时,信息写到 main memory 中。
当 write miss(写操作失效) 时,也有两种处理方法:
- write allocate:把要写的地址所在的 block 先从main memory 调入到 cache 中,然后把信息写入 cache 中。
- no-write allocate:直接把信息写回 main memory 中。
B.2 Cache Performance
比起 miss rate
,测量 cache
性能更好的指标为 average memory access time
:
以下是关于 cache 的所有公式:
B.3 Six Basic Cache Optimizations
- Reducing the miss rate
- Reducing the miss penalty
- Reducing the time to hit in the cache
- Compulsory (冷不命中导致)
- Capacity (块替换导致)
- Conflict (块放置策略为直接映射或组相联)
B.4 Virtual Memory
虚拟存储系统可以分为两类:
- pages: 固定大小的块
- segments: 可变大小的块
B.6 Fallacies and Pitfalls
[Pitfall] 1. Too small an address space. (地址空间太小)
地址空间太小会限制程序的长度,因为程序大小和数据数量不能超过2address size 。
[Pitfall] 2. Ignoring the impact of the operating system on the performance of the memory hierarchy. (忽视操作系统对存储器层次的影响)
[Pitfall] 3. Relying on the operating systems to change the page size over time. (依靠操作系统随时间改变页大小)
不要期望操作系统主动改变页面大小的使用习惯。