serialize.hh (11075:f959b7f89d4d) serialize.hh (11076:463a4b0f0dda)
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 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 * Erik Hallnor
42 * Steve Reinhardt
43 * Andreas Sandberg
44 */
45
46/* @file
47 * Serialization Interface Declarations
48 */
49
50#ifndef __SERIALIZE_HH__
51#define __SERIALIZE_HH__
52
53
54#include <iostream>
55#include <list>
56#include <map>
57#include <stack>
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 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 * Erik Hallnor
42 * Steve Reinhardt
43 * Andreas Sandberg
44 */
45
46/* @file
47 * Serialization Interface Declarations
48 */
49
50#ifndef __SERIALIZE_HH__
51#define __SERIALIZE_HH__
52
53
54#include <iostream>
55#include <list>
56#include <map>
57#include <stack>
58#include <set>
58#include <vector>
59
60#include "base/bitunion.hh"
61#include "base/types.hh"
62
63class IniFile;
64class Serializable;
65class CheckpointIn;
66class SimObject;
67class SimObjectResolver;
68class EventQueue;
69
70typedef std::ostream CheckpointOut;
71
72
73/** The current version of the checkpoint format.
74 * This should be incremented by 1 and only 1 for every new version, where a new
75 * version is defined as a checkpoint created before this version won't work on
76 * the current version until the checkpoint format is updated. Adding a new
77 * SimObject shouldn't cause the version number to increase, only changes to
78 * existing objects such as serializing/unserializing more state, changing sizes
79 * of serialized arrays, etc. */
80static const uint64_t gem5CheckpointVersion = 0x000000000000000f;
81
82template <class T>
83void paramOut(CheckpointOut &cp, const std::string &name, const T &param);
84
85template <typename DataType, typename BitUnion>
86void paramOut(CheckpointOut &cp, const std::string &name,
87 const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
88{
89 paramOut(cp, name, p.__data);
90}
91
92template <class T>
93void paramIn(CheckpointIn &cp, const std::string &name, T &param);
94
95template <typename DataType, typename BitUnion>
96void paramIn(CheckpointIn &cp, const std::string &name,
97 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
98{
99 paramIn(cp, name, p.__data);
100}
101
102template <class T>
103bool optParamIn(CheckpointIn &cp, const std::string &name, T &param,
104 bool warn = true);
105
106template <typename DataType, typename BitUnion>
107bool optParamIn(CheckpointIn &cp, const std::string &name,
108 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p,
109 bool warn = true)
110{
111 return optParamIn(cp, name, p.__data, warn);
112}
113
114template <class T>
115void arrayParamOut(CheckpointOut &cp, const std::string &name,
116 const T *param, unsigned size);
117
118template <class T>
119void arrayParamOut(CheckpointOut &cp, const std::string &name,
120 const std::vector<T> &param);
121
122template <class T>
123void arrayParamOut(CheckpointOut &cp, const std::string &name,
124 const std::list<T> &param);
125
126template <class T>
59#include <vector>
60
61#include "base/bitunion.hh"
62#include "base/types.hh"
63
64class IniFile;
65class Serializable;
66class CheckpointIn;
67class SimObject;
68class SimObjectResolver;
69class EventQueue;
70
71typedef std::ostream CheckpointOut;
72
73
74/** The current version of the checkpoint format.
75 * This should be incremented by 1 and only 1 for every new version, where a new
76 * version is defined as a checkpoint created before this version won't work on
77 * the current version until the checkpoint format is updated. Adding a new
78 * SimObject shouldn't cause the version number to increase, only changes to
79 * existing objects such as serializing/unserializing more state, changing sizes
80 * of serialized arrays, etc. */
81static const uint64_t gem5CheckpointVersion = 0x000000000000000f;
82
83template <class T>
84void paramOut(CheckpointOut &cp, const std::string &name, const T &param);
85
86template <typename DataType, typename BitUnion>
87void paramOut(CheckpointOut &cp, const std::string &name,
88 const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
89{
90 paramOut(cp, name, p.__data);
91}
92
93template <class T>
94void paramIn(CheckpointIn &cp, const std::string &name, T &param);
95
96template <typename DataType, typename BitUnion>
97void paramIn(CheckpointIn &cp, const std::string &name,
98 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
99{
100 paramIn(cp, name, p.__data);
101}
102
103template <class T>
104bool optParamIn(CheckpointIn &cp, const std::string &name, T &param,
105 bool warn = true);
106
107template <typename DataType, typename BitUnion>
108bool optParamIn(CheckpointIn &cp, const std::string &name,
109 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p,
110 bool warn = true)
111{
112 return optParamIn(cp, name, p.__data, warn);
113}
114
115template <class T>
116void arrayParamOut(CheckpointOut &cp, const std::string &name,
117 const T *param, unsigned size);
118
119template <class T>
120void arrayParamOut(CheckpointOut &cp, const std::string &name,
121 const std::vector<T> &param);
122
123template <class T>
124void arrayParamOut(CheckpointOut &cp, const std::string &name,
125 const std::list<T> &param);
126
127template <class T>
128void arrayParamOut(CheckpointOut &cp, const std::string &name,
129 const std::set<T> &param);
130
131template <class T>
127void arrayParamIn(CheckpointIn &cp, const std::string &name,
128 T *param, unsigned size);
129
130template <class T>
131void arrayParamIn(CheckpointIn &cp, const std::string &name,
132 std::vector<T> &param);
133
134template <class T>
135void arrayParamIn(CheckpointIn &cp, const std::string &name,
136 std::list<T> &param);
137
132void arrayParamIn(CheckpointIn &cp, const std::string &name,
133 T *param, unsigned size);
134
135template <class T>
136void arrayParamIn(CheckpointIn &cp, const std::string &name,
137 std::vector<T> &param);
138
139template <class T>
140void arrayParamIn(CheckpointIn &cp, const std::string &name,
141 std::list<T> &param);
142
143template <class T>
144void arrayParamIn(CheckpointIn &cp, const std::string &name,
145 std::set<T> &param);
146
138void
139objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
140
141//
142// These macros are streamlined to use in serialize/unserialize
143// functions. It's assumed that serialize() has a parameter 'os' for
144// the ostream, and unserialize() has parameters 'cp' and 'section'.
145#define SERIALIZE_SCALAR(scalar) paramOut(cp, #scalar, scalar)
146
147#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar)
148#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar)
149
150// ENUMs are like SCALARs, but we cast them to ints on the way out
151#define SERIALIZE_ENUM(scalar) paramOut(cp, #scalar, (int)scalar)
152
153#define UNSERIALIZE_ENUM(scalar) \
154 do { \
155 int tmp; \
156 paramIn(cp, #scalar, tmp); \
157 scalar = static_cast<decltype(scalar)>(tmp); \
158 } while (0)
159
160#define SERIALIZE_ARRAY(member, size) \
161 arrayParamOut(cp, #member, member, size)
162
163#define UNSERIALIZE_ARRAY(member, size) \
164 arrayParamIn(cp, #member, member, size)
165
166#define SERIALIZE_CONTAINER(member) \
167 arrayParamOut(cp, #member, member)
168
169#define UNSERIALIZE_CONTAINER(member) \
170 arrayParamIn(cp, #member, member)
171
172#define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
173
174#define UNSERIALIZE_EVENT(event) \
175 do { \
176 event.unserializeSection(cp, #event); \
177 eventQueue()->checkpointReschedule(&event); \
178 } while(0)
179
180#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
181#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
182
183#define SERIALIZE_OBJPTR(objptr) paramOut(cp, #objptr, (objptr)->name())
184
185#define UNSERIALIZE_OBJPTR(objptr) \
186 do { \
187 SimObject *sptr; \
188 objParamIn(cp, #objptr, sptr); \
189 objptr = dynamic_cast<decltype(objptr)>(sptr); \
190 } while (0)
191
192/**
193 * Basic support for object serialization.
194 *
195 * Objects that support serialization should derive from this
196 * class. Such objects can largely be divided into two categories: 1)
197 * True SimObjects (deriving from SimObject), and 2) child objects
198 * (non-SimObjects).
199 *
200 * SimObjects are serialized automatically into their own sections
201 * automatically by the SimObject base class (see
202 * SimObject::serializeAll().
203 *
204 * SimObjects can contain other serializable objects that are not
205 * SimObjects. Much like normal serialized members are not serialized
206 * automatically, these objects will not be serialized automatically
207 * and it is expected that the objects owning such serializable
208 * objects call the required serialization/unserialization methods on
209 * child objects. The preferred method to serialize a child object is
210 * to call serializeSection() on the child, which serializes the
211 * object into a new subsection in the current section. Another option
212 * is to call serialize() directly, which serializes the object into
213 * the current section. The latter is not recommended as it can lead
214 * to naming clashes between objects.
215 *
216 * @note Many objects that support serialization need to be put in a
217 * consistent state when serialization takes place. We refer to the
218 * action of forcing an object into a consistent state as
219 * 'draining'. Objects that need draining inherit from Drainable. See
220 * Drainable for more information.
221 */
222class Serializable
223{
224 protected:
225 /**
226 * Scoped checkpoint section helper class
227 *
228 * This helper class creates a section within a checkpoint without
229 * the need for a separate serializeable object. It is mainly used
230 * within the Serializable class when serializing or unserializing
231 * section (see serializeSection() and unserializeSection()). It
232 * can also be used to maintain backwards compatibility in
233 * existing code that serializes structs that are not inheriting
234 * from Serializable into subsections.
235 *
236 * When the class is instantiated, it appends a name to the active
237 * path in a checkpoint. The old path is later restored when the
238 * instance is destroyed. For example, serializeSection() could be
239 * implemented by instantiating a ScopedCheckpointSection and then
240 * calling serialize() on an object.
241 */
242 class ScopedCheckpointSection {
243 public:
244 template<class CP>
245 ScopedCheckpointSection(CP &cp, const char *name) {
246 pushName(name);
247 nameOut(cp);
248 }
249
250 template<class CP>
251 ScopedCheckpointSection(CP &cp, const std::string &name) {
252 pushName(name.c_str());
253 nameOut(cp);
254 }
255
256 ~ScopedCheckpointSection();
257
258 ScopedCheckpointSection() = delete;
259 ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
260 ScopedCheckpointSection &operator=(
261 const ScopedCheckpointSection &) = delete;
262 ScopedCheckpointSection &operator=(
263 ScopedCheckpointSection &&) = delete;
264
265 private:
266 void pushName(const char *name);
267 void nameOut(CheckpointOut &cp);
268 void nameOut(CheckpointIn &cp) {};
269 };
270
271 public:
272 Serializable();
273 virtual ~Serializable();
274
275 /**
276 * Serialize an object
277 *
278 * Output an object's state into the current checkpoint section.
279 *
280 * @param cp Checkpoint state
281 */
282 virtual void serialize(CheckpointOut &cp) const = 0;
283
284 /**
285 * Unserialize an object
286 *
287 * Read an object's state from the current checkpoint section.
288 *
289 * @param cp Checkpoint state
290 */
291 virtual void unserialize(CheckpointIn &cp) = 0;
292
293 /**
294 * Serialize an object into a new section
295 *
296 * This method creates a new section in a checkpoint and calls
297 * serialize() to serialize the current object into that
298 * section. The name of the section is appended to the current
299 * checkpoint path.
300 *
301 * @param cp Checkpoint state
302 * @param name Name to append to the active path
303 */
304 void serializeSection(CheckpointOut &cp, const char *name) const;
305
306 void serializeSection(CheckpointOut &cp, const std::string &name) const {
307 serializeSection(cp, name.c_str());
308 }
309
310 /**
311 * Unserialize an a child object
312 *
313 * This method loads a child object from a checkpoint. The object
314 * name is appended to the active path to form a fully qualified
315 * section name and unserialize() is called.
316 *
317 * @param cp Checkpoint state
318 * @param name Name to append to the active path
319 */
320 void unserializeSection(CheckpointIn &cp, const char *name);
321
322 void unserializeSection(CheckpointIn &cp, const std::string &name) {
323 unserializeSection(cp, name.c_str());
324 }
325
326 /**
327 * @{
328 * @name Legacy interface
329 *
330 * Interface for objects that insist on changing their state when
331 * serializing. Such state change should be done in drain(),
332 * memWriteback(), or memInvalidate() and not in the serialization
333 * method. In general, if state changes occur in serialize, it
334 * complicates testing since it breaks assumptions about draining
335 * and serialization. It potentially also makes components more
336 * fragile since they there are no ordering guarantees when
337 * serializing SimObjects.
338 *
339 * @warn This interface is considered deprecated and should never
340 * be used.
341 */
342
343 virtual void serializeOld(CheckpointOut &cp) {
344 serialize(cp);
345 }
346 void serializeSectionOld(CheckpointOut &cp, const char *name);
347 void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
348 serializeSectionOld(cp, name.c_str());
349 }
350 /** @} */
351
352 /** Get the fully-qualified name of the active section */
353 static const std::string &currentSection();
354
355 static int ckptCount;
356 static int ckptMaxCount;
357 static int ckptPrevCount;
358 static void serializeAll(const std::string &cpt_dir);
359 static void unserializeGlobals(CheckpointIn &cp);
360
361 private:
362 static std::stack<std::string> path;
363};
364
365void debug_serialize(const std::string &cpt_dir);
366
367
368class CheckpointIn
369{
370 private:
371
372 IniFile *db;
373
374 SimObjectResolver &objNameResolver;
375
376 public:
377 CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
378 ~CheckpointIn();
379
380 const std::string cptDir;
381
382 bool find(const std::string &section, const std::string &entry,
383 std::string &value);
384
385 bool findObj(const std::string &section, const std::string &entry,
386 SimObject *&value);
387
388 bool sectionExists(const std::string &section);
389
390 // The following static functions have to do with checkpoint
391 // creation rather than restoration. This class makes a handy
392 // namespace for them though. Currently no Checkpoint object is
393 // created on serialization (only unserialization) so we track the
394 // directory name as a global. It would be nice to change this
395 // someday
396
397 private:
398 // current directory we're serializing into.
399 static std::string currentDirectory;
400
401 public:
402 // Set the current directory. This function takes care of
403 // inserting curTick() if there's a '%d' in the argument, and
404 // appends a '/' if necessary. The final name is returned.
405 static std::string setDir(const std::string &base_name);
406
407 // Export current checkpoint directory name so other objects can
408 // derive filenames from it (e.g., memory). The return value is
409 // guaranteed to end in '/' so filenames can be directly appended.
410 // This function is only valid while a checkpoint is being created.
411 static std::string dir();
412
413 // Filename for base checkpoint file within directory.
414 static const char *baseFilename;
415};
416
417#endif // __SERIALIZE_HH__
147void
148objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
149
150//
151// These macros are streamlined to use in serialize/unserialize
152// functions. It's assumed that serialize() has a parameter 'os' for
153// the ostream, and unserialize() has parameters 'cp' and 'section'.
154#define SERIALIZE_SCALAR(scalar) paramOut(cp, #scalar, scalar)
155
156#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar)
157#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar)
158
159// ENUMs are like SCALARs, but we cast them to ints on the way out
160#define SERIALIZE_ENUM(scalar) paramOut(cp, #scalar, (int)scalar)
161
162#define UNSERIALIZE_ENUM(scalar) \
163 do { \
164 int tmp; \
165 paramIn(cp, #scalar, tmp); \
166 scalar = static_cast<decltype(scalar)>(tmp); \
167 } while (0)
168
169#define SERIALIZE_ARRAY(member, size) \
170 arrayParamOut(cp, #member, member, size)
171
172#define UNSERIALIZE_ARRAY(member, size) \
173 arrayParamIn(cp, #member, member, size)
174
175#define SERIALIZE_CONTAINER(member) \
176 arrayParamOut(cp, #member, member)
177
178#define UNSERIALIZE_CONTAINER(member) \
179 arrayParamIn(cp, #member, member)
180
181#define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
182
183#define UNSERIALIZE_EVENT(event) \
184 do { \
185 event.unserializeSection(cp, #event); \
186 eventQueue()->checkpointReschedule(&event); \
187 } while(0)
188
189#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
190#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
191
192#define SERIALIZE_OBJPTR(objptr) paramOut(cp, #objptr, (objptr)->name())
193
194#define UNSERIALIZE_OBJPTR(objptr) \
195 do { \
196 SimObject *sptr; \
197 objParamIn(cp, #objptr, sptr); \
198 objptr = dynamic_cast<decltype(objptr)>(sptr); \
199 } while (0)
200
201/**
202 * Basic support for object serialization.
203 *
204 * Objects that support serialization should derive from this
205 * class. Such objects can largely be divided into two categories: 1)
206 * True SimObjects (deriving from SimObject), and 2) child objects
207 * (non-SimObjects).
208 *
209 * SimObjects are serialized automatically into their own sections
210 * automatically by the SimObject base class (see
211 * SimObject::serializeAll().
212 *
213 * SimObjects can contain other serializable objects that are not
214 * SimObjects. Much like normal serialized members are not serialized
215 * automatically, these objects will not be serialized automatically
216 * and it is expected that the objects owning such serializable
217 * objects call the required serialization/unserialization methods on
218 * child objects. The preferred method to serialize a child object is
219 * to call serializeSection() on the child, which serializes the
220 * object into a new subsection in the current section. Another option
221 * is to call serialize() directly, which serializes the object into
222 * the current section. The latter is not recommended as it can lead
223 * to naming clashes between objects.
224 *
225 * @note Many objects that support serialization need to be put in a
226 * consistent state when serialization takes place. We refer to the
227 * action of forcing an object into a consistent state as
228 * 'draining'. Objects that need draining inherit from Drainable. See
229 * Drainable for more information.
230 */
231class Serializable
232{
233 protected:
234 /**
235 * Scoped checkpoint section helper class
236 *
237 * This helper class creates a section within a checkpoint without
238 * the need for a separate serializeable object. It is mainly used
239 * within the Serializable class when serializing or unserializing
240 * section (see serializeSection() and unserializeSection()). It
241 * can also be used to maintain backwards compatibility in
242 * existing code that serializes structs that are not inheriting
243 * from Serializable into subsections.
244 *
245 * When the class is instantiated, it appends a name to the active
246 * path in a checkpoint. The old path is later restored when the
247 * instance is destroyed. For example, serializeSection() could be
248 * implemented by instantiating a ScopedCheckpointSection and then
249 * calling serialize() on an object.
250 */
251 class ScopedCheckpointSection {
252 public:
253 template<class CP>
254 ScopedCheckpointSection(CP &cp, const char *name) {
255 pushName(name);
256 nameOut(cp);
257 }
258
259 template<class CP>
260 ScopedCheckpointSection(CP &cp, const std::string &name) {
261 pushName(name.c_str());
262 nameOut(cp);
263 }
264
265 ~ScopedCheckpointSection();
266
267 ScopedCheckpointSection() = delete;
268 ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
269 ScopedCheckpointSection &operator=(
270 const ScopedCheckpointSection &) = delete;
271 ScopedCheckpointSection &operator=(
272 ScopedCheckpointSection &&) = delete;
273
274 private:
275 void pushName(const char *name);
276 void nameOut(CheckpointOut &cp);
277 void nameOut(CheckpointIn &cp) {};
278 };
279
280 public:
281 Serializable();
282 virtual ~Serializable();
283
284 /**
285 * Serialize an object
286 *
287 * Output an object's state into the current checkpoint section.
288 *
289 * @param cp Checkpoint state
290 */
291 virtual void serialize(CheckpointOut &cp) const = 0;
292
293 /**
294 * Unserialize an object
295 *
296 * Read an object's state from the current checkpoint section.
297 *
298 * @param cp Checkpoint state
299 */
300 virtual void unserialize(CheckpointIn &cp) = 0;
301
302 /**
303 * Serialize an object into a new section
304 *
305 * This method creates a new section in a checkpoint and calls
306 * serialize() to serialize the current object into that
307 * section. The name of the section is appended to the current
308 * checkpoint path.
309 *
310 * @param cp Checkpoint state
311 * @param name Name to append to the active path
312 */
313 void serializeSection(CheckpointOut &cp, const char *name) const;
314
315 void serializeSection(CheckpointOut &cp, const std::string &name) const {
316 serializeSection(cp, name.c_str());
317 }
318
319 /**
320 * Unserialize an a child object
321 *
322 * This method loads a child object from a checkpoint. The object
323 * name is appended to the active path to form a fully qualified
324 * section name and unserialize() is called.
325 *
326 * @param cp Checkpoint state
327 * @param name Name to append to the active path
328 */
329 void unserializeSection(CheckpointIn &cp, const char *name);
330
331 void unserializeSection(CheckpointIn &cp, const std::string &name) {
332 unserializeSection(cp, name.c_str());
333 }
334
335 /**
336 * @{
337 * @name Legacy interface
338 *
339 * Interface for objects that insist on changing their state when
340 * serializing. Such state change should be done in drain(),
341 * memWriteback(), or memInvalidate() and not in the serialization
342 * method. In general, if state changes occur in serialize, it
343 * complicates testing since it breaks assumptions about draining
344 * and serialization. It potentially also makes components more
345 * fragile since they there are no ordering guarantees when
346 * serializing SimObjects.
347 *
348 * @warn This interface is considered deprecated and should never
349 * be used.
350 */
351
352 virtual void serializeOld(CheckpointOut &cp) {
353 serialize(cp);
354 }
355 void serializeSectionOld(CheckpointOut &cp, const char *name);
356 void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
357 serializeSectionOld(cp, name.c_str());
358 }
359 /** @} */
360
361 /** Get the fully-qualified name of the active section */
362 static const std::string &currentSection();
363
364 static int ckptCount;
365 static int ckptMaxCount;
366 static int ckptPrevCount;
367 static void serializeAll(const std::string &cpt_dir);
368 static void unserializeGlobals(CheckpointIn &cp);
369
370 private:
371 static std::stack<std::string> path;
372};
373
374void debug_serialize(const std::string &cpt_dir);
375
376
377class CheckpointIn
378{
379 private:
380
381 IniFile *db;
382
383 SimObjectResolver &objNameResolver;
384
385 public:
386 CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
387 ~CheckpointIn();
388
389 const std::string cptDir;
390
391 bool find(const std::string &section, const std::string &entry,
392 std::string &value);
393
394 bool findObj(const std::string &section, const std::string &entry,
395 SimObject *&value);
396
397 bool sectionExists(const std::string &section);
398
399 // The following static functions have to do with checkpoint
400 // creation rather than restoration. This class makes a handy
401 // namespace for them though. Currently no Checkpoint object is
402 // created on serialization (only unserialization) so we track the
403 // directory name as a global. It would be nice to change this
404 // someday
405
406 private:
407 // current directory we're serializing into.
408 static std::string currentDirectory;
409
410 public:
411 // Set the current directory. This function takes care of
412 // inserting curTick() if there's a '%d' in the argument, and
413 // appends a '/' if necessary. The final name is returned.
414 static std::string setDir(const std::string &base_name);
415
416 // Export current checkpoint directory name so other objects can
417 // derive filenames from it (e.g., memory). The return value is
418 // guaranteed to end in '/' so filenames can be directly appended.
419 // This function is only valid while a checkpoint is being created.
420 static std::string dir();
421
422 // Filename for base checkpoint file within directory.
423 static const char *baseFilename;
424};
425
426#endif // __SERIALIZE_HH__