Sub topic 15
GATE problem patterns and strategies
This chapter turns the earlier chapters into a repeatable method for numerical cache questions in GATE CS. It assumes you can split an address into tag, index and offset, and that you know direct-mapped, fully associative and set-associative placement. It also assumes you can compute AMAT, and that you know LRU, FIFO, write-through and write-back. Here the focus is on recognizing a question's type, choosing the right formula, and avoiding the traps examiners rely on.
A routine for every cache problem
Most cache questions fail at the setup stage, not the arithmetic. Work through the same steps each time.
- Write down the address width and whether memory is byte-addressable. If it is not, say so, because offset bits then count words.
- Convert every size to a power of two in bytes (64 KB is 2^16 bytes).
- Compute lines = cache size / block size, then sets = lines / ways.
- Derive offset bits = log2(block size), index bits = log2(sets), and tag bits = address bits − index bits − offset bits.
- Only then read what the question asks: storage, hits and misses, or time.
Pattern 1: Geometry and tag bits
A typical question gives a physical address width, cache size, block size and associativity, then asks for tag bits or total tag storage. This helper encodes steps 3 and 4:
def geometry(addr_bits, cache_bytes, block_bytes, ways):
lines = cache_bytes // block_bytes
sets = lines // ways
offset_bits = block_bytes.bit_length() - 1
index_bits = sets.bit_length() - 1
tag_bits = addr_bits - index_bits - offset_bits
return lines, sets, offset_bits, index_bits, tag_bits
# 32-bit address, 64 KB cache, 32-byte blocks, 4-way
print(geometry(32, 64 * 1024, 32, 4)) # (2048, 512, 5, 9, 18)
# Direct-mapped: ways = 1. Fully associative: ways = lines, so index_bits = 0
print(geometry(32, 64 * 1024, 32, 1)) # (2048, 2048, 5, 11, 16)
print(geometry(32, 64 * 1024, 32, 2048)) # (2048, 1, 5, 0, 27)For the 4-way case, the tag array holds 2048 × 18 = 36,864 bits. If the question counts a valid bit per line, each line carries 19 bits of overhead. Total storage is then 2048 × (256 data bits + 19) = 563,200 bits. If the cache is write-back, add one dirty bit per line. Replacement state such as LRU counters is usually ignored unless the question says otherwise.
The table shows how the tag grows as associativity increases, with the same cache and address.
| Organization | Sets | Index bits | Tag bits |
|---|---|---|---|
| Direct-mapped | 2048 | 11 | 16 |
| 4-way | 512 | 9 | 18 |
| Fully associative | 1 | 0 | 27 |
Each doubling of associativity removes one index bit and adds one tag bit. Total data capacity does not change.
Pattern 2: Reference-string tracing
These questions give a sequence of block numbers and ask for hits, misses, or the final cache contents. Convert byte addresses to block numbers first (divide by block size). Then take block mod sets to get the set. Within each set, apply the replacement policy.
Two details cause most errors. LRU updates its order on a hit, and FIFO does not. Each set has its own order, so a block never competes with blocks in other sets.
def run(refs, capacity, policy):
cache = [] # front of the list is the next victim
hits = 0
for r in refs:
if r in cache:
hits += 1
if policy == "LRU":
cache.remove(r)
cache.append(r) # hit makes it most recent
else:
if len(cache) == capacity:
cache.pop(0)
cache.append(r)
return hits, len(refs) - hits
refs = [1, 2, 3, 1, 4, 1, 2]
print(run(refs, 3, "LRU")) # (2, 5)
print(run(refs, 3, "FIFO")) # (1, 6)Walk through FIFO by hand. After 1, 2, 3 the queue is 1, 2, 3. The reference to 1 hits but does not move it. Block 4 evicts 1, so the next reference to 1 misses and evicts 2. Then 2 misses. LRU keeps 1 alive because the hit refreshed it.
For a set-associative cache, repeat this per set. A reference string such as 3, 11, 3, 11 with 4 sets maps every block to set 3. With one way, all four references miss. With two ways, the last two hit.
Pattern 3: Access time
Three formulas cover almost every time question. First decide how the levels are accessed, then choose.
| Situation | Effective access time |
|---|---|
| Cache and memory accessed simultaneously | H × Tc + (1 − H) × Tm |
| Hierarchical (memory accessed after cache miss) | H × Tc + (1 − H) × (Tc + Tm) |
| Two-level, hierarchical | T1 + m1 × (T2 + m2 × Tmem) |
With Tc = 10 ns, Tm = 100 ns and H = 0.9, the simultaneous form gives 0.9 × 10 + 0.1 × 100 = 19 ns. The hierarchical form gives 0.9 × 10 + 0.1 × 110 = 20 ns. If the question says nothing, the hierarchical form is the safer reading when it mentions "first checks the cache". Read the wording closely.
def amat_two_level(t_l1, m_l1, t_l2, m_l2, t_mem):
# m_l2 is the local miss ratio of L2
return t_l1 + m_l1 * (t_l2 + m_l2 * t_mem)
print(amat_two_level(1, 0.1, 10, 0.2, 100)) # 4.0The main trap is local versus global miss ratio. A global L2 miss ratio is measured against all CPU references. A local one is measured against the references that reach L2. Convert with local = global / m1. If L1 misses 10% of references and L2 has a global miss ratio of 2%, the local ratio is 0.02 / 0.1 = 0.2, and that is the value the formula needs.
A second trap is the miss penalty for a multiword block. If the question gives a first-word latency and a per-word transfer time, the penalty is first-word time plus (words − 1) times the per-word time. Do not multiply the single-word time by the block size.
Pattern 4: Write policy and memory traffic
These questions count writes reaching memory, or ask which policy needs a dirty bit. Use the rules below.
| Policy | Write hit | Write miss | Extra state |
|---|---|---|---|
| Write-through, no-write-allocate | Update cache and memory | Write memory only | None |
| Write-back, write-allocate | Update cache, set dirty bit | Fetch block, then write in cache | Dirty bit per line |
Write-through sends one memory write per store. Write-back sends one block write per dirty eviction, however many stores hit that block before eviction. So repeated stores to one block favor write-back. A write-back cache that evicts a clean block writes nothing. The write-allocate fetch on a write miss is a read from memory, so count it as read traffic.
Pattern 5: Classifying misses
The classification follows a fixed test. A first-ever reference to a block is a compulsory miss. If a fully associative cache of the same total size would also miss, the miss is a capacity miss. If the fully associative cache would hit, it is a conflict miss. Only direct-mapped and set-associative caches have conflict misses, and raising associativity can remove them. Larger blocks reduce compulsory misses through spatial locality but can raise conflict misses because the cache holds fewer blocks.
Common traps
- Counting the offset in the wrong unit. In a word-addressable machine, offset bits are log2(words per block).
- Forgetting that a fully associative cache has no index, so the tag is address bits minus offset bits.
- Using cache size in KB without converting to bytes before dividing by block size.
- Treating a hit as a reason to reorder a FIFO queue.
- Adding the valid bit and dirty bit to tag storage only when the question mentions them.
- Reading "hit ratio of 90%" for a level when the question also gives an L1 ratio, and not checking whether it is local.
Exercise
A byte-addressable machine has 32-bit addresses and a direct-mapped cache of 16 KB with 64-byte blocks. How many tag bits are in each line, and how many bits does the tag array hold in total (ignoring valid bits)?
Show answer
Lines = 16384 / 64 = 256, so index bits = 8. Offset bits = 6. Tag bits = 32 − 8 − 6 = 18. Total tag storage = 256 × 18 = 4608 bits.
Exercise
L1 has a hit time of 2 ns and misses on 5% of references. L2 has an access time of 12 ns and a global miss ratio of 1%. Memory takes 120 ns. Using hierarchical access, what is the average memory access time?
Show answer
The local L2 miss ratio is 0.01 / 0.05 = 0.2. AMAT = 2 + 0.05 × (12 + 0.2 × 120) = 2 + 0.05 × 36 = 3.8 ns.
Exercise
A cache has 8 lines and is direct-mapped. The block reference string is 3, 11, 3, 11, 3. How many misses occur? How many misses occur if the cache becomes 2-way with 4 sets and LRU?
Show answer
Direct-mapped: 3 mod 8 = 3 and 11 mod 8 = 3, so both blocks use line 3 and every reference misses, giving 5 misses. For 2-way with 4 sets: 3 mod 4 = 3 and 11 mod 4 = 3, so both blocks go to set 3, which has two ways. The first two references miss, and the remaining three hit, giving 2 misses.