sim,misc: Rename M5OP_ANNOTATE to M5OP_RESERVED1.
[gem5.git] / src / sim / serialize.cc
1 /*
2 * Copyright (c) 2015 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 <string>
53 #include <vector>
54
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"
63
64 // For stat reset hack
65 #include "sim/stat_control.hh"
66
67 using namespace std;
68
69 int Serializable::ckptMaxCount = 0;
70 int Serializable::ckptCount = 0;
71 int Serializable::ckptPrevCount = -1;
72 std::stack<std::string> Serializable::path;
73
74 /////////////////////////////
75
76 /// Container for serializing global variables (not associated with
77 /// any serialized object).
78 class Globals : public Serializable
79 {
80 public:
81 Globals()
82 : unserializedCurTick(0) {}
83
84 void serialize(CheckpointOut &cp) const override;
85 void unserialize(CheckpointIn &cp) override;
86
87 Tick unserializedCurTick;
88 };
89
90 /// The one and only instance of the Globals class.
91 Globals globals;
92
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;
96
97 void
98 Globals::serialize(CheckpointOut &cp) const
99 {
100 paramOut(cp, "curTick", curTick());
101 SERIALIZE_CONTAINER(version_tags);
102 }
103
104 void
105 Globals::unserialize(CheckpointIn &cp)
106 {
107 paramIn(cp, "curTick", unserializedCurTick);
108
109 const std::string &section(Serializable::currentSection());
110 std::string str;
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 "
115 "checkpoint\n");
116 warn("**********************************************************\n");
117 return;
118 }
119
120 std::set<std::string> cpt_tags;
121 arrayParamIn(cp, "version_tags", cpt_tags); // UNSERIALIZE_CONTAINER
122
123 bool err = false;
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
127 if (!err) {
128 warn("*****************************************************\n");
129 warn("!!!! Checkpoint is missing the following version tags:\n");
130 err = true;
131 }
132 warn(" %s\n", t);
133 }
134 }
135 if (err) {
136 warn("You might experience some issues when restoring and should run "
137 "the checkpoint upgrader (util/cpt_upgrader.py) on your "
138 "checkpoint\n");
139 warn("**********************************************************\n");
140 }
141
142 err = false;
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
146 if (!err) {
147 warn("*****************************************************\n");
148 warn("!!!! gem5 is missing the following version tags:\n");
149 err = true;
150 }
151 warn(" %s\n", t);
152 }
153 }
154 if (err) {
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");
159 }
160 }
161
162 Serializable::Serializable()
163 {
164 }
165
166 Serializable::~Serializable()
167 {
168 }
169
170 void
171 Serializable::serializeSection(CheckpointOut &cp, const char *name) const
172 {
173 Serializable::ScopedCheckpointSection sec(cp, name);
174 serialize(cp);
175 }
176
177 void
178 Serializable::unserializeSection(CheckpointIn &cp, const char *name)
179 {
180 Serializable::ScopedCheckpointSection sec(cp, name);
181 unserialize(cp);
182 }
183
184 void
185 Serializable::serializeAll(const string &cpt_dir)
186 {
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);
190
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);
197
198 globals.serializeSection(outstream, "Globals");
199
200 SimObject::serializeAll(outstream);
201 }
202
203 void
204 Serializable::unserializeGlobals(CheckpointIn &cp)
205 {
206 globals.unserializeSection(cp, "Globals");
207
208 for (uint32_t i = 0; i < numMainEventQueues; ++i)
209 mainEventQueue[i]->setCurTick(globals.unserializedCurTick);
210 }
211
212 Serializable::ScopedCheckpointSection::~ScopedCheckpointSection()
213 {
214 assert(!path.empty());
215 DPRINTF(Checkpoint, "Popping: %s\n", path.top());
216 path.pop();
217 }
218
219 void
220 Serializable::ScopedCheckpointSection::pushName(const char *obj_name)
221 {
222 if (path.empty()) {
223 path.push(obj_name);
224 } else {
225 path.push(csprintf("%s.%s", path.top(), obj_name));
226 }
227 DPRINTF(Checkpoint, "ScopedCheckpointSection::pushName: %s\n", obj_name);
228 }
229
230 void
231 Serializable::ScopedCheckpointSection::nameOut(CheckpointOut &cp)
232 {
233 DPRINTF(Checkpoint, "ScopedCheckpointSection::nameOut: %s\n",
234 Serializable::currentSection());
235 cp << "\n[" << Serializable::currentSection() << "]\n";
236 }
237
238 const std::string &
239 Serializable::currentSection()
240 {
241 assert(!path.empty());
242
243 return path.top();
244 }
245
246 const char *CheckpointIn::baseFilename = "m5.cpt";
247
248 string CheckpointIn::currentDirectory;
249
250 string
251 CheckpointIn::setDir(const string &name)
252 {
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;
260 }
261
262 string
263 CheckpointIn::dir()
264 {
265 return currentDirectory;
266 }
267
268 CheckpointIn::CheckpointIn(const string &cpt_dir, SimObjectResolver &resolver)
269 : db(new IniFile), objNameResolver(resolver), cptDir(setDir(cpt_dir))
270 {
271 string filename = cptDir + "/" + 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 bool
283 CheckpointIn::entryExists(const string &section, const string &entry)
284 {
285 return db->entryExists(section, entry);
286 }
287
288 bool
289 CheckpointIn::find(const string &section, const string &entry, string &value)
290 {
291 return db->find(section, entry, value);
292 }
293
294 bool
295 CheckpointIn::findObj(const string &section, const string &entry,
296 SimObject *&value)
297 {
298 string path;
299
300 if (!db->find(section, entry, path))
301 return false;
302
303 value = objNameResolver.resolveSimObject(path);
304 return true;
305 }
306
307 bool
308 CheckpointIn::sectionExists(const string &section)
309 {
310 return db->sectionExists(section);
311 }
312
313 void
314 objParamIn(CheckpointIn &cp, const string &name, SimObject * &param)
315 {
316 const string &section(Serializable::currentSection());
317 if (!cp.findObj(section, name, param)) {
318 fatal("Can't unserialize '%s:%s'\n", section, name);
319 }
320 }
321
322 void
323 debug_serialize(const string &cpt_dir)
324 {
325 Serializable::serializeAll(cpt_dir);
326 }