skip to content
[Jimjimu Notes]
Table of Contents

Course index · Previous: Datapath Control · Next: Virtual Memory

Memory Hierarchy

Pasted image 20260510145519

Caches always contain a copy of the lower levels Pasted image 20260510151024

Pasted image 20260510151427 Pasted image 20260510152541 (缓存如何设计) Pasted image 20260510152609

Direct-Mapped Cache

Pasted image 20260510155752 Pasted image 20260510155802 一般来说我们把缓存的宽度画成和memory一样 上图是 1byte wide,因为**1 Byte (8 bits)** 是 CPU 能够通过内存地址直接去读取或写入的最小数据块。 这里cache运用了取余数的思想,比如说memory address为0、4、8...会进入cache的0 Pasted image 20260510155948 - 但是我们需要更大width的缓存,如上图是2 bytes wide,我们可以把内存也切成一个个 2 Bytes 的块 - 还是利用取模,内存地址的二进制位的倒数第2,3位代表了在cache的位置(cache index),倒数第一位表示cache中的列 - Cache Index(缓存索引)就像是 Cache 内部的“房间号”或“槽位号(Slot),上图中是0、1、2、3 - 举个例子,memory address=6=0b0110,得到cache index=11, offset=0 - 当然我们需要tag,正因为这是一个“多对一”的映射关系(比如地址 0、8、10、18 都会被硬性分配到 Cache Index 0),所以当 CPU 去 Cache Index 0 找数据时,它怎么知道现在里面装的是地址 0 的数据,还是地址 8 的数据呢? Pasted image 20260510160945 Pasted image 20260510161022 Pasted image 20260510161134

一行是一个cache block,并且他们共享tag值

:::

Direct-Mapped Cache Example

Directed Cache

Memory Access With Cache

Pasted image 20260516165250 Pasted image 20260516165303

Cache Terminology

Pasted image 20260516172437 Pasted image 20260516172448 Pasted image 20260516172531 Pasted image 20260516172545 Pasted image 20260516172619 Valid Bit不占据32位TIO中的一位!

Read Cache

read Cache的顺序:IVTO:index , Valid, Tag , Offset 当tag不匹配的时候,进行block replacement

Cache Write

Pasted image 20260516211917 Pasted image 20260516212233

Write-through和write-back现在都有在不同的地方应用

(1) 写直达:AMAT=0.5 x (2 + 50 x 0.04) + 0.5 x (2 + 50 x 0.02 + 50) = 0.5 x 4 + 0.5 x 53 = 28.5 写回: AMAT=0.5x(2 + 50 x 0.04 + 50 x 0.04 x 0.3 ) + 0.5 x (2 + 50 x 0.02 + 50 x 0.02 x 0.3) = 3.95

Block Size Tradeoff

Pasted image 20260516212539 Pasted image 20260516212737 Pasted image 20260516212847

Types of Cache Misses

Pasted image 20260516213213 Pasted image 20260516213230

“Tag 不匹配”是硬件检测到缓存未命中的表象,但导致这个表象的原因并不只有 Conflict

Fully Associative Cache

Pasted image 20260516215643 Pasted image 20260516215701 Pasted image 20260516215715

难点主要是硬件实现很难

Pasted image 20260516215739

在 Fully Associative Cache(全相联缓存)中,绝对不存在 Conflict Miss(冲突缺失)。 并且Capacity Miss主要出现在 Fully Associative Cache

Pasted image 20260516220237

一开始考虑一个全相联、无限大的缓存,遇到的miss就是compulsory 接下去将这个cache变成全相联、有限大的缓存,遇到的miss除去之前的compulsory miss,就是capacity miss 最后将这个cache变成有限相联、有限大的缓存,遇到的miss除去之前的就是conflict miss

Set-Associative Caches

每个组包含很多blocks,同一个组内是全相联结构

Pasted image 20260516231743 Pasted image 20260517000511 Pasted image 20260517000529 Pasted image 20260517000548

:::

Block Replacement Policy & LRU

Pasted image 20260517000638

问题是:组相联的cache,当某一组已经放满了,又来了一个数据,应该替换掉哪一个呢

Pasted image 20260517000810

多路了LRU实现其实很困难,硬件难以知道那个才是最旧的数据,但是如果是2-way组相联,就很简单: 打一个lru label,代表最旧的数据,即要被替换的数据

Pasted image 20260517000833

Average Memory Access Time(AMAT)

Pasted image 20260516233158

为啥不把Hit Time拆分成Hit rate x hit time ? 因为无论是否hit都需要支付hit time

Pasted image 20260517001016 Pasted image 20260517001028 Pasted image 20260517001038 Pasted image 20260517001051 Pasted image 20260517001110 Pasted image 20260517001120 Pasted image 20260517001134

:::

OS & Virtual Memory

Hierarchy(放过很多次了)

Pasted image 20260522201528 Pasted image 20260522201602 Pasted image 20260523110321 Pasted image 20260523110439

Course index · Previous: Datapath Control · Next: Virtual Memory

Backlinks