gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
55namespace gem5
56{
57
59int ckptCount = 0;
61std::stack<std::string> Serializable::path;
62
64
68
72
73void
79
80void
86
87void
88Serializable::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
110void
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
121void
123{
124 DPRINTF(Checkpoint, "ScopedCheckpointSection::nameOut: %s\n",
126 cp << "\n[" << Serializable::currentSection() << "]\n";
127}
128
129const std::string &
131{
132 assert(!path.empty());
133
134 return path.top();
135}
136
137const char *CheckpointIn::baseFilename = "m5.cpt";
138
140
141std::string
142CheckpointIn::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) ?
148 auto isEmptyPath = currentDirectory.empty();
149 auto endsWithSlash = !isEmptyPath && currentDirectory.back() == '/';
150 if (!endsWithSlash) {
151 currentDirectory += "/";
152 }
153 return currentDirectory;
154}
155
156std::string
158{
159 return currentDirectory;
160}
161
162CheckpointIn::CheckpointIn(const std::string &cpt_dir)
163 : db(), _cptDir(setDir(cpt_dir))
164{
165 std::string filename = getCptDir() + "/" + CheckpointIn::baseFilename;
166 if (!db.load(filename)) {
167 fatal("Can't load checkpoint file '%s'\n", filename);
168 }
169}
170
180bool
181CheckpointIn::entryExists(const std::string &section, const std::string &entry)
182{
183 return db.entryExists(section, entry);
184}
195bool
196CheckpointIn::find(const std::string &section, const std::string &entry,
197 std::string &value)
198{
199 return db.find(section, entry, value);
200}
201
202bool
203CheckpointIn::sectionExists(const std::string &section)
204{
205 return db.sectionExists(section);
206}
207
208void
209CheckpointIn::visitSection(const std::string &section,
211{
212 db.visitSection(section, cb);
213}
214
215} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
static std::string currentDirectory
Definition serialize.hh:105
CheckpointIn(const std::string &cpt_dir)
Definition serialize.cc:162
static const char * baseFilename
Definition serialize.hh:132
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:207
void visitSection(const std::string &sectionName, VisitSectionCallback cb)
Iterate over key/value pairs of the given section.
Definition inifile.cc:351
std::function< void( const std::string &, const std::string &)> VisitSectionCallback
Visitor callback that receives key/value pairs.
Definition inifile.hh:214
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:224
bool load(std::istream &f)
Load parameter settings from given istream.
Definition inifile.cc:171
bool sectionExists(const std::string &section) const
Determine whether the named section exists in the .ini file.
Definition inifile.cc:236
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:200
bool entryExists(const std::string &section, const std::string &entry)
Definition serialize.cc:181
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:196
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:157
bool sectionExists(const std::string &section)
Definition serialize.cc:203
void visitSection(const std::string &section, IniFile::VisitSectionCallback cb)
Definition serialize.cc:209
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< 5 > t
Definition misc_types.hh:71
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
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:48

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0