NOTE
Cache
CPU caches bridge the speed gap between processors and memory. These notes begin with cache addressing and continue with coherence, write buffers, and memory barriers.
This is a historical learning note and may contain outdated or incomplete understanding.
[toc]
1. Cache
A high-speed cache sits between the CPU and memory to bridge the speed gap between them.
In a typical set-associative cache, a memory address can be divided into index, tag, and offset.
First use the index to select the cache set.
Then use the tag to identify the cache line.
Then use the offset to select a byte within that cache line.
Finally, check status bits such as the valid bit to determine whether the cache line is valid.
1.1. Problems Introduced by Caching
Multiple processor cores may cache the same memory location. If Core A updates it, how do the other cores learn about the change? This requires introducing a cache-coherence protocol.
2. Cache Coherence
2.1. MESI Protocol
MESI defines four states for a cache line:
- Invalid The cache line is invalid.
- Shared Multiple processors may cache the same line, and the cached data is consistent with memory.
- Exclusive Only the current processor caches the line, and the cached data is consistent with memory.
- Modified The current processor has modified the cache line, so the copy in main memory may be stale.
2.1.1. Specific Implementation
- Processor0 needs to read data S and its state is I. Processor1’s state is M/E/S. Processor0 issues a Read request. If another processor holds the newest data in M state, the coherence protocol supplies that data and changes the relevant states; otherwise the data can be read from another cache or memory.
- Processor0 needs to write data S. If the state is M, it can write directly. If the state is E, it can write directly and change the state to M. If the state is S, it must first obtain exclusive ownership and invalidate other shared copies, then write and enter M. If the state is I, it must first obtain the cache line and exclusive ownership, then write and enter M.
- Summary MESI uses write-invalidate. Typical caches also use write-back, so a write does not have to be written to main memory immediately.
2.1.2. MESI Performance Issues
When a cache line is shared, a writer must first obtain exclusive ownership and invalidate other copies, which incurs coherence communication and waiting overhead.
3. Write Buffer and Invalidation Queue
3.1. Write Buffer
A write buffer is temporary storage on the processor’s write path. When a write is still waiting for a cache-coherence transaction to complete, the data can first enter the write buffer while the processor continues executing later instructions.
Summary After a processor executes a write, other processors may not be able to observe that write immediately.
3.2. Invalidation Queue
After receiving an Invalid message, a processor may queue it for later processing; whether and when it can reply before applying the invalidation depends on the implementation.
3.3. Problems Introduced by These Components
Visibility and reordering.
4. Reordering Problem
Write buffers, out-of-order execution, and related mechanisms can make other processors observe memory operations in an order different from program order.
4.1. Categories
StoreLoad A Load after a Store may be observed as taking effect before that Store.
StoreStore The observable order of two Stores may change.
LoadLoad The observable order of two Loads may change.
LoadStore The observable order of a Load and a later Store may change.
Which reorderings are allowed depends on the processor architecture and memory model.
5. Visibility Problem
If one processor has executed a write but the write has not yet become observable to another processor, the other processor may still read the old value. That is the visibility problem discussed here.
5.1. Solution
Use synchronization primitives or memory barriers to establish the required ordering of memory accesses.
6. Memory Barrier
A memory barrier restricts the reordering of certain memory operations across the barrier, or prevents other processors from observing them in an order that violates the required ordering. XY can be used to describe an ordering constraint: an X-type access before the barrier must not cross the barrier and be observed after a Y-type access that follows it.
6.1. Specific Implementation
Different processor architectures provide different Fence / Barrier instructions and guarantees. A memory barrier should not be understood simply as “clearing the invalidation queue” or “flushing the entire write buffer into cache or memory.”
7. References
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub