mem: Adding verbose debug output in the memory system
[gem5.git] / src / mem / cache / tags / lru.cc
index a99936abf4e7649893591c889fdf407cdc559ae6..f515ed053a2e1932915cb1787c5a6d3abba09315 100644 (file)
@@ -1,4 +1,16 @@
 /*
+ * Copyright (c) 2012 ARM Limited
+ * All rights reserved.
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
  * Copyright (c) 2003-2005 The Regents of The University of Michigan
  * All rights reserved.
  *
 
 #include <string>
 
-#include "mem/cache/base.hh"
 #include "base/intmath.hh"
+#include "debug/Cache.hh"
+#include "debug/CacheRepl.hh"
+#include "mem/cache/tags/cacheset.hh"
 #include "mem/cache/tags/lru.hh"
+#include "mem/cache/base.hh"
 #include "sim/core.hh"
-#include "cacheset.hh"
 
 using namespace std;
 
@@ -91,7 +105,7 @@ LRU::LRU(unsigned _numSets, unsigned _blkSize, unsigned _assoc,
             ++blkIndex;
 
             // invalidate new cache block
-            blk->status = 0;
+            blk->invalidate();
 
             //EGH Fix Me : do we need to initialize blk?
 
@@ -115,7 +129,7 @@ LRU::~LRU()
 }
 
 LRU::BlkType*
-LRU::accessBlock(Addr addr, int &lat, int context_src)
+LRU::accessBlock(Addr addr, Cycles &lat, int master_id)
 {
     Addr tag = extractTag(addr);
     unsigned set = extractSet(addr);
@@ -126,9 +140,9 @@ LRU::accessBlock(Addr addr, int &lat, int context_src)
         sets[set].moveToHead(blk);
         DPRINTF(CacheRepl, "set %x: moving blk %x to MRU\n",
                 set, regenerateBlkAddr(tag, set));
-        if (blk->whenReady > curTick
-            && blk->whenReady - curTick > hitLatency) {
-            lat = blk->whenReady - curTick;
+        if (blk->whenReady > curTick()
+            && cache->ticksToCycles(blk->whenReady - curTick()) > hitLatency) {
+            lat = cache->ticksToCycles(blk->whenReady - curTick());
         }
         blk->refCount += 1;
     }
@@ -152,20 +166,8 @@ LRU::findVictim(Addr addr, PacketList &writebacks)
     unsigned set = extractSet(addr);
     // grab a replacement candidate
     BlkType *blk = sets[set].blks[assoc-1];
-    if (blk->isValid()) {
-        replacements[0]++;
-        totalRefs += blk->refCount;
-        ++sampledRefs;
-        blk->refCount = 0;
-
-        // deal with evicted block
-        if (blk->contextSrc != -1) {
-            occupancies[blk->contextSrc % cache->numCpus()]--;
-            blk->contextSrc = -1;
-        } else {
-            occupancies[cache->numCpus()]--;
-        }
 
+    if (blk->isValid()) {
         DPRINTF(CacheRepl, "set %x: selecting blk %x for replacement\n",
                 set, regenerateBlkAddr(blk->tag, set));
     }
@@ -173,47 +175,60 @@ LRU::findVictim(Addr addr, PacketList &writebacks)
 }
 
 void
-LRU::insertBlock(Addr addr, BlkType *blk, int context_src)
+LRU::insertBlock(Addr addr, BlkType *blk, int master_id)
 {
     if (!blk->isTouched) {
         tagsInUse++;
         blk->isTouched = true;
         if (!warmedUp && tagsInUse.value() >= warmupBound) {
             warmedUp = true;
-            warmupCycle = curTick;
+            warmupCycle = curTick();
         }
     }
 
+    // If we're replacing a block that was previously valid update
+    // stats for it. This can't be done in findBlock() because a
+    // found block might not actually be replaced there if the
+    // coherence protocol says it can't be.
+    if (blk->isValid()) {
+        replacements[0]++;
+        totalRefs += blk->refCount;
+        ++sampledRefs;
+        blk->refCount = 0;
+
+        // deal with evicted block
+        assert(blk->srcMasterId < cache->system->maxMasters());
+        occupancies[blk->srcMasterId]--;
+
+        blk->invalidate();
+    }
+
+    blk->isTouched = true;
     // Set tag for new block.  Caller is responsible for setting status.
     blk->tag = extractTag(addr);
 
     // deal with what we are bringing in
-    if (context_src != -1) {
-        occupancies[context_src % cache->numCpus()]++;
-    } else {
-        occupancies[cache->numCpus()]++;
-    }
-    blk->contextSrc = context_src;
+    assert(master_id < cache->system->maxMasters());
+    occupancies[master_id]++;
+    blk->srcMasterId = master_id;
 
     unsigned set = extractSet(addr);
     sets[set].moveToHead(blk);
 }
 
 void
-LRU::invalidateBlk(BlkType *blk)
+LRU::invalidate(BlkType *blk)
 {
-    if (blk) {
-        blk->status = 0;
-        blk->isTouched = false;
-        blk->clearLoadLocks();
-        tagsInUse--;
-        if (blk->contextSrc != -1) {
-            occupancies[blk->contextSrc % cache->numCpus()]--;
-            blk->contextSrc = -1;
-        } else {
-            occupancies[cache->numCpus()]--;
-        }
-    }
+    assert(blk);
+    assert(blk->isValid());
+    tagsInUse--;
+    assert(blk->srcMasterId < cache->system->maxMasters());
+    occupancies[blk->srcMasterId]--;
+    blk->srcMasterId = Request::invldMasterId;
+
+    // should be evicted before valid blocks
+    unsigned set = blk->set;
+    sets[set].moveToTail(blk);
 }
 
 void
@@ -224,6 +239,23 @@ LRU::clearLocks()
     }
 }
 
+std::string
+LRU::print() const {
+    std::string cache_state;
+    for (unsigned i = 0; i < numSets; ++i) {
+        // link in the data blocks
+        for (unsigned j = 0; j < assoc; ++j) {
+            BlkType *blk = sets[i].blks[j];
+            if (blk->isValid())
+                cache_state += csprintf("\tset: %d block: %d %s\n", i, j,
+                        blk->print());
+        }
+    }
+    if (cache_state.empty())
+        cache_state = "no valid tags\n";
+    return cache_state;
+}
+
 void
 LRU::cleanupRefs()
 {