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
35 #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 signed char &value
)
106 showParam(ostream
&os
, const unsigned char &value
)
108 os
<< (unsigned int)value
;
112 // Use sscanf() for FP types as to_number() only handles integers
115 parseParam(const string
&s
, float &value
)
117 return (sscanf(s
.c_str(), "%f", &value
) == 1);
122 parseParam(const string
&s
, double &value
)
124 return (sscanf(s
.c_str(), "%lf", &value
) == 1);
129 parseParam(const string
&s
, bool &value
)
131 const string
&ls
= to_lower(s
);
146 // Display bools as strings
149 showParam(ostream
&os
, const bool &value
)
151 os
<< (value
? "true" : "false");
155 // String requires no processing to speak of
158 parseParam(const string
&s
, string
&value
)
164 int Serializable::ckptMaxCount
= 0;
165 int Serializable::ckptCount
= 0;
166 int Serializable::ckptPrevCount
= -1;
169 Serializable::nameOut(ostream
&os
)
171 os
<< "\n[" << name() << "]\n";
175 Serializable::nameOut(ostream
&os
, const string
&_name
)
177 os
<< "\n[" << _name
<< "]\n";
182 paramOut(ostream
&os
, const string
&name
, const T
¶m
)
185 showParam(os
, param
);
191 arrayParamOut(ostream
&os
, const string
&name
, const vector
<T
> ¶m
)
193 typename vector
<T
>::size_type size
= param
.size();
196 showParam(os
, param
[0]);
197 for (typename vector
<T
>::size_type i
= 1; i
< size
; ++i
) {
199 showParam(os
, param
[i
]);
206 arrayParamOut(ostream
&os
, const string
&name
, const list
<T
> ¶m
)
208 typename list
<T
>::const_iterator it
= param
.begin();
211 if (param
.size() > 0)
214 while (it
!= param
.end()) {
224 paramIn(Checkpoint
*cp
, const string
§ion
, const string
&name
, T
¶m
)
227 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
228 fatal("Can't unserialize '%s:%s'\n", section
, name
);
234 optParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
, T
¶m
)
237 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
238 warn("optional parameter %s:%s not present\n", section
, name
);
247 arrayParamOut(ostream
&os
, const string
&name
, const T
*param
, unsigned size
)
251 showParam(os
, param
[0]);
252 for (unsigned i
= 1; i
< size
; ++i
) {
254 showParam(os
, param
[i
]);
262 arrayParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
,
263 T
*param
, unsigned size
)
266 if (!cp
->find(section
, name
, str
)) {
267 fatal("Can't unserialize '%s:%s'\n", section
, name
);
270 // code below stolen from VectorParam<T>::parse().
271 // it would be nice to unify these somehow...
273 vector
<string
> tokens
;
275 tokenize(tokens
, str
, ' ');
277 // Need this if we were doing a vector
278 // value.resize(tokens.size());
280 if (tokens
.size() != size
) {
281 fatal("Array size mismatch on %s:%s'\n", section
, name
);
284 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
285 // need to parse into local variable to handle vector<bool>,
286 // for which operator[] returns a special reference class
287 // that's not the same as 'bool&', (since it's a packed
290 if (!parseParam(tokens
[i
], scalar_value
)) {
291 string
err("could not parse \"");
299 // assign parsed value to vector
300 param
[i
] = scalar_value
;
306 arrayParamIn(Checkpoint
*cp
, const string
§ion
,
307 const string
&name
, vector
<T
> ¶m
)
310 if (!cp
->find(section
, name
, str
)) {
311 fatal("Can't unserialize '%s:%s'\n", section
, name
);
314 // code below stolen from VectorParam<T>::parse().
315 // it would be nice to unify these somehow...
317 vector
<string
> tokens
;
319 tokenize(tokens
, str
, ' ');
321 // Need this if we were doing a vector
322 // value.resize(tokens.size());
324 param
.resize(tokens
.size());
326 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
327 // need to parse into local variable to handle vector<bool>,
328 // for which operator[] returns a special reference class
329 // that's not the same as 'bool&', (since it's a packed
332 if (!parseParam(tokens
[i
], scalar_value
)) {
333 string
err("could not parse \"");
341 // assign parsed value to vector
342 param
[i
] = scalar_value
;
348 arrayParamIn(Checkpoint
*cp
, const string
§ion
,
349 const string
&name
, list
<T
> ¶m
)
352 if (!cp
->find(section
, name
, str
)) {
353 fatal("Can't unserialize '%s:%s'\n", section
, name
);
357 vector
<string
> tokens
;
358 tokenize(tokens
, str
, ' ');
360 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
362 if (!parseParam(tokens
[i
], scalar_value
)) {
363 string
err("could not parse \"");
371 // assign parsed value to vector
372 param
.push_back(scalar_value
);
378 objParamIn(Checkpoint
*cp
, const string
§ion
,
379 const string
&name
, SimObject
* ¶m
)
381 if (!cp
->findObj(section
, name
, param
)) {
382 fatal("Can't unserialize '%s:%s'\n", section
, name
);
387 #define INSTANTIATE_PARAM_TEMPLATES(type) \
389 paramOut(ostream &os, const string &name, type const ¶m); \
391 paramIn(Checkpoint *cp, const string §ion, \
392 const string &name, type & param); \
394 optParamIn(Checkpoint *cp, const string §ion, \
395 const string &name, type & param); \
397 arrayParamOut(ostream &os, const string &name, \
398 type const *param, unsigned size); \
400 arrayParamIn(Checkpoint *cp, const string §ion, \
401 const string &name, type *param, unsigned size); \
403 arrayParamOut(ostream &os, const string &name, \
404 const vector<type> ¶m); \
406 arrayParamIn(Checkpoint *cp, const string §ion, \
407 const string &name, vector<type> ¶m); \
409 arrayParamOut(ostream &os, const string &name, \
410 const list<type> ¶m); \
412 arrayParamIn(Checkpoint *cp, const string §ion, \
413 const string &name, list<type> ¶m);
415 INSTANTIATE_PARAM_TEMPLATES(char)
416 INSTANTIATE_PARAM_TEMPLATES(signed char)
417 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
418 INSTANTIATE_PARAM_TEMPLATES(signed short)
419 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
420 INSTANTIATE_PARAM_TEMPLATES(signed int)
421 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
422 INSTANTIATE_PARAM_TEMPLATES(signed long)
423 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
424 INSTANTIATE_PARAM_TEMPLATES(signed long long)
425 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
426 INSTANTIATE_PARAM_TEMPLATES(bool)
427 INSTANTIATE_PARAM_TEMPLATES(float)
428 INSTANTIATE_PARAM_TEMPLATES(double)
429 INSTANTIATE_PARAM_TEMPLATES(string
)
432 /////////////////////////////
434 /// Container for serializing global variables (not associated with
435 /// any serialized object).
436 class Globals
: public Serializable
439 const string
name() const;
440 void serialize(ostream
&os
);
441 void unserialize(Checkpoint
*cp
);
444 /// The one and only instance of the Globals class.
448 Globals::name() const
454 Globals::serialize(ostream
&os
)
457 paramOut(os
, "curTick", curTick());
459 nameOut(os
, "MainEventQueue");
460 mainEventQueue
.serialize(os
);
464 Globals::unserialize(Checkpoint
*cp
)
466 const string
§ion
= name();
468 paramIn(cp
, section
, "curTick", tick
);
471 mainEventQueue
.unserialize(cp
, "MainEventQueue");
474 Serializable::Serializable()
478 Serializable::~Serializable()
483 Serializable::serialize(ostream
&os
)
488 Serializable::unserialize(Checkpoint
*cp
, const string
§ion
)
493 Serializable::serializeAll(const string
&cpt_dir
)
495 string dir
= Checkpoint::setDir(cpt_dir
);
496 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
497 fatal("couldn't mkdir %s\n", dir
);
499 string cpt_file
= dir
+ Checkpoint::baseFilename
;
500 ofstream
outstream(cpt_file
.c_str());
501 time_t t
= time(NULL
);
502 if (!outstream
.is_open())
503 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
504 outstream
<< "## checkpoint generated: " << ctime(&t
);
506 globals
.serialize(outstream
);
507 SimObject::serializeAll(outstream
);
511 Serializable::unserializeGlobals(Checkpoint
*cp
)
513 globals
.unserialize(cp
);
517 debug_serialize(const string
&cpt_dir
)
519 Serializable::serializeAll(cpt_dir
);
523 ////////////////////////////////////////////////////////////////////////
525 // SerializableClass member definitions
527 ////////////////////////////////////////////////////////////////////////
529 // Map of class names to SerializableBuilder creation functions.
530 // Need to make this a pointer so we can force initialization on the
531 // first reference; otherwise, some SerializableClass constructors
532 // may be invoked before the classMap constructor.
533 map
<string
, SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
535 // SerializableClass constructor: add mapping to classMap
536 SerializableClass::SerializableClass(const string
&className
,
537 CreateFunc createFunc
)
539 if (classMap
== NULL
)
540 classMap
= new map
<string
, SerializableClass::CreateFunc
>();
542 if ((*classMap
)[className
])
543 fatal("Error: simulation object class %s redefined\n", className
);
545 // add className --> createFunc to class map
546 (*classMap
)[className
] = createFunc
;
552 SerializableClass::createObject(Checkpoint
*cp
, const string
§ion
)
556 if (!cp
->find(section
, "type", className
)) {
557 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
561 CreateFunc createFunc
= (*classMap
)[className
];
563 if (createFunc
== NULL
) {
564 fatal("Serializable::create: no create function for class '%s'.\n",
568 Serializable
*object
= createFunc(cp
, section
);
570 assert(object
!= NULL
);
577 Serializable::create(Checkpoint
*cp
, const string
§ion
)
579 Serializable
*object
= SerializableClass::createObject(cp
, section
);
580 object
->unserialize(cp
, section
);
585 const char *Checkpoint::baseFilename
= "m5.cpt";
587 string
Checkpoint::currentDirectory
;
590 Checkpoint::setDir(const string
&name
)
592 // use csprintf to insert curTick() into directory name if it
593 // appears to have a format placeholder in it.
594 currentDirectory
= (name
.find("%") != string::npos
) ?
595 csprintf(name
, curTick()) : name
;
596 if (currentDirectory
[currentDirectory
.size() - 1] != '/')
597 currentDirectory
+= "/";
598 return currentDirectory
;
604 return currentDirectory
;
608 Checkpoint::Checkpoint(const string
&cpt_dir
)
609 : db(new IniFile
), cptDir(setDir(cpt_dir
))
611 string filename
= cptDir
+ "/" + Checkpoint::baseFilename
;
612 if (!db
->load(filename
)) {
613 fatal("Can't load checkpoint file '%s'\n", filename
);
619 Checkpoint::find(const string
§ion
, const string
&entry
, string
&value
)
621 return db
->find(section
, entry
, value
);
626 Checkpoint::findObj(const string
§ion
, const string
&entry
,
631 if (!db
->find(section
, entry
, path
))
634 value
= resolveSimObject(path
);
640 Checkpoint::sectionExists(const string
§ion
)
642 return db
->sectionExists(section
);