gem5  v22.1.0.0
serialize.cc
Go to the documentation of this file.
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 <cassert>
50 #include <cerrno>
51 
52 #include "base/trace.hh"
53 #include "debug/Checkpoint.hh"
54 
55 namespace gem5
56 {
57 
58 int ckptMaxCount = 0;
59 int ckptCount = 0;
60 int ckptPrevCount = -1;
61 std::stack<std::string> Serializable::path;
62 
64 
66 {
67 }
68 
70 {
71 }
72 
73 void
75 {
77  serialize(cp);
78 }
79 
80 void
82 {
84  unserialize(cp);
85 }
86 
87 void
88 Serializable::generateCheckpointOut(const std::string &cpt_dir,
89  std::ofstream &outstream)
90 {
91  std::string dir = CheckpointIn::setDir(cpt_dir);
92  if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
93  fatal("couldn't mkdir %s\n", dir);
94 
95  std::string cpt_file = dir + CheckpointIn::baseFilename;
96  outstream = std::ofstream(cpt_file.c_str());
97  time_t t = time(NULL);
98  if (!outstream)
99  fatal("Unable to open file %s for writing\n", cpt_file.c_str());
100  outstream << "## checkpoint generated: " << ctime(&t);
101 }
102 
104 {
105  assert(!path.empty());
106  DPRINTF(Checkpoint, "Popping: %s\n", path.top());
107  path.pop();
108 }
109 
110 void
112 {
113  if (path.empty()) {
114  path.push(obj_name);
115  } else {
116  path.push(csprintf("%s.%s", path.top(), obj_name));
117  }
118  DPRINTF(Checkpoint, "ScopedCheckpointSection::pushName: %s\n", obj_name);
119 }
120 
121 void
123 {
124  DPRINTF(Checkpoint, "ScopedCheckpointSection::nameOut: %s\n",
126  cp << "\n[" << Serializable::currentSection() << "]\n";
127 }
128 
129 const std::string &
131 {
132  assert(!path.empty());
133 
134  return path.top();
135 }
136 
137 const char *CheckpointIn::baseFilename = "m5.cpt";
138 
140 
141 std::string
142 CheckpointIn::setDir(const std::string &name)
143 {
144  // use csprintf to insert curTick() into directory name if it
145  // appears to have a format placeholder in it.
146  currentDirectory = (name.find("%") != std::string::npos) ?
147  csprintf(name, curTick()) : name;
148  if (currentDirectory[currentDirectory.size() - 1] != '/')
149  currentDirectory += "/";
150  return currentDirectory;
151 }
152 
153 std::string
155 {
156  return currentDirectory;
157 }
158 
159 CheckpointIn::CheckpointIn(const std::string &cpt_dir)
160  : db(), _cptDir(setDir(cpt_dir))
161 {
162  std::string filename = getCptDir() + "/" + CheckpointIn::baseFilename;
163  if (!db.load(filename)) {
164  fatal("Can't load checkpoint file '%s'\n", filename);
165  }
166 }
167 
177 bool
178 CheckpointIn::entryExists(const std::string &section, const std::string &entry)
179 {
180  return db.entryExists(section, entry);
181 }
192 bool
193 CheckpointIn::find(const std::string &section, const std::string &entry,
194  std::string &value)
195 {
196  return db.find(section, entry, value);
197 }
198 
199 bool
200 CheckpointIn::sectionExists(const std::string &section)
201 {
202  return db.sectionExists(section);
203 }
204 
205 void
206 CheckpointIn::visitSection(const std::string &section,
208 {
209  db.visitSection(section, cb);
210 }
211 
212 } // namespace gem5
#define DPRINTF(x,...)
Definition: trace.hh:186
static std::string currentDirectory
Definition: serialize.hh:105
CheckpointIn(const std::string &cpt_dir)
Definition: serialize.cc:159
static const char * baseFilename
Definition: serialize.hh:132
std::function< void(const std::string &, const std::string &)> VisitSectionCallback
Visitor callback that receives key/value pairs.
Definition: inifile.hh:216
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:215
void visitSection(const std::string &sectionName, VisitSectionCallback cb)
Iterate over key/value pairs of the given section.
Definition: inifile.cc:364
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:232
bool load(std::istream &f)
Load parameter settings from given istream.
Definition: inifile.cc:179
bool sectionExists(const std::string &section) const
Determine whether the named section exists in the .ini file.
Definition: inifile.cc:244
static std::stack< std::string > path
Definition: serialize.hh:315
virtual ~Serializable()
Definition: serialize.cc:69
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:190
bool entryExists(const std::string &section, const std::string &entry)
Definition: serialize.cc:178
virtual void unserialize(CheckpointIn &cp)=0
Unserialize an object.
bool find(const std::string &section, const std::string &entry, std::string &value)
Definition: serialize.cc:193
static std::string setDir(const std::string &base_name)
Set the current directory.
Definition: serialize.cc:142
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
Definition: serialize.cc:74
static const std::string & currentSection()
Gets the fully-qualified name of the active section.
Definition: serialize.cc:130
virtual void serialize(CheckpointOut &cp) const =0
Serialize an object.
const std::string getCptDir()
Definition: serialize.hh:85
static std::string dir()
Get the current checkout directory name.
Definition: serialize.cc:154
bool sectionExists(const std::string &section)
Definition: serialize.cc:200
void visitSection(const std::string &section, IniFile::VisitSectionCallback cb)
Definition: serialize.cc:206
static void generateCheckpointOut(const std::string &cpt_dir, std::ofstream &outstream)
Generate a checkpoint file so that the serialization can be routed to it.
Definition: serialize.cc:88
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition: serialize.cc:81
Bitfield< 51 > t
Definition: pagetable.hh:56
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
std::ostream CheckpointOut
Definition: serialize.hh:66
int ckptCount
Definition: serialize.cc:59
int ckptPrevCount
Definition: serialize.cc:60
int ckptMaxCount
Definition: serialize.cc:58
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:161
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:40 for gem5 by doxygen 1.9.1