/*
- * Copyright (c) 2003 The Regents of The University of Michigan
+ * Copyright (c) 2002-2004 The Regents of The University of Michigan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
*/
#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <fstream>
#include <list>
#include <string>
#include <vector>
+#include "base/inifile.hh"
#include "base/misc.hh"
#include "base/str.hh"
-
+#include "base/trace.hh"
+#include "sim/config_node.hh"
#include "sim/eventq.hh"
#include "sim/param.hh"
#include "sim/serialize.hh"
-#include "base/inifile.hh"
#include "sim/sim_events.hh"
#include "sim/sim_object.hh"
-#include "base/trace.hh"
using namespace std;
-Serializer *Serializeable::serializer = NULL;
-
-Serializeable::Serializeable(const string &n)
- : objName(n), serialized(false)
-{ }
-
-Serializeable::~Serializeable()
-{ }
-
-void
-Serializeable::mark()
-{
- if (!serialized)
- serializer->add_object(this);
-
- serialized = true;
-}
-
void
-Serializeable::nameOut(ostream &os)
+Serializable::nameOut(ostream &os)
{
os << "\n[" << name() << "]\n";
}
void
-Serializeable::nameOut(ostream &os, const string &_name)
+Serializable::nameOut(ostream &os, const string &_name)
{
os << "\n[" << _name << "]\n";
}
template <class T>
void
-paramOut(ostream &os, const std::string &name, const T& param)
+paramOut(ostream &os, const std::string &name, const T ¶m)
{
os << name << "=";
showParam(os, param);
template <class T>
void
-paramIn(const IniFile *db, const std::string §ion,
- const std::string &name, T& param)
+paramIn(Checkpoint *cp, const std::string §ion,
+ const std::string &name, T ¶m)
{
std::string str;
- if (!db->find(section, name, str) || !parseParam(str, param)) {
+ if (!cp->find(section, name, str) || !parseParam(str, param)) {
fatal("Can't unserialize '%s:%s'\n", section, name);
}
}
template <class T>
void
-arrayParamIn(const IniFile *db, const std::string §ion,
+arrayParamIn(Checkpoint *cp, const std::string §ion,
const std::string &name, T *param, int size)
{
std::string str;
- if (!db->find(section, name, str)) {
+ if (!cp->find(section, name, str)) {
fatal("Can't unserialize '%s:%s'\n", section, name);
}
}
+void
+objParamIn(Checkpoint *cp, const std::string §ion,
+ const std::string &name, Serializable * ¶m)
+{
+ if (!cp->findObj(section, name, param)) {
+ fatal("Can't unserialize '%s:%s'\n", section, name);
+ }
+}
+
+
#define INSTANTIATE_PARAM_TEMPLATES(type) \
template void \
-paramOut(ostream &os, const std::string &name, const type ¶m); \
+paramOut(ostream &os, const std::string &name, type const ¶m); \
template void \
-paramIn(const IniFile *db, const std::string §ion, \
+paramIn(Checkpoint *cp, const std::string §ion, \
const std::string &name, type & param); \
template void \
arrayParamOut(ostream &os, const std::string &name, \
- const type *param, int size); \
+ type const *param, int size); \
template void \
-arrayParamIn(const IniFile *db, const std::string §ion, \
+arrayParamIn(Checkpoint *cp, const std::string §ion, \
const std::string &name, type *param, int size);
INSTANTIATE_PARAM_TEMPLATES(string)
-#if 0
-// unneeded?
-void
-Serializeable::childOut(const string &name, Serializeable *child)
-{
- child->mark();
- if (child->name() == "")
- panic("child is unnamed");
-
- out() << name << "=" << child->name() << "\n";
-}
-#endif
+/////////////////////////////
-void
-Serializeable::setName(const string &name)
+/// Container for serializing global variables (not associated with
+/// any serialized object).
+class Globals : public Serializable
{
- if (objName != "") {
- cprintf("Renaming object '%s' to '%s'.\n", objName, name);
- }
-
- objName = name;
-}
-
-Serializer::Serializer()
-{ }
+ public:
+ const string name() const;
+ void serialize(ostream &os);
+ void unserialize(Checkpoint *cp);
+};
-Serializer::~Serializer()
-{ }
+/// The one and only instance of the Globals class.
+Globals globals;
-ostream &
-Serializer::out() const
+const string
+Globals::name() const
{
- if (!output)
- panic("must set output before serializing");
-
- return *output;
+ return "Globals";
}
void
-Serializer::add_object(Serializeable *obj)
+Globals::serialize(ostream &os)
{
- objects.push_back(obj);
+ nameOut(os);
+ SERIALIZE_SCALAR(curTick);
+
+ nameOut(os, "MainEventQueue");
+ mainEventQueue.serialize(os);
}
void
-Serializer::add_objects()
+Globals::unserialize(Checkpoint *cp)
{
- mainEventQueue.mark();
-
- SimObject::SimObjectList::iterator i = SimObject::simObjectList.begin();
- SimObject::SimObjectList::iterator end = SimObject::simObjectList.end();
+ const string §ion = name();
+ UNSERIALIZE_SCALAR(curTick);
- while (i != end) {
- (*i)->mark();
- ++i;
- }
+ mainEventQueue.unserialize(cp, "MainEventQueue");
}
void
-Serializer::serialize(const string &f)
+Serializable::serializeAll()
{
- if (Serializeable::serializer != NULL)
- panic("in process of serializing!");
+ string dir = Checkpoint::dir();
+ if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
+ fatal("couldn't mkdir %s\n", dir);
- Serializeable::serializer = this;
-
- file = f;
- string cpt_file = file + ".cpt";
- output = new ofstream(cpt_file.c_str());
+ string cpt_file = dir + Checkpoint::baseFilename;
+ ofstream outstream(cpt_file.c_str());
time_t t = time(NULL);
- *output << "// checkpoint generated: " << ctime(&t);
-
- serlist_t list;
-
- add_objects();
- while (!objects.empty()) {
- Serializeable *serial = objects.front();
- DPRINTF(Serialize, "Naming children of %s\n", serial->name());
- serial->nameChildren();
- objects.pop_front();
- list.push_back(serial);
- }
-
- while (!list.empty()) {
- list.front()->serialized = false;
- list.pop_front();
- }
+ outstream << "// checkpoint generated: " << ctime(&t);
- add_objects();
- while (!objects.empty()) {
- Serializeable *obj = objects.front();
- DPRINTF(Serialize, "Serializing %s\n", obj->name());
- obj->nameOut(out());
- obj->serialize(out());
- objects.pop_front();
- list.push_back(obj);
- }
-
- while (!list.empty()) {
- list.front()->serialized = false;
- list.pop_front();
- }
+ globals.serialize(outstream);
+ SimObject::serializeAll(outstream);
+}
- Serializeable::serializer = NULL;
- delete output;
- output = NULL;
- file = "";
+void
+Serializable::unserializeGlobals(Checkpoint *cp)
+{
+ globals.unserialize(cp);
}
+
class SerializeEvent : public Event
{
protected:
- string file;
+ Tick repeat;
public:
- SerializeEvent(EventQueue *q, Tick when, const string &file);
- ~SerializeEvent();
-
+ SerializeEvent(Tick _when, Tick _repeat);
virtual void process();
- virtual void serialize(std::ostream &os);
+ virtual void serialize(std::ostream &os)
+ {
+ panic("Cannot serialize the SerializeEvent");
+ }
+
};
-SerializeEvent::SerializeEvent(EventQueue *q, Tick when, const string &f)
- : Event(q), file(f)
+SerializeEvent::SerializeEvent(Tick _when, Tick _repeat)
+ : Event(&mainEventQueue, Serialize_Pri), repeat(_repeat)
{
setFlags(AutoDelete);
- schedule(when);
+ schedule(_when);
}
-SerializeEvent::~SerializeEvent()
+void
+SerializeEvent::process()
{
+ Serializable::serializeAll();
+ if (repeat)
+ schedule(curTick + repeat);
}
-void
-SerializeEvent::process()
+const char *Checkpoint::baseFilename = "m5.cpt";
+
+static string checkpointDirBase;
+
+string
+Checkpoint::dir()
{
- Serializer serial;
- serial.serialize(file);
- new SimExitEvent("Serialization caused exit");
+ // use csprintf to insert curTick into directory name if it
+ // appears to have a format placeholder in it.
+ return (checkpointDirBase.find("%") != string::npos) ?
+ csprintf(checkpointDirBase, curTick) : checkpointDirBase;
}
void
-SerializeEvent::serialize(ostream &os)
+Checkpoint::setup(Tick when, Tick period)
{
- panic("Cannot serialize the SerializeEvent");
+ new SerializeEvent(when, period);
}
class SerializeParamContext : public ParamContext
SerializeParamContext serialParams("serialize");
+Param<string> serialize_dir(&serialParams, "dir",
+ "dir to stick checkpoint in "
+ "(sprintf format with cycle #)");
+
Param<Counter> serialize_cycle(&serialParams,
"cycle",
"cycle to serialize",
0);
-Param<string> serialize_file(&serialParams,
- "file",
- "file to write to", "");
+Param<Counter> serialize_period(&serialParams,
+ "period",
+ "period to repeat serializations",
+ 0);
+
+
SerializeParamContext::SerializeParamContext(const string §ion)
: ParamContext(section), event(NULL)
void
SerializeParamContext::checkParams()
{
- if (!((string)serialize_file).empty() && serialize_cycle > 0)
- event = new SerializeEvent(&mainEventQueue, serialize_cycle,
- serialize_file);
+ if (serialize_dir.isValid()) {
+ checkpointDirBase = serialize_dir;
+ } else {
+ checkpointDirBase = outputDirectory + "cpt.%012d";
+ }
+
+ // guarantee that directory ends with a '/'
+ if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
+ checkpointDirBase += "/";
+
+ if (serialize_cycle > 0)
+ Checkpoint::setup(serialize_cycle, serialize_period);
}
void
-debug_serialize(const char *file)
+debug_serialize()
{
- Serializer serial;
- serial.serialize(file);
- new SimExitEvent("Serialization caused exit");
+ Serializable::serializeAll();
}
-
-
+void
+debug_serialize(Tick when)
+{
+ new SerializeEvent(when, 0);
+}
////////////////////////////////////////////////////////////////////////
//
-// SerializeableClass member definitions
+// SerializableClass member definitions
//
////////////////////////////////////////////////////////////////////////
-// Map of class names to SerializeableBuilder creation functions.
+// Map of class names to SerializableBuilder creation functions.
// Need to make this a pointer so we can force initialization on the
-// first reference; otherwise, some SerializeableClass constructors
+// first reference; otherwise, some SerializableClass constructors
// may be invoked before the classMap constructor.
-map<string,SerializeableClass::CreateFunc> *SerializeableClass::classMap = 0;
+map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
-// SerializeableClass constructor: add mapping to classMap
-SerializeableClass::SerializeableClass(const string &className,
+// SerializableClass constructor: add mapping to classMap
+SerializableClass::SerializableClass(const string &className,
CreateFunc createFunc)
{
if (classMap == NULL)
- classMap = new map<string,SerializeableClass::CreateFunc>();
+ classMap = new map<string,SerializableClass::CreateFunc>();
if ((*classMap)[className])
{
//
//
-Serializeable *
-SerializeableClass::createObject(IniFile &configDB,
- const string &configClassName)
+Serializable *
+SerializableClass::createObject(Checkpoint *cp,
+ const std::string §ion)
{
- // find simulation object class name from configuration class
- // (specified by 'type=' parameter)
- string simObjClassName;
+ string className;
- if (!configDB.findDefault(configClassName, "type", simObjClassName)) {
- cerr << "Configuration class '" << configClassName << "' not found."
- << endl;
- abort();
+ if (!cp->find(section, "type", className)) {
+ fatal("Serializable::create: no 'type' entry in section '%s'.\n",
+ section);
}
- // 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)[className];
+
+ if (createFunc == NULL) {
+ fatal("Serializable::create: no create function for class '%s'.\n",
+ className);
}
- CreateFunc createFunc = (*classMap)[simObjClassName];
+ Serializable *object = createFunc(cp, section);
- // builder instance
- SerializeableBuilder *objectBuilder = (*createFunc)();
+ assert(object != NULL);
- assert(objectBuilder != NULL);
+ return object;
+}
- // now create the actual simulation object
- Serializeable *object = objectBuilder->create();
- assert(object != NULL);
+Serializable *
+Serializable::create(Checkpoint *cp, const std::string §ion)
+{
+ Serializable *object = SerializableClass::createObject(cp, section);
+ object->unserialize(cp, section);
+ return object;
+}
- // done with the SerializeableBuilder now
- delete objectBuilder;
- return object;
+Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path,
+ const ConfigNode *_configNode)
+ : db(new IniFile), basePath(path), configNode(_configNode), cptDir(cpt_dir)
+{
+ string filename = cpt_dir + "/" + Checkpoint::baseFilename;
+ if (!db->load(filename)) {
+ fatal("Can't load checkpoint file '%s'\n", filename);
+ }
}
+
+bool
+Checkpoint::find(const std::string §ion, const std::string &entry,
+ std::string &value)
+{
+ return db->find(section, entry, value);
+}
+
+
+bool
+Checkpoint::findObj(const std::string §ion, const std::string &entry,
+ Serializable *&value)
+{
+ string path;
+
+ if (!db->find(section, entry, path))
+ return false;
+
+ if ((value = configNode->resolveSimObject(path)) != NULL)
+ return true;
+
+ if ((value = objMap[path]) != NULL)
+ return true;
+
+ return false;
+}
+
+
+bool
+Checkpoint::sectionExists(const std::string §ion)
+{
+ return db->sectionExists(section);
+}