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/serialize.hh"
50 #include "sim/sim_events.hh"
51 #include "sim/sim_exit.hh"
52 #include "sim/sim_object.hh"
54 // For stat reset hack
55 #include "sim/stat_control.hh"
59 extern SimObject
*resolveSimObject(const string
&);
62 // The base implementations use to_number for parsing and '<<' for
63 // displaying, suitable for integer types.
67 parseParam(const string
&s
, T
&value
)
69 return to_number(s
, value
);
74 showParam(ostream
&os
, const T
&value
)
80 // Template specializations:
81 // - char (8-bit integer)
82 // - floating-point types
87 // Treat 8-bit ints (chars) as ints on output, not as chars
90 showParam(ostream
&os
, const char &value
)
98 showParam(ostream
&os
, const unsigned char &value
)
100 os
<< (unsigned int)value
;
104 // Use sscanf() for FP types as to_number() only handles integers
107 parseParam(const string
&s
, float &value
)
109 return (sscanf(s
.c_str(), "%f", &value
) == 1);
114 parseParam(const string
&s
, double &value
)
116 return (sscanf(s
.c_str(), "%lf", &value
) == 1);
121 parseParam(const string
&s
, bool &value
)
123 const string
&ls
= to_lower(s
);
138 // Display bools as strings
141 showParam(ostream
&os
, const bool &value
)
143 os
<< (value
? "true" : "false");
147 // String requires no processing to speak of
150 parseParam(const string
&s
, string
&value
)
156 int Serializable::ckptMaxCount
= 0;
157 int Serializable::ckptCount
= 0;
158 int Serializable::ckptPrevCount
= -1;
161 Serializable::nameOut(ostream
&os
)
163 os
<< "\n[" << name() << "]\n";
167 Serializable::nameOut(ostream
&os
, const string
&_name
)
169 os
<< "\n[" << _name
<< "]\n";
174 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
177 showParam(os
, param
);
183 arrayParamOut(ostream
&os
, const std::string
&name
,
184 const std::vector
<T
> ¶m
)
186 int size
= param
.size();
189 showParam(os
, param
[0]);
190 for (int i
= 1; i
< size
; ++i
) {
192 showParam(os
, param
[i
]);
200 paramIn(Checkpoint
*cp
, const std::string
§ion
,
201 const std::string
&name
, T
¶m
)
204 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
205 fatal("Can't unserialize '%s:%s'\n", section
, name
);
212 arrayParamOut(ostream
&os
, const std::string
&name
,
213 const T
*param
, int size
)
217 showParam(os
, param
[0]);
218 for (int i
= 1; i
< size
; ++i
) {
220 showParam(os
, param
[i
]);
228 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
229 const std::string
&name
, T
*param
, int size
)
232 if (!cp
->find(section
, name
, str
)) {
233 fatal("Can't unserialize '%s:%s'\n", section
, name
);
236 // code below stolen from VectorParam<T>::parse().
237 // it would be nice to unify these somehow...
239 vector
<string
> tokens
;
241 tokenize(tokens
, str
, ' ');
243 // Need this if we were doing a vector
244 // value.resize(tokens.size());
246 if (tokens
.size() != size
) {
247 fatal("Array size mismatch on %s:%s'\n", section
, name
);
250 for (int i
= 0; i
< tokens
.size(); i
++) {
251 // need to parse into local variable to handle vector<bool>,
252 // for which operator[] returns a special reference class
253 // that's not the same as 'bool&', (since it's a packed
256 if (!parseParam(tokens
[i
], scalar_value
)) {
257 string
err("could not parse \"");
265 // assign parsed value to vector
266 param
[i
] = scalar_value
;
272 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
273 const std::string
&name
, std::vector
<T
> ¶m
)
276 if (!cp
->find(section
, name
, str
)) {
277 fatal("Can't unserialize '%s:%s'\n", section
, name
);
280 // code below stolen from VectorParam<T>::parse().
281 // it would be nice to unify these somehow...
283 vector
<string
> tokens
;
285 tokenize(tokens
, str
, ' ');
287 // Need this if we were doing a vector
288 // value.resize(tokens.size());
290 param
.resize(tokens
.size());
292 for (int i
= 0; i
< tokens
.size(); i
++) {
293 // need to parse into local variable to handle vector<bool>,
294 // for which operator[] returns a special reference class
295 // that's not the same as 'bool&', (since it's a packed
298 if (!parseParam(tokens
[i
], scalar_value
)) {
299 string
err("could not parse \"");
307 // assign parsed value to vector
308 param
[i
] = scalar_value
;
315 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
316 const std::string
&name
, SimObject
* ¶m
)
318 if (!cp
->findObj(section
, name
, param
)) {
319 fatal("Can't unserialize '%s:%s'\n", section
, name
);
324 #define INSTANTIATE_PARAM_TEMPLATES(type) \
326 paramOut(ostream &os, const std::string &name, type const ¶m); \
328 paramIn(Checkpoint *cp, const std::string §ion, \
329 const std::string &name, type & param); \
331 arrayParamOut(ostream &os, const std::string &name, \
332 type const *param, int size); \
334 arrayParamIn(Checkpoint *cp, const std::string §ion, \
335 const std::string &name, type *param, int size); \
337 arrayParamOut(ostream &os, const std::string &name, \
338 const std::vector<type> ¶m); \
340 arrayParamIn(Checkpoint *cp, const std::string §ion, \
341 const std::string &name, std::vector<type> ¶m);
343 INSTANTIATE_PARAM_TEMPLATES(signed char)
344 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
345 INSTANTIATE_PARAM_TEMPLATES(signed short)
346 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
347 INSTANTIATE_PARAM_TEMPLATES(signed int)
348 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
349 INSTANTIATE_PARAM_TEMPLATES(signed long)
350 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
351 INSTANTIATE_PARAM_TEMPLATES(signed long long)
352 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
353 INSTANTIATE_PARAM_TEMPLATES(bool)
354 INSTANTIATE_PARAM_TEMPLATES(string
)
357 /////////////////////////////
359 /// Container for serializing global variables (not associated with
360 /// any serialized object).
361 class Globals
: public Serializable
364 const string
name() const;
365 void serialize(ostream
&os
);
366 void unserialize(Checkpoint
*cp
);
369 /// The one and only instance of the Globals class.
373 Globals::name() const
379 Globals::serialize(ostream
&os
)
382 SERIALIZE_SCALAR(curTick
);
384 nameOut(os
, "MainEventQueue");
385 mainEventQueue
.serialize(os
);
389 Globals::unserialize(Checkpoint
*cp
)
391 const string
§ion
= name();
392 UNSERIALIZE_SCALAR(curTick
);
394 mainEventQueue
.unserialize(cp
, "MainEventQueue");
398 Serializable::serializeAll(const std::string
&cpt_dir
)
400 setCheckpointDir(cpt_dir
);
401 string dir
= Checkpoint::dir();
402 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
403 fatal("couldn't mkdir %s\n", dir
);
405 string cpt_file
= dir
+ Checkpoint::baseFilename
;
406 ofstream
outstream(cpt_file
.c_str());
407 time_t t
= time(NULL
);
408 outstream
<< "// checkpoint generated: " << ctime(&t
);
410 globals
.serialize(outstream
);
411 SimObject::serializeAll(outstream
);
415 Serializable::unserializeAll(const std::string
&cpt_dir
)
417 setCheckpointDir(cpt_dir
);
418 string dir
= Checkpoint::dir();
419 string cpt_file
= dir
+ Checkpoint::baseFilename
;
422 DPRINTFR(Config
, "Loading checkpoint dir '%s'\n",
424 Checkpoint
*cp
= new Checkpoint(dir
, section
);
425 unserializeGlobals(cp
);
426 SimObject::unserializeAll(cp
);
430 Serializable::unserializeGlobals(Checkpoint
*cp
)
432 globals
.unserialize(cp
);
435 const char *Checkpoint::baseFilename
= "m5.cpt";
437 static string checkpointDirBase
;
440 setCheckpointDir(const std::string
&name
)
442 checkpointDirBase
= name
;
443 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
444 checkpointDirBase
+= "/";
450 // use csprintf to insert curTick into directory name if it
451 // appears to have a format placeholder in it.
452 return (checkpointDirBase
.find("%") != string::npos
) ?
453 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
457 debug_serialize(const std::string
&cpt_dir
)
459 Serializable::serializeAll(cpt_dir
);
463 ////////////////////////////////////////////////////////////////////////
465 // SerializableClass member definitions
467 ////////////////////////////////////////////////////////////////////////
469 // Map of class names to SerializableBuilder creation functions.
470 // Need to make this a pointer so we can force initialization on the
471 // first reference; otherwise, some SerializableClass constructors
472 // may be invoked before the classMap constructor.
473 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
475 // SerializableClass constructor: add mapping to classMap
476 SerializableClass::SerializableClass(const string
&className
,
477 CreateFunc createFunc
)
479 if (classMap
== NULL
)
480 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
482 if ((*classMap
)[className
])
484 cerr
<< "Error: simulation object class " << className
<< " redefined"
489 // add className --> createFunc to class map
490 (*classMap
)[className
] = createFunc
;
497 SerializableClass::createObject(Checkpoint
*cp
,
498 const std::string
§ion
)
502 if (!cp
->find(section
, "type", className
)) {
503 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
507 CreateFunc createFunc
= (*classMap
)[className
];
509 if (createFunc
== NULL
) {
510 fatal("Serializable::create: no create function for class '%s'.\n",
514 Serializable
*object
= createFunc(cp
, section
);
516 assert(object
!= NULL
);
523 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
525 Serializable
*object
= SerializableClass::createObject(cp
, section
);
526 object
->unserialize(cp
, section
);
531 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
)
532 : db(new IniFile
), basePath(path
), cptDir(cpt_dir
)
534 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
535 if (!db
->load(filename
)) {
536 fatal("Can't load checkpoint file '%s'\n", filename
);
542 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
545 return db
->find(section
, entry
, value
);
550 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
555 if (!db
->find(section
, entry
, path
))
558 value
= resolveSimObject(path
);
564 Checkpoint::sectionExists(const std::string
§ion
)
566 return db
->sectionExists(section
);