Automated merge with ssh://repo.gem5.org/gem5
[gem5.git] / src / sim / serialize.cc
index 0e6d9b254fc44a5156e256b9bfb253eb1174c068..03f900837dc4c9374ece271491823f9ca4e98c34 100644 (file)
  *          Steve Reinhardt
  */
 
+#include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
 
+#include <cerrno>
 #include <fstream>
 #include <list>
 #include <string>
@@ -93,6 +93,14 @@ showParam(ostream &os, const char &value)
 }
 
 
+template <>
+void
+showParam(ostream &os, const signed char &value)
+{
+    os << (int)value;
+}
+
+
 template <>
 void
 showParam(ostream &os, const unsigned char &value)
@@ -193,6 +201,23 @@ arrayParamOut(ostream &os, const string &name, const vector<T> &param)
     os << "\n";
 }
 
+template <class T>
+void
+arrayParamOut(ostream &os, const string &name, const list<T> &param)
+{
+    typename list<T>::const_iterator it = param.begin();
+
+    os << name << "=";
+    if (param.size() > 0)
+        showParam(os, *it);
+    it++;
+    while (it != param.end()) {
+        os << " ";
+        showParam(os, *it);
+        it++;
+    }
+    os << "\n";
+}
 
 template <class T>
 void
@@ -261,7 +286,7 @@ arrayParamIn(Checkpoint *cp, const string &section, const string &name,
         // for which operator[] returns a special reference class
         // that's not the same as 'bool&', (since it's a packed
         // vector)
-        T scalar_value;
+        T scalar_value = 0;
         if (!parseParam(tokens[i], scalar_value)) {
             string err("could not parse \"");
 
@@ -303,7 +328,7 @@ arrayParamIn(Checkpoint *cp, const string &section,
         // for which operator[] returns a special reference class
         // that's not the same as 'bool&', (since it's a packed
         // vector)
-        T scalar_value;
+        T scalar_value = 0;
         if (!parseParam(tokens[i], scalar_value)) {
             string err("could not parse \"");
 
@@ -318,6 +343,37 @@ arrayParamIn(Checkpoint *cp, const string &section,
     }
 }
 
+template <class T>
+void
+arrayParamIn(Checkpoint *cp, const string &section,
+             const string &name, list<T> &param)
+{
+    string str;
+    if (!cp->find(section, name, str)) {
+        fatal("Can't unserialize '%s:%s'\n", section, name);
+    }
+    param.clear();
+
+    vector<string> tokens;
+    tokenize(tokens, str, ' ');
+
+    for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
+        T scalar_value = 0;
+        if (!parseParam(tokens[i], scalar_value)) {
+            string err("could not parse \"");
+
+            err += str;
+            err += "\"";
+
+            fatal(err);
+        }
+
+        // assign parsed value to vector
+        param.push_back(scalar_value);
+    }
+}
+
+
 void
 objParamIn(Checkpoint *cp, const string &section,
            const string &name, SimObject * &param)
@@ -348,8 +404,15 @@ arrayParamOut(ostream &os, const string &name,                          \
               const vector<type> &param);                               \
 template void                                                           \
 arrayParamIn(Checkpoint *cp, const string &section,                     \
-             const string &name, vector<type> &param);
+             const string &name, vector<type> &param);                  \
+template void                                                           \
+arrayParamOut(ostream &os, const string &name,                          \
+              const list<type> &param);                                 \
+template void                                                           \
+arrayParamIn(Checkpoint *cp, const string &section,                     \
+             const string &name, list<type> &param);
 
+INSTANTIATE_PARAM_TEMPLATES(char)
 INSTANTIATE_PARAM_TEMPLATES(signed char)
 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
 INSTANTIATE_PARAM_TEMPLATES(signed short)
@@ -391,7 +454,7 @@ void
 Globals::serialize(ostream &os)
 {
     nameOut(os);
-    SERIALIZE_SCALAR(curTick);
+    paramOut(os, "curTick", curTick());
 
     nameOut(os, "MainEventQueue");
     mainEventQueue.serialize(os);
@@ -401,7 +464,9 @@ void
 Globals::unserialize(Checkpoint *cp)
 {
     const string &section = name();
-    UNSERIALIZE_SCALAR(curTick);
+    Tick tick;
+    paramIn(cp, section, "curTick", tick);
+    curTick(tick);
 
     mainEventQueue.unserialize(cp, "MainEventQueue");
 }
@@ -427,8 +492,7 @@ Serializable::unserialize(Checkpoint *cp, const string &section)
 void
 Serializable::serializeAll(const string &cpt_dir)
 {
-    setCheckpointDir(cpt_dir);
-    string dir = Checkpoint::dir();
+    string dir = Checkpoint::setDir(cpt_dir);
     if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
             fatal("couldn't mkdir %s\n", dir);
 
@@ -443,48 +507,12 @@ Serializable::serializeAll(const string &cpt_dir)
     SimObject::serializeAll(outstream);
 }
 
-void
-Serializable::unserializeAll(const string &cpt_dir)
-{
-    setCheckpointDir(cpt_dir);
-    string dir = Checkpoint::dir();
-    string cpt_file = dir + Checkpoint::baseFilename;
-    string section = "";
-
-    DPRINTFR(Config, "Loading checkpoint dir '%s'\n",
-             dir);
-    Checkpoint *cp = new Checkpoint(dir, section);
-    unserializeGlobals(cp);
-    SimObject::unserializeAll(cp);
-}
-
 void
 Serializable::unserializeGlobals(Checkpoint *cp)
 {
     globals.unserialize(cp);
 }
 
-const char *Checkpoint::baseFilename = "m5.cpt";
-
-static string checkpointDirBase;
-
-void
-setCheckpointDir(const string &name)
-{
-    checkpointDirBase = name;
-    if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
-        checkpointDirBase += "/";
-}
-
-string
-Checkpoint::dir()
-{
-    // 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
 debug_serialize(const string &cpt_dir)
 {
@@ -554,10 +582,33 @@ Serializable::create(Checkpoint *cp, const string &section)
 }
 
 
-Checkpoint::Checkpoint(const string &cpt_dir, const string &path)
-    : db(new IniFile), basePath(path), cptDir(cpt_dir)
+const char *Checkpoint::baseFilename = "m5.cpt";
+
+string Checkpoint::currentDirectory;
+
+string
+Checkpoint::setDir(const string &name)
+{
+    // use csprintf to insert curTick() into directory name if it
+    // appears to have a format placeholder in it.
+    currentDirectory = (name.find("%") != string::npos) ?
+        csprintf(name, curTick()) : name;
+    if (currentDirectory[currentDirectory.size() - 1] != '/')
+        currentDirectory += "/";
+    return currentDirectory;
+}
+
+string
+Checkpoint::dir()
+{
+    return currentDirectory;
+}
+
+
+Checkpoint::Checkpoint(const string &cpt_dir)
+    : db(new IniFile), cptDir(setDir(cpt_dir))
 {
-    string filename = cpt_dir + "/" + Checkpoint::baseFilename;
+    string filename = cptDir + "/" + Checkpoint::baseFilename;
     if (!db->load(filename)) {
         fatal("Can't load checkpoint file '%s'\n", filename);
     }