ACA - Chapter # 4 - Cache Memory
ACA - Chapter # 4 - Cache Memory
Cache Memory
Chapter 4
Cache Memory
1
12/25/2021
2
12/25/2021
Locality of Reference
• During the course of the execution of a program, memory references
tend to cluster
o e.g. Loops
• Spatial locality vs Temporal locality
3
12/25/2021
Cache
• Small amount of fast memory
• Sits between normal main memory and CPU
• May be located on CPU chip or module
4
12/25/2021
Memory Hierarchy
10
5
12/25/2021
11
12
6
12/25/2021
13
Cache Addressing
• Where does cache sit?
o Between processor and virtual memory management unit
o Between MMU and main memory
14
7
12/25/2021
Cache Addressing
• Virtual memory
o Facility that allows programs to address memory from a logical point of view,
without regard to the amount of main memory physically available
o When used, the address fields of machine instructions contain virtual
addresses
o 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
15
16
8
12/25/2021
17
18
9
12/25/2021
19
Mapping Function
• 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
o Direct
▪ The simplest technique
▪ Maps each block of main memory into only one possible cache line
o 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
o Set Associative
▪ A compromise that exhibits the strengths of both the direct and associative approaches while
reducing their disadvantages
20
10
12/25/2021
Mapping Function
• Cache of 64kByte
• Cache block of 4 bytes
o i.e. cache is 16k (214) lines of 4 bytes
• 16MBytes main memory
• 24 bit address
o (224=16M)
21
Direct Mapping
• Each block of main memory maps to only one cache line
o 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)
22
11
12/25/2021
Direct Mapping
• 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
23
• No two blocks in the same line have the same Tag field
• Check contents of cache by finding line and checking Tag
8 14 2
24
12
12/25/2021
Direct Mapping
• Mapping method
• i = j modulo m
• where
o i cache line number
o j main memory block number
o m number of lines in the cache
25
26
13
12/25/2021
1 1,m+1, 2m+1…2s-m+1
…
m-1 m-1, 2m-1,3m-1…2s-1
27
28
14
12/25/2021
29
30
15
12/25/2021
31
Victim Cache
• Lower miss penalty
• Remember what was discarded
o Already fetched
o Use again with little penalty
• Fully associative
• 4 to 16 cache lines
• Between direct mapped L1 cache and next memory level
32
16
12/25/2021
33
34
17
12/25/2021
35
36
18
12/25/2021
37
38
19
12/25/2021
39
40
20
12/25/2021
41
42
21
12/25/2021
43
Word
Tag 9 bit Set 13 bit
2 bit
44
22
12/25/2021
45
46
23
12/25/2021
47
48
24
12/25/2021
Replacement Algorithms
• 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
49
50
25
12/25/2021
51
Write Policy
• Must not overwrite a cache block unless main memory is up to date
o 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
o Write through
o Write back
52
26
12/25/2021
Write Through
• 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
53
Write Back
• 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
54
27
12/25/2021
Line Size
• Retrieve not only desired word but a number of adjacent words as well
• Increased block size will increase hit ratio at first
o the principle of locality
• Hit ratio will decreases as block becomes even bigger
o Probability of using newly fetched information becomes less than probability of
reusing replaced
• Larger blocks
o Reduce number of blocks that fit in cache
o Data overwritten shortly after being fetched
o 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
55
Multilevel Caches
• High logic density enables caches on chip
o The on-chip cache reduces the processor’s external bus activity and speeds up
execution time and increases overall system performance
o Faster than bus access
o Frees bus for other transfers
• Common to use both on and off chip cache
o L1 on chip, L2 off chip in static RAM
o L2 access much faster than DRAM or ROM
o L2 often uses separate data path
o L2 may now be on chip
o 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
56
28
12/25/2021
57
Pentium 4 Cache
• 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
o Data & instructions
• Pentium III – L3 cache added off chip
• Pentium 4
o L1 caches
▪ 16k bytes each
▪ 64 byte lines
▪ four way set associative
o L2 cache
▪ Feeding both L1 caches
▪ 256kB
▪ 128 byte lines
▪ 8 way set associative
o L3 cache on chip
58
29
12/25/2021
59
60
30
12/25/2021
61
62
31
12/25/2021
63
64
32
12/25/2021
65
33