You are on page 1of 70

CACHE MEMORY

CHAPTER # 5 Computer Organization & Architecture


The Bottom Line
S H E H E R YAR MALI K

 How much memory?


 Capacity
 How fast memory?
 Time is money
 How much expensive?

Chapter # 5 Computer Organization & Architecture 2


Memory Hierarchy List
S H E H E R YAR MALI K

 Registers
 L1 Cache
 L2 Cache
 L3 Cache
 Main memory
 Disk cache
 Disk
 Optical
 Tape

Chapter # 5 Computer Organization & Architecture 3


So you want fast?
S H E H E R YAR MALI K

 It is possible to build a computer which uses only


static RAM
 This would be very fast
 This would need no cache
 How can you cache cache?
 This would cost a very large amount

Chapter # 5 Computer Organization & Architecture 4


Locality of Reference
S H E H E R YAR MALI K

 During the course of the execution of a program,


memory references tend to cluster
 e.g. Loops
 Spatial locality vs Temporal locality

Chapter # 5 Computer Organization & Architecture 5


Cache
S H E H E R YAR MALI K

 Small amount of fast memory


 Sits between normal main memory and CPU
 May be located on CPU chip or module

Chapter # 5 Computer Organization & Architecture 6


Cache and Main Memory
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 7


Cache/Main Memory Structure
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 8


Cache Operation – Overview
S H E H E R YAR MALI K

 CPU requests contents of memory location


 Check cache for this data
 If present, get from cache (fast)
 If not present, read required block from main
memory to cache
 Then deliver from cache to CPU
 Cache includes tags to identify which block of main
memory is in each cache slot

Chapter # 5 Computer Organization & Architecture 9


Cache Read Operation - Flowchart
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 10


Elements of Cache Design
S H E H E R YAR MALI K

 Addressing
 Size
 Mapping Function
 Replacement Algorithm
 Write Policy
 Block Size
 Number of Caches

Chapter # 5 Computer Organization & Architecture 11


Cache Addressing
S H E H E R YAR MALI K

 Where does cache sit?


 Between processor and virtual memory management unit
 Between MMU and main memory
 Logical cache (virtual cache) stores data using virtual
addresses
 Processor accesses cache directly, not thorough physical cache
 Cache access faster, before MMU address translation
 Virtual addresses use same address space for different applications
 Must flush cache on each context switch
 Physical cache stores data using main memory physical
addresses

Chapter # 5 Computer Organization & Architecture 12


Cache Addressing
S H E H E R YAR MALI K

 Virtual memory
 Facility that allows programs to address memory from a
logical point of view, without regard to the amount of main
memory physically available
 When used, the address fields of machine instructions
contain virtual addresses
 For reads to and writes from main memory, a hardware
memory management unit (MMU) translates each virtual
address into a physical address in main memory

Chapter # 5 Computer Organization & Architecture 13


Logical vs Physical Cache Addressing
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 14


Cache Size Does Matter
S H E H E R YAR MALI K

 Cost
 More cache is expensive
 Speed
 More cache is faster (up to a point)
 Checking cache for data takes time

Chapter # 5 Computer Organization & Architecture 15


Typical Cache Organization
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 16


Comparison of Cache Sizes
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 17


Mapping Function
S H E H E R YAR MALI K

 Because there are fewer cache lines than main memory blocks,
an algorithm is needed for mapping main memory blocks into
cache lines
 Three techniques can be used
 Direct
 The simplest technique
 Maps each block of main memory into only one possible cache line
 Associative
 Permits each main memory block to be loaded into any line of the cache
 The cache control logic interprets a memory address simply as a Tag and a Word field
 To determine whether a block is in the cache, the cache control logic must
simultaneously examine every line’s Tag for a match
 Set Associative
 A compromise that exhibits the strengths of both the direct and associative
approaches while reducing their disadvantages

Chapter # 5 Computer Organization & Architecture 18


Mapping Function
S H E H E R YAR MALI K

 Cache of 64kByte
 Cache block of 4 bytes
 i.e. cache is 16k (214) lines of 4 bytes
 16MBytes main memory
 24 bit address
 (224=16M)

Chapter # 5 Computer Organization & Architecture 19


Direct Mapping
S H E H E R YAR MALI K

 Each block of main memory maps to only one cache


line
 i.e. if a block is in cache, it must be in one specific place
 Address is in two parts
 Least Significant w bits identify unique word
 Most Significant s bits specify one memory block
 The MSBs are split into a cache line field r and a tag
of s-r (most significant)

Chapter # 5 Computer Organization & Architecture 20


Direct Mapping
S H E H E R YAR MALI K

 Address length = (s + w) bits


 Number of addressable units = 2s+w words or bytes
 Block size line size = 2w words or bytes
 Number of blocks in main memory = 2s
 Number of lines in cache = m = 2r
 Size of cache = 2r+w words or bytes
 Size of tag = (s-r) bits

Chapter # 5 Computer Organization & Architecture 21


Direct Mapping Address Structure
S H E H E R YAR MALI K

 24 bit address
 2 bit word identifier (4 byte block)
 22 bit block identifier
 8 bit tag (=22-14)
 14 bit slot or line
 No two blocks in the same line have the same Tag field
 Check contents of cache by finding line and checking Tag

Tag s-r Line or Slot r Word w

8 14 2

Chapter # 5 Computer Organization & Architecture 22


Direct Mapping
S H E H E R YAR MALI K

 Mapping method
 i = j modulo m
 where
 i cache line number
 j main memory block number
 m number of lines in the cache

Chapter # 5 Computer Organization & Architecture 23


Direct Mapping from Cache to Main Memory
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 24


Direct Mapping Cache Line Table
S H E H E R YAR MALI K

Cache line Main Memory blocks held


0 0, m, 2m, 3m…2s-m

1 1,m+1, 2m+1…2s-m+1


m-1 m-1, 2m-1,3m-1…2s-1

Chapter # 5 Computer Organization & Architecture 25


Direct Mapping Cache Organization
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 26


Direct Mapping Example
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 27


Direct Mapping Summary
S H E H E R YAR MALI K

 Address length = (s + w) bits


 Number of addressable units = 2s+w words or bytes
 Block size = line size = 2w words or bytes
 Number of blocks in main memory = 2s+ w/2w = 2s
 Number of lines in cache = m = 2r
 Size of tag = (s – r) bits

Chapter # 5 Computer Organization & Architecture 28


Direct Mapping Pros & Cons
S H E H E R YAR MALI K

 Simple
 Inexpensive
 Fixed location for given block
 If a program accesses 2 blocks that map to the same line
repeatedly, cache misses are very high

Chapter # 5 Computer Organization & Architecture 29


Victim Cache
S H E H E R YAR MALI K

 Lower miss penalty


 Remember what was discarded
 Already fetched
 Use again with little penalty
 Fully associative
 4 to 16 cache lines
 Between direct mapped L1 cache and next memory
level

Chapter # 5 Computer Organization & Architecture 30


(Fully) Associative Mapping
S H E H E R YAR MALI K

 A main memory block can load into any line of cache


 Memory address is interpreted as tag and word
 Tag uniquely identifies block of memory
 Every line’s tag is examined for a match
 Cache searching gets expensive

Chapter # 5 Computer Organization & Architecture 31


Associative Mapping from Cache to Main Memory
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 32


Fully Associative Cache Organization
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 33


Associative Mapping Example
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 34


Associative Mapping Address Structure
S H E H E R YAR MALI K

Tag 22 bit Word


2 bit

 22 bit tag stored with each 32 bit block of data


 Compare tag field with tag entry in cache to check for hit
 Least significant 2 bits of address identify which 16 bit word is
required from 32 bit data block
 e.g.
 Address Tag Data Cache line
 FFFFFC FFFFFC 24682468 3FFF

Chapter # 5 Computer Organization & Architecture 35


Associative Mapping Summary
S H E H E R YAR MALI K

 Address length = (s + w) bits


 Number of addressable units = 2s+w words or bytes
 Block size = line size = 2w words or bytes
 Number of blocks in main memory = 2s+ w/2w = 2s
 Number of lines in cache = undetermined
 Size of tag = s bits

Chapter # 5 Computer Organization & Architecture 36


Set Associative Mapping
S H E H E R YAR MALI K

 Cache is divided into a number of sets


 Each set contains a number of lines
 A given block maps to any line in a given set
 e.g. Block B can be in any line of set i
 e.g. 2 lines per set
 2 way associative mapping
 A given block can be in one of 2 lines in only one set

Chapter # 5 Computer Organization & Architecture 37


Set Associative Mapping Example
S H E H E R YAR MALI K

 13 bit set number


 Block number in main memory is modulo 213
 000000, 00A000, 00B000, 00C000 … map to same
set

Chapter # 5 Computer Organization & Architecture 38


Mapping From Main Memory to Cache:
v Associative
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 39


Mapping From Main Memory to Cache:
k-way Associative
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 40


K-Way Set Associative Cache Organization
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 41


Set Associative Mapping Address Structure
S H E H E R YAR MALI K

Word
Tag 9 bit Set 13 bit
2 bit

 Use set field to determine cache set to look in


 Compare tag field to see if we have a hit
 e.g
 Address Tag Data Set number
 1FF 7FFC 1FF 12345678 1FFF
 001 7FFC 001 11223344 1FFF

Chapter # 5 Computer Organization & Architecture 42


Two Way Set Associative Mapping Example
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 43


Set Associative Mapping Summary
S H E H E R YAR MALI K

 Address length = (s + w) bits


 Number of addressable units = 2s+w words or bytes
 Block size = line size = 2w words or bytes
 Number of blocks in main memory = 2d
 Number of lines in set = k
 Number of sets = v = 2d
 Number of lines in cache = kv = k * 2d
 Size of tag = (s – d) bits

Chapter # 5 Computer Organization & Architecture 44


Direct and Set Associative Cache
Performance Differences
S H E H E R YAR MALI K

 Significant up to at least 64kB for 2-way


 Difference between 2-way and 4-way at 4kB much
less than 4kB to 8kB
 Cache complexity increases with associativity
 Not justified against increasing cache to 8kB or 16kB
 Above 32kB gives no improvement

Chapter # 5 Computer Organization & Architecture 45


Varying Associativity Over Cache Size
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 46


Replacement Algorithms
S H E H E R YAR MALI K

 Once the cache has been filled, when a new block is


brought into the cache, one of the existing blocks
must be replaced
 For direct mapping there is only one possible line for
any particular block and no choice is possible
 For the associative and set-associative techniques a
replacement algorithm is needed
 To achieve high speed, an algorithm must be
implemented in hardware

Chapter # 5 Computer Organization & Architecture 47


Replacement Algorithms: Direct mapping
S H E H E R YAR MALI K

 No choice
 Each block only maps to one line
 Replace that line

Chapter # 5 Computer Organization & Architecture 48


Replacement Algorithms:
Associative & Set Associative
S H E H E R YAR MALI K

 Hardware implemented algorithm (speed)


 Least Recently used (LRU)
 Most effective
 Replace that block in the set that has been in the cache longest with no
reference to it
 Because of its simplicity of implementation, LRU is the most popular
replacement algorithm
 First in first out (FIFO)
 Replace block that has been in cache longest
 Easily implemented as a round-robin or circular buffer technique
 Least frequently used
 replace block which has had fewest hits
 Random
Chapter # 5 Computer Organization & Architecture 49
LRU Replacement Algorithm
S H E H E R YAR MALI K

 Replace that block in the set that has been in the cache longest with no reference
to it
 For two-way set associative, this is easily implemented.
 Each line includes a USE bit
 When a line is referenced, its USE bit is set to 1 and the USE bit of the other line
in that set is set to 0
 When a block is to be read into the set, the line whose USE bit is 0 is used
 Because we are assuming that more recently used memory locations are more
likely to be referenced, LRU should give the best hit ratio
 LRU is also relatively easy to implement for a fully associative cache
 The cache mechanism maintains a separate list of indexes to all the lines in the
cache
 When a line is referenced, it moves to the front of the list
 For replacement, the line at the back of the list is used
 Because of its simplicity of implementation, LRU is the most popular replacement
algorithm.
Chapter # 5 Computer Organization & Architecture 50
LRU Replacement Algorithm - Method
S H E H E R YAR MALI K

 takes the history of block usage in the cache as a


measure for replacement
 removes the block which has not been referenced in
the cache for the longest time.
 Implementation in hardware using Ageing Counters

Chapter # 5 Computer Organization & Architecture 51


LRU Replacement Algorithm
S H E H E R YAR MALI K

 Using Ageing Counters - Simple Algorithm


 A counter is associated with each block
 Increment each counter at regular intervals
 Reset a counter when the associated line has been
referenced
 The value in each counter indicates the age of a line since
last referenced
 The line with the largest age is replaced at replacement
time

Chapter # 5 Computer Organization & Architecture 52


LRU Replacement Algorithm
S H E H E R YAR MALI K

 Using Ageing Counters - Modified Algorithm


 Counters have a fixed number of bits and that only a
relative age is required
 When a hit occurs,
 The counter associated with the hit block is reset to 0, indicating
that it is the most recently used block
 All counters having a smaller value than the “hit block” counter
originally are incremented by 1.
 All counters having a larger value are unaffected

Chapter # 5 Computer Organization & Architecture 53


LRU Replacement Algorithm
S H E H E R YAR MALI K

 Using Ageing Counters - Modified Algorithm


 When a miss occurs
 If the cache is not full
 the counter associated with the incoming line is reset to 0
 all other counters are incremented by 1
 If the cache is full
 the block with a counter set at the maximum value (three for a 2-
bit counter and four sets) is chosen for replacement and then the
counter is reset to 0
 all other counters are incremented by 1
 The counter with the largest value identifies the least
recently used block

Chapter # 5 Computer Organization & Architecture 54


LRU Replacement Algorithm
S H E H E R YAR MALI K

 Given a 4-way set-associative cache, i.e., there are


four lines or blocks in each set.
 A 2-bit counter is sufficient for each block.
 Let the counters in one set be C0, C1, C2, and C3.
 Initially, all the counters are set to 0.

 Derive the numbers held in the counters of set 0 after


each memory address reference in the following
sequence (given sequence of TAGs from the memory
references) map to set 0 :
 5, 6, 5, 7, 8, 6, 9, 10
Chapter # 5 Computer Organization & Architecture 55
LRU Replacement Algorithm
S H E H E R YAR MALI K

 Solution
_________________________________________________________________________________________________________________________________

Address Tag Hit/Miss C0 C1 C2 C3 Subsequent Action


_________________________________________________________________________________________________________________________________

0 0 0 0 Initialization
5 Miss 0 1 1 1 Block 0 filled
6 Miss 1 0 2 2 Block 1 filled
5 Hit 0 1 2 2 Block 0 accessed
7 Miss 1 2 0 3 Block 2 filled
8 Miss 2 3 1 0 Block 3 filled
6 Hit 3 0 2 1 Block 1 accessed
9 Miss 0 1 3 2 Block 0 replaced
10 Miss 1 2 0 3 Block 2 replaced
------------------------------------------------------------------------------------------------
SET 0 Hit Ratio = 2 / 8 = 0.25
v TAG Block0 v TAG Block1 v
TAG Block2 v TAG Block3 C0 C1 C2 C3

Chapter # 5 Computer Organization & Architecture 56


Write Policy
S H E H E R YAR MALI K

 Must not overwrite a cache block unless main


memory is up to date
 Even if one write operation has been performed on a word
in some line of the cache then main memory must be
updated by writing that line of cache out to the block of
memory before bringing in the new block
 Multiple CPUs may have individual caches
 I/O may address main memory directly
 Two cache write policies
 Write through
 Write back

Chapter # 5 Computer Organization & Architecture 57


Write Through
S H E H E R YAR MALI K

 All writes go to main memory as well as cache


 Multiple CPUs can monitor main memory traffic to
keep local (to CPU) cache up to date
 Lots of traffic
 Slows down writes

Chapter # 5 Computer Organization & Architecture 58


Write Back
S H E H E R YAR MALI K

 Updates initially made in cache only


 Update bit for cache slot is set when update occurs
 If block is to be replaced, write to main memory only
if update bit is set
 Other caches get out of sync
 I/O must access main memory through cache
 15% of memory references are writes

Chapter # 5 Computer Organization & Architecture 59


Line Size
S H E H E R YAR MALI K

 Retrieve not only desired word but a number of adjacent


words as well
 Increased block size will increase hit ratio at first
 the principle of locality
 Hit ratio will decreases as block becomes even bigger
 Probability of using newly fetched information becomes less than
probability of reusing replaced
 Larger blocks
 Reduce number of blocks that fit in cache
 Data overwritten shortly after being fetched
 Each additional word is less local so less likely to be needed
 No definitive optimum value has been found
 8 to 64 bytes seems reasonable
 For HPC systems, 64 and 128 bytes most common

Chapter # 5 Computer Organization & Architecture 60


Multilevel Caches
S H E H E R YAR MALI K

 High logic density enables caches on chip


 The on-chip cache reduces the processor’s external bus activity and speeds
up execution time and increases overall system performance
 Faster than bus access
 Frees bus for other transfers
 Common to use both on and off chip cache
 L1 on chip, L2 off chip in static RAM
 L2 access much faster than DRAM or ROM
 L2 often uses separate data path
 L2 may now be on chip
 Resulting in L3 cache
 The use of multilevel caches complicates all of the design issues
related to caches, including size, replacement algorithm, and write
policy
Chapter # 5 Computer Organization & Architecture 61
Unified v Split Caches
S H E H E R YAR MALI K

 Unified cache
 One cache for data and instructions
 Split cache
 Two separate caches, one for data and one for instructions
 Advantages of unified cache
 Higher hit rate
 Balances load of instruction and data fetch
 Only one cache to design & implement
 Advantages of split cache
 Eliminates cache contention between instruction
fetch/decode unit and execution unit
 Important in pipelining

Chapter # 5 Computer Organization & Architecture 62


Pentium 4 Cache
S H E H E R YAR MALI K

 80386 – no on chip cache


 80486 – 8k using 16 byte lines and four way set associative organization
 Pentium (all versions) – two on chip L1 caches
 Data & instructions
 Pentium III – L3 cache added off chip
 Pentium 4
 L1 caches
 16k bytes each
 64 byte lines
 four way set associative
 L2 cache
 Feeding both L1 caches
 256kB
 128 byte lines
 8 way set associative
 L3 cache on chip

Chapter # 5 Computer Organization & Architecture 63


Intel Cache Evolution
Problem Solution Processor on
which feature
first appears
External memory slower than the system bus Add external cache using faster memory 386
technology
Increased processor speed results in external bus Move external cache on-chip, operating at the 486
becoming a bottleneck for cache access same speed as the processor
Internal cache is rather small, due to limited Add external L2 cache using faster technology 486
space on chip than main memory
Contention occurs when both the Instruction Create separate data and instruction caches Pentium
Prefetcher and the Execution Unit
simultaneously require access to the cache. In
that case, the Prefetcher is stalled while the
Execution Unit’s data access takes place
Increased processor speed results in external bus Create separate back-side bus that runs at higher Pentium Pro
becoming a bottleneck for L2 cache access speed than the main (front-side) external bus.
The BSB is dedicated to the L2 cache
Move L2 cache on to the processor chip Pentium II
Some applications deal with massive databases Add external L3 cache Pentium III
and must have rapid access to large amounts of
data. The on-chip caches are too small Move L3 cache on-chip Pentium 4
Pentium 4 Block Diagram
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 65


Pentium 4 Core Processor
S H E H E R YAR MALI K

 Fetch/Decode Unit
 Fetches instructions from L2 cache
 Decode into micro-ops
 Store micro-ops in L1 cache
 Out of order execution logic
 Schedules micro-ops
 Based on data dependence and resources
 May speculatively execute
 Execution units
 Execute micro-ops
 Data from L1 cache
 Results in registers
 Memory subsystem
 L2 cache and systems bus

Chapter # 5 Computer Organization & Architecture 66


Pentium 4 Design Reasoning
S H E H E R YAR MALI K

 Decodes instructions into RISC like micro-ops before L1 cache


 Micro-ops fixed length
 Superscalar pipelining and scheduling
 Pentium instructions long & complex
 Performance improved by separating decoding from scheduling &
pipelining
 Data cache is write back
 Can be configured to write through
 L1 cache controlled by 2 bits in register
 CD = cache disable
 NW = not write through
 2 instructions to invalidate (flush) cache and write back then invalidate
 L2 and L3 8-way set-associative
 Line size 64-128 bytes

Chapter # 5 Computer Organization & Architecture 67


ARM Cache Features
S H E H E R YAR MALI K

Core Cache Cache Size (kB) Cache Line Associativity Location Write Buffer
Type Size (words) Size (words)
ARM720T Unified 8 4 4-way Logical 8

ARM920T Split 16/16 D/I 8 64-way Logical 16


ARM926EJ-S Split 4-128/4-128 D/I 8 4-way Logical 16

ARM1022E Split 16/16 D/I 8 64-way Logical 16


ARM1026EJ-S Split 4-128/4-128 D/I 8 4-way Logical 8

Intel StrongARM Split 16/16 D/I 4 32-way Logical 32

Intel Xscale Split 32/32 D/I 8 32-way Logical 32


ARM1136-JF-S Split 4-64/4-64 D/I 8 4-way Physical 32

Chapter # 5 Computer Organization & Architecture 68


ARM Cache Organization
S H E H E R YAR MALI K

 Small FIFO write buffer


 Enhances memory write performance
 Between cache and main memory
 Data put in write buffer at processor clock speed
 Processor continues execution
 External write in parallel until empty
 If buffer full, processor stalls
 Data in write buffer not available until written
 So keep buffer small

Chapter # 5 Computer Organization & Architecture 69


ARM Cache and Write Buffer Organization
S H E H E R YAR MALI K

Chapter # 5 Computer Organization & Architecture 70

You might also like