2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * Copyright (c) 2013 Mark D. Hill and David A. Wood
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution;
14 * neither the name of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Authors: Nathan Binkert
37 #include <sys/types.h>
45 #include "base/inifile.hh"
46 #include "base/misc.hh"
47 #include "base/output.hh"
48 #include "base/str.hh"
49 #include "base/trace.hh"
50 #include "sim/eventq.hh"
51 #include "sim/serialize.hh"
52 #include "sim/sim_events.hh"
53 #include "sim/sim_exit.hh"
54 #include "sim/sim_object.hh"
56 // For stat reset hack
57 #include "sim/stat_control.hh"
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
;
114 parseParam(const string
&s
, float &value
)
116 return to_number(s
, value
);
121 parseParam(const string
&s
, double &value
)
123 return to_number(s
, value
);
128 parseParam(const string
&s
, bool &value
)
130 return to_bool(s
, value
);
133 // Display bools as strings
136 showParam(ostream
&os
, const bool &value
)
138 os
<< (value
? "true" : "false");
142 // String requires no processing to speak of
145 parseParam(const string
&s
, string
&value
)
151 int Serializable::ckptMaxCount
= 0;
152 int Serializable::ckptCount
= 0;
153 int Serializable::ckptPrevCount
= -1;
156 Serializable::nameOut(ostream
&os
)
158 os
<< "\n[" << name() << "]\n";
162 Serializable::nameOut(ostream
&os
, const string
&_name
)
164 os
<< "\n[" << _name
<< "]\n";
169 paramOut(ostream
&os
, const string
&name
, const T
¶m
)
172 showParam(os
, param
);
178 arrayParamOut(ostream
&os
, const string
&name
, const vector
<T
> ¶m
)
180 typename vector
<T
>::size_type size
= param
.size();
183 showParam(os
, param
[0]);
184 for (typename vector
<T
>::size_type i
= 1; i
< size
; ++i
) {
186 showParam(os
, param
[i
]);
193 arrayParamOut(ostream
&os
, const string
&name
, const list
<T
> ¶m
)
195 typename list
<T
>::const_iterator it
= param
.begin();
198 if (param
.size() > 0)
201 while (it
!= param
.end()) {
211 paramIn(Checkpoint
*cp
, const string
§ion
, const string
&name
, T
¶m
)
214 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
215 fatal("Can't unserialize '%s:%s'\n", section
, name
);
221 optParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
, T
¶m
)
224 if (!cp
->find(section
, name
, str
) || !parseParam(str
, param
)) {
225 warn("optional parameter %s:%s not present\n", section
, name
);
234 arrayParamOut(ostream
&os
, const string
&name
, const T
*param
, unsigned size
)
238 showParam(os
, param
[0]);
239 for (unsigned i
= 1; i
< size
; ++i
) {
241 showParam(os
, param
[i
]);
249 arrayParamIn(Checkpoint
*cp
, const string
§ion
, const string
&name
,
250 T
*param
, unsigned size
)
253 if (!cp
->find(section
, name
, str
)) {
254 fatal("Can't unserialize '%s:%s'\n", section
, name
);
257 // code below stolen from VectorParam<T>::parse().
258 // it would be nice to unify these somehow...
260 vector
<string
> tokens
;
262 tokenize(tokens
, str
, ' ');
264 // Need this if we were doing a vector
265 // value.resize(tokens.size());
267 if (tokens
.size() != size
) {
268 fatal("Array size mismatch on %s:%s'\n", section
, name
);
271 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
272 // need to parse into local variable to handle vector<bool>,
273 // for which operator[] returns a special reference class
274 // that's not the same as 'bool&', (since it's a packed
277 if (!parseParam(tokens
[i
], scalar_value
)) {
278 string
err("could not parse \"");
286 // assign parsed value to vector
287 param
[i
] = scalar_value
;
293 arrayParamIn(Checkpoint
*cp
, const string
§ion
,
294 const string
&name
, vector
<T
> ¶m
)
297 if (!cp
->find(section
, name
, str
)) {
298 fatal("Can't unserialize '%s:%s'\n", section
, name
);
301 // code below stolen from VectorParam<T>::parse().
302 // it would be nice to unify these somehow...
304 vector
<string
> tokens
;
306 tokenize(tokens
, str
, ' ');
308 // Need this if we were doing a vector
309 // value.resize(tokens.size());
311 param
.resize(tokens
.size());
313 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
314 // need to parse into local variable to handle vector<bool>,
315 // for which operator[] returns a special reference class
316 // that's not the same as 'bool&', (since it's a packed
319 if (!parseParam(tokens
[i
], scalar_value
)) {
320 string
err("could not parse \"");
328 // assign parsed value to vector
329 param
[i
] = scalar_value
;
335 arrayParamIn(Checkpoint
*cp
, const string
§ion
,
336 const string
&name
, list
<T
> ¶m
)
339 if (!cp
->find(section
, name
, str
)) {
340 fatal("Can't unserialize '%s:%s'\n", section
, name
);
344 vector
<string
> tokens
;
345 tokenize(tokens
, str
, ' ');
347 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
349 if (!parseParam(tokens
[i
], scalar_value
)) {
350 string
err("could not parse \"");
358 // assign parsed value to vector
359 param
.push_back(scalar_value
);
365 objParamIn(Checkpoint
*cp
, const string
§ion
,
366 const string
&name
, SimObject
* ¶m
)
368 if (!cp
->findObj(section
, name
, param
)) {
369 fatal("Can't unserialize '%s:%s'\n", section
, name
);
374 #define INSTANTIATE_PARAM_TEMPLATES(type) \
376 paramOut(ostream &os, const string &name, type const ¶m); \
378 paramIn(Checkpoint *cp, const string §ion, \
379 const string &name, type & param); \
381 optParamIn(Checkpoint *cp, const string §ion, \
382 const string &name, type & param); \
384 arrayParamOut(ostream &os, const string &name, \
385 type const *param, unsigned size); \
387 arrayParamIn(Checkpoint *cp, const string §ion, \
388 const string &name, type *param, unsigned size); \
390 arrayParamOut(ostream &os, const string &name, \
391 const vector<type> ¶m); \
393 arrayParamIn(Checkpoint *cp, const string §ion, \
394 const string &name, vector<type> ¶m); \
396 arrayParamOut(ostream &os, const string &name, \
397 const list<type> ¶m); \
399 arrayParamIn(Checkpoint *cp, const string §ion, \
400 const string &name, list<type> ¶m);
402 INSTANTIATE_PARAM_TEMPLATES(char)
403 INSTANTIATE_PARAM_TEMPLATES(signed char)
404 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
405 INSTANTIATE_PARAM_TEMPLATES(signed short)
406 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
407 INSTANTIATE_PARAM_TEMPLATES(signed int)
408 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
409 INSTANTIATE_PARAM_TEMPLATES(signed long)
410 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
411 INSTANTIATE_PARAM_TEMPLATES(signed long long)
412 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
413 INSTANTIATE_PARAM_TEMPLATES(bool)
414 INSTANTIATE_PARAM_TEMPLATES(float)
415 INSTANTIATE_PARAM_TEMPLATES(double)
416 INSTANTIATE_PARAM_TEMPLATES(string
)
419 /////////////////////////////
421 /// Container for serializing global variables (not associated with
422 /// any serialized object).
423 class Globals
: public Serializable
426 const string
name() const;
427 void serialize(ostream
&os
);
428 void unserialize(Checkpoint
*cp
, const std::string
§ion
);
431 /// The one and only instance of the Globals class.
435 Globals::name() const
441 Globals::serialize(ostream
&os
)
444 paramOut(os
, "curTick", curTick());
446 paramOut(os
, "numMainEventQueues", numMainEventQueues
);
448 for (uint32_t i
= 0; i
< numMainEventQueues
; ++i
) {
449 nameOut(os
, "MainEventQueue");
450 mainEventQueue
[i
]->serialize(os
);
455 Globals::unserialize(Checkpoint
*cp
, const std::string
§ion
)
458 paramIn(cp
, section
, "curTick", tick
);
459 paramIn(cp
, section
, "numMainEventQueues", numMainEventQueues
);
461 for (uint32_t i
= 0; i
< numMainEventQueues
; ++i
) {
462 mainEventQueue
[i
]->setCurTick(tick
);
463 mainEventQueue
[i
]->unserialize(cp
, "MainEventQueue");
467 Serializable::Serializable()
471 Serializable::~Serializable()
476 Serializable::serialize(ostream
&os
)
481 Serializable::unserialize(Checkpoint
*cp
, const string
§ion
)
486 Serializable::serializeAll(const string
&cpt_dir
)
488 string dir
= Checkpoint::setDir(cpt_dir
);
489 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
490 fatal("couldn't mkdir %s\n", dir
);
492 string cpt_file
= dir
+ Checkpoint::baseFilename
;
493 ofstream
outstream(cpt_file
.c_str());
494 time_t t
= time(NULL
);
495 if (!outstream
.is_open())
496 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
497 outstream
<< "## checkpoint generated: " << ctime(&t
);
499 globals
.serialize(outstream
);
500 SimObject::serializeAll(outstream
);
504 Serializable::unserializeGlobals(Checkpoint
*cp
)
506 globals
.unserialize(cp
, globals
.name());
510 debug_serialize(const string
&cpt_dir
)
512 Serializable::serializeAll(cpt_dir
);
516 ////////////////////////////////////////////////////////////////////////
518 // SerializableClass member definitions
520 ////////////////////////////////////////////////////////////////////////
522 // Map of class names to SerializableBuilder creation functions.
523 // Need to make this a pointer so we can force initialization on the
524 // first reference; otherwise, some SerializableClass constructors
525 // may be invoked before the classMap constructor.
526 map
<string
, SerializableClass::CreateFunc
> *SerializableClass::classMap
= 0;
528 // SerializableClass constructor: add mapping to classMap
529 SerializableClass::SerializableClass(const string
&className
,
530 CreateFunc createFunc
)
532 if (classMap
== NULL
)
533 classMap
= new map
<string
, SerializableClass::CreateFunc
>();
535 if ((*classMap
)[className
])
536 fatal("Error: simulation object class %s redefined\n", className
);
538 // add className --> createFunc to class map
539 (*classMap
)[className
] = createFunc
;
545 SerializableClass::createObject(Checkpoint
*cp
, const string
§ion
)
549 if (!cp
->find(section
, "type", className
)) {
550 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
554 CreateFunc createFunc
= (*classMap
)[className
];
556 if (createFunc
== NULL
) {
557 fatal("Serializable::create: no create function for class '%s'.\n",
561 Serializable
*object
= createFunc(cp
, section
);
563 assert(object
!= NULL
);
570 Serializable::create(Checkpoint
*cp
, const string
§ion
)
572 Serializable
*object
= SerializableClass::createObject(cp
, section
);
573 object
->unserialize(cp
, section
);
578 const char *Checkpoint::baseFilename
= "m5.cpt";
580 string
Checkpoint::currentDirectory
;
583 Checkpoint::setDir(const string
&name
)
585 // use csprintf to insert curTick() into directory name if it
586 // appears to have a format placeholder in it.
587 currentDirectory
= (name
.find("%") != string::npos
) ?
588 csprintf(name
, curTick()) : name
;
589 if (currentDirectory
[currentDirectory
.size() - 1] != '/')
590 currentDirectory
+= "/";
591 return currentDirectory
;
597 return currentDirectory
;
601 Checkpoint::Checkpoint(const string
&cpt_dir
, SimObjectResolver
&resolver
)
602 : db(new IniFile
), objNameResolver(resolver
), cptDir(setDir(cpt_dir
))
604 string filename
= cptDir
+ "/" + Checkpoint::baseFilename
;
605 if (!db
->load(filename
)) {
606 fatal("Can't load checkpoint file '%s'\n", filename
);
610 Checkpoint::~Checkpoint()
616 Checkpoint::find(const string
§ion
, const string
&entry
, string
&value
)
618 return db
->find(section
, entry
, value
);
623 Checkpoint::findObj(const string
§ion
, const string
&entry
,
628 if (!db
->find(section
, entry
, path
))
631 value
= objNameResolver
.resolveSimObject(path
);
637 Checkpoint::sectionExists(const string
§ion
)
639 return db
->sectionExists(section
);