2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
34 #include <sys/types.h>
43 #include "base/inifile.hh"
44 #include "base/misc.hh"
45 #include "base/output.hh"
46 #include "base/str.hh"
47 #include "base/trace.hh"
48 #include "sim/eventq.hh"
49 #include "sim/param.hh"
50 #include "sim/serialize.hh"
51 #include "sim/sim_events.hh"
52 #include "sim/sim_exit.hh"
53 #include "sim/sim_object.hh"
57 int Serializable::ckptMaxCount
= 0;
58 int Serializable::ckptCount
= 0;
59 int Serializable::ckptPrevCount
= -1;
62 Serializable::nameOut(ostream
&os
)
64 os
<< "\n[" << name() << "]\n";
68 Serializable::nameOut(ostream
&os
, const string
&_name
)
70 os
<< "\n[" << _name
<< "]\n";
75 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
85 paramIn(Checkpoint
*cp
, const std::string
§ion
,
86 const std::string
&name
, T
¶m
)
89 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
90 fatal("Can't unserialize '%s:%s'\n", section
, name
);
97 arrayParamOut(ostream
&os
, const std::string
&name
,
98 const T
*param
, int size
)
102 showParam(os
, param
[0]);
103 for (int i
= 1; i
< size
; ++i
) {
105 showParam(os
, param
[i
]);
113 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
114 const std::string
&name
, T
*param
, int size
)
117 if (!cp
->find(section
, name
, str
)) {
118 fatal("Can't unserialize '%s:%s'\n", section
, name
);
121 // code below stolen from VectorParam<T>::parse().
122 // it would be nice to unify these somehow...
124 vector
<string
> tokens
;
126 tokenize(tokens
, str
, ' ');
128 // Need this if we were doing a vector
129 // value.resize(tokens.size());
131 if (tokens
.size() != size
) {
132 fatal("Array size mismatch on %s:%s'\n", section
, name
);
135 for (int i
= 0; i
< tokens
.size(); i
++) {
136 // need to parse into local variable to handle vector<bool>,
137 // for which operator[] returns a special reference class
138 // that's not the same as 'bool&', (since it's a packed
141 if (!parseParam(tokens
[i
], scalar_value
)) {
142 string
err("could not parse \"");
150 // assign parsed value to vector
151 param
[i
] = scalar_value
;
157 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
158 const std::string
&name
, Serializable
* ¶m
)
160 if (!cp
->findObj(section
, name
, param
)) {
161 fatal("Can't unserialize '%s:%s'\n", section
, name
);
166 #define INSTANTIATE_PARAM_TEMPLATES(type) \
168 paramOut(ostream &os, const std::string &name, type const ¶m); \
170 paramIn(Checkpoint *cp, const std::string §ion, \
171 const std::string &name, type & param); \
173 arrayParamOut(ostream &os, const std::string &name, \
174 type const *param, int size); \
176 arrayParamIn(Checkpoint *cp, const std::string §ion, \
177 const std::string &name, type *param, int size);
179 INSTANTIATE_PARAM_TEMPLATES(signed char)
180 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
181 INSTANTIATE_PARAM_TEMPLATES(signed short)
182 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
183 INSTANTIATE_PARAM_TEMPLATES(signed int)
184 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
185 INSTANTIATE_PARAM_TEMPLATES(signed long)
186 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
187 INSTANTIATE_PARAM_TEMPLATES(signed long long)
188 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
189 INSTANTIATE_PARAM_TEMPLATES(bool)
190 INSTANTIATE_PARAM_TEMPLATES(string
)
193 /////////////////////////////
195 /// Container for serializing global variables (not associated with
196 /// any serialized object).
197 class Globals
: public Serializable
200 const string
name() const;
201 void serialize(ostream
&os
);
202 void unserialize(Checkpoint
*cp
);
205 /// The one and only instance of the Globals class.
209 Globals::name() const
215 Globals::serialize(ostream
&os
)
218 SERIALIZE_SCALAR(curTick
);
220 nameOut(os
, "MainEventQueue");
221 mainEventQueue
.serialize(os
);
225 Globals::unserialize(Checkpoint
*cp
)
227 const string
§ion
= name();
228 UNSERIALIZE_SCALAR(curTick
);
230 mainEventQueue
.unserialize(cp
, "MainEventQueue");
234 Serializable::serializeAll(const std::string
&cpt_dir
)
236 setCheckpointDir(cpt_dir
);
237 string dir
= Checkpoint::dir();
238 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
239 fatal("couldn't mkdir %s\n", dir
);
241 string cpt_file
= dir
+ Checkpoint::baseFilename
;
242 ofstream
outstream(cpt_file
.c_str());
243 time_t t
= time(NULL
);
244 outstream
<< "// checkpoint generated: " << ctime(&t
);
246 globals
.serialize(outstream
);
247 SimObject::serializeAll(outstream
);
251 Serializable::unserializeAll(const std::string
&cpt_dir
)
253 setCheckpointDir(cpt_dir
);
254 string dir
= Checkpoint::dir();
255 string cpt_file
= dir
+ Checkpoint::baseFilename
;
258 DPRINTFR(Config
, "Loading checkpoint dir '%s'\n",
260 Checkpoint
*cp
= new Checkpoint(dir
, section
);
261 unserializeGlobals(cp
);
263 SimObject::unserializeAll(cp
);
267 Serializable::unserializeGlobals(Checkpoint
*cp
)
269 globals
.unserialize(cp
);
272 const char *Checkpoint::baseFilename
= "m5.cpt";
274 static string checkpointDirBase
;
277 setCheckpointDir(const std::string
&name
)
279 checkpointDirBase
= name
;
280 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
281 checkpointDirBase
+= "/";
287 // use csprintf to insert curTick into directory name if it
288 // appears to have a format placeholder in it.
289 return (checkpointDirBase
.find("%") != string::npos
) ?
290 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
294 debug_serialize(const std::string
&cpt_dir
)
296 Serializable::serializeAll(cpt_dir
);
300 ////////////////////////////////////////////////////////////////////////
302 // SerializableClass member definitions
304 ////////////////////////////////////////////////////////////////////////
306 // Map of class names to SerializableBuilder creation functions.
307 // Need to make this a pointer so we can force initialization on the
308 // first reference; otherwise, some SerializableClass constructors
309 // may be invoked before the classMap constructor.
310 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
312 // SerializableClass constructor: add mapping to classMap
313 SerializableClass::SerializableClass(const string
&className
,
314 CreateFunc createFunc
)
316 if (classMap
== NULL
)
317 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
319 if ((*classMap
)[className
])
321 cerr
<< "Error: simulation object class " << className
<< " redefined"
326 // add className --> createFunc to class map
327 (*classMap
)[className
] = createFunc
;
334 SerializableClass::createObject(Checkpoint
*cp
,
335 const std::string
§ion
)
339 if (!cp
->find(section
, "type", className
)) {
340 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
344 CreateFunc createFunc
= (*classMap
)[className
];
346 if (createFunc
== NULL
) {
347 fatal("Serializable::create: no create function for class '%s'.\n",
351 Serializable
*object
= createFunc(cp
, section
);
353 assert(object
!= NULL
);
360 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
362 Serializable
*object
= SerializableClass::createObject(cp
, section
);
363 object
->unserialize(cp
, section
);
368 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
)
369 : db(new IniFile
), basePath(path
), cptDir(cpt_dir
)
371 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
372 if (!db
->load(filename
)) {
373 fatal("Can't load checkpoint file '%s'\n", filename
);
379 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
382 return db
->find(section
, entry
, value
);
387 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
388 Serializable
*&value
)
392 if (!db
->find(section
, entry
, path
))
395 if ((value
= objMap
[path
]) != NULL
)
403 Checkpoint::sectionExists(const std::string
§ion
)
405 return db
->sectionExists(section
);