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 "base/framebuffer.hh"
61 #include "base/inifile.hh"
62 #include "base/misc.hh"
63 #include "base/output.hh"
64 #include "base/str.hh"
65 #include "base/trace.hh"
66 #include "debug/Checkpoint.hh"
67 #include "sim/eventq.hh"
68 #include "sim/sim_events.hh"
69 #include "sim/sim_exit.hh"
70 #include "sim/sim_object.hh"
72 // For stat reset hack
73 #include "sim/stat_control.hh"
78 // The base implementations use to_number for parsing and '<<' for
79 // displaying, suitable for integer types.
83 parseParam(const string
&s
, T
&value
)
85 return to_number(s
, value
);
90 showParam(CheckpointOut
&os
, const T
&value
)
96 // Template specializations:
97 // - char (8-bit integer)
98 // - floating-point types
103 // Treat 8-bit ints (chars) as ints on output, not as chars
106 showParam(CheckpointOut
&os
, const char &value
)
114 showParam(CheckpointOut
&os
, const signed char &value
)
122 showParam(CheckpointOut
&os
, const unsigned char &value
)
124 os
<< (unsigned int)value
;
130 parseParam(const string
&s
, float &value
)
132 return to_number(s
, value
);
137 parseParam(const string
&s
, double &value
)
139 return to_number(s
, value
);
144 parseParam(const string
&s
, bool &value
)
146 return to_bool(s
, value
);
149 // Display bools as strings
152 showParam(CheckpointOut
&os
, const bool &value
)
154 os
<< (value
? "true" : "false");
158 // String requires no processing to speak of
161 parseParam(const string
&s
, string
&value
)
167 int Serializable::ckptMaxCount
= 0;
168 int Serializable::ckptCount
= 0;
169 int Serializable::ckptPrevCount
= -1;
170 std::stack
<std::string
> Serializable::path
;
174 paramOut(CheckpointOut
&os
, const string
&name
, const T
¶m
)
177 showParam(os
, param
);
183 arrayParamOut(CheckpointOut
&os
, const string
&name
, const vector
<T
> ¶m
)
185 typename vector
<T
>::size_type size
= param
.size();
188 showParam(os
, param
[0]);
189 for (typename vector
<T
>::size_type i
= 1; i
< size
; ++i
) {
191 showParam(os
, param
[i
]);
198 arrayParamOut(CheckpointOut
&os
, const string
&name
, const list
<T
> ¶m
)
200 typename list
<T
>::const_iterator it
= param
.begin();
203 if (param
.size() > 0)
206 while (it
!= param
.end()) {
216 arrayParamOut(CheckpointOut
&os
, const string
&name
, const set
<T
> ¶m
)
218 typename set
<T
>::const_iterator it
= param
.begin();
221 if (param
.size() > 0)
224 while (it
!= param
.end()) {
234 paramIn(CheckpointIn
&cp
, const string
&name
, T
¶m
)
236 const string
§ion(Serializable::currentSection());
238 if (!cp
.find(section
, name
, str
) || !parseParam(str
, param
)) {
239 fatal("Can't unserialize '%s:%s'\n", section
, name
);
245 optParamIn(CheckpointIn
&cp
, const string
&name
, T
¶m
, bool warn
)
247 const string
§ion(Serializable::currentSection());
249 if (!cp
.find(section
, name
, str
) || !parseParam(str
, param
)) {
251 warn("optional parameter %s:%s not present\n", section
, name
);
260 arrayParamOut(CheckpointOut
&os
, const string
&name
,
261 const T
*param
, unsigned size
)
265 showParam(os
, param
[0]);
266 for (unsigned i
= 1; i
< size
; ++i
) {
268 showParam(os
, param
[i
]);
276 arrayParamIn(CheckpointIn
&cp
, const string
&name
, T
*param
, unsigned size
)
278 const string
§ion(Serializable::currentSection());
280 if (!cp
.find(section
, name
, str
)) {
281 fatal("Can't unserialize '%s:%s'\n", section
, name
);
284 // code below stolen from VectorParam<T>::parse().
285 // it would be nice to unify these somehow...
287 vector
<string
> tokens
;
289 tokenize(tokens
, str
, ' ');
291 // Need this if we were doing a vector
292 // value.resize(tokens.size());
294 if (tokens
.size() != size
) {
295 fatal("Array size mismatch on %s:%s'\n", section
, name
);
298 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
299 // need to parse into local variable to handle vector<bool>,
300 // for which operator[] returns a special reference class
301 // that's not the same as 'bool&', (since it's a packed
304 if (!parseParam(tokens
[i
], scalar_value
)) {
305 string
err("could not parse \"");
313 // assign parsed value to vector
314 param
[i
] = scalar_value
;
320 arrayParamIn(CheckpointIn
&cp
, const string
&name
, vector
<T
> ¶m
)
322 const string
§ion(Serializable::currentSection());
324 if (!cp
.find(section
, name
, str
)) {
325 fatal("Can't unserialize '%s:%s'\n", section
, name
);
328 // code below stolen from VectorParam<T>::parse().
329 // it would be nice to unify these somehow...
331 vector
<string
> tokens
;
333 tokenize(tokens
, str
, ' ');
335 // Need this if we were doing a vector
336 // value.resize(tokens.size());
338 param
.resize(tokens
.size());
340 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
341 // need to parse into local variable to handle vector<bool>,
342 // for which operator[] returns a special reference class
343 // that's not the same as 'bool&', (since it's a packed
346 if (!parseParam(tokens
[i
], scalar_value
)) {
347 string
err("could not parse \"");
355 // assign parsed value to vector
356 param
[i
] = scalar_value
;
362 arrayParamIn(CheckpointIn
&cp
, const string
&name
, list
<T
> ¶m
)
364 const string
§ion(Serializable::currentSection());
366 if (!cp
.find(section
, name
, str
)) {
367 fatal("Can't unserialize '%s:%s'\n", section
, name
);
371 vector
<string
> tokens
;
372 tokenize(tokens
, str
, ' ');
374 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
376 if (!parseParam(tokens
[i
], scalar_value
)) {
377 string
err("could not parse \"");
385 // assign parsed value to vector
386 param
.push_back(scalar_value
);
392 arrayParamIn(CheckpointIn
&cp
, const string
&name
, set
<T
> ¶m
)
394 const string
§ion(Serializable::currentSection());
396 if (!cp
.find(section
, name
, str
)) {
397 fatal("Can't unserialize '%s:%s'\n", section
, name
);
401 vector
<string
> tokens
;
402 tokenize(tokens
, str
, ' ');
404 for (vector
<string
>::size_type i
= 0; i
< tokens
.size(); i
++) {
406 if (!parseParam(tokens
[i
], scalar_value
)) {
407 string
err("could not parse \"");
415 // assign parsed value to vector
416 param
.insert(scalar_value
);
422 objParamIn(CheckpointIn
&cp
, const string
&name
, SimObject
* ¶m
)
424 const string
§ion(Serializable::currentSection());
425 if (!cp
.findObj(section
, name
, param
)) {
426 fatal("Can't unserialize '%s:%s'\n", section
, name
);
431 #define INSTANTIATE_PARAM_TEMPLATES(type) \
433 paramOut(CheckpointOut &os, const string &name, type const ¶m); \
435 paramIn(CheckpointIn &cp, const string &name, type & param); \
437 optParamIn(CheckpointIn &cp, const string &name, type & param, \
440 arrayParamOut(CheckpointOut &os, const string &name, \
441 type const *param, unsigned size); \
443 arrayParamIn(CheckpointIn &cp, const string &name, \
444 type *param, unsigned size); \
446 arrayParamOut(CheckpointOut &os, const string &name, \
447 const vector<type> ¶m); \
449 arrayParamIn(CheckpointIn &cp, const string &name, \
450 vector<type> ¶m); \
452 arrayParamOut(CheckpointOut &os, const string &name, \
453 const list<type> ¶m); \
455 arrayParamIn(CheckpointIn &cp, const string &name, \
458 INSTANTIATE_PARAM_TEMPLATES(char)
459 INSTANTIATE_PARAM_TEMPLATES(signed char)
460 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
461 INSTANTIATE_PARAM_TEMPLATES(signed short)
462 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
463 INSTANTIATE_PARAM_TEMPLATES(signed int)
464 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
465 INSTANTIATE_PARAM_TEMPLATES(signed long)
466 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
467 INSTANTIATE_PARAM_TEMPLATES(signed long long)
468 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
469 INSTANTIATE_PARAM_TEMPLATES(bool)
470 INSTANTIATE_PARAM_TEMPLATES(float)
471 INSTANTIATE_PARAM_TEMPLATES(double)
472 INSTANTIATE_PARAM_TEMPLATES(string
)
473 INSTANTIATE_PARAM_TEMPLATES(Pixel
)
475 // set is only used with strings and furthermore doesn't agree with Pixel
477 arrayParamOut(CheckpointOut
&, const string
&, const set
<string
> &);
479 arrayParamIn(CheckpointIn
&, const string
&, set
<string
> &);
481 /////////////////////////////
483 /// Container for serializing global variables (not associated with
484 /// any serialized object).
485 class Globals
: public Serializable
489 : unserializedCurTick(0) {}
491 void serialize(CheckpointOut
&cp
) const override
;
492 void unserialize(CheckpointIn
&cp
) override
;
494 Tick unserializedCurTick
;
497 /// The one and only instance of the Globals class.
500 /// The version tags for this build of the simulator, to be stored in the
501 /// Globals section during serialization and compared upon unserialization.
502 extern std::set
<std::string
> version_tags
;
505 Globals::serialize(CheckpointOut
&cp
) const
507 paramOut(cp
, "curTick", curTick());
508 SERIALIZE_CONTAINER(version_tags
);
512 Globals::unserialize(CheckpointIn
&cp
)
514 paramIn(cp
, "curTick", unserializedCurTick
);
516 const std::string
§ion(Serializable::currentSection());
518 if (!cp
.find(section
, "version_tags", str
)) {
519 warn("**********************************************************\n");
520 warn("!!!! Checkpoint uses an old versioning scheme. !!!!\n");
521 warn("Run the checkpoint upgrader (util/cpt_upgrader.py) on your "
523 warn("**********************************************************\n");
527 std::set
<std::string
> cpt_tags
;
528 arrayParamIn(cp
, "version_tags", cpt_tags
); // UNSERIALIZE_CONTAINER
531 for (const auto& t
: version_tags
) {
532 if (cpt_tags
.find(t
) == cpt_tags
.end()) {
533 // checkpoint is missing tag that this binary has
535 warn("*****************************************************\n");
536 warn("!!!! Checkpoint is missing the following version tags:\n");
543 warn("You might experience some issues when restoring and should run "
544 "the checkpoint upgrader (util/cpt_upgrader.py) on your "
546 warn("**********************************************************\n");
550 for (const auto& t
: cpt_tags
) {
551 if (version_tags
.find(t
) == version_tags
.end()) {
552 // gem5 binary is missing tag that this checkpoint has
554 warn("*****************************************************\n");
555 warn("!!!! gem5 is missing the following version tags:\n");
562 warn("Running a checkpoint with incompatible version tags is not "
563 "supported. While it might work, you may experience incorrect "
564 "behavior or crashes.\n");
565 warn("**********************************************************\n");
569 Serializable::Serializable()
573 Serializable::~Serializable()
578 Serializable::serializeSection(CheckpointOut
&cp
, const char *name
) const
580 Serializable::ScopedCheckpointSection
sec(cp
, name
);
585 Serializable::unserializeSection(CheckpointIn
&cp
, const char *name
)
587 Serializable::ScopedCheckpointSection
sec(cp
, name
);
592 Serializable::serializeAll(const string
&cpt_dir
)
594 string dir
= CheckpointIn::setDir(cpt_dir
);
595 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
596 fatal("couldn't mkdir %s\n", dir
);
598 string cpt_file
= dir
+ CheckpointIn::baseFilename
;
599 ofstream
outstream(cpt_file
.c_str());
600 time_t t
= time(NULL
);
601 if (!outstream
.is_open())
602 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
603 outstream
<< "## checkpoint generated: " << ctime(&t
);
605 globals
.serializeSection(outstream
, "Globals");
607 SimObject::serializeAll(outstream
);
611 Serializable::unserializeGlobals(CheckpointIn
&cp
)
613 globals
.unserializeSection(cp
, "Globals");
615 for (uint32_t i
= 0; i
< numMainEventQueues
; ++i
)
616 mainEventQueue
[i
]->setCurTick(globals
.unserializedCurTick
);
619 Serializable::ScopedCheckpointSection::~ScopedCheckpointSection()
621 assert(!path
.empty());
622 DPRINTF(Checkpoint
, "Popping: %s\n", path
.top());
627 Serializable::ScopedCheckpointSection::pushName(const char *obj_name
)
632 path
.push(csprintf("%s.%s", path
.top(), obj_name
));
634 DPRINTF(Checkpoint
, "ScopedCheckpointSection::pushName: %s\n", obj_name
);
638 Serializable::ScopedCheckpointSection::nameOut(CheckpointOut
&cp
)
640 DPRINTF(Checkpoint
, "ScopedCheckpointSection::nameOut: %s\n",
641 Serializable::currentSection());
642 cp
<< "\n[" << Serializable::currentSection() << "]\n";
646 debug_serialize(const string
&cpt_dir
)
648 Serializable::serializeAll(cpt_dir
);
652 Serializable::currentSection()
654 assert(!path
.empty());
659 const char *CheckpointIn::baseFilename
= "m5.cpt";
661 string
CheckpointIn::currentDirectory
;
664 CheckpointIn::setDir(const string
&name
)
666 // use csprintf to insert curTick() into directory name if it
667 // appears to have a format placeholder in it.
668 currentDirectory
= (name
.find("%") != string::npos
) ?
669 csprintf(name
, curTick()) : name
;
670 if (currentDirectory
[currentDirectory
.size() - 1] != '/')
671 currentDirectory
+= "/";
672 return currentDirectory
;
678 return currentDirectory
;
682 CheckpointIn::CheckpointIn(const string
&cpt_dir
, SimObjectResolver
&resolver
)
683 : db(new IniFile
), objNameResolver(resolver
), cptDir(setDir(cpt_dir
))
685 string filename
= cptDir
+ "/" + CheckpointIn::baseFilename
;
686 if (!db
->load(filename
)) {
687 fatal("Can't load checkpoint file '%s'\n", filename
);
691 CheckpointIn::~CheckpointIn()
697 CheckpointIn::entryExists(const string
§ion
, const string
&entry
)
699 return db
->entryExists(section
, entry
);
703 CheckpointIn::find(const string
§ion
, const string
&entry
, string
&value
)
705 return db
->find(section
, entry
, value
);
710 CheckpointIn::findObj(const string
§ion
, const string
&entry
,
715 if (!db
->find(section
, entry
, path
))
718 value
= objNameResolver
.resolveSimObject(path
);
724 CheckpointIn::sectionExists(const string
§ion
)
726 return db
->sectionExists(section
);