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.
43 #include "sim/serialize.hh"
47 #include <sys/types.h>
55 #include "base/inifile.hh"
56 #include "base/output.hh"
57 #include "base/trace.hh"
58 #include "debug/Checkpoint.hh"
59 #include "sim/eventq.hh"
60 #include "sim/sim_events.hh"
61 #include "sim/sim_exit.hh"
62 #include "sim/sim_object.hh"
64 // For stat reset hack
65 #include "sim/stat_control.hh"
69 int Serializable::ckptMaxCount
= 0;
70 int Serializable::ckptCount
= 0;
71 int Serializable::ckptPrevCount
= -1;
72 std::stack
<std::string
> Serializable::path
;
74 /////////////////////////////
76 /// Container for serializing global variables (not associated with
77 /// any serialized object).
78 class Globals
: public Serializable
82 : unserializedCurTick(0) {}
84 void serialize(CheckpointOut
&cp
) const override
;
85 void unserialize(CheckpointIn
&cp
) override
;
87 Tick unserializedCurTick
;
90 /// The one and only instance of the Globals class.
93 /// The version tags for this build of the simulator, to be stored in the
94 /// Globals section during serialization and compared upon unserialization.
95 extern std::set
<std::string
> version_tags
;
98 Globals::serialize(CheckpointOut
&cp
) const
100 paramOut(cp
, "curTick", curTick());
101 SERIALIZE_CONTAINER(version_tags
);
105 Globals::unserialize(CheckpointIn
&cp
)
107 paramIn(cp
, "curTick", unserializedCurTick
);
109 const std::string
§ion(Serializable::currentSection());
111 if (!cp
.find(section
, "version_tags", str
)) {
112 warn("**********************************************************\n");
113 warn("!!!! Checkpoint uses an old versioning scheme. !!!!\n");
114 warn("Run the checkpoint upgrader (util/cpt_upgrader.py) on your "
116 warn("**********************************************************\n");
120 std::set
<std::string
> cpt_tags
;
121 arrayParamIn(cp
, "version_tags", cpt_tags
); // UNSERIALIZE_CONTAINER
124 for (const auto& t
: version_tags
) {
125 if (cpt_tags
.find(t
) == cpt_tags
.end()) {
126 // checkpoint is missing tag that this binary has
128 warn("*****************************************************\n");
129 warn("!!!! Checkpoint is missing the following version tags:\n");
136 warn("You might experience some issues when restoring and should run "
137 "the checkpoint upgrader (util/cpt_upgrader.py) on your "
139 warn("**********************************************************\n");
143 for (const auto& t
: cpt_tags
) {
144 if (version_tags
.find(t
) == version_tags
.end()) {
145 // gem5 binary is missing tag that this checkpoint has
147 warn("*****************************************************\n");
148 warn("!!!! gem5 is missing the following version tags:\n");
155 warn("Running a checkpoint with incompatible version tags is not "
156 "supported. While it might work, you may experience incorrect "
157 "behavior or crashes.\n");
158 warn("**********************************************************\n");
162 Serializable::Serializable()
166 Serializable::~Serializable()
171 Serializable::serializeSection(CheckpointOut
&cp
, const char *name
) const
173 Serializable::ScopedCheckpointSection
sec(cp
, name
);
178 Serializable::unserializeSection(CheckpointIn
&cp
, const char *name
)
180 Serializable::ScopedCheckpointSection
sec(cp
, name
);
185 Serializable::serializeAll(const string
&cpt_dir
)
187 string dir
= CheckpointIn::setDir(cpt_dir
);
188 if (mkdir(dir
.c_str(), 0775) == -1 && errno
!= EEXIST
)
189 fatal("couldn't mkdir %s\n", dir
);
191 string cpt_file
= dir
+ CheckpointIn::baseFilename
;
192 ofstream
outstream(cpt_file
.c_str());
193 time_t t
= time(NULL
);
194 if (!outstream
.is_open())
195 fatal("Unable to open file %s for writing\n", cpt_file
.c_str());
196 outstream
<< "## checkpoint generated: " << ctime(&t
);
198 globals
.serializeSection(outstream
, "Globals");
200 SimObject::serializeAll(outstream
);
204 Serializable::unserializeGlobals(CheckpointIn
&cp
)
206 globals
.unserializeSection(cp
, "Globals");
208 for (uint32_t i
= 0; i
< numMainEventQueues
; ++i
)
209 mainEventQueue
[i
]->setCurTick(globals
.unserializedCurTick
);
212 Serializable::ScopedCheckpointSection::~ScopedCheckpointSection()
214 assert(!path
.empty());
215 DPRINTF(Checkpoint
, "Popping: %s\n", path
.top());
220 Serializable::ScopedCheckpointSection::pushName(const char *obj_name
)
225 path
.push(csprintf("%s.%s", path
.top(), obj_name
));
227 DPRINTF(Checkpoint
, "ScopedCheckpointSection::pushName: %s\n", obj_name
);
231 Serializable::ScopedCheckpointSection::nameOut(CheckpointOut
&cp
)
233 DPRINTF(Checkpoint
, "ScopedCheckpointSection::nameOut: %s\n",
234 Serializable::currentSection());
235 cp
<< "\n[" << Serializable::currentSection() << "]\n";
239 Serializable::currentSection()
241 assert(!path
.empty());
246 const char *CheckpointIn::baseFilename
= "m5.cpt";
248 string
CheckpointIn::currentDirectory
;
251 CheckpointIn::setDir(const string
&name
)
253 // use csprintf to insert curTick() into directory name if it
254 // appears to have a format placeholder in it.
255 currentDirectory
= (name
.find("%") != string::npos
) ?
256 csprintf(name
, curTick()) : name
;
257 if (currentDirectory
[currentDirectory
.size() - 1] != '/')
258 currentDirectory
+= "/";
259 return currentDirectory
;
265 return currentDirectory
;
268 CheckpointIn::CheckpointIn(const string
&cpt_dir
, SimObjectResolver
&resolver
)
269 : db(new IniFile
), objNameResolver(resolver
), cptDir(setDir(cpt_dir
))
271 string filename
= cptDir
+ "/" + CheckpointIn::baseFilename
;
272 if (!db
->load(filename
)) {
273 fatal("Can't load checkpoint file '%s'\n", filename
);
277 CheckpointIn::~CheckpointIn()
283 CheckpointIn::entryExists(const string
§ion
, const string
&entry
)
285 return db
->entryExists(section
, entry
);
289 CheckpointIn::find(const string
§ion
, const string
&entry
, string
&value
)
291 return db
->find(section
, entry
, value
);
295 CheckpointIn::findObj(const string
§ion
, const string
&entry
,
300 if (!db
->find(section
, entry
, path
))
303 value
= objNameResolver
.resolveSimObject(path
);
308 CheckpointIn::sectionExists(const string
§ion
)
310 return db
->sectionExists(section
);
314 objParamIn(CheckpointIn
&cp
, const string
&name
, SimObject
* ¶m
)
316 const string
§ion(Serializable::currentSection());
317 if (!cp
.findObj(section
, name
, param
)) {
318 fatal("Can't unserialize '%s:%s'\n", section
, name
);
323 debug_serialize(const string
&cpt_dir
)
325 Serializable::serializeAll(cpt_dir
);