Download Cache Writing and Performance: Understanding Cache Consistency and Write Strategies and more Slides Computer Science in PDF only on Docsity!
CSE 410Computer Systems
L
t
C
h
W it
d P
f
Lecture 13 – Cache Writes and Performance
Cache Writing & PerformanceCache
Writing & Performance
What’s left?
– Writing to caches: keeping memory consistent & write-
allocationallocation.
– We’ll also investigate some main memory
organizations that can help increase memory system
g
p
y
y
performance.
Later, we’ll talk about Virtual Memory, where memory istreated like a cache of the disktreated like a cache of the disk.
Writing to a cacheWriting
to a cache
Writing to a cache raises several additional issues.
First, let’s assume that the address we want to write to is alreadyloaded in the cache. We’ll assume a simple direct-mapped cache.
Index
Tag
Data
V
Address
Data
g
If we write a new value to that address, we can store the new data inthe cache, and avoid an expensive main memory access.
Mem
[
]
Index
Tag
Data
V
Address
Data
Mem[214] = 21763
Inconsistent memoryInconsistent
memory
But now the cache and memory contain different,
y
inconsistent data!
How can we ensure that subsequent loads will returnth
i ht
l
the right value?
This is also problematic if other devices are sharingthe main memory, as in a multiprocessor system.the main memory, as in a multiprocessor system.
Index
Tag
Data
V
Address
Data
Write
-back caches
Write back caches
In a write-back cache, the memory is not updated until the cache blockneeds to be replaced (
e.g.
, when loading data into a full cache set).
For example, we might write some data to the cache at first, leaving itinconsistent with the main memory as shown before.
- The cache block is marked “dirty” to indicate this inconsistency
Index
Tag
Data
Dirty
Address
Data
Mem[214] = 21763
V
Subsequent reads to the same memory address will be serviced by thecache, which contains the correct, updated data.
p
Finishing the write backFinishing
the write back
We don’t need to store the new value back to main memory unless the cachebl
k
t
l
d
block gets replaced.
For example, on a read from Mem[142], which maps to the same cache block,the modified cache contents will first be written to main memory.
Index
Tag
Data
Dirty
Address
Data 21763
V
Only then can the cache block be replaced with data from address 142.
Index
Tag
Data
Address
Data 21763
Dirty
V
Write
-back cache discussion
Write back cache discussion•
Each block in a write-back cache needs a dirty bit to
y
indicate whether or not it must be saved to mainmemory before being replaced—otherwise we mightperform unnecessary writebacks.p
y
Notice the penalty for the main memory access willnot be applied until the execution of some subsequent
instruction following the write
subsequent
instruction following the write.
– In our example, the write to Mem[214] affected
only the cache.B t th
l
d f
M
[142]
lt d i
t
– But the load from Mem[142] resulted in
t
wo
memory accesses: one to save data to address214, and one to load data from address 142.
Write missesWrite
misses
A second scenario is if we try to write to an address that is
t
l
d
t i
d i
th
h
thi
i
ll d
it
not already contained in the cache; this is called a writemiss.
Let’s say we want to store 21763 into Mem[1101 0110] butwe find that address is not currently in the cachewe find that address is not currently in the cache.
Index
Tag
Data
V
Address
Data
When we update Mem[1101 0110], should we
also
load it
into the cache?
Allocate on writeAllocate
on write
An allocate on write strategy would instead load the newlywritten data into the cache.
Mem[214] = 21763
Index
Tag
Data
V
Address
Data
[
]
If that data is needed again soon, it will be available in thecache.
Basic main memory designBasic
main memory design
There are some ways the main memory can be organized to reducemiss penalties and help with cachingmiss penalties and help with caching.
For some concrete examples, let’s assume the followingthree steps are taken when a cache needs to load datafrom the main memory.
1
It t k
1
l
t
d
dd
t
th
RAM
CPU
1
. It takes 1 cycle to send an address to the RAM. 2. There is a 15-cycle latency for each RAM access.3. It takes 1 cycle to return data from the RAM. -
In the setup shown here the buses from the CPU to the
Cache
In
the setup shown here, the buses from the CPU to the
cache and from the cache to RAM are all one word wide.
If the cache has one-word blocks, then filling a blockfrom RAM (
i.e.
, the miss penalty) would take 17 cycles.
Main
1 + 15 + 1 = 17 clock cycles
The cache controller has to send the desired address tothe RAM, wait and receive the data.
Main
Memory
the
RAM, wait and receive the data.
A wider memoryA
wider memory
A simple way to decrease themiss penalty is to widen the
CPU
p
y
memory and its interface tothe cache, so we can readmultiple words from RAM inone shot
CPU
one shot.
If we could read four wordsfrom the memory at once, afour-word cache load would
Cache
need just 17 cycles.
1 + 15 + 1 = 17 cycles
Main
Memory
The disadvantage is the costof the wider buses—eachadditional bit of memory widthrequires another connection torequires another connection tothe cache.
An interleaved memoryAn
interleaved memory
Another approach is to interleavethe memory, or split it into “banks”
CPU
the memory, or split it into
banks
that can be accessed individually.
The main benefit is overlappingthe latencies of accessing eachword
CPU
word.
For example, if our main memoryhas four banks, each one bytewide, then we could load fourbytes into a cache block in just 20
Cache
bytes
into a cache block in just 20
cycles.
1 + 15 + (4 x 1) = 20 cycles
Main Memory
Our buses are still one byte widehere, so four cycles are needed totransfer data to the caches.
This is cheaper than implementing
Bank 0
Bank 1
Bank 2
Bank 3
p
p
g
a four-byte bus, but not too muchslower.
SummarySummary •
Writing to a cache poses a couple of interesting issues.
Write
through
and
write
back
policies keep the cache consistent with main
Write
through
and
write-back policies keep the cache consistent with main
memory in different ways for write hits.
Write-around and allocate-on-write are two strategies to handle write misses,differing in whether updated data is loaded into the cache.
Memory system performance depends upon the cache hit time miss rate and
Memory system performance depends upon the cache hit time, miss rate andmiss penalty, as well as the actual program being executed.
We can use these numbers to find the average memory access time.
We can also revise our CPU time formula to include stall cycles.
AMAT = Hit time + (Miss rate x Miss penalty)
Memory stall cycles = Memory accesses x miss rate x miss penalty
CPU time = (CPU execution cycles + Memory stall cycles) x Cycle time
The organization of a memory system affects its performance.
The cache size, block size, and associativity affect the miss rate.
We can organize the main memory to help reduce miss penalties. Forexample interleaved memory supports pipelined data accessesexample, interleaved memory supports pipelined data accesses.