Merge zizzer:/bk/m5 into zeep.eecs.umich.edu:/z/saidi/work/m5
[gem5.git] / sim / sim_object.cc
index 955c43bb8a113849ad643c35e2b34f5b961df0bd..818648b98253a996864a43f1a425806c71799d10 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003 The Regents of The University of Michigan
+ * Copyright (c) 2001-2004 The Regents of The University of Michigan
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 
 #include <assert.h>
 
-#include "sim/sim_object.hh"
+#include "base/callback.hh"
 #include "base/inifile.hh"
-#include "sim/configfile.hh"
-#include "sim/host.hh"
+#include "base/match.hh"
 #include "base/misc.hh"
 #include "base/trace.hh"
-#include "sim/sim_stats.hh"
+#include "base/stats/events.hh"
+#include "sim/configfile.hh"
+#include "sim/host.hh"
+#include "sim/sim_object.hh"
+#include "sim/stats.hh"
+#include "sim/param.hh"
 
 using namespace std;
 
@@ -50,15 +54,29 @@ using namespace std;
 //
 SimObject::SimObjectList SimObject::simObjectList;
 
+namespace Stats {
+    extern ObjectMatch event_ignore;
+}
+
 //
 // SimObject constructor: used to maintain static simObjectList
 //
 SimObject::SimObject(const string &_name)
-    : Serializeable(_name)
+    : objName(_name)
 {
+#ifdef DEBUG
+    doDebugBreak = false;
+#endif
+
+    doRecordEvent = !Stats::event_ignore.match(_name);
     simObjectList.push_back(this);
 }
 
+void
+SimObject::init()
+{
+}
+
 //
 // no default statistics, so nothing to do in base implementation
 //
@@ -72,11 +90,8 @@ SimObject::regFormulas()
 {
 }
 
-//
-// no default extra output
-//
 void
-SimObject::printExtraOutput(ostream &os)
+SimObject::resetStats()
 {
 }
 
@@ -85,6 +100,15 @@ SimObject::printExtraOutput(ostream &os)
 //   call regStats() on all SimObjects and then regFormulas() on all
 //   SimObjects.
 //
+struct SimObjectResetCB : public Callback
+{
+    virtual void process() { SimObject::resetAllStats(); }
+};
+
+namespace {
+    static SimObjectResetCB StatResetCB;
+}
+
 void
 SimObject::regAllStats()
 {
@@ -106,170 +130,87 @@ SimObject::regAllStats()
         cprintf("registering formulas for %s\n", (*i)->name());
 #endif
         (*i)->regFormulas();
-   }
+    }
+
+    Stats::registerResetCallback(&StatResetCB);
 }
 
 //
-// static function: call printExtraOutput() on all SimObjects.
+// static function: call init() on all SimObjects.
 //
 void
-SimObject::printAllExtraOutput(ostream &os)
+SimObject::initAll()
 {
-    SimObjectList::iterator i;
+    SimObjectList::iterator i = simObjectList.begin();
+    SimObjectList::iterator end = simObjectList.end();
 
-    for (i = simObjectList.begin(); i != simObjectList.end(); ++i) {
-        (*i)->printExtraOutput(os);
-   }
+    for (; i != end; ++i) {
+        SimObject *obj = *i;
+        obj->init();
+    }
 }
 
-///////////////////////////////////////////
 //
-// SimObjectBuilder member definitions
+// static function: call resetStats() on all SimObjects.
 //
-///////////////////////////////////////////
-
-// override ParamContext::parseParams() to check params based on
-// instance name first.  If not found, then check based on iniSection
-// (as in default ParamContext implementation).
 void
-SimObjectBuilder::parseParams(IniFile &iniFile)
+SimObject::resetAllStats()
 {
-    iniFilePtr = &iniFile;     // set object member
-
-    ParamList::iterator i;
-
-    for (i = paramList->begin(); i != paramList->end(); ++i) {
-        string string_value;
+    SimObjectList::iterator i = simObjectList.begin();
+    SimObjectList::iterator end = simObjectList.end();
 
-        if (iniFile.findDefault(instanceName, (*i)->name, string_value)) {
-            (*i)->parse(string_value);
-        }
-        else if (iniFile.findDefault(iniSection, (*i)->name, string_value)) {
-            (*i)->parse(string_value);
-        }
+    for (; i != end; ++i) {
+        SimObject *obj = *i;
+        obj->resetStats();
     }
 }
 
-
-void
-SimObjectBuilder::printErrorProlog(ostream &os)
-{
-    os << "Error creating object '" << getInstanceName()
-       << "' of type '" << simObjClassName
-       << "', section '" << iniSection << "':" << endl;
-}
-
-
-////////////////////////////////////////////////////////////////////////
 //
-// SimObjectClass member definitions
+// static function: serialize all SimObjects.
 //
-////////////////////////////////////////////////////////////////////////
-
-// Map of class names to SimObjectBuilder creation functions.  Need to
-// make this a pointer so we can force initialization on the first
-// reference; otherwise, some SimObjectClass constructors may be invoked
-// before the classMap constructor.
-map<string,SimObjectClass::CreateFunc> *SimObjectClass::classMap = NULL;
-
-// SimObjectClass constructor: add mapping to classMap
-SimObjectClass::SimObjectClass(const string &className, CreateFunc createFunc)
+void
+SimObject::serializeAll(ostream &os)
 {
-    if (classMap == NULL)
-        classMap = new map<string,SimObjectClass::CreateFunc>();
+    SimObjectList::reverse_iterator ri = simObjectList.rbegin();
+    SimObjectList::reverse_iterator rend = simObjectList.rend();
 
-    if ((*classMap)[className])
-    {
-        cerr << "Error: simulation object class " << className << " redefined"
-             << endl;
-        fatal("");
-    }
-
-    // add className --> createFunc to class map
-    (*classMap)[className] = createFunc;
+    for (; ri != rend; ++ri) {
+        SimObject *obj = *ri;
+        obj->nameOut(os);
+        obj->serialize(os);
+   }
 }
 
-
+#ifdef DEBUG
 //
+// static function: flag which objects should have the debugger break
 //
-SimObject *
-SimObjectClass::createObject(IniFile &configDB,
-                             const string &configClassName,
-                             const string &objName,
-                             ConfigNode *configNode)
+void
+SimObject::debugObjectBreak(const string &objs)
 {
-    // find simulation object class name from configuration class
-    // (specified by 'type=' parameter)
-    string simObjClassName;
-
-    if (!configDB.findDefault(configClassName, "type", simObjClassName)) {
-        cerr << "Configuration class '" << configClassName << "' not found."
-             << endl;
-        abort();
-    }
-
-    // look up className to get appropriate createFunc
-    if (classMap->find(simObjClassName) == classMap->end()) {
-        cerr << "Simulator object class '" << simObjClassName << "' not found."
-             << endl;
-        abort();
-    }
-
-    CreateFunc createFunc = (*classMap)[simObjClassName];
-
-    // call createFunc with config hierarchy node to get object
-    // builder instance (context with parameters for object creation)
-    SimObjectBuilder *objectBuilder = (*createFunc)(configClassName,
-                                                    objName, configNode,
-                                                    simObjClassName);
-
-    assert(objectBuilder != NULL);
-
-    // parse all parameters in context to generate parameter values
-    objectBuilder->parseParams(configDB);
-
-    // now create the actual simulation object
-    SimObject *object = objectBuilder->create();
+    SimObjectList::const_iterator i = simObjectList.begin();
+    SimObjectList::const_iterator end = simObjectList.end();
 
-    assert(object != NULL);
-
-    // echo object parameters to stats file (for documenting the
-    // config used to generate the associated stats)
-    *statStream << "[" << object->name() << "]" << endl;
-    *statStream << "type=" << simObjClassName << endl;
-    objectBuilder->showParams(*statStream);
-    *statStream << endl;
-
-    // done with the SimObjectBuilder now
-    delete objectBuilder;
-
-    return object;
+    ObjectMatch match(objs);
+    for (; i != end; ++i) {
+        SimObject *obj = *i;
+        obj->doDebugBreak = match.match(obj->name());
+   }
 }
 
-
-//
-// static method:
-//
+extern "C"
 void
-SimObjectClass::describeAllClasses(ostream &os)
+debugObjectBreak(const char *objs)
 {
-    map<string,CreateFunc>::iterator iter;
-
-    for (iter = classMap->begin(); iter != classMap->end(); ++iter) {
-        const string &className = iter->first;
-        CreateFunc createFunc = iter->second;
-
-        os << "[" << className << "]\n";
-
-        // create dummy object builder just to instantiate parameters
-        SimObjectBuilder *objectBuilder = (*createFunc)("", "", NULL, "");
-
-        // now get the object builder to describe ite params
-        objectBuilder->describeParams(os);
-
-        os << endl;
+    SimObject::debugObjectBreak(string(objs));
+}
+#endif
 
-        // done with the object builder now
-        delete objectBuilder;
-    }
+void
+SimObject::recordEvent(const std::string &stat)
+{
+    if (doRecordEvent)
+        Stats::recordEvent(stat);
 }
+
+DEFINE_SIM_OBJECT_CLASS_NAME("SimObject", SimObject)