






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































Today:
— How do caches work?
Caches are divided into blocks, which may be of various sizes.
— The number of blocks in a cache is usually a power of 2. — For now we’ll say that each block contains one byte. This won’t take advantage of spatial locality, but we’ll do that next time.
Here is an example cache with eight blocks, each holding one byte.
000 001 010 011 100 101 110 111
Block index 8-bit data
A direct-mapped cache is the simplest approach: each main memory address maps to exactly one cache block.
For example, on the right is a 16-byte main memory and a 4-byte cache (four 1-byte blocks).
Memory locations 0, 4, 8 and 12 all map to cache block 0.
Addresses 1, 5, 9 and 13 map to cache block 1, etc.
How can we compute this mapping?
0 1 2 3
Index
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15
Memory Address
One way to figure out which cache block a particular memory address should go to is to use the mod (remainder) operator.
If the cache contains 2 k blocks, then the data at memory address i would go to cache block index
i mod 2 k
For instance, with the four-block cache here, address 14 would map to cache block 2.
14 mod 4 = 2
0 1 2 3
Index
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15
Memory Address
The second question was how to determine whether or not the data we’re interested in is already stored in the cache.
If we want to read memory address i , we can use the mod trick to determine which cache block would contain i.
But other addresses might also map to the same cache block. How can we distinguish between them?
For instance, cache block 2 could contain data from addresses 2, 6, 10 or 14.
0 1 2 3
Index
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15
Memory Address
We need to add tags to the cache, which supply the rest of the address bits to let us distinguish between different memory locations that map to the same cache block.
00 01 10 11
Index
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Tag Data 00 ?? 01 01
When started, the cache is empty and does not contain valid data.
We should account for this by adding a valid bit for each cache block.
— When the system is initialized, all the valid bits are set to 0. — When data is loaded into a particular cache block, the corresponding valid bit is set to 1.
So the cache contains more than just copies of the data in memory; it also has bits to help us find data within the cache and verify its validity.
00 01 10 11
Index Tag Data 00 11 01 01
00 + 00 = 0000 Invalid ??? ???
Main memory address in cache block 1 0 0 1
Valid Bit
When the CPU tries to read from memory, the address will be sent to a cache controller. — The lowest k bits of the address will index a block in the cache. — If the block is valid and the tag matches the upper ( m - k ) bits of the m -bit address, then that data will be sent to the CPU.
Here is a diagram of a 32-bit memory address and a 2^10 -byte cache.
0 1 2 3 ... ... 1022 1023
Address (32 bits)^ Index^ Valid Tag^ Data
=
To CPU
Hit
22 10 Index
Tag
After data is read from main memory, putting a copy of that data into the cache is straightforward. — The lowest k bits of the address specify a cache block. — The upper ( m - k ) address bits are stored in the block’s tag field. — The data from main memory is stored in the block’s data field. — The valid bit is set to 1.
0 1 2 3 ...
... ...
Address (32 bits)^ Index^ Valid Tag^ Data
22 10 Index Tag
Data
1
Our third question was what to do if we run out of space in our cache, or if we need to reuse a block for a different memory address.
We answered this question implicitly on the last page!
— A miss causes a new block to be loaded into the cache, automatically overwriting any previously stored data. — This is a least recently used replacement policy, which assumes that older data is less likely to be requested than newer data.
We’ll see a few other policies next.
Now we’ll explore some alternate cache organizations. — How can we take advantage of spatial locality too? — How can we reduce the number of potential conflicts?
We’ll first motivate it with a brief discussion about cache performance.
To examine the performance of a memory system, we need to focus on a couple of important factors. — How long does it take to send data from the cache to the CPU? — How long does it take to copy data from memory into the cache? — How often do we have to access main memory?
There are names for all of these variables.
— The hit time is how long it takes data to be sent from the cache to the processor. This is usually fast, on the order of 1-3 clock cycles. — The miss penalty is the time to copy data from main memory to the cache. This often requires dozens of clock cycles (at least). — The miss rate is the percentage of misses.
Lots of dynamic RAM
A little static RAM (cache)
CPU
Assume that 33% of the instructions in a program are data accesses. The cache hit ratio is 97% and the hit time is one cycle, but the miss penalty is 20 cycles.
=
How can we reduce miss rate?
One-byte cache blocks don’t take advantage of spatial locality, which predicts that an access to one address will be followed by an access to a nearby address.
What can we do?