|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from cachesimulator.bin_addr import BinaryAddress |
| 4 | +from cachesimulator.reference import ReferenceCacheStatus |
| 5 | +from cachesimulator.word_addr import WordAddress |
| 6 | + |
| 7 | + |
| 8 | +class Cache(dict): |
| 9 | + |
| 10 | + # Initializes the reference cache with a fixed number of sets |
| 11 | + def __init__(self, cache=None, num_sets=None, num_index_bits=0): |
| 12 | + |
| 13 | + # A list of recently ordered addresses, ordered from least-recently |
| 14 | + # used to most |
| 15 | + self.recently_used_addrs = [] |
| 16 | + |
| 17 | + if cache is not None: |
| 18 | + self.update(cache) |
| 19 | + else: |
| 20 | + for i in range(num_sets): |
| 21 | + index = BinaryAddress( |
| 22 | + word_addr=WordAddress(i), num_addr_bits=num_index_bits) |
| 23 | + self[index] = [] |
| 24 | + |
| 25 | + # Every time we see an address, place it at the top of the |
| 26 | + # list of recently-seen addresses |
| 27 | + def mark_ref_as_last_seen(self, ref): |
| 28 | + |
| 29 | + # The index and tag (not the offset) uniquely identify each address |
| 30 | + addr_id = (ref.index, ref.tag) |
| 31 | + if addr_id in self.recently_used_addrs: |
| 32 | + self.recently_used_addrs.remove(addr_id) |
| 33 | + self.recently_used_addrs.append(addr_id) |
| 34 | + |
| 35 | + # Returns True if a block at the given index and tag exists in the cache, |
| 36 | + # indicating a hit; returns False otherwise, indicating a miss |
| 37 | + def is_hit(self, addr_index, addr_tag): |
| 38 | + |
| 39 | + # Ensure that indexless fully associative caches are accessed correctly |
| 40 | + if addr_index is None: |
| 41 | + blocks = self['0'] |
| 42 | + elif addr_index in self: |
| 43 | + blocks = self[addr_index] |
| 44 | + else: |
| 45 | + return False |
| 46 | + |
| 47 | + for block in blocks: |
| 48 | + if block['tag'] == addr_tag: |
| 49 | + return True |
| 50 | + |
| 51 | + return False |
| 52 | + |
| 53 | + # Adds the given entry to the cache at the given index |
| 54 | + def set_block(self, replacement_policy, |
| 55 | + num_blocks_per_set, addr_index, new_entry): |
| 56 | + |
| 57 | + # Place all cache entries in a single set if cache is fully associative |
| 58 | + if addr_index is None: |
| 59 | + blocks = self['0'] |
| 60 | + else: |
| 61 | + blocks = self[addr_index] |
| 62 | + # Replace MRU or LRU entry if number of blocks in set exceeds the limit |
| 63 | + if len(blocks) == num_blocks_per_set: |
| 64 | + # Iterate through the recently-used entries in reverse order for |
| 65 | + # MRU |
| 66 | + if replacement_policy == 'mru': |
| 67 | + recently_used_addrs = reversed(self.recently_used_addrs) |
| 68 | + else: |
| 69 | + recently_used_addrs = self.recently_used_addrs |
| 70 | + # Replace the first matching entry with the entry to add |
| 71 | + for recent_index, recent_tag in recently_used_addrs: |
| 72 | + for i, block in enumerate(blocks): |
| 73 | + if (recent_index == addr_index and |
| 74 | + block['tag'] == recent_tag): |
| 75 | + blocks[i] = new_entry |
| 76 | + return |
| 77 | + else: |
| 78 | + blocks.append(new_entry) |
| 79 | + |
| 80 | + # Simulate the cache by reading the given address references into it |
| 81 | + def read_refs(self, num_blocks_per_set, |
| 82 | + num_words_per_block, replacement_policy, refs): |
| 83 | + |
| 84 | + for ref in refs: |
| 85 | + self.mark_ref_as_last_seen(ref) |
| 86 | + |
| 87 | + # Record if the reference is already in the cache or not |
| 88 | + if self.is_hit(ref.index, ref.tag): |
| 89 | + # Give emphasis to hits in contrast to misses |
| 90 | + ref.cache_status = ReferenceCacheStatus.hit |
| 91 | + else: |
| 92 | + ref.cache_status = ReferenceCacheStatus.miss |
| 93 | + self.set_block( |
| 94 | + replacement_policy=replacement_policy, |
| 95 | + num_blocks_per_set=num_blocks_per_set, |
| 96 | + addr_index=ref.index, |
| 97 | + new_entry=ref.get_cache_entry(num_words_per_block)) |
0 commit comments