G27
Self-checkedTier 1 11 min readcoa.memory-hierarchy

Memory hierarchy and performance (AMAT)

Why computers have levels of memory, the principle of locality, hit and miss, hit ratio, average memory access time for one and several levels, hierarchical against simultaneous access, and what a hierarchy buys.

Why it exists

Fast memory is small and expensive; large memory is cheap and slow. No single technology gives all three of large, fast and cheap. A computer therefore uses a hierarchy: a few very fast registers, a small fast cache, a larger and slower main memory, and a very large and very slow disk. The hierarchy works because programs do not use memory at random: they reuse what they used recently and use what is near it. With that behaviour most accesses are served by the fast levels, so the average access is nearly as fast as the fastest level while the total capacity and cost are those of the slowest and cheapest. GATE asks for the average access time, the effect of hit ratios, and how two or three levels combine.

Prerequisites

None. This is a starting point.

No earlier page is needed beyond ordinary arithmetic with ratios.

Precise definitions

The levels (smaller, faster and more expensive per bit toward the top):

LevelTypical roleRelative speed (orders of magnitude, illustrative)
Registersoperands of the current instructionsabout one clock
L1 cacherecently used instructions and dataa few clocks
L2, L3 cachelarger, sharedten or more clocks
Main memory (DRAM)all running programsabout a hundred clocks
Disk / SSDfiles, swapped-out pageshundreds of thousands to millions of clocks

Principle of locality. Programs tend to access a small part of their address space at any time.

  • Temporal locality: an item used recently is likely to be used again soon (loop variables, instructions of a loop).
  • Spatial locality: items near a recently used item are likely to be used soon (sequential instructions, array elements).

Hardware exploits temporal locality by keeping recently used items in the fast level and spatial locality by moving data in blocks (lines) rather than single words.

Hit and miss. A hit is an access satisfied by the level being examined; a miss is one that must go to the next level. Hit ratio hh = hits / accesses, miss ratio =1−h= 1 - h. The hit time is the time to access the level on a hit and the miss penalty is the extra time to fetch the block from the next level.

Average memory access time (AMAT) for one cache in front of memory:

AMAT=hit time+miss rate×miss penalty.\text{AMAT} = \text{hit time} + \text{miss rate} \times \text{miss penalty} .

Two levels, two ways of counting. Let t1t_1 and t2t_2 be the access times of level 1 and level 2 and h1h_1 the hit ratio of level 1.

  • Hierarchical (sequential) access: level 2 is accessed only after level 1 has missed, so a miss costs t1+t2t_1 + t_2: T=h1t1+(1−h1)(t1+t2)=t1+(1−h1) t2T = h_1 t_1 + (1 - h_1)(t_1 + t_2) = t_1 + (1 - h_1)\,t_2.
  • Simultaneous (parallel) access: both levels are started together and level 2's result is used on a miss: T=h1t1+(1−h1) t2T = h_1 t_1 + (1 - h_1)\,t_2.

The first form equals AMAT with hit time t1t_1 and miss penalty t2t_2. A GATE question states which of the two to use; "simultaneous" and "hierarchical" are the usual words.

Several levels. With local miss rates m1m_1 (of L1 accesses) and m2m_2 (of L2 accesses):

AMAT=tL1+m1 (tL2+m2 tmem).\text{AMAT} = t_{L1} + m_1\,(t_{L2} + m_2\,t_{\text{mem}}) .

The global miss rate of L2 (misses per CPU access) is m1m2m_1 m_2.

Inclusion. In an inclusive hierarchy everything in a level is also in the level below it. The bottom level (disk, via virtual memory) holds everything.

What a hierarchy buys. The average access time approaches that of the top level when hit ratios are high, while the cost per bit approaches that of the bottom level.

How it works

The animation is a direct-mapped cache serving 14 reads. The cache counts cycles: 1 for a hit, 11 for a miss (hit time 1 plus miss penalty 10). The running "avg cycles per access" is the measured AMAT. At the end it is 84/14=684/14 = 6, which equals 1+0.5×101 + 0.5 \times 10: hit time plus miss rate (7 misses in 14) times the penalty.

Direct-mapped cache: 8 lines of 4 bytes: start

Cache simulated, not typed

8 sets x 1 way, direct mapped. Hit time 1, miss penalty 10 cycles.

hits
0
misses
0
hit rate
-
avg cycles per access
-
memory reads
0
memory writes
0
setway 0
0empty
1empty
2empty
3empty
4empty
5empty
6empty
7empty

Step 1

A direct-mapped cache with 8 lines of 4 bytes and 8-bit addresses: 2 offset bits, 3 index bits, 3 tag bits. Each memory block can live in exactly one line, chosen by the index bits.

1 / 15
computed by script: exact simulation of the cache, one access per step. python3 scripts/gen-coa-mem-traces.py

Worked examples

EX 1Every formula in a program

Easy
#include <stdio.h>
 
/* average memory access time for one cache level and for a hierarchy */
static double amat(double hit_time, double miss_rate, double miss_penalty) { return hit_time + miss_rate * miss_penalty; }
 
int main(void) {
    /* the 14 accesses of the direct-mapped animation: 7 hits, 7 misses, hit time 1, miss penalty 10 */
    int hits = 7, accesses = 14;
    double m = 1.0 - (double)hits / accesses;
    printf("measured: %d cycles in total = %.2f per access; formula 1 + %.2f x 10 = %.2f\n", hits * 1 + (accesses - hits) * 11, (hits * 1.0 + (accesses - hits) * 11.0) / accesses, m, amat(1, m, 10));
    /* two levels, hit ratio h1 = 0.9, t1 = 2, t2 = 20 */
    double h1 = 0.9, t1 = 2, t2 = 20;
    printf("hierarchical access (level 2 only after a miss): %.1f\n", h1 * t1 + (1 - h1) * (t1 + t2));
    printf("simultaneous access (both started together):     %.1f\n", h1 * t1 + (1 - h1) * t2);
    printf("hit time + miss rate x miss penalty:             %.1f\n", amat(t1, 1 - h1, t2));
    /* three levels: L1 1 ns, 5%% local miss; L2 10 ns, 10%% local miss; memory 100 ns */
    double l1 = 1, l2 = 10, mem = 100, m1 = 0.05, m2 = 0.10;
    printf("three levels: AMAT = %.2f ns, global miss rate of L2 = %.3f\n", l1 + m1 * (l2 + m2 * mem), m1 * m2);
    /* what the hierarchy buys: without the caches every access would cost 100 ns */
    printf("speed-up over main memory alone: %.1f\n", mem / (l1 + m1 * (l2 + m2 * mem)));
    /* locality in the numbers: a miss rate of 1/16 (sequential access, 16 words per block) */
    printf("sequential access, 16 words per block: miss rate %.4f, AMAT %.2f cycles with a 100-cycle penalty\n", 1.0 / 16, amat(1, 1.0 / 16, 100));
    return 0;
}
measured: 84 cycles in total = 6.00 per access; formula 1 + 0.50 x 10 = 6.00
hierarchical access (level 2 only after a miss): 4.0
simultaneous access (both started together):     3.8
hit time + miss rate x miss penalty:             4.0
three levels: AMAT = 2.00 ns, global miss rate of L2 = 0.005
speed-up over main memory alone: 50.0
sequential access, 16 words per block: miss rate 0.0625, AMAT 7.25 cycles with a 100-cycle penalty

Reading it. The first line is the animation: 84 cycles in 14 accesses, 6 per access, matching the formula. In the two-level example with h1=0.9h_1 = 0.9, t1=2t_1 = 2, t2=20t_2 = 20: hierarchical access takes 0.9×2+0.1×22=4.00.9 \times 2 + 0.1 \times 22 = 4.0; simultaneous access takes 0.9×2+0.1×20=3.80.9 \times 2 + 0.1 \times 20 = 3.8. With three levels the average is 2 ns instead of the 100 ns of main memory alone (a speed-up of 50), and the global miss rate of L2 is 0.5%.

EX 2Locality and hit ratio

Easy

A loop adds the 16 words of an array, one block per 16 words. The first access misses and loads the block; the other 15 words are hits (spatial locality). The miss rate is 1/16=6.25%1/16 = 6.25\% and with a 1-cycle hit and a 100-cycle penalty the average is 1+0.0625×100=7.251 + 0.0625 \times 100 = 7.25 cycles. If the loop is executed again with the block still in the cache, even the first access hits (temporal locality) and the average drops to 1 cycle.

EX 3Required hit ratio

Medium

A cache has t1=10t_1 = 10 ns and main memory t2=100t_2 = 100 ns. What hit ratio gives an average access time of 20 ns with hierarchical access? 20=10+(1−h) 10020 = 10 + (1 - h)\,100, so 1−h=0.11 - h = 0.1 and h=0.9h = 0.9. With simultaneous access: 20=10h+100(1−h)20 = 10h + 100(1 - h), so 90h=8090h = 80 and h=0.889h = 0.889. The hierarchical form needs the higher ratio (0.9 against 0.889), because each miss costs t1t_1 more.

EX 4Three levels

Medium

L1: 1 ns, local miss rate 5%. L2: 10 ns, local miss rate 10%. Main memory: 100 ns. AMAT=1+0.05 (10+0.10×100)=1+0.05×20=2\text{AMAT} = 1 + 0.05\,(10 + 0.10 \times 100) = 1 + 0.05 \times 20 = 2 ns. If L2 is removed and L1 misses go straight to memory the average is 1+0.05×100=61 + 0.05 \times 100 = 6 ns. L2 cuts the average from 6 to 2 ns. The global L2 miss rate is 0.05×0.10=0.5%0.05 \times 0.10 = 0.5\%, but its local miss rate is 10%.

EX 5Effective access time with a page fault

GATE level

The same formula applies one level lower: main memory as a cache for disk. If memory access takes 100 ns, a page fault service takes 8 ms and the page-fault rate is p=10−5p = 10^{-5}, the effective access time is (1−p) 100 ns+p×8 000 000 ns=99.999+80≈180(1 - p)\,100\ \text{ns} + p \times 8\,000\,000\ \text{ns} = 99.999 + 80 \approx 180 ns. Even a tiny fault rate nearly doubles the average time because the disk is so slow.

Cheat box

Cheat box

  • Locality: temporal (reuse) and spatial (neighbours). Blocks exploit spatial locality.
  • AMAT = hit time + miss rate ×\times miss penalty.
  • Two levels, hierarchical: T=h1t1+(1−h1)(t1+t2)T = h_1 t_1 + (1 - h_1)(t_1 + t_2). Simultaneous: T=h1t1+(1−h1) t2T = h_1 t_1 + (1 - h_1)\,t_2.
  • Multilevel with local rates: tL1+m1(tL2+m2tmem)t_{L1} + m_1 (t_{L2} + m_2 t_{mem}). Global L2 miss rate =m1m2= m_1 m_2.
  • Smaller means faster and more expensive per bit. Hierarchy: near-top speed at near-bottom cost.
  • Effective access time of paging: (1−p) tmem+p tfault(1-p)\,t_{mem} + p\,t_{fault}.

Traps and GATE patterns

Connections

The cache is the next topic, starting with how blocks are placed, then replacement and writes and multilevel performance. The main memory is built from chips by memory interfacing, and the disk level is in disk structure. The same formulas drive the performance equations.

PYQs

No PYQs attached to this concept yet

Previous year questions are being added section by section from your PYQ book, each with my own re-checked answer. Nothing is shown from memory.

Self-check

Question 1

A cache has a hit time of 2 ns, a miss rate of 4% and a miss penalty of 50 ns. What is the AMAT?

Show answer

2+0.04×50=42 + 0.04 \times 50 = 4 ns.

Question 2

t1=5t_1 = 5 ns, t2=50t_2 = 50 ns, h1=0.8h_1 = 0.8. Average access time with simultaneous access and with hierarchical access?

Show answer

Simultaneous: 0.8×5+0.2×50=140.8 \times 5 + 0.2 \times 50 = 14 ns. Hierarchical: 0.8×5+0.2×55=150.8 \times 5 + 0.2 \times 55 = 15 ns.

Question 3

Which kind of locality makes caches use blocks of several words?

Show answer

Spatial locality: after a word is used its neighbours are likely to be used, so the whole block is fetched.

Question 4

L1 misses 10% of CPU accesses and L2 misses 25% of the accesses it receives. What is the global miss rate of L2?

Show answer

0.10×0.25=2.5%0.10 \times 0.25 = 2.5\%.

Revision card

One-screen revision

  • Hierarchy: small and fast on top, large and cheap below; works because of temporal and spatial locality.
  • AMAT = hit time + miss rate ×\times penalty. Hierarchical: t1+(1−h)t2t_1 + (1-h)t_2; simultaneous: ht1+(1−h)t2h t_1 + (1-h)t_2.
  • Multilevel: use local miss rates; global =m1m2= m_1 m_2.

References

  • ·Hennessy and Patterson, Computer Organization and Design, Chapter 5 Large and Fast: Exploiting Memory Hierarchy (introduction, the basics of caches)
  • ·Hamacher, Vranesic, Zaky, Computer Organization, 5th ed., Chapter 8 The Memory System (memory hierarchy, cache memories, performance considerations)
  • ·Silberschatz, Galvin, Gagne, Operating System Concepts, Chapter 9/10 Main Memory and Virtual Memory (effective access time)

Verification

The average-access-time formulas and every number on the page (one level, hierarchical and simultaneous two-level access, three levels with local miss rates, the 50 times speed-up, the sequential-access miss rate) are recomputed in scripts/enum-checks/coa_mem.py and by the C program (npm run verify:c). The measured average of the animation (84 cycles for 14 accesses) is the exact simulation of the cache trace re-checked in tests/coa-mem-traces.test.ts. The relative speeds and sizes of the levels are orders of magnitude for illustration, not data for a particular machine. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand (long C programs were not re-read line by line: they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never