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/annotate.hh"
44 #include "base/inifile.hh"
45 #include "base/misc.hh"
46 #include "base/output.hh"
47 #include "base/str.hh"
48 #include "base/trace.hh"
49 #include "sim/eventq.hh"
50 #include "sim/serialize.hh"
51 #include "sim/sim_events.hh"
52 #include "sim/sim_exit.hh"
53 #include "sim/sim_object.hh"
55 // For stat reset hack
56 #include "sim/stat_control.hh"
60 extern SimObject
*resolveSimObject(const string
&);
63 // The base implementations use to_number for parsing and '<<' for
64 // displaying, suitable for integer types.
68 parseParam(const string
&s
, T
&value
)
70 return to_number(s
, value
);
75 showParam(ostream
&os
, const T
&value
)
81 // Template specializations:
82 // - char (8-bit integer)
83 // - floating-point types
88 // Treat 8-bit ints (chars) as ints on output, not as chars
91 showParam(ostream
&os
, const char &value
)
99 showParam(ostream
&os
, const unsigned char &value
)
101 os
<< (unsigned int)value
;
105 // Use sscanf() for FP types as to_number() only handles integers
108 parseParam(const string
&s
, float &value
)
110 return (sscanf(s
.c_str(), "%f", &value
) == 1);
115 parseParam(const string
&s
, double &value
)
117 return (sscanf(s
.c_str(), "%lf", &value
) == 1);
122 parseParam(const string
&s
, bool &value
)
124 const string
&ls
= to_lower(s
);
139 // Display bools as strings
142 showParam(ostream
&os
, const bool &value
)
144 os
<< (value
? "true" : "false");
148 // String requires no processing to speak of
151 parseParam(const string
&s
, string
&value
)
157 int Serializable::ckptMaxCount
= 0;
158 int Serializable::ckptCount
= 0;
159 int Serializable::ckptPrevCount
= -1;
162 Serializable::nameOut(ostream
&os
)
164 os
<< "\n[" << name() << "]\n";
168 Serializable::nameOut(ostream
&os
, const string
&_name
)
170 os
<< "\n[" << _name
<< "]\n";
175 paramOut(ostream
&os
, const std::string
&name
, const T
¶m
)
178 showParam(os
, param
);
184 arrayParamOut(ostream
&os
, const std::string
&name
,
185 const std::vector
<T
> ¶m
)
187 int size
= param
.size();
190 showParam(os
, param
[0]);
191 for (int i
= 1; i
< size
; ++i
) {
193 showParam(os
, param
[i
]);
201 paramIn(Checkpoint
*cp
, const std::string
§ion
,
202 const std::string
&name
, T
¶m
)
205 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
206 fatal("Can't unserialize '%s:%s'\n", section
, name
);
213 arrayParamOut(ostream
&os
, const std::string
&name
,
214 const T
*param
, int size
)
218 showParam(os
, param
[0]);
219 for (int i
= 1; i
< size
; ++i
) {
221 showParam(os
, param
[i
]);
229 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
230 const std::string
&name
, T
*param
, int size
)
233 if (!cp
->find(section
, name
, str
)) {
234 fatal("Can't unserialize '%s:%s'\n", section
, name
);
237 // code below stolen from VectorParam<T>::parse().
238 // it would be nice to unify these somehow...
240 vector
<string
> tokens
;
242 tokenize(tokens
, str
, ' ');
244 // Need this if we were doing a vector
245 // value.resize(tokens.size());
247 if (tokens
.size() != size
) {
248 fatal("Array size mismatch on %s:%s'\n", section
, name
);
251 for (int i
= 0; i
< tokens
.size(); i
++) {
252 // need to parse into local variable to handle vector<bool>,
253 // for which operator[] returns a special reference class
254 // that's not the same as 'bool&', (since it's a packed
257 if (!parseParam(tokens
[i
], scalar_value
)) {
258 string
err("could not parse \"");
266 // assign parsed value to vector
267 param
[i
] = scalar_value
;
273 arrayParamIn(Checkpoint
*cp
, const std::string
§ion
,
274 const std::string
&name
, std::vector
<T
> ¶m
)
277 if (!cp
->find(section
, name
, str
)) {
278 fatal("Can't unserialize '%s:%s'\n", section
, name
);
281 // code below stolen from VectorParam<T>::parse().
282 // it would be nice to unify these somehow...
284 vector
<string
> tokens
;
286 tokenize(tokens
, str
, ' ');
288 // Need this if we were doing a vector
289 // value.resize(tokens.size());
291 param
.resize(tokens
.size());
293 for (int i
= 0; i
< tokens
.size(); i
++) {
294 // need to parse into local variable to handle vector<bool>,
295 // for which operator[] returns a special reference class
296 // that's not the same as 'bool&', (since it's a packed
299 if (!parseParam(tokens
[i
], scalar_value
)) {
300 string
err("could not parse \"");
308 // assign parsed value to vector
309 param
[i
] = scalar_value
;
316 objParamIn(Checkpoint
*cp
, const std::string
§ion
,
317 const std::string
&name
, SimObject
* ¶m
)
319 if (!cp
->findObj(section
, name
, param
)) {
320 fatal("Can't unserialize '%s:%s'\n", section
, name
);
325 #define INSTANTIATE_PARAM_TEMPLATES(type) \
327 paramOut(ostream &os, const std::string &name, type const ¶m); \
329 paramIn(Checkpoint *cp, const std::string §ion, \
330 const std::string &name, type & param); \
332 arrayParamOut(ostream &os, const std::string &name, \
333 type const *param, int size); \
335 arrayParamIn(Checkpoint *cp, const std::string §ion, \
336 const std::string &name, type *param, int size); \
338 arrayParamOut(ostream &os, const std::string &name, \
339 const std::vector<type> ¶m); \
341 arrayParamIn(Checkpoint *cp, const std::string §ion, \
342 const std::string &name, std::vector<type> ¶m);
344 INSTANTIATE_PARAM_TEMPLATES(signed char)
345 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
346 INSTANTIATE_PARAM_TEMPLATES(signed short)
347 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
348 INSTANTIATE_PARAM_TEMPLATES(signed int)
349 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
350 INSTANTIATE_PARAM_TEMPLATES(signed long)
351 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
352 INSTANTIATE_PARAM_TEMPLATES(signed long long)
353 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
354 INSTANTIATE_PARAM_TEMPLATES(bool)
355 INSTANTIATE_PARAM_TEMPLATES(string
)
358 /////////////////////////////
360 /// Container for serializing global variables (not associated with
361 /// any serialized object).
362 class Globals
: public Serializable
365 const string
name() const;
366 void serialize(ostream
&os
);
367 void unserialize(Checkpoint
*cp
);
370 /// The one and only instance of the Globals class.
374 Globals::name() const
380 Globals::serialize(ostream
&os
)
383 SERIALIZE_SCALAR(curTick
);
385 nameOut(os
, "MainEventQueue");
386 mainEventQueue
.serialize(os
);
390 Globals::unserialize(Checkpoint
*cp
)
392 const string
§ion
= name();
393 UNSERIALIZE_SCALAR(curTick
);
395 mainEventQueue
.unserialize(cp
, "MainEventQueue");
399 Serializable::serializeAll(const std::string
&cpt_dir
)
401 setCheckpointDir(cpt_dir
);
402 string dir
= Checkpoint::dir();
403 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
404 fatal("couldn't mkdir %s\n", dir
);
406 string cpt_file
= dir
+ Checkpoint::baseFilename
;
407 ofstream
outstream(cpt_file
.c_str());
408 time_t t
= time(NULL
);
409 outstream
<< "// checkpoint generated: " << ctime(&t
);
411 globals
.serialize(outstream
);
412 Annotate::annotations
.serialize(outstream
);
413 SimObject::serializeAll(outstream
);
417 Serializable::unserializeAll(const std::string
&cpt_dir
)
419 setCheckpointDir(cpt_dir
);
420 string dir
= Checkpoint::dir();
421 string cpt_file
= dir
+ Checkpoint::baseFilename
;
424 DPRINTFR(Config
, "Loading checkpoint dir '%s'\n",
426 Checkpoint
*cp
= new Checkpoint(dir
, section
);
427 unserializeGlobals(cp
);
428 Annotate::annotations
.unserialize(cp
);
429 SimObject::unserializeAll(cp
);
433 Serializable::unserializeGlobals(Checkpoint
*cp
)
435 globals
.unserialize(cp
);
438 const char *Checkpoint::baseFilename
= "m5.cpt";
440 static string checkpointDirBase
;
443 setCheckpointDir(const std::string
&name
)
445 checkpointDirBase
= name
;
446 if (checkpointDirBase
[checkpointDirBase
.size() - 1] != '/')
447 checkpointDirBase
+= "/";
453 // use csprintf to insert curTick into directory name if it
454 // appears to have a format placeholder in it.
455 return (checkpointDirBase
.find("%") != string::npos
) ?
456 csprintf(checkpointDirBase
, curTick
) : checkpointDirBase
;
460 debug_serialize(const std::string
&cpt_dir
)
462 Serializable::serializeAll(cpt_dir
);
466 ////////////////////////////////////////////////////////////////////////
468 // SerializableClass member definitions
470 ////////////////////////////////////////////////////////////////////////
472 // Map of class names to SerializableBuilder creation functions.
473 // Need to make this a pointer so we can force initialization on the
474 // first reference; otherwise, some SerializableClass constructors
475 // may be invoked before the classMap constructor.
476 map
<string
,SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
478 // SerializableClass constructor: add mapping to classMap
479 SerializableClass::SerializableClass(const string
&className
,
480 CreateFunc createFunc
)
482 if (classMap
== NULL
)
483 classMap
= new map
<string
,SerializableClass::CreateFunc
>();
485 if ((*classMap
)[className
])
487 cerr
<< "Error: simulation object class " << className
<< " redefined"
492 // add className --> createFunc to class map
493 (*classMap
)[className
] = createFunc
;
500 SerializableClass::createObject(Checkpoint
*cp
,
501 const std::string
§ion
)
505 if (!cp
->find(section
, "type", className
)) {
506 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
510 CreateFunc createFunc
= (*classMap
)[className
];
512 if (createFunc
== NULL
) {
513 fatal("Serializable::create: no create function for class '%s'.\n",
517 Serializable
*object
= createFunc(cp
, section
);
519 assert(object
!= NULL
);
526 Serializable::create(Checkpoint
*cp
, const std::string
§ion
)
528 Serializable
*object
= SerializableClass::createObject(cp
, section
);
529 object
->unserialize(cp
, section
);
534 Checkpoint::Checkpoint(const std::string
&cpt_dir
, const std::string
&path
)
535 : db(new IniFile
), basePath(path
), cptDir(cpt_dir
)
537 string filename
= cpt_dir
+ "/" + Checkpoint::baseFilename
;
538 if (!db
->load(filename
)) {
539 fatal("Can't load checkpoint file '%s'\n", filename
);
545 Checkpoint::find(const std::string
§ion
, const std::string
&entry
,
548 return db
->find(section
, entry
, value
);
553 Checkpoint::findObj(const std::string
§ion
, const std::string
&entry
,
558 if (!db
->find(section
, entry
, path
))
561 value
= resolveSimObject(path
);
567 Checkpoint::sectionExists(const std::string
§ion
)
569 return db
->sectionExists(section
);