arch-arm,cpu: Introduce a getEMI virtual method on StaticInst.
[gem5.git] / src / sim / serialize.cc
1 /*
2 * Copyright (c) 2015, 2020 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
18 *
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.
29 *
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.
41 */
42
43 #include "sim/serialize.hh"
44
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/types.h>
48
49 #include <cerrno>
50 #include <fstream>
51 #include <list>
52 #include <set>
53 #include <string>
54 #include <vector>
55
56 #include "base/inifile.hh"
57 #include "base/output.hh"
58 #include "base/trace.hh"
59 #include "debug/Checkpoint.hh"
60 #include "sim/eventq.hh"
61 #include "sim/sim_events.hh"
62 #include "sim/sim_exit.hh"
63 #include "sim/sim_object.hh"
64
65 // For stat reset hack
66 #include "sim/stat_control.hh"
67
68 int ckptMaxCount = 0;
69 int ckptCount = 0;
70 int ckptPrevCount = -1;
71 std::stack<std::string> Serializable::path;
72
73 /////////////////////////////
74
75 /// Container for serializing global variables (not associated with
76 /// any serialized object).
77 class Globals : public Serializable
78 {
79 public:
80 Globals()
81 : unserializedCurTick(0) {}
82
83 void serialize(CheckpointOut &cp) const override;
84 void unserialize(CheckpointIn &cp) override;
85
86 Tick unserializedCurTick;
87 };
88
89 /// The one and only instance of the Globals class.
90 Globals globals;
91
92 /// The version tags for this build of the simulator, to be stored in the
93 /// Globals section during serialization and compared upon unserialization.
94 extern std::set<std::string> version_tags;
95
96 void
97 Globals::serialize(CheckpointOut &cp) const
98 {
99 paramOut(cp, "curTick", curTick());
100 SERIALIZE_CONTAINER(version_tags);
101 }
102
103 void
104 Globals::unserialize(CheckpointIn &cp)
105 {
106 paramIn(cp, "curTick", unserializedCurTick);
107
108 const std::string &section(Serializable::currentSection());
109 std::string str;
110 if (!cp.find(section, "version_tags", str)) {
111 warn("**********************************************************\n");
112 warn("!!!! Checkpoint uses an old versioning scheme. !!!!\n");
113 warn("Run the checkpoint upgrader (util/cpt_upgrader.py) on your "
114 "checkpoint\n");
115 warn("**********************************************************\n");
116 return;
117 }
118
119 std::set<std::string> cpt_tags;
120 arrayParamIn(cp, "version_tags", cpt_tags); // UNSERIALIZE_CONTAINER
121
122 bool err = false;
123 for (const auto& t : version_tags) {
124 if (cpt_tags.find(t) == cpt_tags.end()) {
125 // checkpoint is missing tag that this binary has
126 if (!err) {
127 warn("*****************************************************\n");
128 warn("!!!! Checkpoint is missing the following version tags:\n");
129 err = true;
130 }
131 warn(" %s\n", t);
132 }
133 }
134 if (err) {
135 warn("You might experience some issues when restoring and should run "
136 "the checkpoint upgrader (util/cpt_upgrader.py) on your "
137 "checkpoint\n");
138 warn("**********************************************************\n");
139 }
140
141 err = false;
142 for (const auto& t : cpt_tags) {
143 if (version_tags.find(t) == version_tags.end()) {
144 // gem5 binary is missing tag that this checkpoint has
145 if (!err) {
146 warn("*****************************************************\n");
147 warn("!!!! gem5 is missing the following version tags:\n");
148 err = true;
149 }
150 warn(" %s\n", t);
151 }
152 }
153 if (err) {
154 warn("Running a checkpoint with incompatible version tags is not "
155 "supported. While it might work, you may experience incorrect "
156 "behavior or crashes.\n");
157 warn("**********************************************************\n");
158 }
159 }
160
161 Serializable::Serializable()
162 {
163 }
164
165 Serializable::~Serializable()
166 {
167 }
168
169 void
170 Serializable::serializeSection(CheckpointOut &cp, const char *name) const
171 {
172 Serializable::ScopedCheckpointSection sec(cp, name);
173 serialize(cp);
174 }
175
176 void
177 Serializable::unserializeSection(CheckpointIn &cp, const char *name)
178 {
179 Serializable::ScopedCheckpointSection sec(cp, name);
180 unserialize(cp);
181 }
182
183 void
184 Serializable::serializeAll(const std::string &cpt_dir)
185 {
186 std::string dir = CheckpointIn::setDir(cpt_dir);
187 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
188 fatal("couldn't mkdir %s\n", dir);
189
190 std::string cpt_file = dir + CheckpointIn::baseFilename;
191 std::ofstream outstream(cpt_file.c_str());
192 time_t t = time(NULL);
193 if (!outstream.is_open())
194 fatal("Unable to open file %s for writing\n", cpt_file.c_str());
195 outstream << "## checkpoint generated: " << ctime(&t);
196
197 globals.serializeSection(outstream, "Globals");
198
199 SimObject::serializeAll(outstream);
200 }
201
202 void
203 Serializable::unserializeGlobals(CheckpointIn &cp)
204 {
205 globals.unserializeSection(cp, "Globals");
206
207 for (uint32_t i = 0; i < numMainEventQueues; ++i)
208 mainEventQueue[i]->setCurTick(globals.unserializedCurTick);
209 }
210
211 Serializable::ScopedCheckpointSection::~ScopedCheckpointSection()
212 {
213 assert(!path.empty());
214 DPRINTF(Checkpoint, "Popping: %s\n", path.top());
215 path.pop();
216 }
217
218 void
219 Serializable::ScopedCheckpointSection::pushName(const char *obj_name)
220 {
221 if (path.empty()) {
222 path.push(obj_name);
223 } else {
224 path.push(csprintf("%s.%s", path.top(), obj_name));
225 }
226 DPRINTF(Checkpoint, "ScopedCheckpointSection::pushName: %s\n", obj_name);
227 }
228
229 void
230 Serializable::ScopedCheckpointSection::nameOut(CheckpointOut &cp)
231 {
232 DPRINTF(Checkpoint, "ScopedCheckpointSection::nameOut: %s\n",
233 Serializable::currentSection());
234 cp << "\n[" << Serializable::currentSection() << "]\n";
235 }
236
237 const std::string &
238 Serializable::currentSection()
239 {
240 assert(!path.empty());
241
242 return path.top();
243 }
244
245 const char *CheckpointIn::baseFilename = "m5.cpt";
246
247 std::string CheckpointIn::currentDirectory;
248
249 std::string
250 CheckpointIn::setDir(const std::string &name)
251 {
252 // use csprintf to insert curTick() into directory name if it
253 // appears to have a format placeholder in it.
254 currentDirectory = (name.find("%") != std::string::npos) ?
255 csprintf(name, curTick()) : name;
256 if (currentDirectory[currentDirectory.size() - 1] != '/')
257 currentDirectory += "/";
258 return currentDirectory;
259 }
260
261 std::string
262 CheckpointIn::dir()
263 {
264 return currentDirectory;
265 }
266
267 CheckpointIn::CheckpointIn(const std::string &cpt_dir,
268 SimObjectResolver &resolver)
269 : db(new IniFile), objNameResolver(resolver), _cptDir(setDir(cpt_dir))
270 {
271 std::string filename = getCptDir() + "/" + CheckpointIn::baseFilename;
272 if (!db->load(filename)) {
273 fatal("Can't load checkpoint file '%s'\n", filename);
274 }
275 }
276
277 CheckpointIn::~CheckpointIn()
278 {
279 delete db;
280 }
281 /**
282 * @param section Here we mention the section we are looking for
283 * (example: currentsection).
284 * @param entry Mention the entry we are looking for (example: interrupt
285 * time) in the section.
286 *
287 * @return Returns true if the entry exists in the named section
288 * we are looking in.
289 */
290 bool
291 CheckpointIn::entryExists(const std::string &section, const std::string &entry)
292 {
293 return db->entryExists(section, entry);
294 }
295 /**
296 * @param section Here we mention the section we are looking for
297 * (example: currentsection).
298 * @param entry Mention the entry we are looking for (example: Cache
299 * line size etc) in the section.
300 * @param value Give the value at the said entry.
301 *
302 * @return Returns true if the searched parameter exists with
303 * the value, given the section .
304 */
305 bool
306 CheckpointIn::find(const std::string &section, const std::string &entry,
307 std::string &value)
308 {
309 return db->find(section, entry, value);
310 }
311 /**
312 * @param section Here we mention the section we are looking for
313 * (example: currentsection).
314 * @param entry Mention the SimObject we are looking for (example:
315 * interruput time) in the section.
316 * @param value Give the value at the said entry.
317 *
318 * @return Returns true if a SimObject exists in the section.
319 *
320 */
321 bool
322 CheckpointIn::findObj(const std::string &section, const std::string &entry,
323 SimObject *&value)
324 {
325 std::string path;
326
327 if (!db->find(section, entry, path))
328 return false;
329
330 value = objNameResolver.resolveSimObject(path);
331 return true;
332 }
333
334 bool
335 CheckpointIn::sectionExists(const std::string &section)
336 {
337 return db->sectionExists(section);
338 }
339
340 void
341 CheckpointIn::visitSection(const std::string &section,
342 IniFile::VisitSectionCallback cb)
343 {
344 db->visitSection(section, cb);
345 }
346
347 void
348 objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param)
349 {
350 const std::string &section(Serializable::currentSection());
351 if (!cp.findObj(section, name, param)) {
352 fatal("Can't unserialize '%s:%s'\n", section, name);
353 }
354 }
355
356 void
357 debug_serialize(const std::string &cpt_dir)
358 {
359 Serializable::serializeAll(cpt_dir);
360 }