CH10 - Memory Hierarchy
CH10 - Memory Hierarchy
Exploiting Memory
Hierarchy
Computer Organization
502044
Acknowledgement
This slide show is intended for use in class, and is not a complete document.
Students need to refer to the book to read more lessons and exercises. Students
have the right to download and store lecture slides for reference purposes; Do not
redistribute or use for purposes outside of the course.
📧 trantrungtin.tdtu.edu.vn
2
Principle of Locality
● Programs access a small proportion of their address space at any time
● Temporal locality
○ Items accessed recently are likely to be accessed again soon
○ e.g., instructions in a loop, induction variables
● Spatial locality
○ Items near those accessed recently are likely to be accessed soon
○ E.g., sequential instruction access, array data
3
Taking Advantage of Locality
● Memory hierarchy
● Store everything on disk
● Copy recently accessed (and nearby) items from disk to smaller DRAM
memory
○ Main memory
● Copy more recently accessed (and nearby) items from DRAM to smaller
SRAM memory
○ Cache memory attached to CPU
4
Memory Hierarchy Levels
● Block (aka line): unit of copying
○ May be multiple words
● If accessed data is present in upper level
○ Hit: access satisfied by upper level
■ Hit ratio: hits/accesses
● If accessed data is absent
○ Miss: block copied from lower level
■ Time taken: miss penalty
■ Miss ratio: misses/accesses
= 1 – hit ratio
○ Then accessed data supplied from upper level
5
Memory Technology
6
DRAM Technology
● Data stored as a charge in a capacitor
○ Single transistor used to access the charge
○ Must periodically be refreshed
■ Read contents and write back
■ Performed on a DRAM “row”
7
Advanced DRAM Organization
● Bits in a DRAM are organized as a rectangular array
○ DRAM accesses an entire row
○ Burst mode: supply successive words from a row with reduced latency
● Double data rate (DDR) DRAM
○ Transfer on rising and falling clock edges
● Quad data rate (QDR) DRAM
○ Separate DDR inputs and outputs
8
DRAM Generations
Year Capacity $/GB
1980 64Kbit $1500000
1983 256Kbit $500000
1985 1Mbit $200000
1989 4Mbit $50000
1992 16Mbit $15000
1996 64Mbit $10000
1998 128Mbit $4000
2000 256Mbit $1000
2004 512Mbit $250
2007 1Gbit $50
9
DRAM Performance Factors
● Row buffer
○ Allows several words to be read and refreshed in parallel
● Synchronous DRAM
○ Allows for consecutive accesses in bursts without needing to send each address
○ Improves bandwidth
● DRAM banking
○ Allows simultaneous access to multiple DRAMs
○ Improves bandwidth
10
Increasing Memory Bandwidth
12
Flash Types
● NOR flash: bit cell like a NOR gate
○ Random read/write access
○ Used for instruction memory in embedded systems
● NAND flash: bit cell like a NAND gate
○ Denser (bits/area), but block-at-a-time access
○ Cheaper per GB
○ Used for USB keys, media storage, …
● Flash bits wears out after 1000’s of accesses
○ Not suitable for direct RAM or disk replacement
○ Wear leveling: remap data to less used blocks
13
Disk Storage
14
Disk Sectors and Access
● Each sector records
○ Sector ID
○ Data (512 bytes, 4096 bytes proposed)
○ Error correcting code (ECC)
■ Used to hide defects and recording errors
○ Synchronization fields and gaps
● Access to a sector involves
○ Queuing delay if other accesses are pending
○ Seek: move the heads
○ Rotational latency
○ Data transfer
○ Controller overhead
15
Disk Access Example
● Given
○ 512B sector, 15,000rpm, 4ms average seek time, 100MB/s transfer rate, 0.2ms controller
overhead, idle disk
● Average read time
○ 4ms seek time
+ ½ / (15,000/60) = 2ms rotational latency
+ 512 / 100MB/s = 0.005ms transfer time
+ 0.2ms controller delay
= 6.2ms
● If actual average seek time is 1ms
○ Average read time = 3.2ms
16
Disk Performance Issues
● Manufacturers quote average seek time
○ Based on all possible seeks
○ Locality and OS scheduling lead to smaller actual average seek times
● Smart disk controller allocate physical sectors on disk
○ Present logical sector interface to host
○ SCSI, ATA, SATA
● Disk drives include caches
○ Prefetch sectors in anticipation of access
○ Avoid seek and rotational delay
17
Cache Memory
■ How do we know if
the data is
present?
■ Where do we look?
18
Direct Mapped Cache
● Location determined by address
● Direct mapped: only one choice
○ (Block address) modulo (#Blocks in cache)
■ #Blocks is a
power of 2
■ Use low-
order
address bits
19
Tags and Valid Bits
● How do we know which particular block is stored in a cache location?
○ Store block address as well as the data
○ Actually, only need the high-order bits
○ Called the tag
● What if there is no data in a location?
○ Valid bit: 1 = present, 0 = not present
○ Initially 0
20
Cache Example
● 8-blocks, 1 word/block, direct mapped
● Initial state
27
Example: Larger Block Size
● 64 blocks, 16 bytes/block
○ To what block number does address 1200 map?
● Block address = ⎣1200/16⎦ = 75
● Block number = 75 modulo 64 = 11
31 10 9 4 3 0
Offse
Tag Index
22 bits 6 bits
t
4 bits
28
Block Size Considerations
● Larger blocks should reduce miss rate
○ Due to spatial locality
● But in a fixed-sized cache
○ Larger blocks ⇒ fewer of them
■ More competition ⇒ increased miss rate
○ Larger blocks ⇒ pollution
● Larger miss penalty
○ Can override benefit of reduced miss rate
○ Early restart and critical-word-first can help
29
Cache Misses
● On cache hit, CPU proceeds normally
● On cache miss
○ Stall the CPU pipeline
○ Fetch block from next level of hierarchy
○ Instruction cache miss
■ Restart instruction fetch
○ Data cache miss
■ Complete data access
30
Write-Through
● On data-write hit, could just update the block in cache
○ But then cache and memory would be inconsistent
● Write through: also update memory
● But makes writes take longer
○ e.g., if base CPI = 1, 10% of instructions are stores, write to memory takes 100 cycles
■ Effective CPI = 1 + 0.1×100 = 11
● Solution: write buffer
○ Holds data waiting to be written to memory
○ CPU continues immediately
■ Only stalls on write if write buffer is already full
31
Write-Back
● Alternative: On data-write hit, just update the block in cache
○ Keep track of whether each block is dirty
● When a dirty block is replaced
○ Write it back to memory
○ Can use a write buffer to allow replacing block to be read first
32
Write Allocation
● What should happen on a write miss?
● Alternatives for write-through
○ Allocate on miss: fetch the block
○ Write around: don’t fetch the block
■ Since programs often write a whole block before reading it (e.g., initialization)
● For write-back
○ Usually fetch the block
33
Example: Intrinsity FastMATH
● Embedded MIPS processor
○ 12-stage pipeline
○ Instruction and data access on each cycle
● Split cache: separate I-cache and D-cache
○ Each 16KB: 256 blocks × 16 words/block
○ D-cache: write-through or write-back
● SPEC2000 miss rates
○ I-cache: 0.4%
○ D-cache: 11.4%
○ Weighted average: 3.2%
34
Example: Intrinsity FastMATH
35
Main Memory Supporting Caches
● Use DRAMs for main memory
○ Fixed width (e.g., 1 word)
○ Connected by fixed-width clocked bus
■ Bus clock is typically slower than CPU clock
● Example cache block read
○ 1 bus cycle for address transfer
○ 15 bus cycles per DRAM access
○ 1 bus cycle per data transfer
● For 4-word block, 1-word-wide DRAM
○ Miss penalty = 1 + 4×15 + 4×1 = 65 bus cycles
○ Bandwidth = 16 bytes / 65 cycles = 0.25 B/cycle
36
Measuring Cache Performance
37
Cache Performance Example
● Given
○ I-cache miss rate = 2%
○ D-cache miss rate = 4%
○ Miss penalty = 100 cycles
○ Base CPI (ideal cache) = 2
○ Load & stores are 36% of instructions
● Miss cycles per instruction
○ I-cache: 0.02 × 100 = 2
○ D-cache: 0.36 × 0.04 × 100 = 1.44
● Actual CPI = 2 + 2 + 1.44 = 5.44
○ Ideal CPU is 5.44/2 =2.72 times faster
38
Average Access Time
● Hit time is also important for performance
● Average memory access time (AMAT)
○ AMAT = Hit time + Miss rate × Miss penalty
● Example
○ CPU with 1ns clock, hit time = 1 cycle, miss penalty = 20 cycles, I-cache miss rate = 5%
○ AMAT = 1 + 0.05 × 20 = 2ns
■ 2 cycles per instruction
39
Performance Summary
● When CPU performance increased
○ Miss penalty becomes more significant
● Decreasing base CPI
○ Greater proportion of time spent on memory stalls
● Increasing clock rate
○ Memory stalls account for more CPU cycles
● Can’t neglect cache behavior when evaluating system performance
40
Associative Caches
● Fully associative
○ Allow a given block to go in any cache entry
○ Requires all entries to be searched at once
○ Comparator per entry (expensive)
● n-way set associative
○ Each set contains n entries
○ Block number determines which set
■ (Block number) modulo (#Sets in cache)
○ Search all entries in a given set at once
○ n comparators (less expensive)
41
Associative Cache Example
42
Spectrum of Associativity
● For a cache with 8 entries
43
Associativity Example
● Compare 4-block caches
○ Direct mapped, 2-way set associative,
fully associative
○ Block access sequence: 0, 8, 0, 6, 8
● Direct mapped
44
Associativity Example
● 2-way set associative
Block Cache Hit/miss Cache content after access
address index Set 0 Set 1
0 0 miss Mem[0]
8 0 miss Mem[0] Mem[8]
0 0 hit Mem[0] Mem[8]
6 0 miss Mem[0] Mem[6]
8 0 miss Mem[8] Mem[6]
■ Fully associative
Block Hit/miss Cache content after access
address
0 miss Mem[0]
8 miss Mem[0] Mem[8]
0 hit Mem[0] Mem[8]
6 miss Mem[0] Mem[8] Mem[6]
8 hit Mem[0] Mem[8] Mem[6]
45
How Much Associativity
● Increased associativity decreases miss rate
○ But with diminishing returns
● Simulation of a system with 64KB
D-cache, 16-word blocks, SPEC2000
○ 1-way: 10.3%
○ 2-way: 8.6%
○ 4-way: 8.3%
○ 8-way: 8.1%
46
Set Associative Cache Organization
47
Replacement Policy
● Direct mapped: no choice
● Set associative
○ Prefer non-valid entry, if there is one
○ Otherwise, choose among entries in the set
● Least-recently used (LRU)
○ Choose the one unused for the longest time
■ Simple for 2-way, manageable for 4-way, too hard beyond that
● Random
○ Gives approximately the same performance as LRU for high associativity
48
Multilevel Caches
● Primary cache attached to CPU
○ Small, but fast
● Level-2 cache services misses from primary cache
○ Larger, slower, but still faster than main memory
● Main memory services L-2 cache misses
● Some high-end systems include L-3 cache
49
Multilevel Cache Example
● Given
○ CPU base CPI = 1, clock rate = 4GHz
○ Miss rate/instruction = 2%
○ Main memory access time = 100ns
● With just primary cache
○ Miss penalty = 100ns/0.25ns = 400 cycles
○ Effective CPI = 1 + 0.02 × 400 = 9
50
Example (cont.)
● Now add L-2 cache
○ Access time = 5ns
○ Global miss rate to main memory = 0.5%
● Primary miss with L-2 hit
○ Penalty = 5ns/0.25ns = 20 cycles
● Primary miss with L-2 miss
○ Extra penalty = 500 cycles
● CPI = 1 + 0.02 × 20 + 0.005 × 400 = 3.4
● Performance ratio = 9/3.4 = 2.6
51
Multilevel Cache Considerations
● Primary cache
○ Focus on minimal hit time
● L-2 cache
○ Focus on low miss rate to avoid main memory access
○ Hit time has less overall impact
● Results
○ L-1 cache usually smaller than a single cache
○ L-1 block size smaller than L-2 block size
52
Interactions with Advanced CPUs
● Out-of-order CPUs can execute instructions during cache miss
○ Pending store stays in load/store unit
○ Dependent instructions wait in reservation stations
■ Independent instructions continue
● Effect of miss depends on program data flow
○ Much harder to analyse
○ Use system simulation
53
Interactions with Software
● Misses depend on memory access patterns
○ Algorithm behavior
○ Compiler optimization for memory access
54
Software Optimization via Blocking
● Goal: maximize accesses to data before it is replaced
● Consider inner loops of DGEMM:
55
DGEMM Access Pattern
● C, A, and B arrays
older accesses
new accesses
56
Cache Blocked DGEMM
● 1 #define BLOCKSIZE 32
● 2 void do_block (int n, int si, int sj, int sk, double *A, double
● 3 *B, double *C)
● 4{
● 5 for (int i = si; i < si+BLOCKSIZE; ++i)
● 6 for (int j = sj; j < sj+BLOCKSIZE; ++j)
● 7 {
● 8 double cij = C[i+j*n];/* cij = C[i][j] */
● 9 for( int k = sk; k < sk+BLOCKSIZE; k++ )
● 10 cij += A[i+k*n] * B[k+j*n];/* cij+=A[i][k]*B[k][j] */
● 11 C[i+j*n] = cij;/* C[i][j] = cij */
● 12 }
● 13 }
● 14 void dgemm (int n, double* A, double* B, double* C)
● 15 {
● 16 for ( int sj = 0; sj < n; sj += BLOCKSIZE )
● 17 for ( int si = 0; si < n; si += BLOCKSIZE )
● 18 for ( int sk = 0; sk < n; sk += BLOCKSIZE )
● 19 do_block(n, si, sj, sk, A, B, C);
● 20 }
57
Blocked DGEMM Access Pattern
Unoptimized Blocked
58
Dependability
Service interruption
Deviation from
specified service
59
Dependability Measures
● Reliability: mean time to failure (MTTF)
● Service interruption: mean time to repair (MTTR)
● Mean time between failures
○ MTBF = MTTF + MTTR
● Availability = MTTF / (MTTF + MTTR)
● Improving Availability
○ Increase MTTF: fault avoidance, fault tolerance, fault forecasting
○ Reduce MTTR: improved tools and processes for diagnosis and repair
60
The Hamming SEC Code
● Hamming distance
○ Number of bits that are different between two bit patterns
● Minimum distance = 2 provides single bit error detection
○ E.g. parity code
● Minimum distance = 3 provides single error correction, 2 bit error
detection
61
Encoding SEC
● To calculate Hamming code:
○ Number bits from 1 on the left
○ All bit positions that are a power 2 are parity bits
○ Each parity bit checks certain data bits:
62
Decoding SEC
● Value of parity bits indicates which bits are in error
○ Use numbering from encoding procedure
○ E.g.
■ Parity bits = 0000 indicates no error
■ Parity bits = 1010 indicates bit 10 was flipped
63
SEC/DEC Code
● Add an additional parity bit for the whole word (pn)
● Make Hamming distance = 4
● Decoding:
○ Let H = SEC parity bits
■ H even, pn even, no error
■ H odd, pn odd, correctable single bit error
■ H even, pn odd, error in pn bit
■ H odd, pn even, double error occurred
● Note: ECC DRAM uses SEC/DEC with 8 bits protecting each 64 bits
64
Virtual Machines
65
Virtual Machine Monitor
● Maps virtual resources to physical resources
○ Memory, I/O devices, CPUs
● Guest code runs on native machine in user mode
○ Traps to VMM on privileged instructions and access to protected resources
● Guest OS may be different from host OS
● VMM handles real I/O devices
○ Emulates generic virtual I/O devices for guest
66
Example: Timer Virtualization
● In native machine, on timer interrupt
○ OS suspends current process, handles interrupt, selects and resumes next process
● With Virtual Machine Monitor
○ VMM suspends current VM, handles interrupt, selects and resumes next VM
● If a VM requires timer interrupts
○ VMM emulates a virtual timer
○ Emulates interrupt for VM when physical timer interrupt occurs
67
Instruction Set Support
● User and System modes
● Privileged instructions only available in system mode
○ Trap to system if executed in user mode
● All physical resources only accessible using privileged instructions
○ Including page tables, interrupt controls, I/O registers
● Renaissance of virtualization support
○ Current ISAs (e.g., x86) adapting
68
Virtual Memory
69
Address Translation
● Fixed-size pages (e.g., 4K)
70
Page Fault Penalty
● On page fault, the page must be fetched from disk
○ Takes millions of clock cycles
○ Handled by OS code
● Try to minimize page fault rate
○ Fully associative placement
○ Smart replacement algorithms
71
Page Tables
● Stores placement information
○ Array of page table entries, indexed by virtual page number
○ Page table register in CPU points to page table in physical memory
● If page is present in memory
○ PTE stores the physical page number
○ Plus other status bits (referenced, dirty, …)
● If page is not present
○ PTE can refer to location in swap space on disk
72
Translation Using a Page Table
73
Mapping Pages to Storage
74
Replacement and Writes
● To reduce page fault rate, prefer least-recently used (LRU) replacement
○ Reference bit (aka use bit) in PTE set to 1 on access to page
○ Periodically cleared to 0 by OS
○ A page with reference bit = 0 has not been used recently
● Disk writes take millions of cycles
○ Block at once, not individual locations
○ Write through is impractical
○ Use write-back
○ Dirty bit in PTE set when page is written
75
Fast Translation Using a TLB
● Address translation would appear to require extra memory references
○ One to access the PTE
○ Then the actual memory access
● But access to page tables has good locality
○ So use a fast cache of PTEs within the CPU
○ Called a Translation Look-aside Buffer (TLB)
○ Typical: 16–512 PTEs, 0.5–1 cycle for hit, 10–100 cycles for miss, 0.01%–1% miss rate
○ Misses could be handled by hardware or software
76
Fast Translation Using a TLB
77
TLB Misses
● If page is in memory
○ Load the PTE from memory and retry
○ Could be handled in hardware
■ Can get complex for more complicated page table structures
○ Or in software
■ Raise a special exception, with optimized handler
● If page is not in memory (page fault)
○ OS handles fetching the page and updating the page table
○ Then restart the faulting instruction
78
TLB Miss Handler
● TLB miss indicates
○ Page present, but PTE not in TLB
○ Page not preset
● Must recognize TLB miss before destination register overwritten
○ Raise exception
● Handler copies PTE from memory to TLB
○ Then restarts instruction
○ If page not present, page fault will occur
79
Page Fault Handler
● Use faulting virtual address to find PTE
● Locate page on disk
● Choose page to replace
○ If dirty, write to disk first
● Read page into memory and update page table
● Make process runnable again
○ Restart from faulting instruction
80
TLB and Cache Interaction
● If cache tag uses physical
address
○ Need to translate before cache
lookup
● Alternative: use virtual
address tag
○ Complications due to aliasing
■ Different virtual
addresses for shared
physical address
81
Memory Protection
● Different tasks can share parts of their virtual address spaces
○ But need to protect against errant access
○ Requires OS assistance
● Hardware support for OS protection
○ Privileged supervisor mode (aka kernel mode)
○ Privileged instructions
○ Page tables and other state information only accessible in supervisor mode
○ System call exception (e.g., syscall in MIPS)
82
The Memory Hierarchy
83
Block Placement
● Determined by associativity
○ Direct mapped (1-way associative)
■ One choice for placement
○ n-way set associative
■ n choices within a set
○ Fully associative
■ Any location
● Higher associativity reduces miss rate
○ Increases complexity, cost, and access time
84
Finding a Block
● Hardware caches
○ Reduce comparisons to reduce cost
● Virtual memory
○ Full table lookup makes full associativity feasible
○ Benefit in reduced miss rate
85
Replacement
● Choice of entry to replace on a miss
○ Least recently used (LRU)
■ Complex and costly hardware for high associativity
○ Random
■ Close to LRU, easier to implement
● Virtual memory
○ LRU approximation with hardware support
86
Write Policy
● Write-through
○ Update both upper and lower levels
○ Simplifies replacement, but may require write buffer
● Write-back
○ Update upper level only
○ Update lower level when block is replaced
○ Need to keep more state
● Virtual memory
○ Only write-back is feasible, given disk write latency
87
Sources of Misses
● Compulsory misses (aka cold start misses)
○ First access to a block
● Capacity misses
○ Due to finite cache size
○ A replaced block is later accessed again
● Conflict misses (aka collision misses)
○ In a non-fully associative cache
○ Due to competition for entries in a set
○ Would not occur in a fully associative cache of the same total size
88
Cache Design Trade-offs
Design change Effect on miss rate Negative performance
effect
Increase cache size Decrease capacity May increase access
misses time
Increase associativity Decrease conflict May increase access
misses time
Increase block size Decrease compulsory Increases miss
misses penalty. For very
large block size, may
increase miss rate
due to pollution.
89
Cache Control
31 10 9 4 3 0
Offse
Tag Index
18 bits 10 bits
t
4 bits
90
Interface Signals
Read/Write Read/Write
Valid Valid
Address 32 Address 32
Ready Ready
91
Finite State Machines
● Use an FSM to sequence control steps
● Set of states, transition on each clock edge
○ State values are binary encoded
○ Current state stored in a register
○ Next state
= fn (current state,
current inputs)
● Control output signals
= fo (current state)
92
Cache Controller FSM
Could partition
into separate
states to reduce
clock cycle time
93
Cache Coherence Problem
1 CPU A reads X 0 0
2 CPU B reads X 0 0 0
3 CPU A writes 1 to X 1 0 1
94
Coherence Defined
● Informally: Reads return most recently written value
● Formally:
○ P writes X; P reads X (no intervening writes)
⇒ read returns written value
○ P1 writes X; P2 reads X (sufficiently later)
⇒ read returns written value
■ c.f. CPU B reading X after step 3 in example
○ P1 writes X, P2 writes X
⇒ all processors see writes in the same order
■ End up with the same final value for X
95
Cache Coherence Protocols
● Operations performed by caches in multiprocessors to ensure coherence
○ Migration of data to local caches
■ Reduces bandwidth for shared memory
○ Replication of read-shared data
■ Reduces contention for access
● Snooping protocols
○ Each cache monitors bus reads/writes
● Directory-based protocols
○ Caches and memory record sharing status of blocks in a directory
96
Invalidating Snooping Protocols
● Cache gets exclusive access to a block when it is to be written
○ Broadcasts an invalidate message on the bus
○ Subsequent read in another cache misses
■ Owning cache supplies updated value
98
99
100
Supporting Multiple Issue
● Both have multi-banked caches that allow multiple accesses per cycle
assuming no bank conflicts
● Core i7 cache optimizations
○ Return requested word first
○ Non-blocking cache
■ Hit under miss
■ Miss under miss
○ Data prefetching
101
DGEMM
102
Pitfalls
103
Pitfalls
● In multiprocessor with shared L2 or L3 cache
○ Less associativity than cores results in conflict misses
○ More cores ⇒ need to increase associativity
● Using AMAT to evaluate performance of out-of-order processors
○ Ignores effect of non-blocked accesses
○ Instead, evaluate performance by simulation
104
Pitfalls
● Extending address range using segments
○ E.g., Intel 80286
○ But a segment is not always big enough
○ Makes address arithmetic complicated
● Implementing a VMM on an ISA not designed for virtualization
○ E.g., non-privileged instructions accessing hardware resources
○ Either extend ISA, or require guest OS not to use problematic instructions
105
Concluding Remarks
106