gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
serialize.cc
Go to the documentation of this file.
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  * Authors: Nathan Binkert
43  * Erik Hallnor
44  * Steve Reinhardt
45  * Andreas Sandberg
46  */
47 
48 #include "sim/serialize.hh"
49 
50 #include <sys/stat.h>
51 #include <sys/time.h>
52 #include <sys/types.h>
53 
54 #include <cerrno>
55 #include <fstream>
56 #include <list>
57 #include <string>
58 #include <vector>
59 
60 #include "base/inifile.hh"
61 #include "base/output.hh"
62 #include "base/trace.hh"
63 #include "debug/Checkpoint.hh"
64 #include "sim/eventq.hh"
65 #include "sim/sim_events.hh"
66 #include "sim/sim_exit.hh"
67 #include "sim/sim_object.hh"
68 
69 // For stat reset hack
70 #include "sim/stat_control.hh"
71 
72 using namespace std;
73 
77 std::stack<std::string> Serializable::path;
78 
80 
83 class Globals : public Serializable
84 {
85  public:
87  : unserializedCurTick(0) {}
88 
89  void serialize(CheckpointOut &cp) const override;
90  void unserialize(CheckpointIn &cp) override;
91 
93 };
94 
97 
100 extern std::set<std::string> version_tags;
101 
102 void
104 {
105  paramOut(cp, "curTick", curTick());
107 }
108 
109 void
111 {
112  paramIn(cp, "curTick", unserializedCurTick);
113 
114  const std::string &section(Serializable::currentSection());
115  std::string str;
116  if (!cp.find(section, "version_tags", str)) {
117  warn("**********************************************************\n");
118  warn("!!!! Checkpoint uses an old versioning scheme. !!!!\n");
119  warn("Run the checkpoint upgrader (util/cpt_upgrader.py) on your "
120  "checkpoint\n");
121  warn("**********************************************************\n");
122  return;
123  }
124 
125  std::set<std::string> cpt_tags;
126  arrayParamIn(cp, "version_tags", cpt_tags); // UNSERIALIZE_CONTAINER
127 
128  bool err = false;
129  for (const auto& t : version_tags) {
130  if (cpt_tags.find(t) == cpt_tags.end()) {
131  // checkpoint is missing tag that this binary has
132  if (!err) {
133  warn("*****************************************************\n");
134  warn("!!!! Checkpoint is missing the following version tags:\n");
135  err = true;
136  }
137  warn(" %s\n", t);
138  }
139  }
140  if (err) {
141  warn("You might experience some issues when restoring and should run "
142  "the checkpoint upgrader (util/cpt_upgrader.py) on your "
143  "checkpoint\n");
144  warn("**********************************************************\n");
145  }
146 
147  err = false;
148  for (const auto& t : cpt_tags) {
149  if (version_tags.find(t) == version_tags.end()) {
150  // gem5 binary is missing tag that this checkpoint has
151  if (!err) {
152  warn("*****************************************************\n");
153  warn("!!!! gem5 is missing the following version tags:\n");
154  err = true;
155  }
156  warn(" %s\n", t);
157  }
158  }
159  if (err) {
160  warn("Running a checkpoint with incompatible version tags is not "
161  "supported. While it might work, you may experience incorrect "
162  "behavior or crashes.\n");
163  warn("**********************************************************\n");
164  }
165 }
166 
168 {
169 }
170 
172 {
173 }
174 
175 void
177 {
179  serialize(cp);
180 }
181 
182 void
184 {
186  unserialize(cp);
187 }
188 
189 void
190 Serializable::serializeAll(const string &cpt_dir)
191 {
192  string dir = CheckpointIn::setDir(cpt_dir);
193  if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
194  fatal("couldn't mkdir %s\n", dir);
195 
196  string cpt_file = dir + CheckpointIn::baseFilename;
197  ofstream outstream(cpt_file.c_str());
198  time_t t = time(NULL);
199  if (!outstream.is_open())
200  fatal("Unable to open file %s for writing\n", cpt_file.c_str());
201  outstream << "## checkpoint generated: " << ctime(&t);
202 
203  globals.serializeSection(outstream, "Globals");
204 
205  SimObject::serializeAll(outstream);
206 }
207 
208 void
210 {
211  globals.unserializeSection(cp, "Globals");
212 
213  for (uint32_t i = 0; i < numMainEventQueues; ++i)
214  mainEventQueue[i]->setCurTick(globals.unserializedCurTick);
215 }
216 
218 {
219  assert(!path.empty());
220  DPRINTF(Checkpoint, "Popping: %s\n", path.top());
221  path.pop();
222 }
223 
224 void
226 {
227  if (path.empty()) {
228  path.push(obj_name);
229  } else {
230  path.push(csprintf("%s.%s", path.top(), obj_name));
231  }
232  DPRINTF(Checkpoint, "ScopedCheckpointSection::pushName: %s\n", obj_name);
233 }
234 
235 void
237 {
238  DPRINTF(Checkpoint, "ScopedCheckpointSection::nameOut: %s\n",
240  cp << "\n[" << Serializable::currentSection() << "]\n";
241 }
242 
243 const std::string &
245 {
246  assert(!path.empty());
247 
248  return path.top();
249 }
250 
251 const char *CheckpointIn::baseFilename = "m5.cpt";
252 
254 
255 string
257 {
258  // use csprintf to insert curTick() into directory name if it
259  // appears to have a format placeholder in it.
260  currentDirectory = (name.find("%") != string::npos) ?
261  csprintf(name, curTick()) : name;
262  if (currentDirectory[currentDirectory.size() - 1] != '/')
263  currentDirectory += "/";
264  return currentDirectory;
265 }
266 
267 string
269 {
270  return currentDirectory;
271 }
272 
273 CheckpointIn::CheckpointIn(const string &cpt_dir, SimObjectResolver &resolver)
274  : db(new IniFile), objNameResolver(resolver), cptDir(setDir(cpt_dir))
275 {
276  string filename = cptDir + "/" + CheckpointIn::baseFilename;
277  if (!db->load(filename)) {
278  fatal("Can't load checkpoint file '%s'\n", filename);
279  }
280 }
281 
283 {
284  delete db;
285 }
286 
287 bool
288 CheckpointIn::entryExists(const string &section, const string &entry)
289 {
290  return db->entryExists(section, entry);
291 }
292 
293 bool
294 CheckpointIn::find(const string &section, const string &entry, string &value)
295 {
296  return db->find(section, entry, value);
297 }
298 
299 bool
300 CheckpointIn::findObj(const string &section, const string &entry,
301  SimObject *&value)
302 {
303  string path;
304 
305  if (!db->find(section, entry, path))
306  return false;
307 
308  value = objNameResolver.resolveSimObject(path);
309  return true;
310 }
311 
312 bool
313 CheckpointIn::sectionExists(const string &section)
314 {
315  return db->sectionExists(section);
316 }
317 
318 void
319 objParamIn(CheckpointIn &cp, const string &name, SimObject * &param)
320 {
321  const string &section(Serializable::currentSection());
322  if (!cp.findObj(section, name, param)) {
323  fatal("Can't unserialize '%s:%s'\n", section, name);
324  }
325 }
326 
327 void
328 debug_serialize(const string &cpt_dir)
329 {
331 }
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: serialize.cc:110
#define DPRINTF(x,...)
Definition: trace.hh:229
static void unserializeGlobals(CheckpointIn &cp)
Definition: serialize.cc:209
const std::string cptDir
Definition: serialize.hh:84
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:175
static std::string dir()
Definition: serialize.cc:268
void pushName(const char *name)
Definition: serialize.cc:225
const std::string & name()
Definition: trace.cc:54
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
Definition: serialize.cc:176
Bitfield< 7 > i
bool find(const std::string &section, const std::string &entry, std::string &value) const
Find value corresponding to given section and entry names.
Definition: inifile.cc:217
static std::stack< std::string > path
Definition: serialize.hh:267
SimObjectResolver & objNameResolver
Definition: serialize.hh:78
std::set< std::string > version_tags
The version tags for this build of the simulator, to be stored in the Globals section during serializ...
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition: serialize.cc:183
static std::string currentDirectory
Definition: serialize.hh:105
static void serializeAll(CheckpointOut &cp)
Serialize all SimObjects in the system.
Definition: sim_object.cc:135
static int ckptPrevCount
Definition: serialize.hh:262
void debug_serialize(const string &cpt_dir)
Definition: serialize.cc:328
static int ckptCount
Definition: serialize.hh:260
vector< EventQueue * > mainEventQueue
Array for main event queues.
Definition: eventq.cc:59
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:586
Definition: cprintf.cc:42
Globals globals
The one and only instance of the Globals class.
Definition: serialize.cc:96
Container for serializing global variables (not associated with any serialized object).
Definition: serialize.cc:83
Base class to wrap object resolving functionality.
Definition: sim_object.hh:242
virtual SimObject * resolveSimObject(const std::string &name)=0
#define SERIALIZE_CONTAINER(member)
Definition: serialize.hh:664
Tick curTick()
The current simulated tick.
Definition: core.hh:47
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:162
bool find(const std::string &section, const std::string &entry, std::string &value)
Definition: serialize.cc:294
bool entryExists(const std::string &section, const std::string &entry) const
Determine whether the entry exists within named section exists in the .ini file.
Definition: inifile.cc:234
uint64_t Tick
Tick count type.
Definition: types.hh:63
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
Definition: types.cc:40
void serialize(const ThreadContext &tc, CheckpointOut &cp)
Thread context serialization helpers.
bool load(std::istream &f)
Load parameter settings from given istream.
Declaration of IniFile object.
void nameOut(CheckpointOut &cp)
Definition: serialize.cc:236
Basic support for object serialization.
Definition: serialize.hh:153
virtual ~Serializable()
Definition: serialize.cc:171
Tick unserializedCurTick
Definition: serialize.cc:92
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: serialize.cc:103
std::ostream CheckpointOut
Definition: serialize.hh:68
uint32_t numMainEventQueues
Current number of allocated main event queues.
Definition: eventq.cc:58
bool sectionExists(const std::string &section)
Definition: serialize.cc:313
static int ckptMaxCount
Definition: serialize.hh:261
static const std::string & currentSection()
Get the fully-qualified name of the active section.
Definition: serialize.cc:244
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
Definition: types.cc:71
bool entryExists(const std::string &section, const std::string &entry)
Definition: serialize.cc:288
IniFile * db
Definition: serialize.hh:76
void unserialize(ThreadContext &tc, CheckpointIn &cp)
Globals()
Definition: serialize.cc:86
void arrayParamIn(CheckpointIn &cp, const std::string &name, CircleBuf< T > &param)
Definition: circlebuf.hh:186
bool findObj(const std::string &section, const std::string &entry, SimObject *&value)
Definition: serialize.cc:300
bool sectionExists(const std::string &section) const
Determine whether the named section exists in the .ini file.
Definition: inifile.cc:245
#define warn(...)
Definition: logging.hh:212
Bitfield< 5 > t
Scoped checkpoint section helper class.
Definition: serialize.hh:173
Abstract superclass for simulation objects.
Definition: sim_object.hh:96
This class represents the contents of a ".ini" file.
Definition: inifile.hh:54
static const char * baseFilename
Definition: serialize.hh:120
void objParamIn(CheckpointIn &cp, const string &name, SimObject *&param)
Definition: serialize.cc:319
CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver)
Definition: serialize.cc:273
static std::string setDir(const std::string &base_name)
Definition: serialize.cc:256
static void serializeAll(const std::string &cpt_dir)
Definition: serialize.cc:190

Generated on Fri Feb 28 2020 16:27:03 for gem5 by doxygen 1.8.13