Sub topic 1
Why memory is a hierarchy
Prerequisites
This chapter assumes you can read binary and hexadecimal numbers, know that a CPU fetches instructions and data from memory, and can compute a weighted average.
Why memory is a hierarchy
Fast memory is small and expensive. Large memory is slow and cheap. Computers therefore stack several levels: registers, one or more caches, main memory (DRAM), and secondary storage such as an SSD. Each level down is larger, slower and cheaper per byte. The stack works only because programs reuse data.
Temporal and spatial locality
Temporal locality means that a recently used item is likely to be used again soon, such as a loop counter. Spatial locality means that addresses near a recently used one are likely to be used soon, such as the next element of an array. Caches rely on both. They keep recently used data and fetch whole blocks, not single bytes.
Cache basics: blocks, lines, hits and misses
A block is a fixed-size group of bytes, such as 64 bytes. A cache line is a slot that holds one block and its tag. When the CPU requests an address, the cache checks whether the block is present. If it is, the access is a hit. If not, the access is a miss, and the block is copied from the next level.
Hit ratio, miss ratio and miss penalty
Hit ratio equals hits divided by total accesses. Miss ratio equals 1 minus the hit ratio. The miss penalty is the extra time a miss costs, which is the time needed to fetch the block from the next level.
Average memory access time
Average memory access time (AMAT) is the expected time per access:
AMAT = hit time + miss ratio × miss penalty
Assume a hit time of 1 cycle and a miss penalty of 100 cycles:
| Miss ratio | AMAT (cycles) |
|---|---|
| 0.02 | 3 |
| 0.05 | 6 |
| 0.10 | 11 |
Halving the miss ratio from 0.10 to 0.05 nearly halves the AMAT, so the miss ratio usually dominates performance.
Exercise
A cache has a hit time of 2 cycles, a miss ratio of 0.1 and a miss penalty of 50 cycles. What is the AMAT?
Show answer
AMAT = 2 + 0.1 × 50 = 2 + 5 = 7 cycles.