Cache Operation
Cache Parameters SIZE = total amount of cache data storage, in bytes BLOCKSIZE = total number of bytes in a single block ASSOC = associativity, i.e., # of blocks in a set
Cache Parameters (cont.) Equation for # of cache blocks in cache: Equation for # of sets in cache:
Address Fields 31 block offset index tag 31 block offset index tag Tag field is compared to the tag(s) of the indexed cache block(s). If there is a match, memory block is there (hit). If there isn’t a match, memory block is not there (miss). Used to lookup a “set”, which contains one or more memory blocks. The number of blocks in a set is the “associativity”. Once block is found, offset selects a particular byte or word of data in the block.
Address Fields (cont.) Widths of address fields (# bits) # index bits = log2(# sets) # block offset bits = log2(block size) # tag bits = 32 - # index bits - # block offset bits Assuming 32-bit addresses 31 block offset index tag
31 block offset index tag Address (from processor) TAGS DATA Match? hit/miss byte or word select requested data (byte or word) (to processor)
Example --- Direct-mapped cache Example: Processor accesses a 256 Byte direct-mapped cache, which has block size of 32 Bytes, with following sequence of addresses. Show contents of cache after each access, count # of hits, count # of replacements.
Example address sequence Address (hex) Tag (hex) Index & Offset bits (binary) Index (decimal) Comment 0xFF0040E0 0xBEEF005C 0xFF0040E2 0xFF0040E8 0x00101078 0x002183E0 0x00101064 0x0012255C 0x00122544
# index bits = log2(# sets) = log2(8) = 3 # block offset bits = log2(block size) = log2(32 bytes) = 5 # tag bits = 32 bits – 3 bits – 5 bits = 24 SO: Top 6 nibbles (24 bits) of address form the tag and lower 2 nibbles (8 bits) of address form the index and block offset fields
Index & Offset bits (binary) Index (decimal) Comment Address (hex) Tag (hex) Index & Offset bits (binary) Index (decimal) Comment 0xFF0040E0 0xFF0040 1110 0000 7 0xBEEF005C 0xBEEF00 0101 1100 2 0xFF0040E2 1110 0010 0xFF0040E8 1110 1000 0x00101078 0x001010 0111 1000 3 0x002183E0 0x002183 0x00101064 0110 0100 0x0012255C 0x001225 0x00122544 0100 0100
MISS TAGS DATA 31 block offset index tag 8 7 5 4 FF0040 7 24 3 FF0040 31 block offset index tag 8 7 5 4 1 2 3 6 FF0040 7 24 3 FF0040 Get block from memory (slow) Match? MISS
MISS TAGS DATA 31 block offset index tag 8 7 5 4 1 2 3 6 FF0040 BEEF00 31 block offset index tag 8 7 5 4 1 2 3 6 FF0040 BEEF00 2 24 3 BEEF00 Get block from memory (slow) Match? MISS
HIT TAGS DATA 31 block offset index tag 8 7 5 4 FF0040 FF0040 7 24 3 31 block offset index tag 8 7 5 4 1 2 3 6 FF0040 FF0040 7 24 3 BEEF00 Match? HIT
HIT TAGS DATA 31 block offset index tag 8 7 5 4 FF0040 FF0040 7 24 3 31 block offset index tag 8 7 5 4 1 2 3 6 FF0040 FF0040 7 24 3 BEEF00 Match? HIT
MISS TAGS DATA 31 block offset index tag 8 7 5 4 FF0040 001010 3 24 3 31 block offset index tag 8 7 5 4 1 2 3 6 FF0040 001010 3 24 3 BEEF00 Get block from memory (slow) 001010 Match? MISS
MISS & REPLACE TAGS DATA 31 block offset index tag 8 7 5 4 FF0040 31 block offset index tag 8 7 5 4 1 2 3 6 FF0040 002183 7 24 3 BEEF00 001010 Get block from memory (slow) 002183 Match? MISS & REPLACE
HIT TAGS DATA 31 block offset index tag 8 7 5 4 001010 3 24 3 BEEF00 31 block offset index tag 8 7 5 4 1 2 3 6 001010 3 24 3 BEEF00 001010 002183 Match? HIT
MISS & REPLACE TAGS DATA 31 block offset index tag 8 7 5 4 001225 2 24 31 block offset index tag 8 7 5 4 1 2 3 6 001225 2 24 3 Get block from memory (slow) 001225 BEEF00 001010 002183 Match? MISS & REPLACE
HIT TAGS DATA 31 block offset index tag 8 7 5 4 001225 2 24 3 001225 31 block offset index tag 8 7 5 4 1 2 3 6 001225 2 24 3 001225 001010 002183 Match? HIT
Index & Offset bits (binary) Index (decimal) Comment Address (hex) Tag (hex) Index & Offset bits (binary) Index (decimal) Comment 0xFF0040E0 0xFF0040 1110 0000 7 Miss 0xBEEF005C 0xBEEF00 0101 1100 2 0xFF0040E2 1110 0010 Hit 0xFF0040E8 1110 1000 0x00101078 0x001010 0111 1000 3 0x002183E0 0x002183 Miss/Repl 0x00101064 0110 0100 0x0012255C 0x001225 0x00122544 0100 0100
Example --- N-way set-associative cache Example: Processor accesses a 256 byte 2-way set-associative cache, which has block size of 32 bytes, with following sequence of addresses. Show contents of cache after each access, count # of hits, count # of replacements.
31 block offset index tag Address (from processor) TAGS DATA (32 bytes) (32 bytes) select a block Match? Match? select certain bytes hit OR
# index bits = log2(# sets) = log2(4) = 2 # block offset bits = log2(block size) = log2(32 bytes) = 5 # tag bits = total # address bits - # index bits - # block offset bits = 32 bits – 2 bits – 5 bits = 25
Index & Offset bits (binary) Index (decimal) Comment Address (hex) Tag (hex) Index & Offset bits (binary) Index (decimal) Comment 0xFF0040E0 0x1FE0081 110 0000 3 0xBEEF005C 0x17DDE00 101 1100 2 0x00101078 0x0002020 111 1000 0xFF0040E2 110 0010 0x002183E0 0x0004307 0x00101064 110 0100
MISS 31 7 6 5 4 tag index block offset 1FE0081 3 25 2 TAGS DATA tag index block offset 1FE0081 3 25 2 TAGS 1 2 3 LRU DATA not shown for convenience 1FE0081 1 Match? Match? MISS
MISS 31 7 6 5 4 tag index block offset 17DDE00 2 25 2 TAGS 17DDE00 tag index block offset 17DDE00 2 25 2 TAGS 1 2 3 17DDE00 1 LRU DATA not shown for convenience 1FE0081 Match? Match? MISS
MISS 31 7 6 5 4 tag index block offset 0002020 3 25 2 TAGS 17DDE00 tag index block offset 0002020 3 25 2 TAGS 1 2 3 17DDE00 DATA not shown for convenience 1FE0081 0002020 1 1 LRU Match? Match? MISS
HIT 31 7 6 5 4 tag index block offset 1FE0081 3 25 2 TAGS 17DDE00 DATA tag index block offset 1FE0081 3 25 2 TAGS 1 2 3 17DDE00 DATA not shown for convenience 1FE0081 0002020 1 1 LRU Match? Match? HIT
HIT 31 7 6 5 4 tag index block offset 0002020 3 25 2 TAGS 17DDE00 DATA tag index block offset 0002020 3 25 2 TAGS 1 2 3 17DDE00 DATA not shown for convenience 1FE0081 0002020 1 1 LRU Match? Match? HIT
MISS & REPLACE LRU BLOCK 31 7 6 5 4 tag index block offset 0004307 3 25 2 TAGS 1 2 3 17DDE00 0004307 DATA not shown for convenience 1FE0081 0002020 1 1 LRU Match? Match? MISS & REPLACE LRU BLOCK
HIT 31 7 6 5 4 tag index block offset 0002020 3 25 2 TAGS 17DDE00 DATA tag index block offset 0002020 3 25 2 TAGS 1 2 3 17DDE00 DATA not shown for convenience 0004307 0002020 1 1 LRU Match? Match? HIT
Index & Offset bits (binary) Index (decimal) Comment Address (hex) Tag (hex) Index & Offset bits (binary) Index (decimal) Comment 0xFF0040E0 0x1FE0081 110 0000 3 Miss 0xBEEF005C 0x17DDE00 101 1100 2 0x00101078 0x0002020 111 1000 0xFF0040E2 110 0010 Hit 0x002183E0 0x0004307 Miss/Repl 0x00101064 110 0100