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
);
209 optParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
, T
¶m
)
212 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
213 warn("optional parameter %s:%s not present\n", section
, name
);
222 arrayParamOut(ostream
&os
, const string
&name
, const T
*param
, unsigned size
)
226 showParam(os
, param
[0]);
227 for (unsigned i
= 1; i
< size
; ++i
) {
229 showParam(os
, param
[i
]);
237 arrayParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
,
238 T
*param
, unsigned size
)
241 if (!cp
->find(section
, name
, str
)) {
242 fatal("Can't unserialize '%s:%s'\n", section
, name
);
245 // code below stolen from VectorParam<T>::parse().
246 // it would be nice to unify these somehow...
248 vector
<string
> tokens
;
250 tokenize(tokens
, str
, ' ');
252 // Need this if we were doing a vector
253 // value.resize(tokens.size());
255 if (tokens
.size() != size
) {
256 fatal("Array size mismatch on %s:%s'\n", section
, name
);
259 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
260 // need to parse into local variable to handle vector<bool>,
261 // for which operator[] returns a special reference class
262 // that's not the same as 'bool&', (since it's a packed
265 if (!parseParam(tokens
[i
], scalar_value
)) {
266 string
err("could not parse \"");
274 // assign parsed value to vector
275 param
[i
] = scalar_value
;
281 arrayParamIn(Checkpoint
*cp
, const string
§ion
,
282 const string
&name
, vector
<T
> ¶m
)
285 if (!cp
->find(section
, name
, str
)) {
286 fatal("Can't unserialize '%s:%s'\n", section
, name
);
289 // code below stolen from VectorParam<T>::parse().
290 // it would be nice to unify these somehow...
292 vector
<string
> tokens
;
294 tokenize(tokens
, str
, ' ');
296 // Need this if we were doing a vector
297 // value.resize(tokens.size());
299 param
.resize(tokens
.size());
301 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
302 // need to parse into local variable to handle vector<bool>,
303 // for which operator[] returns a special reference class
304 // that's not the same as 'bool&', (since it's a packed
307 if (!parseParam(tokens
[i
], scalar_value
)) {
308 string
err("could not parse \"");
316 // assign parsed value to vector
317 param
[i
] = scalar_value
;
322 objParamIn(Checkpoint
*cp
, const string
§ion
,
323 const string
&name
, SimObject
* ¶m
)
325 if (!cp
->findObj(section
, name
, param
)) {
326 fatal("Can't unserialize '%s:%s'\n", section
, name
);
331 #define INSTANTIATE_PARAM_TEMPLATES(type) \
333 paramOut(ostream &os, const string &name, type const ¶m); \
335 paramIn(Checkpoint *cp, const string §ion, \
336 const string &name, type & param); \
338 optParamIn(Checkpoint *cp, const string §ion, \
339 const string &name, type & param); \
341 arrayParamOut(ostream &os, const string &name, \
342 type const *param, unsigned size); \
344 arrayParamIn(Checkpoint *cp, const string §ion, \
345 const string &name, type *param, unsigned size); \
347 arrayParamOut(ostream &os, const string &name, \
348 const vector<type> ¶m); \
350 arrayParamIn(Checkpoint *cp, const string §ion, \
351 const string &name, vector<type> ¶m);
353 INSTANTIATE_PARAM_TEMPLATES(signed char)
354 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
355 INSTANTIATE_PARAM_TEMPLATES(signed short)
356 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
357 INSTANTIATE_PARAM_TEMPLATES(signed int)
358 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
359 INSTANTIATE_PARAM_TEMPLATES(signed long)
360 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
361 INSTANTIATE_PARAM_TEMPLATES(signed long long)
362 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
363 INSTANTIATE_PARAM_TEMPLATES(bool)
364 INSTANTIATE_PARAM_TEMPLATES(float)
365 INSTANTIATE_PARAM_TEMPLATES(double)
366 INSTANTIATE_PARAM_TEMPLATES(string
)
369 /////////////////////////////
371 /// Container for serializing global variables (not associated with
372 /// any serialized object).
373 class Globals
: public Serializable
376 const string
name() const;
377 void serialize(ostream
&os
);
378 void unserialize(Checkpoint
*cp
);
381 /// The one and only instance of the Globals class.
385 Globals::name() const
391 Globals::serialize(ostream
&os
)
394 SERIALIZE_SCALAR(curTick
);
396 nameOut(os
, "MainEventQueue");
397 mainEventQueue
.serialize(os
);
401 Globals::unserialize(Checkpoint
*cp
)
403 const string
§ion
= name();
404 UNSERIALIZE_SCALAR(curTick
);
406 mainEventQueue
.unserialize(cp
, "MainEventQueue");
409 Serializable::Serializable()
413 Serializable::~Serializable()
418 Serializable::serialize(ostream
&os
)
423 Serializable::unserialize(Checkpoint
*cp
, const string
§ion
)
428 Serializable::serializeAll(const string
&cpt_dir
)
430 setCheckpointDir(cpt_dir
);
431 string dir
= Checkpoint::dir();
432 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
433 fatal("couldn't mkdir %s\n", dir
);
435 string cpt_file
= dir
+ Checkpoint::baseFilename
;
436 ofstream
outstream(cpt_file
.c_str());
437 time_t t
= time(NULL
);
438 if (!outstream
.is_open())
439 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
440 outstream
<< "## checkpoint generated: " << ctime(&t
);
442 globals
.serialize(outstream
);
443 SimObject::serializeAll(outstream
);
447 Serializable::unserializeAll(const string
&cpt_dir
)
449 setCheckpointDir(cpt_dir
);
450 string dir
= Checkpoint::dir();
451 string cpt_file
= dir
+ Checkpoint::baseFilename
;
454 DPRINTFR(Config
, "Loading checkpoint dir '%s'\n",
456 Checkpoint
*cp
= new Checkpoint(dir
, section
);
457 unserializeGlobals(cp
);
458 SimObject::unserializeAll(cp
);
462 Serializable::unserializeGlobals(Checkpoint
*cp
)
464 globals
.unserialize(cp
);
467 const char *Checkpoint::baseFilename
= "m5.cpt";
469 static string checkpointDirBase
;
472 setCheckpointDir(const string
&name
)
474 checkpointDirBase
= name
;
475 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
476 checkpointDirBase
+= "/";
482 // use csprintf to insert curTick into directory name if it
483 // appears to have a format placeholder in it.
484 return (checkpointDirBase
.find("%") != string::npos
) ?
485 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
489 debug_serialize(const string
&cpt_dir
)
491 Serializable::serializeAll(cpt_dir
);
495 ////////////////////////////////////////////////////////////////////////
497 // SerializableClass member definitions
499 ////////////////////////////////////////////////////////////////////////
501 // Map of class names to SerializableBuilder creation functions.
502 // Need to make this a pointer so we can force initialization on the
503 // first reference; otherwise, some SerializableClass constructors
504 // may be invoked before the classMap constructor.
505 map
<string
, SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
507 // SerializableClass constructor: add mapping to classMap
508 SerializableClass::SerializableClass(const string
&className
,
509 CreateFunc createFunc
)
511 if (classMap
== NULL
)
512 classMap
= new map
<string
, SerializableClass::CreateFunc
>();
514 if ((*classMap
)[className
])
515 fatal("Error: simulation object class %s redefined\n", className
);
517 // add className --> createFunc to class map
518 (*classMap
)[className
] = createFunc
;
524 SerializableClass::createObject(Checkpoint
*cp
, const string
§ion
)
528 if (!cp
->find(section
, "type", className
)) {
529 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
533 CreateFunc createFunc
= (*classMap
)[className
];
535 if (createFunc
== NULL
) {
536 fatal("Serializable::create: no create function for class '%s'.\n",
540 Serializable
*object
= createFunc(cp
, section
);
542 assert(object
!= NULL
);
549 Serializable::create(Checkpoint
*cp
, const string
§ion
)
551 Serializable
*object
= SerializableClass::createObject(cp
, section
);
552 object
->unserialize(cp
, section
);
557 Checkpoint::Checkpoint(const string
&cpt_dir
, const string
&path
)
558 : db(new IniFile
), basePath(path
), cptDir(cpt_dir
)
560 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
561 if (!db
->load(filename
)) {
562 fatal("Can't load checkpoint file '%s'\n", filename
);
568 Checkpoint::find(const string
§ion
, const string
&entry
, string
&value
)
570 return db
->find(section
, entry
, value
);
575 Checkpoint::findObj(const string
§ion
, const string
&entry
,
580 if (!db
->find(section
, entry
, path
))
583 value
= resolveSimObject(path
);
589 Checkpoint::sectionExists(const string
§ion
)
591 return db
->sectionExists(section
);