



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




L16 – Cache Issues 1
4/7/
Cache miss
Why the book? Lecture
notes are faster!
modified 4/6/09 14:
L16 – Cache Issues 2
6.004 – Spring 2009
4/7/
ON REFERENCE TO Mem[X]:
Look for X among cache tags...
HIT:
X = TAG(i) , for some cache line i
READ:
return DATA(i)
WRITE:
change DATA(i); Start Write to Mem(X)
MISS:
X not found in TAG of any cache line
REPLACEMENT SELECTION:
Select some line k to hold Mem[X] (Allocation)
READ:
Read Mem[X] Set TAG(k)=X, DATA(K)=Mem[X]
WRITE:
Start Write to Mem(X) Set TAG(k)=X, DATA(K)= new Mem[X]
MAIN
MEMORY
(
!
)
Tag
Data
Mem[A]Mem[B]
L16 – Cache Issues 3
4/7/
Parallel Searching (expensive) vs
Constraints on which addresses can be stored where
OK, we’ve missed. Gotta add this new address/value pair to thecache. What do we kick out?
Least Recently Used:
discard the one we haven’t used the longest.
Plausible alternatives, (e.g. random replacement.
Amortizing cost of tag over multiple words of data
When do we write cache contents to main memory?
L16 – Cache Issues 4
6.004 – Spring 2009
4/7/
0 1
Fully Associative
expensive!
flexible: any address can becached in any line
Direct Mapped
cheap (ordinary SRAM)
contention: addresses compete for cache lines
L16 – Cache Issues 5
4/7/
Loop B:
Pgm at
1024,dataat2048:
… but not here!
Loop A:
Pgm at
1024,dataat 37:
Works GREAThere…
Direct-Mapped Cache Contention
Assume 1024-line direct-
mapped cache, 1 word/line.Consider tight loop, atsteady state: (assume WORD, not BYTE,
addressing)
Memory Address
Cache
Line
Hit/ Miss
We need
some
associativity,
But not
full
associativity…
L16 – Cache Issues 6
6.004 – Spring 2009
4/7/
Fully-assoc. vs. Direct-mapped
N tag comparators, registers used
for tag/data storage ($$$) •
Location A might be cached in any one
of the N cache lines; no restrictions! •
Replacement strategy (e.g., LRU)used to pick which line to use whenloading new word(s) into cache
PROBLEM: Cost!
1 tag comparator, SRAM used for tag/data storage ($)
Location A is cached in a specific line
of the cache determined by itsaddress; address “collisions” possible •
Replacement strategy not needed:
each word can only be cached in onespecific cache line •
PROBLEM: Contention!
L16 – Cache Issues 7
4/7/
Cost vs Contention
... so lets build HUGE direct-mapped caches, using cheap SRAM!
independent “hot spots” --
Instruction fetches vs stack frame vs data structures, etc
Ability to simultaneously cache a few (2? 4? 8?) hot spots eliminates most collisions
... so lets build caches that allow each location to be
stored in some restricted set of cache lines,rather than in exactly one (direct mapped) orevery line (fully associative).
Insight: an N-way
set-associative
cache affords modest parallelism
parallel lookup (associativity): restricted to small set of N lines
modest parallelism deals with most contention at modest cost
can implement using N direct-mapped caches, running in parallel
L16 – Cache Issues 8
6.004 – Spring 2009
4/7/
N-way Set-Associative Cache
k
HIT
DATA TO CPU
INCOMING ADDRESS
t
0
MEM DATA
N direct-mapped caches, each with 2
t
lines
set Simultaneously addressed line in each subcache constitutes a
L16 – Cache Issues 13
6.004 – Spring 2009
4/7/
Cache Simulation
Addr
Line#
Miss?
100
0
M
1000
1
M
101
2
M
102
3
M
100
0
1001
1
M
101
2
102
3
100
0
1002
1
M
101
2
102
3
100
0
1003
1
M
101
2
102
3
4-line Fully-associative/LRU
1/4 miss
Addr
Line#
Miss?
100
0
M
1000
0
M
101
1
M
102
2
M
100
0
M
1001
1
M
101
1
M
102
2
100
0
1002
2
M
101
1
102
2
M
100
0
1003
3
M
101
1
102
2
4-line Direct-mapped
7/16 miss
Compulsory
Misses
Collision Collision
miss
L16 – Cache Issues 14
6.004 – Spring 2009
4/7/
Associativity: Full vs 2-way
Addr
Line#
Miss?
100
0
M
1000
1
M
101
2
M
102
3
M
100
0
1001
4
M
101
2
102
3
100
0
1002
5
M
101
2
102
3
100
0
1003
6
M
101
2
102
3
8-line Fully-associative, LRU
1/4 miss
Addr
Set#/N
Miss?
100
0,
M
1000
0,
M
101
1,
M
102
2,
M
100
0,
1001
1,
M
101
1,
102
2,
100
0,
1002
2,
M
101
1,
102
2,
100
0,
1003
3,
M
101
1,
102
2,
2-way, 8-line total, LRU
1/4 miss
L16 – Cache Issues 15
6.004 – Spring 2009
4/7/
8 6 4 2 0
14 12 10
1k
2k
4k
8k
16k
32k
64k
128k
1-way2-way4-way8-wayfully assoc.
Associativity vs. miss rate
Miss rate
Cache size (bytes)
Associativity
H&P: Figure 5.
8-way is (almost) as effective as fully-associative
rule of thumb: N-line direct-mapped == N/2-line 2-way set assoc.
(direct-mapped)
L16 – Cache Issues 16
6.004 – Spring 2009
4/7/
Devil’s Advocacy Games
Your company uses the
cheaper FIFO cache,the competition usesLRU. Can you devisea benchmark to makeyour cache lookbetter?
Assume 0x100 sets,
2-way…
Set 0 tags:
Adr
Set, #
Set, #
A carefully-designed benchmark can make
either
look better…
Pessimal case: next adr referenced is the one just replaced!
Random
replacement makes this game harder…
L16 – Cache Issues 17
4/7/
Increasing Block Size
More Data/Tag
A
31:
Mem[A]
Mem[A+4]
Mem[A+8]
Mem[A+12]
=?
[3:2] [31:4]
32
ADDR
DATAHIT
B
B
B
TAG
D
D
D
D
4 x 32 = 128 bits
28 bits
Overhead < ¼ bit of
Tag per bit of data
L16 – Cache Issues 18
6.004 – Spring 2009
4/7/
4-word block, DM Cache
=?
[3:2] [31:8]
32
ADDR
DATAHIT
TAG
D
D
D
D
[7:4]
Use ordinary(fast) staticRAM for tag anddata storage
Only one comparator for entire cache!
0 15
16 cache lines
4 bit index
0x
M[0x1230]
M[0x1234]
M[0x1238]
M[0x123C]
0x
M[0x1240]
M[0x1244]
M[0x1248]
M[0x124C]
24-bit Tag!
L16 – Cache Issues 19
4/7/
Valid bits
V 0010010
MAIN
MEMORY
CPU
A
Mem[A]
B
Mem[B]
TAG
DATA
Problem:
Ignoring cache lines that don't contain anything of value... e.g., on
start-up
“Back door” changes to memory (eg loading program from disk)
Solution:
Extend each TAG with VALID bit.
Valid bit must be set for cache line to HIT.
At power-up / reset : clear all valid bits
Set valid bit when cache line is first
replaced.
Cache Control Feature:
Flush
cache by clearing all valid bits, Under
program/external control.
L16 – Cache Issues 20
6.004 – Spring 2009
4/7/
Handling of WRITES
Observation: Most (90+%) of memory accesses are
READs
. How should we
handle writes? Issues:
Write-through: CPU writes are cached, but also written to main memory
(stalling the CPU until write is completed). Memory always holds “thetruth”.
Write-behind: CPU writes are cached; writes to main memory may be
buffered,
perhaps pipelined. CPU keeps executing while writes are
completed (in order) in the background.
Write-back: CPU writes are cached, but not immediately written to main
memory. Memory contents can be “stale”.
Our cache thus far uses write-through.Can we improve write performance?