cs-cache
Contents
There can be many caches
stacked
on top of each other. Cache 可以一层一层累积。
- if you miss in one you try in the “lower level cache” Lower level, mean higher number. 在上层的 Cache miss 了,可以在下层的 Cache 去找。依次类推。
- There can also be
separate
caches for data and instructions. Or the cache can be“unified”
. 数据和指令的 Cache 可以独立,也可以混合。 - to wit:
- the
L1 data cache (d-cache)
is the one nearest processor. It corresponds to the “data memory” block in our pipeline diagrams - the
L1 instruction cache (i-cache)
corresponds to the “instruction memory” block in our pipeline diagrams. - The L2 sits
underneath
the L1s. - There is often an
L3 in modern systems
.
- the
cache 指导思想
局部性原理
temporal locality (时间局部性)
Taking advantage of temporal locality:
- bring data into cache whenever its referenced
- kick out something that hasn’t been used recently
spatial locality (空间局部性)
Taking advantage of spatial locality:
bring in a block of contiguous data (cacheline)
, not just
the requested data
.
基本问题
怎样找到 cache 中的数据?
以 32bits 的内存地址,来分析。 (index, offset) 二维坐标来定位一个 byte 的数据。
index
定位 cache line,可视为横坐标
。
现在可以通过 cache lines = cache size / cache line size 来计算。
index bits = log2(cache lines)
offset
通过 index 定位到 cache line 后,offset 定位到这个 cache line 的哪一个 byte。
可以视为纵坐标
。
offset bits = log2(offset)
tag
32bits 剩下的部分。
cache line 应该是多大?
这其实是分块(block)思想。在利用空间局部性原理。
cache line size 越大
- Exploits more spatial locality.
- Large cache lines effectively prefetch data that we have not explicitly asked for. 更好地利用空间局部性,提前获取将要访问的数据。
cache line size 越小
- Focuses on temporal locality.
- If there is little spatial locality, large cache lines waste
space and bandwidth.
聚焦在时间局部性上,如果没有较好的空间局部性,提前
fetch
了数据,那就浪费了空间和带宽。
#(cache 组织方式)
实例分析
内存地址 32bits。
1024 cache lines, 32 Bytes per line.
index bits = log2(1024) = 10 offset bits = log2(32) = 5 tags bits = 32 - index - offset = 17
32KB cache, 64byte lines
index bits = log2(32KB / 64Bytes) = 9 offset bits = log2(64) = 6 tags bits = 32 - 9 - 6 = 17
set 是干什么的?
(set) Associativity
means providing more than one
place for a cache line to live.
One group of lines corresponds to each index.
- it is called a “set”
Each line
in a set is called a“way”
- N-Way associativity requires N parallel comparators set = ?
{% asset_img cache-2-ways-cache.svg 2-ways-cache%}
cache 哪一种 address?
virtual memory address
physical memory address
#(缓存类型)