gem5  v20.1.0.0
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 
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 ckptMaxCount = 0;
70 int ckptCount = 0;
71 int ckptPrevCount = -1;
72 std::stack<std::string> Serializable::path;
73 
75 
78 class Globals : public Serializable
79 {
80  public:
82  : unserializedCurTick(0) {}
83 
84  void serialize(CheckpointOut &cp) const override;
85  void unserialize(CheckpointIn &cp) override;
86 
88 };
89 
92 
95 extern std::set<std::string> version_tags;
96 
97 void
99 {
100  paramOut(cp, "curTick", curTick());
102 }
103 
104 void
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 
163 {
164 }
165 
167 {
168 }
169 
170 void
172 {
174  serialize(cp);
175 }
176 
177 void
179 {
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
205 {
206  globals.unserializeSection(cp, "Globals");
207 
208  for (uint32_t i = 0; i < numMainEventQueues; ++i)
210 }
211 
213 {
214  assert(!path.empty());
215  DPRINTF(Checkpoint, "Popping: %s\n", path.top());
216  path.pop();
217 }
218 
219 void
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
232 {
233  DPRINTF(Checkpoint, "ScopedCheckpointSection::nameOut: %s\n",
235  cp << "\n[" << Serializable::currentSection() << "]\n";
236 }
237 
238 const std::string &
240 {
241  assert(!path.empty());
242 
243  return path.top();
244 }
245 
246 const char *CheckpointIn::baseFilename = "m5.cpt";
247 
249 
250 string
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
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 = getCptDir() + "/" + CheckpointIn::baseFilename;
272  if (!db->load(filename)) {
273  fatal("Can't load checkpoint file '%s'\n", filename);
274  }
275 }
276 
278 {
279  delete db;
280 }
290 bool
291 CheckpointIn::entryExists(const string &section, const string &entry)
292 {
293  return db->entryExists(section, entry);
294 }
305 bool
306 CheckpointIn::find(const string &section, const string &entry, string &value)
307 {
308  return db->find(section, entry, value);
309 }
320 bool
321 CheckpointIn::findObj(const string &section, const string &entry,
322  SimObject *&value)
323 {
324  string path;
325 
326  if (!db->find(section, entry, path))
327  return false;
328 
329  value = objNameResolver.resolveSimObject(path);
330  return true;
331 }
332 
333 bool
334 CheckpointIn::sectionExists(const string &section)
335 {
336  return db->sectionExists(section);
337 }
338 
339 void
340 objParamIn(CheckpointIn &cp, const string &name, SimObject * &param)
341 {
342  const string &section(Serializable::currentSection());
343  if (!cp.findObj(section, name, param)) {
344  fatal("Can't unserialize '%s:%s'\n", section, name);
345  }
346 }
347 
348 void
349 debug_serialize(const string &cpt_dir)
350 {
352 }
Serializable::ScopedCheckpointSection::nameOut
void nameOut(CheckpointOut &cp)
Definition: serialize.cc:231
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:183
SimObjectResolver
Base class to wrap object resolving functionality.
Definition: sim_object.hh:294
Globals::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: serialize.cc:98
Serializable::unserializeSection
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition: serialize.cc:178
warn
#define warn(...)
Definition: logging.hh:239
CheckpointIn::objNameResolver
SimObjectResolver & objNameResolver
Definition: serialize.hh:73
serialize.hh
CheckpointIn::CheckpointIn
CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver)
Definition: serialize.cc:268
sim_events.hh
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Serializable::ScopedCheckpointSection::~ScopedCheckpointSection
~ScopedCheckpointSection()
Definition: serialize.cc:212
globals
Globals globals
The one and only instance of the Globals class.
Definition: serialize.cc:91
SimObject::serializeAll
static void serializeAll(CheckpointOut &cp)
Serialize all SimObjects in the system.
Definition: sim_object.cc:132
Globals::Globals
Globals()
Definition: serialize.cc:81
Serializable
Basic support for object serialization.
Definition: serialize.hh:172
CheckpointIn::sectionExists
bool sectionExists(const std::string &section)
Definition: serialize.cc:334
SimObjectResolver::resolveSimObject
virtual SimObject * resolveSimObject(const std::string &name)=0
Find a SimObject given a full path name.
IniFile::find
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:214
Globals::unserializedCurTick
Tick unserializedCurTick
Definition: serialize.cc:87
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
IniFile
This class represents the contents of a ".ini" file.
Definition: inifile.hh:51
version_tags
std::set< std::string > version_tags
The version tags for this build of the simulator, to be stored in the Globals section during serializ...
serialize
void serialize(const ThreadContext &tc, CheckpointOut &cp)
Thread context serialization helpers.
Definition: thread_context.cc:142
CheckpointIn::findObj
bool findObj(const std::string &section, const std::string &entry, SimObject *&value)
Definition: serialize.cc:321
CheckpointIn::setDir
static std::string setDir(const std::string &base_name)
Set the current directory.
Definition: serialize.cc:251
CheckpointIn::find
bool find(const std::string &section, const std::string &entry, std::string &value)
Definition: serialize.cc:306
Serializable::serializeSection
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
Definition: serialize.cc:171
sim_exit.hh
IniFile::entryExists
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:231
output.hh
paramOut
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
Definition: types.cc:38
unserialize
void unserialize(ThreadContext &tc, CheckpointIn &cp)
Definition: thread_context.cc:183
debug_serialize
void debug_serialize(const string &cpt_dir)
Definition: serialize.cc:349
mainEventQueue
vector< EventQueue * > mainEventQueue
Array for main event queues.
Definition: eventq.cc:56
Globals
Container for serializing global variables (not associated with any serialized object).
Definition: serialize.cc:78
cp
Definition: cprintf.cc:40
Serializable::Serializable
Serializable()
Definition: serialize.cc:162
Serializable::ScopedCheckpointSection::pushName
void pushName(const char *name)
Definition: serialize.cc:220
sim_object.hh
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
Serializable::~Serializable
virtual ~Serializable()
Definition: serialize.cc:166
CheckpointIn::baseFilename
static const char * baseFilename
Definition: serialize.hh:135
Serializable::unserializeGlobals
static void unserializeGlobals(CheckpointIn &cp)
Definition: serialize.cc:204
CheckpointIn::db
IniFile * db
Definition: serialize.hh:71
ckptPrevCount
int ckptPrevCount
Definition: serialize.cc:71
inifile.hh
Serializable::ScopedCheckpointSection
Definition: serialize.hh:175
name
const std::string & name()
Definition: trace.cc:50
CheckpointIn::~CheckpointIn
~CheckpointIn()
Definition: serialize.cc:277
Globals::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: serialize.cc:105
objParamIn
void objParamIn(CheckpointIn &cp, const string &name, SimObject *&param)
Definition: serialize.cc:340
CheckpointIn::dir
static std::string dir()
Get the current checkout directory name.
Definition: serialize.cc:263
ckptCount
int ckptCount
Definition: serialize.cc:70
stat_control.hh
CheckpointIn::getCptDir
const std::string getCptDir()
Definition: serialize.hh:87
CheckpointIn::entryExists
bool entryExists(const std::string &section, const std::string &entry)
Definition: serialize.cc:291
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
SERIALIZE_CONTAINER
#define SERIALIZE_CONTAINER(member)
Definition: serialize.hh:848
paramIn
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
Definition: types.cc:69
ckptMaxCount
int ckptMaxCount
Definition: serialize.cc:69
Serializable::serializeAll
static void serializeAll(const std::string &cpt_dir)
Serializes all the SimObjects.
Definition: serialize.cc:185
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
arrayParamIn
void arrayParamIn(CheckpointIn &cp, const std::string &name, CircleBuf< T > &param)
Definition: circlebuf.hh:184
Serializable::currentSection
static const std::string & currentSection()
Gets the fully-qualified name of the active section.
Definition: serialize.cc:239
trace.hh
IniFile::load
bool load(std::istream &f)
Load parameter settings from given istream.
ArmISA::err
Bitfield< 6 > err
Definition: miscregs_types.hh:744
numMainEventQueues
uint32_t numMainEventQueues
Current number of allocated main event queues.
Definition: eventq.cc:55
CheckpointIn
Definition: serialize.hh:67
IniFile::sectionExists
bool sectionExists(const std::string &section) const
Determine whether the named section exists in the .ini file.
Definition: inifile.cc:242
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
CheckpointIn::currentDirectory
static std::string currentDirectory
Definition: serialize.hh:108
Serializable::path
static std::stack< std::string > path
Definition: serialize.hh:319
curTick
Tick curTick()
The current simulated tick.
Definition: core.hh:45
eventq.hh
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17