2 * Copyright (c) 2015 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2013 Advanced Micro Devices, Inc.
16 * Copyright (c) 2013 Mark D. Hill and David A. Wood
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met: redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer;
23 * redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution;
26 * neither the name of the copyright holders nor the names of its
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * Authors: Nathan Binkert
50 #include <sys/types.h>
58 #include "base/framebuffer.hh"
59 #include "base/inifile.hh"
60 #include "base/misc.hh"
61 #include "base/output.hh"
62 #include "base/str.hh"
63 #include "base/trace.hh"
64 #include "debug/Checkpoint.hh"
65 #include "sim/eventq.hh"
66 #include "sim/serialize.hh"
67 #include "sim/sim_events.hh"
68 #include "sim/sim_exit.hh"
69 #include "sim/sim_object.hh"
71 // For stat reset hack
72 #include "sim/stat_control.hh"
77 // The base implementations use to_number for parsing and '<<' for
78 // displaying, suitable for integer types.
82 parseParam(const string
&s
, T
&value
)
84 return to_number(s
, value
);
89 showParam(CheckpointOut
&os
, const T
&value
)
95 // Template specializations:
96 // - char (8-bit integer)
97 // - floating-point types
102 // Treat 8-bit ints (chars) as ints on output, not as chars
105 showParam(CheckpointOut
&os
, const char &value
)
113 showParam(CheckpointOut
&os
, const signed char &value
)
121 showParam(CheckpointOut
&os
, const unsigned char &value
)
123 os
<< (unsigned int)value
;
129 parseParam(const string
&s
, float &value
)
131 return to_number(s
, value
);
136 parseParam(const string
&s
, double &value
)
138 return to_number(s
, value
);
143 parseParam(const string
&s
, bool &value
)
145 return to_bool(s
, value
);
148 // Display bools as strings
151 showParam(CheckpointOut
&os
, const bool &value
)
153 os
<< (value
? "true" : "false");
157 // String requires no processing to speak of
160 parseParam(const string
&s
, string
&value
)
166 int Serializable::ckptMaxCount
= 0;
167 int Serializable::ckptCount
= 0;
168 int Serializable::ckptPrevCount
= -1;
169 std::stack
<std::string
> Serializable::path
;
173 paramOut(CheckpointOut
&os
, const string
&name
, const T
¶m
)
176 showParam(os
, param
);
182 arrayParamOut(CheckpointOut
&os
, const string
&name
, const vector
<T
> ¶m
)
184 typename vector
<T
>::size_type size
= param
.size();
187 showParam(os
, param
[0]);
188 for (typename vector
<T
>::size_type i
= 1; i
< size
; ++i
) {
190 showParam(os
, param
[i
]);
197 arrayParamOut(CheckpointOut
&os
, const string
&name
, const list
<T
> ¶m
)
199 typename list
<T
>::const_iterator it
= param
.begin();
202 if (param
.size() > 0)
205 while (it
!= param
.end()) {
215 arrayParamOut(CheckpointOut
&os
, const string
&name
, const set
<T
> ¶m
)
217 typename set
<T
>::const_iterator it
= param
.begin();
220 if (param
.size() > 0)
223 while (it
!= param
.end()) {
233 paramIn(CheckpointIn
&cp
, const string
&name
, T
¶m
)
235 const string
§ion(Serializable::currentSection());
237 if (!cp
.find(section
, name
, str
) || !parseParam(str
, param
)) {
238 fatal("Can't unserialize '%s:%s'\n", section
, name
);
244 optParamIn(CheckpointIn
&cp
, const string
&name
, T
¶m
, bool warn
)
246 const string
§ion(Serializable::currentSection());
248 if (!cp
.find(section
, name
, str
) || !parseParam(str
, param
)) {
250 warn("optional parameter %s:%s not present\n", section
, name
);
259 arrayParamOut(CheckpointOut
&os
, const string
&name
,
260 const T
*param
, unsigned size
)
264 showParam(os
, param
[0]);
265 for (unsigned i
= 1; i
< size
; ++i
) {
267 showParam(os
, param
[i
]);
275 arrayParamIn(CheckpointIn
&cp
, const string
&name
, T
*param
, unsigned size
)
277 const string
§ion(Serializable::currentSection());
279 if (!cp
.find(section
, name
, str
)) {
280 fatal("Can't unserialize '%s:%s'\n", section
, name
);
283 // code below stolen from VectorParam<T>::parse().
284 // it would be nice to unify these somehow...
286 vector
<string
> tokens
;
288 tokenize(tokens
, str
, ' ');
290 // Need this if we were doing a vector
291 // value.resize(tokens.size());
293 if (tokens
.size() != size
) {
294 fatal("Array size mismatch on %s:%s'\n", section
, name
);
297 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
298 // need to parse into local variable to handle vector<bool>,
299 // for which operator[] returns a special reference class
300 // that's not the same as 'bool&', (since it's a packed
303 if (!parseParam(tokens
[i
], scalar_value
)) {
304 string
err("could not parse \"");
312 // assign parsed value to vector
313 param
[i
] = scalar_value
;
319 arrayParamIn(CheckpointIn
&cp
, const string
&name
, vector
<T
> ¶m
)
321 const string
§ion(Serializable::currentSection());
323 if (!cp
.find(section
, name
, str
)) {
324 fatal("Can't unserialize '%s:%s'\n", section
, name
);
327 // code below stolen from VectorParam<T>::parse().
328 // it would be nice to unify these somehow...
330 vector
<string
> tokens
;
332 tokenize(tokens
, str
, ' ');
334 // Need this if we were doing a vector
335 // value.resize(tokens.size());
337 param
.resize(tokens
.size());
339 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
340 // need to parse into local variable to handle vector<bool>,
341 // for which operator[] returns a special reference class
342 // that's not the same as 'bool&', (since it's a packed
345 if (!parseParam(tokens
[i
], scalar_value
)) {
346 string
err("could not parse \"");
354 // assign parsed value to vector
355 param
[i
] = scalar_value
;
361 arrayParamIn(CheckpointIn
&cp
, const string
&name
, list
<T
> ¶m
)
363 const string
§ion(Serializable::currentSection());
365 if (!cp
.find(section
, name
, str
)) {
366 fatal("Can't unserialize '%s:%s'\n", section
, name
);
370 vector
<string
> tokens
;
371 tokenize(tokens
, str
, ' ');
373 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
375 if (!parseParam(tokens
[i
], scalar_value
)) {
376 string
err("could not parse \"");
384 // assign parsed value to vector
385 param
.push_back(scalar_value
);
391 arrayParamIn(CheckpointIn
&cp
, const string
&name
, set
<T
> ¶m
)
393 const string
§ion(Serializable::currentSection());
395 if (!cp
.find(section
, name
, str
)) {
396 fatal("Can't unserialize '%s:%s'\n", section
, name
);
400 vector
<string
> tokens
;
401 tokenize(tokens
, str
, ' ');
403 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
405 if (!parseParam(tokens
[i
], scalar_value
)) {
406 string
err("could not parse \"");
414 // assign parsed value to vector
415 param
.insert(scalar_value
);
421 objParamIn(CheckpointIn
&cp
, const string
&name
, SimObject
* ¶m
)
423 const string
§ion(Serializable::currentSection());
424 if (!cp
.findObj(section
, name
, param
)) {
425 fatal("Can't unserialize '%s:%s'\n", section
, name
);
430 #define INSTANTIATE_PARAM_TEMPLATES(type) \
432 paramOut(CheckpointOut &os, const string &name, type const ¶m); \
434 paramIn(CheckpointIn &cp, const string &name, type & param); \
436 optParamIn(CheckpointIn &cp, const string &name, type & param, \
439 arrayParamOut(CheckpointOut &os, const string &name, \
440 type const *param, unsigned size); \
442 arrayParamIn(CheckpointIn &cp, const string &name, \
443 type *param, unsigned size); \
445 arrayParamOut(CheckpointOut &os, const string &name, \
446 const vector<type> ¶m); \
448 arrayParamIn(CheckpointIn &cp, const string &name, \
449 vector<type> ¶m); \
451 arrayParamOut(CheckpointOut &os, const string &name, \
452 const list<type> ¶m); \
454 arrayParamIn(CheckpointIn &cp, const string &name, \
457 INSTANTIATE_PARAM_TEMPLATES(char)
458 INSTANTIATE_PARAM_TEMPLATES(signed char)
459 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
460 INSTANTIATE_PARAM_TEMPLATES(signed short)
461 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
462 INSTANTIATE_PARAM_TEMPLATES(signed int)
463 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
464 INSTANTIATE_PARAM_TEMPLATES(signed long)
465 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
466 INSTANTIATE_PARAM_TEMPLATES(signed long long)
467 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
468 INSTANTIATE_PARAM_TEMPLATES(bool)
469 INSTANTIATE_PARAM_TEMPLATES(float)
470 INSTANTIATE_PARAM_TEMPLATES(double)
471 INSTANTIATE_PARAM_TEMPLATES(string
)
472 INSTANTIATE_PARAM_TEMPLATES(Pixel
)
474 // set is only used with strings and furthermore doesn't agree with Pixel
476 arrayParamOut(CheckpointOut
&, const string
&, const set
<string
> &);
478 arrayParamIn(CheckpointIn
&, const string
&, set
<string
> &);
480 /////////////////////////////
482 /// Container for serializing global variables (not associated with
483 /// any serialized object).
484 class Globals
: public Serializable
488 : unserializedCurTick(0) {}
490 void serialize(CheckpointOut
&cp
) const override
;
491 void unserialize(CheckpointIn
&cp
) override
;
493 Tick unserializedCurTick
;
496 /// The one and only instance of the Globals class.
499 /// The version tags for this build of the simulator, to be stored in the
500 /// Globals section during serialization and compared upon unserialization.
501 extern std::set
<std::string
> version_tags
;
504 Globals::serialize(CheckpointOut
&cp
) const
506 paramOut(cp
, "curTick", curTick());
507 SERIALIZE_CONTAINER(version_tags
);
511 Globals::unserialize(CheckpointIn
&cp
)
513 paramIn(cp
, "curTick", unserializedCurTick
);
515 const std::string
§ion(Serializable::currentSection());
517 if (!cp
.find(section
, "version_tags", str
)) {
518 warn("**********************************************************\n");
519 warn("!!!! Checkpoint uses an old versioning scheme. !!!!\n");
520 warn("Run the checkpoint upgrader (util/cpt_upgrader.py) on your "
522 warn("**********************************************************\n");
526 std::set
<std::string
> cpt_tags
;
527 arrayParamIn(cp
, "version_tags", cpt_tags
); // UNSERIALIZE_CONTAINER
530 for (const auto& t
: version_tags
) {
531 if (cpt_tags
.find(t
) == cpt_tags
.end()) {
532 // checkpoint is missing tag that this binary has
534 warn("*****************************************************\n");
535 warn("!!!! Checkpoint is missing the following version tags:\n");
542 warn("You might experience some issues when restoring and should run "
543 "the checkpoint upgrader (util/cpt_upgrader.py) on your "
545 warn("**********************************************************\n");
549 for (const auto& t
: cpt_tags
) {
550 if (version_tags
.find(t
) == version_tags
.end()) {
551 // gem5 binary is missing tag that this checkpoint has
553 warn("*****************************************************\n");
554 warn("!!!! gem5 is missing the following version tags:\n");
561 warn("Running a checkpoint with incompatible version tags is not "
562 "supported. While it might work, you may experience incorrect "
563 "behavior or crashes.\n");
564 warn("**********************************************************\n");
568 Serializable::Serializable()
572 Serializable::~Serializable()
577 Serializable::serializeSection(CheckpointOut
&cp
, const char *name
) const
579 Serializable::ScopedCheckpointSection
sec(cp
, name
);
584 Serializable::unserializeSection(CheckpointIn
&cp
, const char *name
)
586 Serializable::ScopedCheckpointSection
sec(cp
, name
);
591 Serializable::serializeAll(const string
&cpt_dir
)
593 string dir
= CheckpointIn::setDir(cpt_dir
);
594 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
595 fatal("couldn't mkdir %s\n", dir
);
597 string cpt_file
= dir
+ CheckpointIn::baseFilename
;
598 ofstream
outstream(cpt_file
.c_str());
599 time_t t
= time(NULL
);
600 if (!outstream
.is_open())
601 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
602 outstream
<< "## checkpoint generated: " << ctime(&t
);
604 globals
.serializeSection(outstream
, "Globals");
606 SimObject::serializeAll(outstream
);
610 Serializable::unserializeGlobals(CheckpointIn
&cp
)
612 globals
.unserializeSection(cp
, "Globals");
614 for (uint32_t i
= 0; i
< numMainEventQueues
; ++i
)
615 mainEventQueue
[i
]->setCurTick(globals
.unserializedCurTick
);
618 Serializable::ScopedCheckpointSection::~ScopedCheckpointSection()
620 assert(!path
.empty());
621 DPRINTF(Checkpoint
, "Popping: %s\n", path
.top());
626 Serializable::ScopedCheckpointSection::pushName(const char *obj_name
)
631 path
.push(csprintf("%s.%s", path
.top(), obj_name
));
633 DPRINTF(Checkpoint
, "ScopedCheckpointSection::pushName: %s\n", obj_name
);
637 Serializable::ScopedCheckpointSection::nameOut(CheckpointOut
&cp
)
639 DPRINTF(Checkpoint
, "ScopedCheckpointSection::nameOut: %s\n",
640 Serializable::currentSection());
641 cp
<< "\n[" << Serializable::currentSection() << "]\n";
645 debug_serialize(const string
&cpt_dir
)
647 Serializable::serializeAll(cpt_dir
);
651 Serializable::currentSection()
653 assert(!path
.empty());
658 const char *CheckpointIn::baseFilename
= "m5.cpt";
660 string
CheckpointIn::currentDirectory
;
663 CheckpointIn::setDir(const string
&name
)
665 // use csprintf to insert curTick() into directory name if it
666 // appears to have a format placeholder in it.
667 currentDirectory
= (name
.find("%") != string::npos
) ?
668 csprintf(name
, curTick()) : name
;
669 if (currentDirectory
[currentDirectory
.size() - 1] != '/')
670 currentDirectory
+= "/";
671 return currentDirectory
;
677 return currentDirectory
;
681 CheckpointIn::CheckpointIn(const string
&cpt_dir
, SimObjectResolver
&resolver
)
682 : db(new IniFile
), objNameResolver(resolver
), cptDir(setDir(cpt_dir
))
684 string filename
= cptDir
+ "/" + CheckpointIn::baseFilename
;
685 if (!db
->load(filename
)) {
686 fatal("Can't load checkpoint file '%s'\n", filename
);
690 CheckpointIn::~CheckpointIn()
696 CheckpointIn::find(const string
§ion
, const string
&entry
, string
&value
)
698 return db
->find(section
, entry
, value
);
703 CheckpointIn::findObj(const string
§ion
, const string
&entry
,
708 if (!db
->find(section
, entry
, path
))
711 value
= objNameResolver
.resolveSimObject(path
);
717 CheckpointIn::sectionExists(const string
§ion
)
719 return db
->sectionExists(section
);