When the data requested from the processor is in the cache, a cache hit occurs:
In the animation, there is a memory hierarchy with a cache in between the processor and main memory. The processor requests the data located at the main memory address
2. The address is found inside the cache so a cache hit occurs. The character
"c" will be returned from the cache and the main memory is never accessed.
The goal of the memory hierarchy is to reduce data access time by getting as many cache hits when requesting data from memory.
Cache() class now has entries in
self.data. This data will result in a series of cache hits when the simulated instructions are run.
Cache() class also added a
.read() method. Inside the method:
super().read()is called. This will call the
.read()method and will output
"Cache read: "as well as add the cache
access_timeto the total execution time of the simulation.
datavariable is initialized to
Run the code. The output should have
Cache read: NO DATA.
.get_entry() has also been added to the
Cache() class. It takes a memory address as a parameter and searches for that address in
self.data. If it finds the address,
CACHE HIT is output and the cache entry is returned as a dictionary. Otherwise,
CACHE MISS is output and
None is returned.
- Define the
entryvariable and assign it a call to
addressas the parameter.
Run the code. The output should now indicate a cache hit on each read along with the
NO DATA error.
Now check the entry for a cache hit.
entry variable definition:
- Write an
ifstatement that checks if
entryis not equal to
- Inside the
datato the value in
The last step is to return the data retrieved from the cache.
- Return the
When you run the code, the output should now show each character retrieved from the cache at the end of each line. The output string should now be complete.