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 string
&name
, const T
¶m
)
177 showParam(os
, param
);
183 arrayParamOut(ostream
&os
, const string
&name
, const vector
<T
> ¶m
)
185 typename vector
<T
>::size_type size
= param
.size();
188 showParam(os
, param
[0]);
189 for (typename vector
<T
>::size_type i
= 1; i
< size
; ++i
) {
191 showParam(os
, param
[i
]);
199 paramIn(Checkpoint
*cp
, const string
§ion
, const string
&name
, T
¶m
)
202 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
203 fatal("Can't unserialize '%s:%s'\n", section
, name
);
210 arrayParamOut(ostream
&os
, const string
&name
, const T
*param
, unsigned size
)
214 showParam(os
, param
[0]);
215 for (unsigned i
= 1; i
< size
; ++i
) {
217 showParam(os
, param
[i
]);
225 arrayParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
,
226 T
*param
, unsigned size
)
229 if (!cp
->find(section
, name
, str
)) {
230 fatal("Can't unserialize '%s:%s'\n", section
, name
);
233 // code below stolen from VectorParam<T>::parse().
234 // it would be nice to unify these somehow...
236 vector
<string
> tokens
;
238 tokenize(tokens
, str
, ' ');
240 // Need this if we were doing a vector
241 // value.resize(tokens.size());
243 if (tokens
.size() != size
) {
244 fatal("Array size mismatch on %s:%s'\n", section
, name
);
247 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
248 // need to parse into local variable to handle vector<bool>,
249 // for which operator[] returns a special reference class
250 // that's not the same as 'bool&', (since it's a packed
253 if (!parseParam(tokens
[i
], scalar_value
)) {
254 string
err("could not parse \"");
262 // assign parsed value to vector
263 param
[i
] = scalar_value
;
269 arrayParamIn(Checkpoint
*cp
, const string
§ion
,
270 const string
&name
, vector
<T
> ¶m
)
273 if (!cp
->find(section
, name
, str
)) {
274 fatal("Can't unserialize '%s:%s'\n", section
, name
);
277 // code below stolen from VectorParam<T>::parse().
278 // it would be nice to unify these somehow...
280 vector
<string
> tokens
;
282 tokenize(tokens
, str
, ' ');
284 // Need this if we were doing a vector
285 // value.resize(tokens.size());
287 param
.resize(tokens
.size());
289 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
290 // need to parse into local variable to handle vector<bool>,
291 // for which operator[] returns a special reference class
292 // that's not the same as 'bool&', (since it's a packed
295 if (!parseParam(tokens
[i
], scalar_value
)) {
296 string
err("could not parse \"");
304 // assign parsed value to vector
305 param
[i
] = scalar_value
;
310 objParamIn(Checkpoint
*cp
, const string
§ion
,
311 const string
&name
, SimObject
* ¶m
)
313 if (!cp
->findObj(section
, name
, param
)) {
314 fatal("Can't unserialize '%s:%s'\n", section
, name
);
319 #define INSTANTIATE_PARAM_TEMPLATES(type) \
321 paramOut(ostream &os, const string &name, type const ¶m); \
323 paramIn(Checkpoint *cp, const string §ion, \
324 const string &name, type & param); \
326 arrayParamOut(ostream &os, const string &name, \
327 type const *param, unsigned size); \
329 arrayParamIn(Checkpoint *cp, const string §ion, \
330 const string &name, type *param, unsigned size); \
332 arrayParamOut(ostream &os, const string &name, \
333 const vector<type> ¶m); \
335 arrayParamIn(Checkpoint *cp, const string §ion, \
336 const string &name, vector<type> ¶m);
338 INSTANTIATE_PARAM_TEMPLATES(signed char)
339 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
340 INSTANTIATE_PARAM_TEMPLATES(signed short)
341 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
342 INSTANTIATE_PARAM_TEMPLATES(signed int)
343 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
344 INSTANTIATE_PARAM_TEMPLATES(signed long)
345 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
346 INSTANTIATE_PARAM_TEMPLATES(signed long long)
347 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
348 INSTANTIATE_PARAM_TEMPLATES(bool)
349 INSTANTIATE_PARAM_TEMPLATES(float)
350 INSTANTIATE_PARAM_TEMPLATES(double)
351 INSTANTIATE_PARAM_TEMPLATES(string
)
354 /////////////////////////////
356 /// Container for serializing global variables (not associated with
357 /// any serialized object).
358 class Globals
: public Serializable
361 const string
name() const;
362 void serialize(ostream
&os
);
363 void unserialize(Checkpoint
*cp
);
366 /// The one and only instance of the Globals class.
370 Globals::name() const
376 Globals::serialize(ostream
&os
)
379 SERIALIZE_SCALAR(curTick
);
381 nameOut(os
, "MainEventQueue");
382 mainEventQueue
.serialize(os
);
386 Globals::unserialize(Checkpoint
*cp
)
388 const string
§ion
= name();
389 UNSERIALIZE_SCALAR(curTick
);
391 mainEventQueue
.unserialize(cp
, "MainEventQueue");
394 Serializable::Serializable()
398 Serializable::~Serializable()
403 Serializable::serialize(ostream
&os
)
408 Serializable::unserialize(Checkpoint
*cp
, const string
§ion
)
413 Serializable::serializeAll(const string
&cpt_dir
)
415 setCheckpointDir(cpt_dir
);
416 string dir
= Checkpoint::dir();
417 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
418 fatal("couldn't mkdir %s\n", dir
);
420 string cpt_file
= dir
+ Checkpoint::baseFilename
;
421 ofstream
outstream(cpt_file
.c_str());
422 time_t t
= time(NULL
);
423 if (!outstream
.is_open())
424 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
425 outstream
<< "// checkpoint generated: " << ctime(&t
);
427 globals
.serialize(outstream
);
428 SimObject::serializeAll(outstream
);
432 Serializable::unserializeAll(const string
&cpt_dir
)
434 setCheckpointDir(cpt_dir
);
435 string dir
= Checkpoint::dir();
436 string cpt_file
= dir
+ Checkpoint::baseFilename
;
439 DPRINTFR(Config
, "Loading checkpoint dir '%s'\n",
441 Checkpoint
*cp
= new Checkpoint(dir
, section
);
442 unserializeGlobals(cp
);
443 SimObject::unserializeAll(cp
);
447 Serializable::unserializeGlobals(Checkpoint
*cp
)
449 globals
.unserialize(cp
);
452 const char *Checkpoint::baseFilename
= "m5.cpt";
454 static string checkpointDirBase
;
457 setCheckpointDir(const string
&name
)
459 checkpointDirBase
= name
;
460 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
461 checkpointDirBase
+= "/";
467 // use csprintf to insert curTick into directory name if it
468 // appears to have a format placeholder in it.
469 return (checkpointDirBase
.find("%") != string::npos
) ?
470 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
474 debug_serialize(const string
&cpt_dir
)
476 Serializable::serializeAll(cpt_dir
);
480 ////////////////////////////////////////////////////////////////////////
482 // SerializableClass member definitions
484 ////////////////////////////////////////////////////////////////////////
486 // Map of class names to SerializableBuilder creation functions.
487 // Need to make this a pointer so we can force initialization on the
488 // first reference; otherwise, some SerializableClass constructors
489 // may be invoked before the classMap constructor.
490 map
<string
, SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
492 // SerializableClass constructor: add mapping to classMap
493 SerializableClass::SerializableClass(const string
&className
,
494 CreateFunc createFunc
)
496 if (classMap
== NULL
)
497 classMap
= new map
<string
, SerializableClass::CreateFunc
>();
499 if ((*classMap
)[className
])
500 fatal("Error: simulation object class %s redefined\n", className
);
502 // add className --> createFunc to class map
503 (*classMap
)[className
] = createFunc
;
509 SerializableClass::createObject(Checkpoint
*cp
, const string
§ion
)
513 if (!cp
->find(section
, "type", className
)) {
514 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
518 CreateFunc createFunc
= (*classMap
)[className
];
520 if (createFunc
== NULL
) {
521 fatal("Serializable::create: no create function for class '%s'.\n",
525 Serializable
*object
= createFunc(cp
, section
);
527 assert(object
!= NULL
);
534 Serializable::create(Checkpoint
*cp
, const string
§ion
)
536 Serializable
*object
= SerializableClass::createObject(cp
, section
);
537 object
->unserialize(cp
, section
);
542 Checkpoint::Checkpoint(const string
&cpt_dir
, const string
&path
)
543 : db(new IniFile
), basePath(path
), cptDir(cpt_dir
)
545 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
546 if (!db
->load(filename
)) {
547 fatal("Can't load checkpoint file '%s'\n", filename
);
553 Checkpoint::find(const string
§ion
, const string
&entry
, string
&value
)
555 return db
->find(section
, entry
, value
);
560 Checkpoint::findObj(const string
§ion
, const string
&entry
,
565 if (!db
->find(section
, entry
, path
))
568 value
= resolveSimObject(path
);
574 Checkpoint::sectionExists(const string
§ion
)
576 return db
->sectionExists(section
);