Cache Simulator
Synthetic Access Patterns
This tool doesn’t execute real code (e.g. for i in range(10): print(i)
);
instead it generates one of four abstract memory‐access sequences:
- Sequential (0, 1, 2, …): models a simple forward loop like
for(i=0;i<N;i++) load A[i]
.
- Reverse (N−1, N−2, …): models iterating backward through an array.
- Strided (0, S, 2S, …): models stepping through every Sth element (
for(i=0;i<N;i+=S)
).
- Random (a shuffle of [0…N−1]): models pointer-chasing or other cache-unfriendly accesses.
Memory vs. Cache
- Main Memory
- A large, relatively slow array of words, each addressed by a unique number.
- Cache
- A smaller, faster store divided into fixed-size blocks (lines), each holding several consecutive words.
- Set-Associativity
- Splits the cache into “sets”; each block can only live in one set but within that set can occupy one of several lines.
How an Access Works
- Break the requested address into three fields:
- Tag: Identifies which block is in the cache line.
- Set index: Chooses which small group (set) of lines to search.
- Offset: Picks which word within the block to use.
- If a matching tag is found in that set → cache hit, return the data.
- If not → cache miss, load the block from RAM, evicting the least-recently-used line if needed.
- Optionally, prefetch future blocks on a miss to reduce upcoming misses.
Replacement Policy (LRU)
When you need to make room, evict the line that hasn’t been accessed for the longest time.
This maximizes the chance that “hot” data stays resident.
Performance Metrics
- Hit Rate: fraction of accesses served from cache.
- Miss Rate: fraction requiring a RAM fetch.
- Prefetch Count: extra blocks loaded speculatively.
- Total Time: cumulative latency (hits + misses + prefetches).
Visualizations
You get three synchronized views:
- Cache Table (lines, tags, offsets, LRU ages)
- RAM View (addresses, values, highlighting for current & prefetched)
- Chart (cumulative hits vs. misses over time)
Play with block size, associativity, and prefetch distance to see in real time how they affect performance!