When the data requested from the processor is not in the cache, a cache miss occurs:
The above animation represents a cache miss. The data request first goes to the cache. When the data is not found in the cache, a cache miss occurs and the request goes to the main memory. The memory address and retrieved data will then be placed in the cache. Finally, the processor will finish the request by retrieving the data from the cache.
While a cache miss helps put the needed data in the cache, the goal of the cache is to limit the cache misses.
Instructions
Observe the following changes to the Cache()
class:
self.data
has no entries with all tags equal toNone
and data equal to empty strings.- The
self.main_memory
variable has been added and holds an instance of theMain_Memory()
class. This will be used to read data stored in the main memory. - The
.add_entry()
method has been added and is used to add an entry to the cache given the memory address and data.
Run the code. The cache with no entries causes all cache misses. The output indicates NO DATA
because the cache does not retrieve anything from the main memory yet.
Now it’s time to add the cache miss behavior. Inside the .read()
method:
- Add an
else
clause at the end of theif
statement. - Set the
data
variable to a call toself.main_memory.read()
withaddress
as the argument.
The output now indicates a read from the main memory and the execution time should be increased from the cache hits in the last exercise.
Now add an entry to the cache with the retrieved data. Inside the else
clause:
- Call
self.add_entry()
withaddress
anddata
as arguments.
Running the code will start with cache misses, but result in cache hits once the data is found in the cache.