misc: Standardize the way create() constructs SimObjects.
[gem5.git] / src / mem / cache / tags / sector_tags.cc
index f256807e2657eaf9f7dad999cf806334ca04e520..aa6cb24ac70f36f98efcbfde38f571e934decdb4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Inria
+ * Copyright (c) 2018, 2020 Inria
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -24,8 +24,6 @@
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Authors: Daniel Carvalho
  */
 
 /**
 #include "base/intmath.hh"
 #include "base/logging.hh"
 #include "base/types.hh"
-#include "debug/CacheRepl.hh"
 #include "mem/cache/base.hh"
 #include "mem/cache/replacement_policies/base.hh"
+#include "mem/cache/replacement_policies/replaceable_entry.hh"
 #include "mem/cache/tags/indexing_policies/base.hh"
 
-SectorTags::SectorTags(const SectorTagsParams *p)
-    : BaseTags(p), allocAssoc(p->assoc),
-      sequentialAccess(p->sequential_access),
-      replacementPolicy(p->replacement_policy),
-      numBlocksPerSector(p->num_blocks_per_sector),
-      numSectors(numBlocks / p->num_blocks_per_sector), blks(numBlocks),
-      secBlks(numSectors), sectorShift(floorLog2(blkSize)),
-      sectorMask(numBlocksPerSector - 1)
+SectorTags::SectorTags(const SectorTagsParams &p)
+    : BaseTags(p), allocAssoc(p.assoc),
+      sequentialAccess(p.sequential_access),
+      replacementPolicy(p.replacement_policy),
+      numBlocksPerSector(p.num_blocks_per_sector),
+      numSectors(numBlocks / numBlocksPerSector),
+      sectorShift(floorLog2(blkSize)), sectorMask(numBlocksPerSector - 1),
+      sectorStats(stats, *this)
 {
     // Check parameters
     fatal_if(blkSize < 4 || !isPowerOf2(blkSize),
@@ -64,10 +62,11 @@ SectorTags::SectorTags(const SectorTagsParams *p)
 }
 
 void
-SectorTags::init(BaseCache* cache)
+SectorTags::tagsInit()
 {
-    // Set parent cache
-    setCache(cache);
+    // Create blocks and sector blocks
+    blks = std::vector<SectorSubBlk>(numBlocks);
+    secBlks = std::vector<SectorBlk>(numSectors);
 
     // Initialize all blocks
     unsigned blk_index = 0;       // index into blks array
@@ -77,9 +76,6 @@ SectorTags::init(BaseCache* cache)
         // Locate next cache sector
         SectorBlk* sec_blk = &secBlks[sec_blk_index];
 
-        // Link block to indexing policy
-        indexingPolicy->setEntry(sec_blk, sec_blk_index);
-
         // Associate a replacement data entry to the sector
         sec_blk->replacementData = replacementPolicy->instantiateEntry();
 
@@ -107,6 +103,9 @@ SectorTags::init(BaseCache* cache)
             // Update block index
             ++blk_index;
         }
+
+        // Link block to indexing policy
+        indexingPolicy->setEntry(sec_blk, sec_blk_index);
     }
 }
 
@@ -125,7 +124,7 @@ SectorTags::invalidate(CacheBlk *blk)
     // in the sector.
     if (!sector_blk->isValid()) {
         // Decrease the number of tags in use
-        tagsInUse--;
+        stats.tagsInUse--;
 
         // Invalidate replacement data, as we're invalidating the sector
         replacementPolicy->invalidate(sector_blk->replacementData);
@@ -140,29 +139,19 @@ SectorTags::accessBlock(Addr addr, bool is_secure, Cycles &lat)
     // Access all tags in parallel, hence one in each way.  The data side
     // either accesses all blocks in parallel, or one block sequentially on
     // a hit.  Sequential access with a miss doesn't access data.
-    tagAccesses += allocAssoc;
+    stats.tagAccesses += allocAssoc;
     if (sequentialAccess) {
         if (blk != nullptr) {
-            dataAccesses += 1;
+            stats.dataAccesses += 1;
         }
     } else {
-        dataAccesses += allocAssoc*numBlocksPerSector;
+        stats.dataAccesses += allocAssoc*numBlocksPerSector;
     }
 
+    // If a cache hit
     if (blk != nullptr) {
-        // If a cache hit
-        lat = accessLatency;
-        // Check if the block to be accessed is available. If not,
-        // apply the accessLatency on top of block->whenReady.
-        if (blk->whenReady > curTick() &&
-            cache->ticksToCycles(blk->whenReady - curTick()) >
-            accessLatency) {
-            lat = cache->ticksToCycles(blk->whenReady - curTick()) +
-            accessLatency;
-        }
-
         // Update number of references to accessed block
-        blk->refCount++;
+        blk->increaseRefCount();
 
         // Get block's sector
         SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
@@ -171,39 +160,36 @@ SectorTags::accessBlock(Addr addr, bool is_secure, Cycles &lat)
         // Update replacement data of accessed block, which is shared with
         // the whole sector it belongs to
         replacementPolicy->touch(sector_blk->replacementData);
-    } else {
-        // If a cache miss
-        lat = lookupLatency;
     }
 
+    // The tag lookup latency is the same for a hit or a miss
+    lat = lookupLatency;
+
     return blk;
 }
 
 void
-SectorTags::insertBlock(const Addr addr, const bool is_secure,
-                        const int src_master_ID, const uint32_t task_ID,
-                        CacheBlk *blk)
+SectorTags::insertBlock(const PacketPtr pkt, CacheBlk *blk)
 {
-    // Do common block insertion functionality
-    BaseTags::insertBlock(addr, is_secure, src_master_ID, task_ID, blk);
-
     // Get block's sector
     SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
     const SectorBlk* sector_blk = sub_blk->getSectorBlock();
 
     // When a block is inserted, the tag is only a newly used tag if the
     // sector was not previously present in the cache.
-    // This assumes BaseTags::insertBlock does not set the valid bit.
     if (sector_blk->isValid()) {
         // An existing entry's replacement data is just updated
         replacementPolicy->touch(sector_blk->replacementData);
     } else {
         // Increment tag counter
-        tagsInUse++;
+        stats.tagsInUse++;
 
         // A new entry resets the replacement data
         replacementPolicy->reset(sector_blk->replacementData);
     }
+
+    // Do common block insertion functionality
+    BaseTags::insertBlock(pkt, blk);
 }
 
 CacheBlk*
@@ -223,8 +209,7 @@ SectorTags::findBlock(Addr addr, bool is_secure) const
     // Search for block
     for (const auto& sector : entries) {
         auto blk = static_cast<SectorBlk*>(sector)->blks[offset];
-        if (blk->getTag() == tag && blk->isValid() &&
-            blk->isSecure() == is_secure) {
+        if (blk->matchTag(tag, is_secure)) {
             return blk;
         }
     }
@@ -234,8 +219,8 @@ SectorTags::findBlock(Addr addr, bool is_secure) const
 }
 
 CacheBlk*
-SectorTags::findVictim(Addr addr, const bool is_secure,
-                       std::vector<CacheBlk*>& evict_blks) const
+SectorTags::findVictim(Addr addr, const bool is_secure, const std::size_t size,
+                       std::vector<CacheBlk*>& evict_blks)
 {
     // Get possible entries to be victimized
     const std::vector<ReplaceableEntry*> sector_entries =
@@ -246,8 +231,7 @@ SectorTags::findVictim(Addr addr, const bool is_secure,
     SectorBlk* victim_sector = nullptr;
     for (const auto& sector : sector_entries) {
         SectorBlk* sector_blk = static_cast<SectorBlk*>(sector);
-        if ((tag == sector_blk->getTag()) && sector_blk->isValid() &&
-            (is_secure == sector_blk->isSecure())){
+        if (sector_blk->matchTag(tag, is_secure)) {
             victim_sector = sector_blk;
             break;
         }
@@ -265,21 +249,21 @@ SectorTags::findVictim(Addr addr, const bool is_secure,
 
     // Get evicted blocks. Blocks are only evicted if the sectors mismatch and
     // the currently existing sector is valid.
-    if ((tag == victim_sector->getTag()) &&
-        (is_secure == victim_sector->isSecure())){
+    if (victim_sector->matchTag(tag, is_secure)) {
         // It would be a hit if victim was valid, and upgrades do not call
         // findVictim, so it cannot happen
         assert(!victim->isValid());
     } else {
         // The whole sector must be evicted to make room for the new sector
         for (const auto& blk : victim_sector->blks){
-            evict_blks.push_back(blk);
+            if (blk->isValid()) {
+                evict_blks.push_back(blk);
+            }
         }
     }
 
-    DPRINTF(CacheRepl, "set %x, way %x, sector offset %x: selecting blk " \
-            "for replacement\n", victim->getSet(), victim->getWay(),
-            victim->getSectorOffset());
+    // Update number of sub-blocks evicted due to a replacement
+    sectorStats.evictionsReplacement[evict_blks.size()]++;
 
     return victim;
 }
@@ -295,10 +279,32 @@ SectorTags::regenerateBlkAddr(const CacheBlk* blk) const
 {
     const SectorSubBlk* blk_cast = static_cast<const SectorSubBlk*>(blk);
     const SectorBlk* sec_blk = blk_cast->getSectorBlock();
-    const Addr sec_addr = indexingPolicy->regenerateAddr(blk->tag, sec_blk);
+    const Addr sec_addr =
+        indexingPolicy->regenerateAddr(blk->getTag(), sec_blk);
     return sec_addr | ((Addr)blk_cast->getSectorOffset() << sectorShift);
 }
 
+SectorTags::SectorTagsStats::SectorTagsStats(BaseTagStats &base_group,
+    SectorTags& _tags)
+  : Stats::Group(&base_group), tags(_tags),
+    evictionsReplacement(this, "evictions_replacement",
+        "Number of blocks evicted due to a replacement")
+{
+}
+
+void
+SectorTags::SectorTagsStats::regStats()
+{
+    Stats::Group::regStats();
+
+    evictionsReplacement.init(tags.numBlocksPerSector + 1);
+    for (unsigned i = 0; i <= tags.numBlocksPerSector; ++i) {
+        evictionsReplacement.subname(i, std::to_string(i));
+        evictionsReplacement.subdesc(i, "Number of replacements that caused " \
+            "the eviction of " + std::to_string(i) + " blocks");
+    }
+}
+
 void
 SectorTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
 {
@@ -319,10 +325,10 @@ SectorTags::anyBlk(std::function<bool(CacheBlk &)> visitor)
 }
 
 SectorTags *
-SectorTagsParams::create()
+SectorTagsParams::create() const
 {
     // There must be a indexing policy
     fatal_if(!indexing_policy, "An indexing policy is required");
 
-    return new SectorTags(this);
+    return new SectorTags(*this);
 }