Caches
Table of Contents
A note on capacity misses Slide 3
Example 1: small, direct-mapped cache Slide 4
Example 2: small, fully-associative cache Slide 15
A note on conflict vs. capacity misses
How do I know if it’s a capacity miss?
If n blocks fit in our cache, and we are accessing the (n+1)st unique block, then this is a capacity miss! Because in a fully-associative cache with an LRU replacement policy (not necessarily all fully-associative caches, depending on the replacement policy chosen!) this would cause a miss.
See the flowchart on the next page!
Flowchart
Example 1: small, direct-mapped cache
Memory size: 32B
Cache size: 8B
Block size: 4B (1 word)
Total address bits?
Index bits?
Offset bits?
Tag bits?
Example 1: small, direct-mapped cache
Memory size: 32B
Cache size: 8B
Block size: 4B (1 word)
Total address bits? log2(# bytes in memory) = log2(32) = 5 bits
Index bits? log2(# sets in cache) = log2(# blocks in direct mapped cache) = log2(8/4) = log2(2) = 1 bit
Offset bits? log2(# bytes in one block) log2(4) = 2 bits
Tag bits? Total address bits - index bits - offset bits = 5 - 1 - 2 = 2 bits
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
| 0 | | | | |
| 1 | | | | |
Tag
Cache Contents
Cache
Memory accesses: imagine each access is for a character stored at that address, and characters are one byte
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
| 0 | | | | |
| 1 | | | | |
Tag
Cache Contents
Cache
Memory accesses:
Index
Data is stored little-endian: at address 0b11100, the word stored is 0xFA235262 in hex.
FA is the MSB (most significant byte) and has address 0b1111
62 is the LSB (least significant byte) and has address 0b11100.
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
00 | 0 | 97 | 81 | 82 | 66 |
| 1 | | | | |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
00 0 01
Compulsory miss
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
00 | 0 | 97 | 81 | 82 | 66 |
| 1 | | | | |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
00 0 00
Hit
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
00 | 0 | 97 | 81 | 82 | 66 |
| 1 | | | | |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
00 0 10
Hit
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
00 | 0 | 97 | 81 | 82 | 66 |
00 | 1 | 00 | 00 | 00 | 01 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
00 1 00
Compulsory miss
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
00 | 0 | 97 | 81 | 82 | 66 |
01 | 1 | 00 | 00 | 00 | 03 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
01 1 00
Compulsory miss
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
00 | 0 | 97 | 81 | 82 | 66 |
00 | 1 | 00 | 00 | 00 | 01 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
00 1 00
Capacity miss
Even though the block with tag 00 and index 0 was pulled into the cache on access 1, we have looked at 3 unique blocks from memory already. Our cache can hold a maximum of 2 unique blocks, so in some fully associative caches, we still would have had a miss here. This leads to a capacity miss.
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
11 | 0 | CD | 28 | 19 | 35 |
00 | 1 | 00 | 00 | 00 | 01 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
11 0 10
Compulsory miss
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
11 | 0 | CD | 28 | 19 | 35 |
00 | 1 | 00 | 00 | 00 | 01 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
11 0 11
Hit
Index
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
00 | 0 | 97 | 81 | 82 | 66 |
00 | 1 | 00 | 00 | 00 | 01 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Index Offset
00 0 11
Capacity miss
Even though the block with tag 00 and index 0 was pulled into the cache on access 1, we have looked at 5 unique blocks from memory already. Our cache can hold a maximum of 2 unique blocks, so in some fully associative caches, we still would have had a miss here. This leads to a capacity miss.
Index
Hit & Miss Rate
Total memory accesses: 9
Hits: 3
Misses: 6
Hit rate = 3/9 = 33.33333… %
Miss rate = 6/9 = 66.6666… %
Example 2: small, fully-associative cache
Memory size: 32B
Cache size: 8B
Block size: 4B (1 word)
Replacement Policy: FIFO (first in, first out)
Total address bits?
Index bits?
Offset bits?
Tag bits?
Example 2: small, fully-associative cache
Memory size: 32B
Cache size: 8B
Block size: 4B (1 word)
Replacement policy: FIFO (first in, first out)
Total address bits? log2(# bytes in memory) = log2(32) = 5 bits
Index bits? log2(# sets in cache) = log2(1 in a fully associative cache) = 0 bits
Offset bits? log2(# bytes in one block) log2(4) = 2 bits
Tag bits? Total address bits - index bits - offset bits = 5 - 0 - 2 = 3 bits
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
000 | 97 | 81 | 82 | 66 |
| | | | |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
000 01
Compulsory miss
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
000 | 97 | 81 | 82 | 66 |
| | | | |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
000 00
Hit
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
000 | 97 | 81 | 82 | 66 |
| | | | |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
000 10
Hit
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
000 | 97 | 81 | 82 | 66 |
001 | 00 | 00 | 00 | 01 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
001 00
Compulsory miss
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
011 | 00 | 00 | 00 | 03 |
001 | 00 | 00 | 00 | 01 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
011 00
Compulsory miss
Tag 000 came in before Tag 001, so this is the block in the cache that we replaced (FIFO)
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
011 | 00 | 00 | 00 | 03 |
001 | 00 | 00 | 00 | 01 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
001 00
Hit
In the direct-mapped cache, this was a conflict miss
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
011 | 00 | 00 | 00 | 03 |
110 | CD | 28 | 19 | 35 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
110 10
Compulsory miss
Tag 001 came in before Tag 011, so this is the block in the cache that we replace (FIFO)
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
011 | 00 | 00 | 00 | 03 |
110 | CD | 28 | 19 | 35 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
110 11
Hit
0b00000 | 97 | 81 | 82 | 66 |
0b00100 | 00 | 00 | 00 | 01 |
0b01000 | 00 | 00 | 00 | 02 |
0b01100 | 00 | 00 | 00 | 03 |
0b10000 | 00 | 00 | 00 | 04 |
0b10100 | FF | FA | 03 | 23 |
0b11000 | CD | 28 | 19 | 35 |
0b11100 | FA | 23 | 52 | 62 |
Memory
Address
Memory Contents
000 | 97 | 81 | 82 | 66 |
110 | CD | 28 | 19 | 35 |
Tag
Cache Contents
Cache
Memory accesses:
Tag Offset
000 11
Capacity miss
The block with tag 000 was previously pulled into the cache on access 1, but was kicked out due to a lack of space; if the cache were bigger, it would still be there
Hit & Miss Rate
Total memory accesses = 9
Hits: 4
Misses: 5
Hit rate = 4/9 = 44.4444… %
Miss rate = 5/9 = 55.5555… %
Extra Slides
Example 3: bigger, 4-way associative cache
Memory size: 4096B
Cache size: 128B
Block size: 16B (4 words)
Total address bits?
Index bits?
Offset bits?
Tag bits?
Example 3: bigger, 4-way associative cache
Memory size: 4096B
Cache size: 128B
Block size: 16B (4 words)
Total address bits? log2(4096) = 12 bits
Index bits? log2(128/(16*4)) = log2(2) = 1 bit
Offset bits? log2(16) = 4 bits
Tag bits? 12 - 1 - 4 = 7 bits
Memory
Address
Memory Contents
Tag
Cache Contents
Cache
| 0 | | | | |
| | | | | |
| | | | | |
| | | | | |
| 1 | | | | |
| | | | | |
| | | | | |
| | | | |
0x000 | | | | |
0x004 | | | | |
0x008 | | | | |
0x00C | | | | |
0x010 | | | | |
0x014 | | | | |
0x4A8 | | | | |
0x4AC | | | | |
0x4B0 | | | | |
0x4B4 | | | | |
0x4B8 | | | | |
0x4BC | | | | |
...
Memory accesses:
...