ruby: reset and dump stats along with reset of the system
[gem5.git] / src / mem / ruby / system / System.cc
index ad67cdc8024a28727dc4d7656b65c6f096731855..bbdcb3ebbbe587f0b348b342e069f23529acc0d5 100644 (file)
@@ -1,6 +1,5 @@
-
 /*
- * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
+ * Copyright (c) 1999-2011 Mark D. Hill and David A. Wood
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- * RubySystem.cc
- *
- * Description: See System.hh
- *
- * $Id$
- *
- */
+#include <fcntl.h>
+#include <zlib.h>
 
+#include <cstdio>
 
-#include "mem/ruby/system/System.hh"
+#include "base/intmath.hh"
+#include "base/statistics.hh"
+#include "debug/RubyCacheTrace.hh"
+#include "debug/RubySystem.hh"
 #include "mem/ruby/common/Address.hh"
-#include "mem/ruby/profiler/Profiler.hh"
 #include "mem/ruby/network/Network.hh"
-#include "mem/ruby/recorder/Tracer.hh"
-#include "mem/protocol/Protocol.hh"
-#include "mem/ruby/buffers/MessageBuffer.hh"
-#include "mem/ruby/system/Sequencer.hh"
-#include "mem/ruby/system/DMASequencer.hh"
-#include "mem/ruby/system/MemoryVector.hh"
-#include "mem/protocol/ControllerFactory.hh"
-#include "mem/ruby/slicc_interface/AbstractController.hh"
-#include "mem/ruby/system/CacheMemory.hh"
-#include "mem/ruby/system/DirectoryMemory.hh"
-#include "mem/ruby/network/simple/Topology.hh"
-#include "mem/ruby/network/simple/SimpleNetwork.hh"
-#include "mem/ruby/system/RubyPort.hh"
-#include "mem/ruby/network/garnet-flexible-pipeline/GarnetNetwork.hh"
-#include "mem/ruby/network/garnet-fixed-pipeline/GarnetNetwork_d.hh"
-#include "mem/ruby/system/MemoryControl.hh"
+#include "mem/ruby/profiler/Profiler.hh"
+#include "mem/ruby/system/System.hh"
+#include "sim/eventq.hh"
+#include "sim/simulate.hh"
+
+using namespace std;
 
 int RubySystem::m_random_seed;
 bool RubySystem::m_randomization;
-int RubySystem::m_tech_nm;
-int RubySystem::m_freq_mhz;
 int RubySystem::m_block_size_bytes;
 int RubySystem::m_block_size_bits;
 uint64 RubySystem::m_memory_size_bytes;
 int RubySystem::m_memory_size_bits;
 
-map< string, RubyPort* > RubySystem::m_ports;
-map< string, CacheMemory* > RubySystem::m_caches;
-map< string, DirectoryMemory* > RubySystem::m_directories;
-map< string, Sequencer* > RubySystem::m_sequencers;
-map< string, DMASequencer* > RubySystem::m_dma_sequencers;
-map< string, AbstractController* > RubySystem::m_controllers;
-map< string, MemoryControl* > RubySystem::m_memorycontrols;
+RubySystem::RubySystem(const Params *p)
+    : ClockedObject(p)
+{
+    if (g_system_ptr != NULL)
+        fatal("Only one RubySystem object currently allowed.\n");
+
+    m_random_seed = p->random_seed;
+    srandom(m_random_seed);
+    m_randomization = p->randomization;
+
+    m_block_size_bytes = p->block_size_bytes;
+    assert(isPowerOf2(m_block_size_bytes));
+    m_block_size_bits = floorLog2(m_block_size_bytes);
 
+    m_memory_size_bytes = p->mem_size;
+    if (m_memory_size_bytes == 0) {
+        m_memory_size_bits = 0;
+    } else {
+        m_memory_size_bits = floorLog2(m_memory_size_bytes);
+    }
 
-Network* RubySystem::m_network_ptr;
-map< string, Topology*> RubySystem::m_topologies;
-Profiler* RubySystem::m_profiler_ptr;
-Tracer* RubySystem::m_tracer_ptr;
+    g_system_ptr = this;
+    if (p->no_mem_vec) {
+        m_mem_vec_ptr = NULL;
+    } else {
+        m_mem_vec_ptr = new MemoryVector;
+        m_mem_vec_ptr->resize(m_memory_size_bytes);
+    }
 
-MemoryVector* RubySystem::m_mem_vec_ptr;
+    // Print ruby configuration and stats at exit and when asked for
+    Stats::registerDumpCallback(new RubyDumpStatsCallback(p->stats_filename,
+                                                          this));
 
+    m_warmup_enabled = false;
+    m_cooldown_enabled = false;
+}
 
-RubySystem* RubySystem::create(const vector <RubyObjConf> & sys_conf)
+void
+RubySystem::init()
 {
-  if (g_system_ptr == NULL)
-    return new RubySystem(sys_conf);
-  return g_system_ptr;
+    m_profiler_ptr->clearStats();
+    m_network_ptr->clearStats();
 }
 
-void RubySystem::init(const vector<string> & argv)
+void
+RubySystem::registerNetwork(Network* network_ptr)
 {
-  for (size_t i=0; i < argv.size(); i+=2) {
-    if (argv[i] == "random_seed") {
-      m_random_seed = atoi(argv[i+1].c_str());
-      srandom(m_random_seed);
-    } else if (argv[i] == "randomization") {
-      m_randomization = string_to_bool(argv[i+1]);
-    } else if (argv[i] == "tech_nm") {
-      m_tech_nm = atoi(argv[i+1].c_str());
-    } else if (argv[i] == "freq_mhz") {
-      m_freq_mhz = atoi(argv[i+1].c_str());
-    } else if (argv[i] == "block_size_bytes") {
-      m_block_size_bytes = atoi(argv[i+1].c_str());
-      assert(is_power_of_2(m_block_size_bytes));
-      m_block_size_bits = log_int(m_block_size_bytes);
-    } else if (argv[i] == "debug") {
-
-    } else if (argv[i] == "tracer") {
-
-    } else if (argv[i] == "profiler") {
-
-  //  } else if (argv[i] == "MI_example") {
+  m_network_ptr = network_ptr;
+}
 
-    } else {
-      cerr << "Error: Unknown RubySystem config parameter -- " << argv[i] << endl;
-     assert(0);
-    }
-  }
+void
+RubySystem::registerProfiler(Profiler* profiler_ptr)
+{
+  m_profiler_ptr = profiler_ptr;
+}
+
+void
+RubySystem::registerAbstractController(AbstractController* cntrl)
+{
+  m_abs_cntrl_vec.push_back(cntrl);
+}
+
+void
+RubySystem::registerSparseMemory(SparseMemory* s)
+{
+    m_sparse_memory_vector.push_back(s);
+}
+
+void
+RubySystem::registerMemController(MemoryControl *mc) {
+    m_memory_controller_vec.push_back(mc);
+}
+
+RubySystem::~RubySystem()
+{
+    delete m_network_ptr;
+    delete m_profiler_ptr;
+    if (m_mem_vec_ptr)
+        delete m_mem_vec_ptr;
 }
 
-RubySystem::RubySystem(const vector <RubyObjConf> & sys_conf)
+void
+RubySystem::printStats(ostream& out)
 {
-  //  DEBUG_MSG(SYSTEM_COMP, MedPrio,"initializing");
-
-  for (size_t i=0;i<sys_conf.size(); i++) {
-    const string & type = sys_conf[i].type;
-    const string & name = sys_conf[i].name;
-    const vector<string> & argv = sys_conf[i].argv;
-    if (type == "System") {
-      init(argv);  // initialize system-wide variables before doing anything else!
-    } else if (type == "Debug") {
-      g_debug_ptr = new Debug(name, argv);
+    const time_t T = time(NULL);
+    tm *localTime = localtime(&T);
+    char buf[100];
+    strftime(buf, 100, "%b/%d/%Y %H:%M:%S", localTime);
+
+    out << "Real time: " << buf << endl;
+
+    m_profiler_ptr->printStats(out);
+    m_network_ptr->printStats(out);
+}
+
+void
+RubySystem::writeCompressedTrace(uint8_t *raw_data, string filename,
+                                 uint64 uncompressed_trace_size)
+{
+    // Create the checkpoint file for the memory
+    string thefile = Checkpoint::dir() + "/" + filename.c_str();
+
+    int fd = creat(thefile.c_str(), 0664);
+    if (fd < 0) {
+        perror("creat");
+        fatal("Can't open memory trace file '%s'\n", filename);
     }
-  }
-
-  assert( g_debug_ptr != NULL);
-  g_eventQueue_ptr = new RubyEventQueue;
-  g_system_ptr = this;
-  m_mem_vec_ptr = new MemoryVector;
-
-  /* object contruction is broken into two steps (Constructor and init) to avoid cyclic dependencies
-   *  e.g. a sequencer needs a pointer to a controller and a controller needs a pointer to a sequencer
-   */
-
-  vector<string> memory_control_names;
-
-  for (size_t i=0;i<sys_conf.size(); i++) {
-    const string & type = sys_conf[i].type;
-    const string & name = sys_conf[i].name;
-    if (type == "System" || type == "Debug")
-      continue;
-    else if (type == "SetAssociativeCache")
-      m_caches[name] = new CacheMemory(name);
-    else if (type == "DirectoryMemory")
-      m_directories[name] = new DirectoryMemory(name);
-    else if (type == "Sequencer") {
-      m_sequencers[name] = new Sequencer(name);
-      m_ports[name] = m_sequencers[name];
-    } else if (type == "DMASequencer") {
-      m_dma_sequencers[name] = new DMASequencer(name);
-      m_ports[name] = m_dma_sequencers[name];
-    } else if (type == "Topology") {
-      assert(m_topologies.size() == 0); // only one toplogy at a time is supported right now
-      m_topologies[name] = new Topology(name);
-    } else if (type == "SimpleNetwork") {
-      assert(m_network_ptr == NULL); // only one network at a time is supported right now
-      m_network_ptr = new SimpleNetwork(name);
-    } else if (type.find("generated") == 0) {
-      string controller_type = type.substr(10);
-      m_controllers[name] = ControllerFactory::createController(controller_type, name);
-//      printf ("ss: generated %s \n", controller_type);
-//added by SS
-    } else if (type == "Tracer") {
-      //m_tracers[name] = new Tracer(name);
-      m_tracer_ptr = new Tracer(name);
-    } else if (type == "Profiler") {
-      m_profiler_ptr = new Profiler(name);
-    } else if (type == "GarnetNetwork") {
-      assert(m_network_ptr == NULL); // only one network at a time is supported right now
-      m_network_ptr = new GarnetNetwork(name);
-    } else if (type == "GarnetNetwork_d") {
-      assert(m_network_ptr == NULL); // only one network at a time is supported right now
-      m_network_ptr = new GarnetNetwork_d(name);
-    } else if (type == "MemoryControl") {
-      m_memorycontrols[name] = new MemoryControl(name);
-      memory_control_names.push_back (name);
-    } else {
-      cerr << "Error: Unknown object type -- " << type << endl;
-      assert(0);
+
+    gzFile compressedMemory = gzdopen(fd, "wb");
+    if (compressedMemory == NULL)
+        fatal("Insufficient memory to allocate compression state for %s\n",
+              filename);
+
+    if (gzwrite(compressedMemory, raw_data, uncompressed_trace_size) !=
+        uncompressed_trace_size) {
+        fatal("Write failed on memory trace file '%s'\n", filename);
+    }
+
+    if (gzclose(compressedMemory)) {
+        fatal("Close failed on memory trace file '%s'\n", filename);
     }
-  }
-
-  for (size_t i=0;i<sys_conf.size(); i++) {
-    string type = sys_conf[i].type;
-    string name = sys_conf[i].name;
-    const vector<string> & argv = sys_conf[i].argv;
-    if (type == "Topology")
-      m_topologies[name]->init(argv);
-  }
-
-  for (size_t i=0;i<sys_conf.size(); i++) {
-    string type = sys_conf[i].type;
-    string name = sys_conf[i].name;
-    const vector<string> & argv = sys_conf[i].argv;
-    if (type == "SimpleNetwork" || type == "GarnetNetwork" || type == "GarnetNetwork_d"){
-      m_network_ptr->init(argv);
+    delete raw_data;
+}
+
+void
+RubySystem::serialize(std::ostream &os)
+{
+    m_cooldown_enabled = true;
+
+    vector<Sequencer*> sequencer_map;
+    Sequencer* sequencer_ptr = NULL;
+    int cntrl_id = -1;
+
+
+    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
+        sequencer_map.push_back(m_abs_cntrl_vec[cntrl]->getSequencer());
+        if (sequencer_ptr == NULL) {
+            sequencer_ptr = sequencer_map[cntrl];
+            cntrl_id = cntrl;
+        }
     }
-  }
-
-  for (size_t i=0;i<sys_conf.size(); i++) {
-    string type = sys_conf[i].type;
-    string name = sys_conf[i].name;
-    const vector<string> & argv = sys_conf[i].argv;
-    if (type == "MemoryControl" ){
-      m_memorycontrols[name]->init(argv);
+
+    assert(sequencer_ptr != NULL);
+
+    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
+        if (sequencer_map[cntrl] == NULL) {
+            sequencer_map[cntrl] = sequencer_ptr;
+        }
     }
-  }
-
-  for (size_t i=0;i<sys_conf.size(); i++) {
-    string type = sys_conf[i].type;
-    string name = sys_conf[i].name;
-    const vector<string> & argv = sys_conf[i].argv;
-    if (type == "System" || type == "Debug")
-      continue;
-    else if (type == "SetAssociativeCache")
-      m_caches[name]->init(argv);
-    else if (type == "DirectoryMemory")
-      m_directories[name]->init(argv);
-    else if (type == "MemoryControl")
-      continue;
-    else if (type == "Sequencer")
-      m_sequencers[name]->init(argv);
-    else if (type == "DMASequencer")
-      m_dma_sequencers[name]->init(argv);
-    else if (type == "Topology")
-      continue;
-    else if (type == "SimpleNetwork" || type == "GarnetNetwork" || type == "GarnetNetwork_d")
-      continue;
-    else if (type.find("generated") == 0) {
-      string controller_type = type.substr(11);
-      m_controllers[name]->init(m_network_ptr, argv);
+
+    DPRINTF(RubyCacheTrace, "Recording Cache Trace\n");
+    // Create the CacheRecorder and record the cache trace
+    m_cache_recorder = new CacheRecorder(NULL, 0, sequencer_map);
+
+    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
+        m_abs_cntrl_vec[cntrl]->recordCacheTrace(cntrl, m_cache_recorder);
     }
-//added by SS
-    else if (type == "Tracer")
-      //m_tracers[name]->init(argv);
-      m_tracer_ptr->init(argv);
-    else if (type == "Profiler")
-      m_profiler_ptr->init(argv, memory_control_names);
-//    else if (type == "MI_example"){
-//    }
-    else
-      assert(0);
-  }
-
-//  m_profiler_ptr = new Profiler;
-
-  // calculate system-wide parameters
-  m_memory_size_bytes = 0;
-  DirectoryMemory* prev = NULL;
-  for (map< string, DirectoryMemory*>::const_iterator it = m_directories.begin();
-       it != m_directories.end(); it++) {
-    if (prev != NULL)
-      assert((*it).second->getSize() == prev->getSize()); // must be equal for proper address mapping
-    m_memory_size_bytes += (*it).second->getSize();
-    prev = (*it).second;
-  }
-  m_mem_vec_ptr->setSize(m_memory_size_bytes);
-  m_memory_size_bits = log_int(m_memory_size_bytes);
-
-//  m_tracer_ptr = new Tracer;
-  DEBUG_MSG(SYSTEM_COMP, MedPrio,"finished initializing");
-  DEBUG_NEWLINE(SYSTEM_COMP, MedPrio);
+
+    DPRINTF(RubyCacheTrace, "Cache Trace Complete\n");
+    // save the current tick value
+    Tick curtick_original = curTick();
+    // save the event queue head
+    Event* eventq_head = eventq->replaceHead(NULL);
+    DPRINTF(RubyCacheTrace, "Recording current tick %ld and event queue\n",
+            curtick_original);
+
+    // Schedule an event to start cache cooldown
+    DPRINTF(RubyCacheTrace, "Starting cache flush\n");
+    enqueueRubyEvent(curTick());
+    simulate();
+    DPRINTF(RubyCacheTrace, "Cache flush complete\n");
+
+    // Restore eventq head
+    eventq_head = eventq->replaceHead(eventq_head);
+    // Restore curTick
+    curTick(curtick_original);
+
+    uint8_t *raw_data = NULL;
+
+    if (m_mem_vec_ptr != NULL) {
+        uint64 memory_trace_size = m_mem_vec_ptr->collatePages(raw_data);
+
+        string memory_trace_file = name() + ".memory.gz";
+        writeCompressedTrace(raw_data, memory_trace_file,
+                             memory_trace_size);
+
+        SERIALIZE_SCALAR(memory_trace_file);
+        SERIALIZE_SCALAR(memory_trace_size);
+
+    } else {
+        for (int i = 0; i < m_sparse_memory_vector.size(); ++i) {
+            m_sparse_memory_vector[i]->recordBlocks(cntrl_id,
+                                                    m_cache_recorder);
+        }
+    }
+
+    // Aggergate the trace entries together into a single array
+    raw_data = new uint8_t[4096];
+    uint64 cache_trace_size = m_cache_recorder->aggregateRecords(&raw_data,
+                                                                 4096);
+    string cache_trace_file = name() + ".cache.gz";
+    writeCompressedTrace(raw_data, cache_trace_file, cache_trace_size);
+
+    SERIALIZE_SCALAR(cache_trace_file);
+    SERIALIZE_SCALAR(cache_trace_size);
+
+    m_cooldown_enabled = false;
 }
 
-RubySystem::~RubySystem()
+void
+RubySystem::readCompressedTrace(string filename, uint8_t *&raw_data,
+                                uint64& uncompressed_trace_size)
 {
+    // Read the trace file
+    gzFile compressedTrace;
+
+    // trace file
+    int fd = open(filename.c_str(), O_RDONLY);
+    if (fd < 0) {
+        perror("open");
+        fatal("Unable to open trace file %s", filename);
+    }
+
+    compressedTrace = gzdopen(fd, "rb");
+    if (compressedTrace == NULL) {
+        fatal("Insufficient memory to allocate compression state for %s\n",
+              filename);
+    }
 
+    raw_data = new uint8_t[uncompressed_trace_size];
+    if (gzread(compressedTrace, raw_data, uncompressed_trace_size) <
+            uncompressed_trace_size) {
+        fatal("Unable to read complete trace from file %s\n", filename);
+    }
+
+    if (gzclose(compressedTrace)) {
+        fatal("Failed to close cache trace file '%s'\n", filename);
+    }
 }
 
-void RubySystem::printSystemConfig(ostream & out)
+void
+RubySystem::unserialize(Checkpoint *cp, const string &section)
 {
-  out << "RubySystem config:" << endl;
-  out << "  random_seed: " << m_random_seed << endl;
-  out << "  randomization: " << m_randomization << endl;
-  out << "  tech_nm: " << m_tech_nm << endl;
-  out << "  freq_mhz: " << m_freq_mhz << endl;
-  out << "  block_size_bytes: " << m_block_size_bytes << endl;
-  out << "  block_size_bits: " << m_block_size_bits << endl;
-  out << "  memory_size_bytes: " << m_memory_size_bytes << endl;
-  out << "  memory_size_bits: " << m_memory_size_bits << endl;
+    //
+    // The main purpose for clearing stats in the unserialize process is so
+    // that the profiler can correctly set its start time to the unserialized
+    // value of curTick()
+    //
+    resetStats();
+    uint8_t *uncompressed_trace = NULL;
+
+    if (m_mem_vec_ptr != NULL) {
+        string memory_trace_file;
+        uint64 memory_trace_size = 0;
+
+        UNSERIALIZE_SCALAR(memory_trace_file);
+        UNSERIALIZE_SCALAR(memory_trace_size);
+        memory_trace_file = cp->cptDir + "/" + memory_trace_file;
+
+        readCompressedTrace(memory_trace_file, uncompressed_trace,
+                            memory_trace_size);
+        m_mem_vec_ptr->populatePages(uncompressed_trace);
+
+        delete uncompressed_trace;
+        uncompressed_trace = NULL;
+    }
 
+    string cache_trace_file;
+    uint64 cache_trace_size = 0;
+
+    UNSERIALIZE_SCALAR(cache_trace_file);
+    UNSERIALIZE_SCALAR(cache_trace_size);
+    cache_trace_file = cp->cptDir + "/" + cache_trace_file;
+
+    readCompressedTrace(cache_trace_file, uncompressed_trace,
+                        cache_trace_size);
+    m_warmup_enabled = true;
+
+    vector<Sequencer*> sequencer_map;
+    Sequencer* t = NULL;
+    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
+        sequencer_map.push_back(m_abs_cntrl_vec[cntrl]->getSequencer());
+        if (t == NULL) t = sequencer_map[cntrl];
+    }
+
+    assert(t != NULL);
+
+    for (int cntrl = 0; cntrl < m_abs_cntrl_vec.size(); cntrl++) {
+        if (sequencer_map[cntrl] == NULL) {
+            sequencer_map[cntrl] = t;
+        }
+    }
+
+    m_cache_recorder = new CacheRecorder(uncompressed_trace, cache_trace_size,
+                                         sequencer_map);
 }
 
-void RubySystem::printConfig(ostream& out)
+void
+RubySystem::startup()
 {
-  out << "\n================ Begin RubySystem Configuration Print ================\n\n";
-  printSystemConfig(out);
-  for (map<string, AbstractController*>::const_iterator it = m_controllers.begin();
-       it != m_controllers.end(); it++) {
-    (*it).second->printConfig(out);
-  }
-  for (map<string, CacheMemory*>::const_iterator it = m_caches.begin();
-       it != m_caches.end(); it++) {
-    (*it).second->printConfig(out);
-  }
-  DirectoryMemory::printGlobalConfig(out);
-  for (map<string, DirectoryMemory*>::const_iterator it = m_directories.begin();
-       it != m_directories.end(); it++) {
-    (*it).second->printConfig(out);
-  }
-  for (map<string, Sequencer*>::const_iterator it = m_sequencers.begin();
-       it != m_sequencers.end(); it++) {
-    (*it).second->printConfig(out);
-  }
-
-  m_network_ptr->printConfig(out);
-  m_profiler_ptr->printConfig(out);
-
-  out << "\n================ End RubySystem Configuration Print ================\n\n";
+    if (m_warmup_enabled) {
+        // save the current tick value
+        Tick curtick_original = curTick();
+        // save the event queue head
+        Event* eventq_head = eventq->replaceHead(NULL);
+        // set curTick to 0 and reset Ruby System's clock
+        curTick(0);
+        resetClock();
+
+        // Schedule an event to start cache warmup
+        enqueueRubyEvent(curTick());
+        simulate();
+
+        delete m_cache_recorder;
+        m_cache_recorder = NULL;
+        m_warmup_enabled = false;
+
+        // reset DRAM so that it's not waiting for events on the old event
+        // queue
+        for (int i = 0; i < m_memory_controller_vec.size(); ++i) {
+            m_memory_controller_vec[i]->reset();
+        }
+
+        // Restore eventq head
+        eventq_head = eventq->replaceHead(eventq_head);
+        // Restore curTick and Ruby System's clock
+        curTick(curtick_original);
+        resetClock();
+    }
 }
 
-void RubySystem::printStats(ostream& out)
+void
+RubySystem::RubyEvent::process()
 {
+    if (ruby_system->m_warmup_enabled) {
+        ruby_system->m_cache_recorder->enqueueNextFetchRequest();
+    }  else if (ruby_system->m_cooldown_enabled) {
+        ruby_system->m_cache_recorder->enqueueNextFlushRequest();
+    }
+}
 
-  const time_t T = time(NULL);
-  tm *localTime = localtime(&T);
-  char buf[100];
-  strftime(buf, 100, "%b/%d/%Y %H:%M:%S", localTime);
-
-  out << "Real time: " << buf << endl;
-
-  m_profiler_ptr->printStats(out);
-  m_network_ptr->printStats(out);
-  for (map<string, CacheMemory*>::const_iterator it = m_caches.begin();
-       it != m_caches.end(); it++) {
-    (*it).second->printStats(out);
-  }
-  for (map<string, AbstractController*>::const_iterator it = m_controllers.begin();
-       it != m_controllers.end(); it++) {
-    (*it).second->printStats(out);
-  }
+void
+RubySystem::resetStats()
+{
+    m_profiler_ptr->clearStats();
+    m_network_ptr->clearStats();
 }
 
-void RubySystem::clearStats() const
+bool
+RubySystem::functionalRead(PacketPtr pkt)
 {
-  m_profiler_ptr->clearStats();
-  m_network_ptr->clearStats();
-  for (map<string, CacheMemory*>::const_iterator it = m_caches.begin();
-       it != m_caches.end(); it++) {
-    (*it).second->clearStats();
-  }
-  for (map<string, AbstractController*>::const_iterator it = m_controllers.begin();
-       it != m_controllers.end(); it++) {
-    (*it).second->clearStats();
-  }
+    Address address(pkt->getAddr());
+    Address line_address(address);
+    line_address.makeLineAddress();
+
+    AccessPermission access_perm = AccessPermission_NotPresent;
+    int num_controllers = m_abs_cntrl_vec.size();
+
+    DPRINTF(RubySystem, "Functional Read request for %s\n",address);
+
+    unsigned int num_ro = 0;
+    unsigned int num_rw = 0;
+    unsigned int num_busy = 0;
+    unsigned int num_backing_store = 0;
+    unsigned int num_invalid = 0;
+
+    // In this loop we count the number of controllers that have the given
+    // address in read only, read write and busy states.
+    for (unsigned int i = 0; i < num_controllers; ++i) {
+        access_perm = m_abs_cntrl_vec[i]-> getAccessPermission(line_address);
+        if (access_perm == AccessPermission_Read_Only)
+            num_ro++;
+        else if (access_perm == AccessPermission_Read_Write)
+            num_rw++;
+        else if (access_perm == AccessPermission_Busy)
+            num_busy++;
+        else if (access_perm == AccessPermission_Backing_Store)
+            // See RubySlicc_Exports.sm for details, but Backing_Store is meant
+            // to represent blocks in memory *for Broadcast/Snooping protocols*,
+            // where memory has no idea whether it has an exclusive copy of data
+            // or not.
+            num_backing_store++;
+        else if (access_perm == AccessPermission_Invalid ||
+                 access_perm == AccessPermission_NotPresent)
+            num_invalid++;
+    }
+    assert(num_rw <= 1);
+
+    uint8_t *data = pkt->getPtr<uint8_t>(true);
+    unsigned int size_in_bytes = pkt->getSize();
+    unsigned startByte = address.getAddress() - line_address.getAddress();
+
+    // This if case is meant to capture what happens in a Broadcast/Snoop
+    // protocol where the block does not exist in the cache hierarchy. You
+    // only want to read from the Backing_Store memory if there is no copy in
+    // the cache hierarchy, otherwise you want to try to read the RO or RW
+    // copies existing in the cache hierarchy (covered by the else statement).
+    // The reason is because the Backing_Store memory could easily be stale, if
+    // there are copies floating around the cache hierarchy, so you want to read
+    // it only if it's not in the cache hierarchy at all.
+    if (num_invalid == (num_controllers - 1) &&
+            num_backing_store == 1) {
+        DPRINTF(RubySystem, "only copy in Backing_Store memory, read from it\n");
+        for (unsigned int i = 0; i < num_controllers; ++i) {
+            access_perm = m_abs_cntrl_vec[i]->getAccessPermission(line_address);
+            if (access_perm == AccessPermission_Backing_Store) {
+                DataBlock& block = m_abs_cntrl_vec[i]->
+                    getDataBlock(line_address);
+
+                DPRINTF(RubySystem, "reading from %s block %s\n",
+                        m_abs_cntrl_vec[i]->name(), block);
+                for (unsigned i = 0; i < size_in_bytes; ++i) {
+                    data[i] = block.getByte(i + startByte);
+                }
+                return true;
+            }
+        }
+    } else if (num_ro > 0 || num_rw == 1) {
+        // In Broadcast/Snoop protocols, this covers if you know the block
+        // exists somewhere in the caching hierarchy, then you want to read any
+        // valid RO or RW block.  In directory protocols, same thing, you want
+        // to read any valid readable copy of the block.
+        DPRINTF(RubySystem, "num_busy = %d, num_ro = %d, num_rw = %d\n",
+                num_busy, num_ro, num_rw);
+        // In this loop, we try to figure which controller has a read only or
+        // a read write copy of the given address. Any valid copy would suffice
+        // for a functional read.
+        for (unsigned int i = 0;i < num_controllers;++i) {
+            access_perm = m_abs_cntrl_vec[i]->getAccessPermission(line_address);
+            if (access_perm == AccessPermission_Read_Only ||
+                access_perm == AccessPermission_Read_Write) {
+                DataBlock& block = m_abs_cntrl_vec[i]->
+                    getDataBlock(line_address);
+
+                DPRINTF(RubySystem, "reading from %s block %s\n",
+                        m_abs_cntrl_vec[i]->name(), block);
+                for (unsigned i = 0; i < size_in_bytes; ++i) {
+                    data[i] = block.getByte(i + startByte);
+                }
+                return true;
+            }
+        }
+    }
+
+    // Since we are here, this means that none of the controllers hold this
+    // address in a stable/base state. The function searches through all the
+    // buffers that exist in different cache, directory and memory
+    // controllers, and in the network components and reads the data portion
+    // of the first message that holds address specified in the packet.
+    for (unsigned int i = 0; i < num_controllers;++i) {
+        if (m_abs_cntrl_vec[i]->functionalReadBuffers(pkt)) {
+            return true;
+        }
+    }
+
+    for (unsigned int i = 0; i < m_memory_controller_vec.size(); ++i) {
+        if (m_memory_controller_vec[i]->functionalReadBuffers(pkt)) {
+            return true;
+        }
+    }
+
+    if (m_network_ptr->functionalRead(pkt)) {
+        return true;
+    }
+    return false;
 }
 
-void RubySystem::recordCacheContents(CacheRecorder& tr) const
+// The function searches through all the buffers that exist in different
+// cache, directory and memory controllers, and in the network components
+// and writes the data portion of those that hold the address specified
+// in the packet.
+bool
+RubySystem::functionalWrite(PacketPtr pkt)
 {
+    Address addr(pkt->getAddr());
+    Address line_addr = line_address(addr);
+    AccessPermission access_perm = AccessPermission_NotPresent;
+    int num_controllers = m_abs_cntrl_vec.size();
+
+    DPRINTF(RubySystem, "Functional Write request for %s\n",addr);
+
+    uint8_t *data = pkt->getPtr<uint8_t>(true);
+    unsigned int size_in_bytes = pkt->getSize();
+    unsigned startByte = addr.getAddress() - line_addr.getAddress();
+
+    for (unsigned int i = 0; i < num_controllers;++i) {
+        m_abs_cntrl_vec[i]->functionalWriteBuffers(pkt);
+
+        access_perm = m_abs_cntrl_vec[i]->getAccessPermission(line_addr);
+        if (access_perm != AccessPermission_Invalid &&
+            access_perm != AccessPermission_NotPresent) {
+
+            DataBlock& block = m_abs_cntrl_vec[i]->getDataBlock(line_addr);
+            DPRINTF(RubySystem, "%s\n",block);
+            for (unsigned i = 0; i < size_in_bytes; ++i) {
+              block.setByte(i + startByte, data[i]);
+            }
+            DPRINTF(RubySystem, "%s\n",block);
+        }
+    }
 
+    uint32_t M5_VAR_USED num_functional_writes = 0;
+    for (unsigned int i = 0; i < m_memory_controller_vec.size() ;++i) {
+        num_functional_writes +=
+            m_memory_controller_vec[i]->functionalWriteBuffers(pkt);
+    }
+
+    num_functional_writes += m_network_ptr->functionalWrite(pkt);
+    DPRINTF(RubySystem, "Messages written = %u\n", num_functional_writes);
+
+    return true;
 }
 
 #ifdef CHECK_COHERENCE
@@ -373,51 +573,61 @@ void RubySystem::recordCacheContents(CacheRecorder& tr) const
 // in setState.  The SLICC spec must also define methods "isBlockShared"
 // and "isBlockExclusive" that are specific to that protocol
 //
-void RubySystem::checkGlobalCoherenceInvariant(const Address& addr  )  {
-  /*
-  NodeID exclusive = -1;
-  bool sharedDetected = false;
-  NodeID lastShared = -1;
-
-  for (int i = 0; i < m_chip_vector.size(); i++) {
-
-    if (m_chip_vector[i]->isBlockExclusive(addr)) {
-      if (exclusive != -1) {
-        // coherence violation
-        WARN_EXPR(exclusive);
-        WARN_EXPR(m_chip_vector[i]->getID());
-        WARN_EXPR(addr);
-        WARN_EXPR(g_eventQueue_ptr->getTime());
-        ERROR_MSG("Coherence Violation Detected -- 2 exclusive chips");
-      }
-      else if (sharedDetected) {
-        WARN_EXPR(lastShared);
-        WARN_EXPR(m_chip_vector[i]->getID());
-        WARN_EXPR(addr);
-        WARN_EXPR(g_eventQueue_ptr->getTime());
-        ERROR_MSG("Coherence Violation Detected -- exclusive chip with >=1 shared");
-      }
-      else {
-        exclusive = m_chip_vector[i]->getID();
-      }
-    }
-    else if (m_chip_vector[i]->isBlockShared(addr)) {
-      sharedDetected = true;
-      lastShared = m_chip_vector[i]->getID();
-
-      if (exclusive != -1) {
-        WARN_EXPR(lastShared);
-        WARN_EXPR(exclusive);
-        WARN_EXPR(addr);
-        WARN_EXPR(g_eventQueue_ptr->getTime());
-        ERROR_MSG("Coherence Violation Detected -- exclusive chip with >=1 shared");
-      }
+void
+RubySystem::checkGlobalCoherenceInvariant(const Address& addr)
+{
+#if 0
+    NodeID exclusive = -1;
+    bool sharedDetected = false;
+    NodeID lastShared = -1;
+
+    for (int i = 0; i < m_chip_vector.size(); i++) {
+        if (m_chip_vector[i]->isBlockExclusive(addr)) {
+            if (exclusive != -1) {
+                // coherence violation
+                WARN_EXPR(exclusive);
+                WARN_EXPR(m_chip_vector[i]->getID());
+                WARN_EXPR(addr);
+                WARN_EXPR(getTime());
+                ERROR_MSG("Coherence Violation Detected -- 2 exclusive chips");
+            } else if (sharedDetected) {
+                WARN_EXPR(lastShared);
+                WARN_EXPR(m_chip_vector[i]->getID());
+                WARN_EXPR(addr);
+                WARN_EXPR(getTime());
+                ERROR_MSG("Coherence Violation Detected -- exclusive chip with >=1 shared");
+            } else {
+                exclusive = m_chip_vector[i]->getID();
+            }
+        } else if (m_chip_vector[i]->isBlockShared(addr)) {
+            sharedDetected = true;
+            lastShared = m_chip_vector[i]->getID();
+
+            if (exclusive != -1) {
+                WARN_EXPR(lastShared);
+                WARN_EXPR(exclusive);
+                WARN_EXPR(addr);
+                WARN_EXPR(getTime());
+                ERROR_MSG("Coherence Violation Detected -- exclusive chip with >=1 shared");
+            }
+        }
     }
-  }
-  */
+#endif
 }
 #endif
 
+RubySystem *
+RubySystemParams::create()
+{
+    return new RubySystem(this);
+}
 
-
-
+/**
+ * virtual process function that is invoked when the callback
+ * queue is executed.
+ */
+void
+RubyDumpStatsCallback::process()
+{
+    ruby_system->printStats(*os);
+}