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
48 #include "sim/serialize.hh"
52 #include <sys/types.h>
60 #include "arch/generic/vec_reg.hh"
61 #include "base/framebuffer.hh"
62 #include "base/inifile.hh"
63 #include "base/misc.hh"
64 #include "base/output.hh"
65 #include "base/str.hh"
66 #include "base/trace.hh"
67 #include "debug/Checkpoint.hh"
68 #include "sim/eventq.hh"
69 #include "sim/sim_events.hh"
70 #include "sim/sim_exit.hh"
71 #include "sim/sim_object.hh"
73 // For stat reset hack
74 #include "sim/stat_control.hh"
79 // The base implementations use to_number for parsing and '<<' for
80 // displaying, suitable for integer types.
84 parseParam(const string
&s
, T
&value
)
86 return to_number(s
, value
);
91 showParam(CheckpointOut
&os
, const T
&value
)
97 // Template specializations:
98 // - char (8-bit integer)
99 // - floating-point types
104 // Treat 8-bit ints (chars) as ints on output, not as chars
107 showParam(CheckpointOut
&os
, const char &value
)
115 showParam(CheckpointOut
&os
, const signed char &value
)
123 showParam(CheckpointOut
&os
, const unsigned char &value
)
125 os
<< (unsigned int)value
;
131 parseParam(const string
&s
, float &value
)
133 return to_number(s
, value
);
138 parseParam(const string
&s
, double &value
)
140 return to_number(s
, value
);
145 parseParam(const string
&s
, bool &value
)
147 return to_bool(s
, value
);
150 // Display bools as strings
153 showParam(CheckpointOut
&os
, const bool &value
)
155 os
<< (value
? "true" : "false");
159 // String requires no processing to speak of
162 parseParam(const string
&s
, string
&value
)
168 int Serializable::ckptMaxCount
= 0;
169 int Serializable::ckptCount
= 0;
170 int Serializable::ckptPrevCount
= -1;
171 std::stack
<std::string
> Serializable::path
;
175 paramOut(CheckpointOut
&os
, const string
&name
, const T
¶m
)
178 showParam(os
, param
);
184 arrayParamOut(CheckpointOut
&os
, const string
&name
, const vector
<T
> ¶m
)
186 typename vector
<T
>::size_type size
= param
.size();
189 showParam(os
, param
[0]);
190 for (typename vector
<T
>::size_type i
= 1; i
< size
; ++i
) {
192 showParam(os
, param
[i
]);
199 arrayParamOut(CheckpointOut
&os
, const string
&name
, const list
<T
> ¶m
)
201 typename list
<T
>::const_iterator it
= param
.begin();
204 if (param
.size() > 0)
207 while (it
!= param
.end()) {
217 arrayParamOut(CheckpointOut
&os
, const string
&name
, const set
<T
> ¶m
)
219 typename set
<T
>::const_iterator it
= param
.begin();
222 if (param
.size() > 0)
225 while (it
!= param
.end()) {
235 paramIn(CheckpointIn
&cp
, const string
&name
, T
¶m
)
237 const string
§ion(Serializable::currentSection());
239 if (!cp
.find(section
, name
, str
) || !parseParam(str
, param
)) {
240 fatal("Can't unserialize '%s:%s'\n", section
, name
);
246 optParamIn(CheckpointIn
&cp
, const string
&name
, T
¶m
, bool warn
)
248 const string
§ion(Serializable::currentSection());
250 if (!cp
.find(section
, name
, str
) || !parseParam(str
, param
)) {
252 warn("optional parameter %s:%s not present\n", section
, name
);
261 arrayParamOut(CheckpointOut
&os
, const string
&name
,
262 const T
*param
, unsigned size
)
266 showParam(os
, param
[0]);
267 for (unsigned i
= 1; i
< size
; ++i
) {
269 showParam(os
, param
[i
]);
277 arrayParamIn(CheckpointIn
&cp
, const string
&name
, T
*param
, unsigned size
)
279 const string
§ion(Serializable::currentSection());
281 if (!cp
.find(section
, name
, str
)) {
282 fatal("Can't unserialize '%s:%s'\n", section
, name
);
285 // code below stolen from VectorParam<T>::parse().
286 // it would be nice to unify these somehow...
288 vector
<string
> tokens
;
290 tokenize(tokens
, str
, ' ');
292 // Need this if we were doing a vector
293 // value.resize(tokens.size());
295 if (tokens
.size() != size
) {
296 fatal("Array size mismatch on %s:%s'\n", section
, name
);
299 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
300 // need to parse into local variable to handle vector<bool>,
301 // for which operator[] returns a special reference class
302 // that's not the same as 'bool&', (since it's a packed
305 if (!parseParam(tokens
[i
], scalar_value
)) {
306 string
err("could not parse \"");
314 // assign parsed value to vector
315 param
[i
] = scalar_value
;
321 arrayParamIn(CheckpointIn
&cp
, const string
&name
, vector
<T
> ¶m
)
323 const string
§ion(Serializable::currentSection());
325 if (!cp
.find(section
, name
, str
)) {
326 fatal("Can't unserialize '%s:%s'\n", section
, name
);
329 // code below stolen from VectorParam<T>::parse().
330 // it would be nice to unify these somehow...
332 vector
<string
> tokens
;
334 tokenize(tokens
, str
, ' ');
336 // Need this if we were doing a vector
337 // value.resize(tokens.size());
339 param
.resize(tokens
.size());
341 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
342 // need to parse into local variable to handle vector<bool>,
343 // for which operator[] returns a special reference class
344 // that's not the same as 'bool&', (since it's a packed
347 if (!parseParam(tokens
[i
], scalar_value
)) {
348 string
err("could not parse \"");
356 // assign parsed value to vector
357 param
[i
] = scalar_value
;
363 arrayParamIn(CheckpointIn
&cp
, const string
&name
, list
<T
> ¶m
)
365 const string
§ion(Serializable::currentSection());
367 if (!cp
.find(section
, name
, str
)) {
368 fatal("Can't unserialize '%s:%s'\n", section
, name
);
372 vector
<string
> tokens
;
373 tokenize(tokens
, str
, ' ');
375 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
377 if (!parseParam(tokens
[i
], scalar_value
)) {
378 string
err("could not parse \"");
386 // assign parsed value to vector
387 param
.push_back(scalar_value
);
393 arrayParamIn(CheckpointIn
&cp
, const string
&name
, set
<T
> ¶m
)
395 const string
§ion(Serializable::currentSection());
397 if (!cp
.find(section
, name
, str
)) {
398 fatal("Can't unserialize '%s:%s'\n", section
, name
);
402 vector
<string
> tokens
;
403 tokenize(tokens
, str
, ' ');
405 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
407 if (!parseParam(tokens
[i
], scalar_value
)) {
408 string
err("could not parse \"");
416 // assign parsed value to vector
417 param
.insert(scalar_value
);
423 objParamIn(CheckpointIn
&cp
, const string
&name
, SimObject
* ¶m
)
425 const string
§ion(Serializable::currentSection());
426 if (!cp
.findObj(section
, name
, param
)) {
427 fatal("Can't unserialize '%s:%s'\n", section
, name
);
432 #define INSTANTIATE_PARAM_TEMPLATES(type) \
434 paramOut(CheckpointOut &os, const string &name, type const ¶m); \
436 paramIn(CheckpointIn &cp, const string &name, type & param); \
438 optParamIn(CheckpointIn &cp, const string &name, type & param, \
441 arrayParamOut(CheckpointOut &os, const string &name, \
442 type const *param, unsigned size); \
444 arrayParamIn(CheckpointIn &cp, const string &name, \
445 type *param, unsigned size); \
447 arrayParamOut(CheckpointOut &os, const string &name, \
448 const vector<type> ¶m); \
450 arrayParamIn(CheckpointIn &cp, const string &name, \
451 vector<type> ¶m); \
453 arrayParamOut(CheckpointOut &os, const string &name, \
454 const list<type> ¶m); \
456 arrayParamIn(CheckpointIn &cp, const string &name, \
459 INSTANTIATE_PARAM_TEMPLATES(char)
460 INSTANTIATE_PARAM_TEMPLATES(signed char)
461 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
462 INSTANTIATE_PARAM_TEMPLATES(signed short)
463 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
464 INSTANTIATE_PARAM_TEMPLATES(signed int)
465 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
466 INSTANTIATE_PARAM_TEMPLATES(signed long)
467 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
468 INSTANTIATE_PARAM_TEMPLATES(signed long long)
469 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
470 INSTANTIATE_PARAM_TEMPLATES(bool)
471 INSTANTIATE_PARAM_TEMPLATES(float)
472 INSTANTIATE_PARAM_TEMPLATES(double)
473 INSTANTIATE_PARAM_TEMPLATES(string
)
474 INSTANTIATE_PARAM_TEMPLATES(Pixel
)
475 INSTANTIATE_PARAM_TEMPLATES(VecRegContainer
<8>)
476 INSTANTIATE_PARAM_TEMPLATES(VecRegContainer
<16>)
478 // set is only used with strings and furthermore doesn't agree with Pixel
480 arrayParamOut(CheckpointOut
&, const string
&, const set
<string
> &);
482 arrayParamIn(CheckpointIn
&, const string
&, set
<string
> &);
484 /////////////////////////////
486 /// Container for serializing global variables (not associated with
487 /// any serialized object).
488 class Globals
: public Serializable
492 : unserializedCurTick(0) {}
494 void serialize(CheckpointOut
&cp
) const override
;
495 void unserialize(CheckpointIn
&cp
) override
;
497 Tick unserializedCurTick
;
500 /// The one and only instance of the Globals class.
503 /// The version tags for this build of the simulator, to be stored in the
504 /// Globals section during serialization and compared upon unserialization.
505 extern std::set
<std::string
> version_tags
;
508 Globals::serialize(CheckpointOut
&cp
) const
510 paramOut(cp
, "curTick", curTick());
511 SERIALIZE_CONTAINER(version_tags
);
515 Globals::unserialize(CheckpointIn
&cp
)
517 paramIn(cp
, "curTick", unserializedCurTick
);
519 const std::string
§ion(Serializable::currentSection());
521 if (!cp
.find(section
, "version_tags", str
)) {
522 warn("**********************************************************\n");
523 warn("!!!! Checkpoint uses an old versioning scheme. !!!!\n");
524 warn("Run the checkpoint upgrader (util/cpt_upgrader.py) on your "
526 warn("**********************************************************\n");
530 std::set
<std::string
> cpt_tags
;
531 arrayParamIn(cp
, "version_tags", cpt_tags
); // UNSERIALIZE_CONTAINER
534 for (const auto& t
: version_tags
) {
535 if (cpt_tags
.find(t
) == cpt_tags
.end()) {
536 // checkpoint is missing tag that this binary has
538 warn("*****************************************************\n");
539 warn("!!!! Checkpoint is missing the following version tags:\n");
546 warn("You might experience some issues when restoring and should run "
547 "the checkpoint upgrader (util/cpt_upgrader.py) on your "
549 warn("**********************************************************\n");
553 for (const auto& t
: cpt_tags
) {
554 if (version_tags
.find(t
) == version_tags
.end()) {
555 // gem5 binary is missing tag that this checkpoint has
557 warn("*****************************************************\n");
558 warn("!!!! gem5 is missing the following version tags:\n");
565 warn("Running a checkpoint with incompatible version tags is not "
566 "supported. While it might work, you may experience incorrect "
567 "behavior or crashes.\n");
568 warn("**********************************************************\n");
572 Serializable::Serializable()
576 Serializable::~Serializable()
581 Serializable::serializeSection(CheckpointOut
&cp
, const char *name
) const
583 Serializable::ScopedCheckpointSection
sec(cp
, name
);
588 Serializable::unserializeSection(CheckpointIn
&cp
, const char *name
)
590 Serializable::ScopedCheckpointSection
sec(cp
, name
);
595 Serializable::serializeAll(const string
&cpt_dir
)
597 string dir
= CheckpointIn::setDir(cpt_dir
);
598 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
599 fatal("couldn't mkdir %s\n", dir
);
601 string cpt_file
= dir
+ CheckpointIn::baseFilename
;
602 ofstream
outstream(cpt_file
.c_str());
603 time_t t
= time(NULL
);
604 if (!outstream
.is_open())
605 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
606 outstream
<< "## checkpoint generated: " << ctime(&t
);
608 globals
.serializeSection(outstream
, "Globals");
610 SimObject::serializeAll(outstream
);
614 Serializable::unserializeGlobals(CheckpointIn
&cp
)
616 globals
.unserializeSection(cp
, "Globals");
618 for (uint32_t i
= 0; i
< numMainEventQueues
; ++i
)
619 mainEventQueue
[i
]->setCurTick(globals
.unserializedCurTick
);
622 Serializable::ScopedCheckpointSection::~ScopedCheckpointSection()
624 assert(!path
.empty());
625 DPRINTF(Checkpoint
, "Popping: %s\n", path
.top());
630 Serializable::ScopedCheckpointSection::pushName(const char *obj_name
)
635 path
.push(csprintf("%s.%s", path
.top(), obj_name
));
637 DPRINTF(Checkpoint
, "ScopedCheckpointSection::pushName: %s\n", obj_name
);
641 Serializable::ScopedCheckpointSection::nameOut(CheckpointOut
&cp
)
643 DPRINTF(Checkpoint
, "ScopedCheckpointSection::nameOut: %s\n",
644 Serializable::currentSection());
645 cp
<< "\n[" << Serializable::currentSection() << "]\n";
649 debug_serialize(const string
&cpt_dir
)
651 Serializable::serializeAll(cpt_dir
);
655 Serializable::currentSection()
657 assert(!path
.empty());
662 const char *CheckpointIn::baseFilename
= "m5.cpt";
664 string
CheckpointIn::currentDirectory
;
667 CheckpointIn::setDir(const string
&name
)
669 // use csprintf to insert curTick() into directory name if it
670 // appears to have a format placeholder in it.
671 currentDirectory
= (name
.find("%") != string::npos
) ?
672 csprintf(name
, curTick()) : name
;
673 if (currentDirectory
[currentDirectory
.size() - 1] != '/')
674 currentDirectory
+= "/";
675 return currentDirectory
;
681 return currentDirectory
;
685 CheckpointIn::CheckpointIn(const string
&cpt_dir
, SimObjectResolver
&resolver
)
686 : db(new IniFile
), objNameResolver(resolver
), cptDir(setDir(cpt_dir
))
688 string filename
= cptDir
+ "/" + CheckpointIn::baseFilename
;
689 if (!db
->load(filename
)) {
690 fatal("Can't load checkpoint file '%s'\n", filename
);
694 CheckpointIn::~CheckpointIn()
700 CheckpointIn::entryExists(const string
§ion
, const string
&entry
)
702 return db
->entryExists(section
, entry
);
706 CheckpointIn::find(const string
§ion
, const string
&entry
, string
&value
)
708 return db
->find(section
, entry
, value
);
713 CheckpointIn::findObj(const string
§ion
, const string
&entry
,
718 if (!db
->find(section
, entry
, path
))
721 value
= objNameResolver
.resolveSimObject(path
);
727 CheckpointIn::sectionExists(const string
§ion
)
729 return db
->sectionExists(section
);