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 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
]);
207 paramIn(Checkpoint
*cp
, const string
§ion
, const string
&name
, T
¶m
)
210 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
211 fatal("Can't unserialize '%s:%s'\n", section
, name
);
217 optParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
, T
¶m
)
220 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
221 warn("optional parameter %s:%s not present\n", section
, name
);
230 arrayParamOut(ostream
&os
, const string
&name
, const T
*param
, unsigned size
)
234 showParam(os
, param
[0]);
235 for (unsigned i
= 1; i
< size
; ++i
) {
237 showParam(os
, param
[i
]);
245 arrayParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
,
246 T
*param
, unsigned size
)
249 if (!cp
->find(section
, name
, str
)) {
250 fatal("Can't unserialize '%s:%s'\n", section
, name
);
253 // code below stolen from VectorParam<T>::parse().
254 // it would be nice to unify these somehow...
256 vector
<string
> tokens
;
258 tokenize(tokens
, str
, ' ');
260 // Need this if we were doing a vector
261 // value.resize(tokens.size());
263 if (tokens
.size() != size
) {
264 fatal("Array size mismatch on %s:%s'\n", section
, name
);
267 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
268 // need to parse into local variable to handle vector<bool>,
269 // for which operator[] returns a special reference class
270 // that's not the same as 'bool&', (since it's a packed
273 if (!parseParam(tokens
[i
], scalar_value
)) {
274 string
err("could not parse \"");
282 // assign parsed value to vector
283 param
[i
] = scalar_value
;
289 arrayParamIn(Checkpoint
*cp
, const string
§ion
,
290 const string
&name
, vector
<T
> ¶m
)
293 if (!cp
->find(section
, name
, str
)) {
294 fatal("Can't unserialize '%s:%s'\n", section
, name
);
297 // code below stolen from VectorParam<T>::parse().
298 // it would be nice to unify these somehow...
300 vector
<string
> tokens
;
302 tokenize(tokens
, str
, ' ');
304 // Need this if we were doing a vector
305 // value.resize(tokens.size());
307 param
.resize(tokens
.size());
309 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
310 // need to parse into local variable to handle vector<bool>,
311 // for which operator[] returns a special reference class
312 // that's not the same as 'bool&', (since it's a packed
315 if (!parseParam(tokens
[i
], scalar_value
)) {
316 string
err("could not parse \"");
324 // assign parsed value to vector
325 param
[i
] = scalar_value
;
330 objParamIn(Checkpoint
*cp
, const string
§ion
,
331 const string
&name
, SimObject
* ¶m
)
333 if (!cp
->findObj(section
, name
, param
)) {
334 fatal("Can't unserialize '%s:%s'\n", section
, name
);
339 #define INSTANTIATE_PARAM_TEMPLATES(type) \
341 paramOut(ostream &os, const string &name, type const ¶m); \
343 paramIn(Checkpoint *cp, const string §ion, \
344 const string &name, type & param); \
346 optParamIn(Checkpoint *cp, const string §ion, \
347 const string &name, type & param); \
349 arrayParamOut(ostream &os, const string &name, \
350 type const *param, unsigned size); \
352 arrayParamIn(Checkpoint *cp, const string §ion, \
353 const string &name, type *param, unsigned size); \
355 arrayParamOut(ostream &os, const string &name, \
356 const vector<type> ¶m); \
358 arrayParamIn(Checkpoint *cp, const string §ion, \
359 const string &name, vector<type> ¶m);
361 INSTANTIATE_PARAM_TEMPLATES(char)
362 INSTANTIATE_PARAM_TEMPLATES(signed char)
363 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
364 INSTANTIATE_PARAM_TEMPLATES(signed short)
365 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
366 INSTANTIATE_PARAM_TEMPLATES(signed int)
367 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
368 INSTANTIATE_PARAM_TEMPLATES(signed long)
369 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
370 INSTANTIATE_PARAM_TEMPLATES(signed long long)
371 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
372 INSTANTIATE_PARAM_TEMPLATES(bool)
373 INSTANTIATE_PARAM_TEMPLATES(float)
374 INSTANTIATE_PARAM_TEMPLATES(double)
375 INSTANTIATE_PARAM_TEMPLATES(string
)
378 /////////////////////////////
380 /// Container for serializing global variables (not associated with
381 /// any serialized object).
382 class Globals
: public Serializable
385 const string
name() const;
386 void serialize(ostream
&os
);
387 void unserialize(Checkpoint
*cp
);
390 /// The one and only instance of the Globals class.
394 Globals::name() const
400 Globals::serialize(ostream
&os
)
403 paramOut(os
, "curTick", curTick());
405 nameOut(os
, "MainEventQueue");
406 mainEventQueue
.serialize(os
);
410 Globals::unserialize(Checkpoint
*cp
)
412 const string
§ion
= name();
414 paramIn(cp
, section
, "curTick", tick
);
417 mainEventQueue
.unserialize(cp
, "MainEventQueue");
420 Serializable::Serializable()
424 Serializable::~Serializable()
429 Serializable::serialize(ostream
&os
)
434 Serializable::unserialize(Checkpoint
*cp
, const string
§ion
)
439 Serializable::serializeAll(const string
&cpt_dir
)
441 string dir
= Checkpoint::setDir(cpt_dir
);
442 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
443 fatal("couldn't mkdir %s\n", dir
);
445 string cpt_file
= dir
+ Checkpoint::baseFilename
;
446 ofstream
outstream(cpt_file
.c_str());
447 time_t t
= time(NULL
);
448 if (!outstream
.is_open())
449 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
450 outstream
<< "## checkpoint generated: " << ctime(&t
);
452 globals
.serialize(outstream
);
453 SimObject::serializeAll(outstream
);
457 Serializable::unserializeGlobals(Checkpoint
*cp
)
459 globals
.unserialize(cp
);
463 debug_serialize(const string
&cpt_dir
)
465 Serializable::serializeAll(cpt_dir
);
469 ////////////////////////////////////////////////////////////////////////
471 // SerializableClass member definitions
473 ////////////////////////////////////////////////////////////////////////
475 // Map of class names to SerializableBuilder creation functions.
476 // Need to make this a pointer so we can force initialization on the
477 // first reference; otherwise, some SerializableClass constructors
478 // may be invoked before the classMap constructor.
479 map
<string
, SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
481 // SerializableClass constructor: add mapping to classMap
482 SerializableClass::SerializableClass(const string
&className
,
483 CreateFunc createFunc
)
485 if (classMap
== NULL
)
486 classMap
= new map
<string
, SerializableClass::CreateFunc
>();
488 if ((*classMap
)[className
])
489 fatal("Error: simulation object class %s redefined\n", className
);
491 // add className --> createFunc to class map
492 (*classMap
)[className
] = createFunc
;
498 SerializableClass::createObject(Checkpoint
*cp
, const 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 string
§ion
)
525 Serializable
*object
= SerializableClass::createObject(cp
, section
);
526 object
->unserialize(cp
, section
);
531 const char *Checkpoint::baseFilename
= "m5.cpt";
533 string
Checkpoint::currentDirectory
;
536 Checkpoint::setDir(const string
&name
)
538 // use csprintf to insert curTick() into directory name if it
539 // appears to have a format placeholder in it.
540 currentDirectory
= (name
.find("%") != string::npos
) ?
541 csprintf(name
, curTick()) : name
;
542 if (currentDirectory
[currentDirectory
.size() - 1] != '/')
543 currentDirectory
+= "/";
544 return currentDirectory
;
550 return currentDirectory
;
554 Checkpoint::Checkpoint(const string
&cpt_dir
)
555 : db(new IniFile
), cptDir(setDir(cpt_dir
))
557 string filename
= cptDir
+ "/" + Checkpoint::baseFilename
;
558 if (!db
->load(filename
)) {
559 fatal("Can't load checkpoint file '%s'\n", filename
);
565 Checkpoint::find(const string
§ion
, const string
&entry
, string
&value
)
567 return db
->find(section
, entry
, value
);
572 Checkpoint::findObj(const string
§ion
, const string
&entry
,
577 if (!db
->find(section
, entry
, path
))
580 value
= resolveSimObject(path
);
586 Checkpoint::sectionExists(const string
§ion
)
588 return db
->sectionExists(section
);