Introduction

No single memory technology is at once fast, large and cheap. Static RAM is fast but costly per bit, DRAM is slower but dense, and magnetic disks and flash are slower again but cheaper still. A memory hierarchy stacks these technologies so that a small, fast level sits close to the processor and holds the data in current use, while larger, slower levels hold everything else. If most accesses are served by the fast level, the whole system behaves almost as fast as the top level and almost as large and cheap as the bottom one.

The hierarchy works because programs show locality of reference. This note defines the performance measures (hit ratio, miss penalty, average memory access time), derives the two access models GATE uses (hierarchical and simultaneous), extends them to several levels, and shows how memory stalls enter CPI. How a cache is organised is the subject of Cache Memory and Cache Mapping.

Prerequisites

  • None within the notes. The note uses only averages and percentages; interleaving, which reduces the miss penalty, is explained in the memory interfacing note listed under Related topics.

Core concepts

Levels and locality

Level (top to bottom)Typical technologyManaged by
RegistersFlip-flops inside the processorCompiler
Cache (L1, L2, L3)SRAMHardware
Main memoryDRAMOperating system (with hardware for address translation)
Secondary storageMagnetic disk or flash (SSD)Operating system

Going down: capacity grows, access time grows, cost per bit falls. Data move between adjacent levels in fixed units: blocks (lines) between cache and main memory, pages between main memory and disk.

  • Temporal locality: an item referenced now is likely to be referenced again soon (loop variables, instructions of a loop body). It justifies keeping recently used data in the fast level.
  • Spatial locality: items near a referenced item are likely to be referenced soon (sequential instructions, array elements). It justifies moving a whole block, not one word.

Inclusion. In many designs every block held in a level is also held in the level below it. This is a design choice, not a law; exclusive caches exist. Unless a question says otherwise, assume the lowest level named always holds the data.

Performance measures

TermMeaning
HitThe requested word is found in the level searched
Hit ratio hhFraction of accesses that hit
Miss ratio mm1−h1 - h
Hit time tct_cTime to access the level on a hit
Miss penaltyExtra time to get the word from the level below (often to fetch the whole block)
Average memory access time (AMAT, effective access time)Expected time per access over hits and misses
Local miss rate of level iiMisses at level ii divided by accesses that reach level ii
Global miss rate of level iiMisses at level ii divided by all accesses made by the processor

Different count: local versus global miss rate. With L1 local miss rate m1m_1 and L2 local miss rate m2m_2, the global L2 miss rate is m1m2m_1 m_2. Given one, compute the other.

How it works

On every access the processor looks in the top level first. Two organisations decide what happens on a miss.

  • Hierarchical (look-through, serial) access. The next level is started only after the miss is detected. A miss costs the cache's time plus the memory's time.
  • Simultaneous (look-aside, parallel) access. Both levels start together; on a hit the slower access is abandoned. A miss costs only the memory's time.
Hierarchical and simultaneous access timelines for a hit and a miss
Figure 1. With a 10 ns cache and a 100 ns memory, a miss costs 110 ns under hierarchical access and 100 ns under simultaneous access; a hit costs 10 ns in both, so with h=0.9h = 0.9 the averages are 20 ns and 19 ns.

Figure 2 summarises the trade-off along the levels.

Memory hierarchy pyramid from registers to secondary storage
Figure 2. The memory hierarchy: registers, cache, main memory and secondary storage from top to bottom; capacity and access time grow downwards while cost per bit falls.

Algorithms and pseudocode

Text
ACCESS(address)                       // two-level, hierarchical
  if address is in cache then
    return data after t_c             // hit
  else
    fetch the block from memory       // detected after t_c
    place it in the cache (may evict a block)
    return data after t_c + t_m       // miss

For simultaneous access the miss returns after tmt_m, because the memory access began at time 0. Many questions model the miss penalty as "one memory access time"; others charge the time to transfer the whole block. Read which one is meant.

Mathematical explanation

Let tct_c be the cache access time, tmt_m the main-memory access time (or the miss penalty when the question gives a block transfer time), and hh the hit ratio.

Hierarchical access.

Tavg=h tc+(1−h)(tc+tm)=tc+(1−h) tmT_{\text{avg}} = h\,t_c + (1 - h)(t_c + t_m) = t_c + (1 - h)\,t_m

Simultaneous access.

Tavg=h tc+(1−h) tmT_{\text{avg}} = h\,t_c + (1 - h)\,t_m

The two differ by (1−h) tc(1 - h)\,t_c, which is small when hh is high.

Three levels, hierarchical. With L1 time t1t_1 and local miss rate m1m_1, L2 time t2t_2 and local miss rate m2m_2, and memory time t3t_3:

Tavg=t1+m1 (t2+m2 t3).T_{\text{avg}} = t_1 + m_1\,(t_2 + m_2\,t_3).

Three levels, simultaneous. Each access is served by the first level that holds the word, at that level's time:

Tavg=(1−m1) t1+m1(1−m2) t2+m1m2 t3.T_{\text{avg}} = (1 - m_1)\,t_1 + m_1(1 - m_2)\,t_2 + m_1 m_2\,t_3.

Memory stalls in CPI. For a processor with base CPI C0C_0 (all hits), aIa_I instruction fetches and aDa_D data accesses per instruction, miss rates mIm_I and mDm_D, and a miss penalty of PP cycles:

CPI=C0+aI mI P+aD mD P.\text{CPI} = C_0 + a_I\,m_I\,P + a_D\,m_D\,P.

Usually aI=1a_I = 1. The speedup of a perfect cache over this one is CPI/C0\text{CPI} / C_0.

Block miss penalty. If a block of ww words is fetched from a memory that needs cac_a cycles to send the address, cmc_m cycles per access and ctc_t cycles to transfer a word:

Memory organisationMiss penalty (cycles)
One word wideca+w (cm+ct)c_a + w\,(c_m + c_t)
ww words wideca+cm+ctc_a + c_m + c_t
ww interleaved one-word banksca+cm+w ctc_a + c_m + w\,c_t

Average cost per bit. Two levels of sizes S1,S2S_1, S_2 and costs per bit c1,c2c_1, c_2 cost on average (c1S1+c2S2)/(S1+S2)(c_1 S_1 + c_2 S_2)/(S_1 + S_2) per bit, close to c2c_2 when S2≫S1S_2 \gg S_1.

Disk access time (secondary storage level). Access time == seek time ++ rotational latency ++ transfer time (+(+ controller overhead)). The average rotational latency is half a revolution: at rr revolutions per minute it is 12⋅60r\frac{1}{2} \cdot \frac{60}{r} seconds. Transfer time is bytes divided by transfer rate.

Worked examples

Example 1: hierarchical versus simultaneous

Cache access 10 ns, main memory 100 ns, hit ratio 0.9.

  • Hierarchical: 10+0.1×100=2010 + 0.1 \times 100 = 20 ns.
  • Simultaneous: 0.9×10+0.1×100=190.9 \times 10 + 0.1 \times 100 = 19 ns.
  • Without a cache every access takes 100 ns, so the cache gives a speedup of 100/20=5100/20 = 5 (hierarchical).

Example 2: the hit ratio needed for a target

With the same times, what hit ratio gives an average of 15 ns under hierarchical access? 10+(1−h)×100=1510 + (1 - h) \times 100 = 15 gives 1−h=0.051 - h = 0.05, so h=0.95h = 0.95. Under simultaneous access, 10h+100(1−h)=1510h + 100(1 - h) = 15 gives h=85/90≈0.944h = 85/90 \approx 0.944.

Example 3: two cache levels

L1: 1 ns, local miss rate 10%. L2: 10 ns, local miss rate 20%. Main memory: 100 ns.

  • Hierarchical: 1+0.1×(10+0.2×100)=1+0.1×30=41 + 0.1 \times (10 + 0.2 \times 100) = 1 + 0.1 \times 30 = 4 ns.
  • Global L2 miss rate: 0.1×0.2=0.020.1 \times 0.2 = 0.02, that is 2% of all accesses go to memory.
  • Simultaneous: 0.9×1+0.08×10+0.02×100=0.9+0.8+2.0=3.70.9 \times 1 + 0.08 \times 10 + 0.02 \times 100 = 0.9 + 0.8 + 2.0 = 3.7 ns.
  • Without L2 (L1 then memory, hierarchical): 1+0.1×100=111 + 0.1 \times 100 = 11 ns, so L2 cuts the average from 11 ns to 4 ns.

Example 4: CPI with cache misses

Base CPI 1.0, one instruction fetch and 0.3 data accesses per instruction, instruction-cache miss rate 2%, data-cache miss rate 5%, miss penalty 100 cycles.

  • Instruction-miss stalls: 1×0.02×100=2.01 \times 0.02 \times 100 = 2.0 cycles per instruction.
  • Data-miss stalls: 0.3×0.05×100=1.50.3 \times 0.05 \times 100 = 1.5 cycles per instruction.
  • CPI =1.0+2.0+1.5=4.5= 1.0 + 2.0 + 1.5 = 4.5. A perfect cache would make the processor 4.54.5 times faster.
  • Halving the miss penalty to 50 cycles gives CPI =1+1+0.75=2.75= 1 + 1 + 0.75 = 2.75.

Example 5: block miss penalty

A block has 4 words. Sending the address takes 1 cycle, each DRAM access 15 cycles, each word transfer 1 cycle.

  • One-word-wide memory: 1+4×(15+1)=651 + 4 \times (15 + 1) = 65 cycles.
  • Four-word-wide memory and bus: 1+15+1=171 + 15 + 1 = 17 cycles.
  • Four interleaved banks, one-word bus: 1+15+4×1=201 + 15 + 4 \times 1 = 20 cycles.

Interleaving gets most of the benefit of a wide memory with a narrow bus (see Memory Interfacing and Organization).

Example 6: disk access

A disk spins at 7200 rpm, the average seek time is 4 ms and the transfer rate is 50×10650 \times 10^6 bytes per second. Read one 4096-byte block.

  • One revolution: 60/720060/7200 s =8.333= 8.333 ms, so the average rotational latency is 4.16674.1667 ms.
  • Transfer: 4096/(50×106)4096 / (50 \times 10^6) s =0.08192= 0.08192 ms.
  • Average access time: 4+4.1667+0.0819≈8.254 + 4.1667 + 0.0819 \approx 8.25 ms, millions of times slower than a cache hit. This gap is why a page fault is so expensive.

Important properties and observations

  • Hierarchical access always costs the cache time. On a miss the total is tc+tmt_c + t_m, not tmt_m.
  • Simultaneous access cannot be slower than hierarchical access with the same parts; the difference is (1−h) tc(1 - h)\,t_c.
  • Small miss rates matter. With a 100 ns memory and a 10 ns cache, going from 90% to 95% hits cuts the hierarchical average from 20 ns to 15 ns.
  • Miss penalty, miss rate and hit time trade off. Larger blocks can lower compulsory misses through spatial locality but raise the miss penalty and, at fixed cache capacity, can increase capacity or conflict misses; a larger cache lowers the miss rate but may raise the hit time.
  • Multi-level formulas use local miss rates inside the nested expression; the global rate of the last level is the product of the local rates.
  • Memory stalls dominate CPI when the miss penalty is large: in Example 4 the stalls are 3.5 of the 4.5 cycles.
  • Common misconceptions.
    • "AMAT =h tc+(1−h) tm= h\,t_c + (1 - h)\,t_m always." That is the simultaneous model; hierarchical access adds (1−h) tc(1 - h)\,t_c.
    • "The L2 miss rate given is out of all accesses." Usually it is the local rate; check the wording.
    • "Rotational latency is one full revolution." The average is half a revolution.

GATE examination perspective

  • ACCESS MODEL. Identify hierarchical (the default when the question says the lower level is accessed after a miss) or simultaneous (when it says the levels are accessed in parallel).
  • WHAT A MISS COSTS. One memory word, or a whole block transfer, or a block transfer plus the cache read: use the question's definition.
  • LOCAL OR GLOBAL. Convert miss rates before substituting into the nested formula.
  • READS AND WRITES. Write policies change the time of a write (see Cache Memory); combine read and write times with their fractions.
  • CPI WITH MISSES. There is normally one instruction fetch per instruction; weight its penalty by the instruction-cache miss rate. Weight data accesses by the load/store frequency and data-cache miss rate.
  • INVERSE QUESTIONS. Solve for the hit ratio or the memory time that achieves a target average.
  • DISK TIME. Seek plus half a rotation plus transfer, with consistent units.

Summary

Sources

  1. C. Hamacher, Z. Vranesic, S. Zaky, Computer Organization, 5th ed., McGraw-Hill, 2002: Chapter 5, Sections 5.4 (Speed, Size, and Cost), 5.6.1 (Interleaving), 5.6.2 (Hit Rate and Miss Penalty) and 5.9.1 (Magnetic Hard Disks).
  2. D. A. Patterson, J. L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, 5th ed. (MIPS edition), Morgan Kaufmann, 2013: Sections 5.1 (Introduction) and 5.4 (Measuring and Improving Cache Performance).
  3. W. Stallings, Computer Organization and Architecture, 10th ed., Pearson: Section 4.1 (Computer Memory System Overview), Appendix 4A (Performance Characteristics of Two-Level Memories) and Section 6.1 (Magnetic Disk).

Next steps