Sub topic 14
Multilevel caches and access time
This chapter assumes you can compute average memory access time (AMAT) for one cache level. You should also know hit ratio, miss ratio and miss penalty, and the difference between direct-mapped and set-associative caches. Here we extend AMAT to two or more cache levels and learn to read GATE questions that mix local and global ratios.
Why add more than one cache level
A single cache forces a trade-off. A small cache can be fast but misses often. A large cache misses less but is slower, because it has longer wires, more tag comparisons and more lines to search.
Real processors split the job:
- L1 is small (tens of KB) and very fast, often 1 to 4 cycles. It is built to keep the hit time low.
- L2 is larger (hundreds of KB to a few MB) and slower. It is built to catch most L1 misses.
- L3, if present, is larger still and is usually shared between cores.
- Main memory is the final backstop, and every miss in the last cache level ends up there.
Each level reduces the number of requests that reach the next, slower level. The average cost of a memory access then stays close to the L1 hit time.
Local and global miss ratio
With two levels, "miss ratio" can mean two different things. GATE questions often depend on which one is meant.
- Local miss ratio of a level is the fraction of the accesses that reach that level which miss there.
- Global miss ratio of a level is the fraction of all CPU memory references that miss there and in every level above it.
For L1 the two are the same, because every CPU reference reaches L1. For L2:
global miss ratio of L2 = m1 × m2
Here m1 is the L1 miss ratio and m2 is the L2 local miss ratio. For example, if m1 = 0.1 and m2 = 0.2, then 0.02 of all references go to main memory. That is 2 per 100.
The L2 local miss ratio often looks poor, 20% to 50%, even when the L2 is working well. L1 already absorbed the easy accesses, which are the ones with good locality. The accesses that remain are harder to predict. For this reason, compare designs using the global miss ratio, and use the local one inside the AMAT formula.
The two-level AMAT formula
An access always pays the L1 time. Only the fraction m1 that misses also pays the L2 time. Only the fraction m1 × m2 pays the memory time as well. This is the hierarchical (sequential) model:
AMAT = t1 + m1 × (t2 + m2 × t_mem)
The inner bracket is the L1 miss penalty. It is the L2 time plus the L2 local miss ratio times the memory time. The same idea works at any depth: the miss penalty of one level is the average access time of the level below it.
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)) # 1 + 0.1 * (10 + 0.2 * 100) = 4.0Without L2, the same system would have an AMAT of 1 + 0.1 × 100 = 11. The L2 cuts that to 4.
A general function for any number of levels
Work from the bottom up. Start with the memory time as the penalty. For each level, going upward, the new penalty is that level's time plus its local miss ratio times the penalty below.
def amat(levels, t_mem):
# levels: list of (access_time, local_miss_ratio), L1 first
penalty = t_mem
for t, m in reversed(levels):
penalty = t + m * penalty
return penalty
print(round(amat([(1, 0.1), (10, 0.2)], 100), 4)) # 4.0
print(round(amat([(1, 0.1), (10, 0.2), (30, 0.5)], 100), 4)) # 3.6In the three-level case, the L3 gives 30 + 0.5 × 100 = 80. The L2 gives 10 + 0.2 × 80 = 26. The L1 gives 1 + 0.1 × 26 = 3.6.
Following one request through the hierarchy
Each arrow's probability is conditional on reaching that step. This is why the formula nests the terms: m2 applies only after an L1 miss.
Simultaneous access: the second model
Some GATE questions say that the caches and memory are accessed simultaneously, or that the access time of a level is the total time to get data from that level. In that case a level's time already includes the lookups above it, and the times are not added up. The formula is a plain weighted average over where the data was found:
AMAT = H1 × t1 + (1 − H1) × H2 × t2 + (1 − H1) × (1 − H2) × t_mem
Here H1 and H2 are hit ratios, with H2 local to L2.
With t1 = 1, H1 = 0.9, t2 = 10, H2 = 0.8 and t_mem = 100:
- Simultaneous: 0.9 × 1 + 0.1 × 0.8 × 10 + 0.1 × 0.2 × 100 = 0.9 + 0.8 + 2.0 = 3.7.
- Hierarchical, from the previous section: 4.0.
def amat_simultaneous(t1, h1, t2, h2, t_mem):
return h1 * t1 + (1 - h1) * h2 * t2 + (1 - h1) * (1 - h2) * t_mem
print(round(amat_simultaneous(1, 0.9, 10, 0.8, 100), 4)) # 3.7The simultaneous model is lower because a request that misses in L1 does not pay t1 again. In the hierarchical model it does.
Converting between local and global ratios
GATE questions often give the L2 ratio in one form and the formula needs the other. The conversion is one division or one multiplication.
| Given | You need | Conversion |
|---|---|---|
| L2 global miss ratio g, L1 miss ratio m1 | L2 local miss ratio m2 | m2 = g / m1 |
| L2 local miss ratio m2, L1 miss ratio m1 | L2 global miss ratio | g = m1 × m2 |
| Hit ratio H | Miss ratio | m = 1 − H |
| "L2 hit rate" with no qualifier, in the AMAT context | Usually local | Check the wording for "of all references" |
Phrases such as "of the references that miss in L1" or "for accesses reaching L2" mean local. Phrases such as "overall", "of all CPU references" or "global" mean global.
Counting accesses instead of ratios
Some questions give a trace or counts. Suppose 1000 references produce 80 L1 misses, and 20 of those also miss in L2.
- m1 = 80 / 1000 = 0.08.
- L2 local miss ratio = 20 / 80 = 0.25.
- L2 global miss ratio = 20 / 1000 = 0.02, which equals 0.08 × 0.25.
Be careful with the denominator. The L2 local ratio divides by the L2 accesses, not by 1000.
Other effects to keep in mind
The formulas treat reads as the main case. Real systems have extra details that GATE sometimes mentions in a single phrase.
- Writes. With write-through, a store also goes to the next level. A question may then ask you to add a write cost per store. Follow the numbers the question gives.
- Block size. L2 blocks are usually at least as large as L1 blocks. An L1 miss then brings in a whole L1 block, but the L2 may hold more neighbouring data. This is one reason L2 hits are common after L1 misses.
- Inclusion. In an inclusive hierarchy, everything in L1 is also in L2. In an exclusive one, a block lives in only one of them. Exclusive designs give more total capacity, and inclusive designs make coherence and invalidation simpler.
- Penalty in cycles. If the times are in cycles and the clock is given in GHz, convert at the end. For example, 4.0 cycles at 2 GHz is 2.0 ns.
Common mistakes
- Using the global L2 miss ratio where the formula expects the local one. This undercounts the memory term by a factor of m1.
- Forgetting that L1 time is paid by every access, and adding it only for the misses.
- Using the simultaneous formula when the question describes a sequential lookup, or the reverse.
- Treating a "hit rate" as a miss rate. Always write m = 1 − H before substituting.
- Mixing units, such as L1 in cycles and memory in nanoseconds.
Exercises
Exercise
A system has an L1 cache with access time 2 ns and miss ratio 0.05. The L2 cache has access time 15 ns. Main memory has access time 200 ns. The global miss ratio of L2 is 0.01, and the lookups are sequential. Find the AMAT.
Show answer
The L2 local miss ratio is m2 = global / m1 = 0.01 / 0.05 = 0.2. Then AMAT = 2 + 0.05 × (15 + 0.2 × 200) = 2 + 0.05 × 55 = 4.75 ns.
Exercise
L1 has access time 2 ns and hit ratio 0.8. L2 has access time 20 ns and a local hit ratio of 0.9. Memory has access time 100 ns. Compute the AMAT under (a) the hierarchical model and (b) the simultaneous model.
Show answer
(a) Hierarchical: 2 + 0.2 × (20 + 0.1 × 100) = 2 + 0.2 × 30 = 8.0 ns. (b) Simultaneous: 0.8 × 2 + 0.2 × 0.9 × 20 + 0.2 × 0.1 × 100 = 1.6 + 3.6 + 2.0 = 7.2 ns.
Exercise
Out of 2000 memory references, 100 miss in L1 and 40 of those also miss in L2. State the L1 miss ratio, the L2 local miss ratio and the L2 global miss ratio.
Show answer
The L1 miss ratio is 100 / 2000 = 0.05. The L2 local miss ratio is 40 / 100 = 0.4. The L2 global miss ratio is 40 / 2000 = 0.02, which equals 0.05 × 0.4.