serialize.hh (10903:022e5d110a22) serialize.hh (10905:a6ca6831e775)
1/*
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 *
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright

--- 13 unchanged lines hidden (view full) ---

23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 * Erik Hallnor
30 * Steve Reinhardt
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

--- 13 unchanged lines hidden (view full) ---

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
31 */
32
33/* @file
34 * Serialization Interface Declarations
35 */
36
37#ifndef __SERIALIZE_HH__
38#define __SERIALIZE_HH__
39
40
41#include <iostream>
42#include <list>
43#include <map>
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>
44#include <vector>
45
46#include "base/bitunion.hh"
47#include "base/types.hh"
48
49class IniFile;
50class Serializable;
58#include <vector>
59
60#include "base/bitunion.hh"
61#include "base/types.hh"
62
63class IniFile;
64class Serializable;
51class Checkpoint;
65class CheckpointIn;
52class SimObject;
53class EventQueue;
54
66class SimObject;
67class EventQueue;
68
69typedef std::ostream CheckpointOut;
70
71
55/** The current version of the checkpoint format.
56 * This should be incremented by 1 and only 1 for every new version, where a new
57 * version is defined as a checkpoint created before this version won't work on
58 * the current version until the checkpoint format is updated. Adding a new
59 * SimObject shouldn't cause the version number to increase, only changes to
60 * existing objects such as serializing/unserializing more state, changing sizes
61 * of serialized arrays, etc. */
62static const uint64_t gem5CheckpointVersion = 0x000000000000000e;
63
64template <class T>
72/** The current version of the checkpoint format.
73 * This should be incremented by 1 and only 1 for every new version, where a new
74 * version is defined as a checkpoint created before this version won't work on
75 * the current version until the checkpoint format is updated. Adding a new
76 * SimObject shouldn't cause the version number to increase, only changes to
77 * existing objects such as serializing/unserializing more state, changing sizes
78 * of serialized arrays, etc. */
79static const uint64_t gem5CheckpointVersion = 0x000000000000000e;
80
81template <class T>
65void paramOut(std::ostream &os, const std::string &name, const T &param);
82void paramOut(CheckpointOut &cp, const std::string &name, const T &param);
66
67template <typename DataType, typename BitUnion>
83
84template <typename DataType, typename BitUnion>
68void paramOut(std::ostream &os, const std::string &name,
85void paramOut(CheckpointOut &cp, const std::string &name,
69 const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
70{
86 const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
87{
71 paramOut(os, name, p.__data);
88 paramOut(cp, name, p.__data);
72}
73
74template <class T>
89}
90
91template <class T>
75void paramIn(Checkpoint *cp, const std::string &section,
76 const std::string &name, T &param);
92void paramIn(CheckpointIn &cp, const std::string &name, T &param);
77
78template <typename DataType, typename BitUnion>
93
94template <typename DataType, typename BitUnion>
79void paramIn(Checkpoint *cp, const std::string &section,
80 const std::string &name,
95void paramIn(CheckpointIn &cp, const std::string &name,
81 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
82{
96 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
97{
83 paramIn(cp, section, name, p.__data);
98 paramIn(cp, name, p.__data);
84}
85
86template <class T>
99}
100
101template <class T>
87bool optParamIn(Checkpoint *cp, const std::string &section,
88 const std::string &name, T &param);
102bool optParamIn(CheckpointIn &cp, const std::string &name, T &param);
89
90template <typename DataType, typename BitUnion>
103
104template <typename DataType, typename BitUnion>
91bool optParamIn(Checkpoint *cp, const std::string &section,
92 const std::string &name,
105bool optParamIn(CheckpointIn &cp, const std::string &name,
93 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
94{
106 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
107{
95 return optParamIn(cp, section, name, p.__data);
108 return optParamIn(cp, name, p.__data);
96}
97
98template <class T>
109}
110
111template <class T>
99void arrayParamOut(std::ostream &os, const std::string &name,
112void arrayParamOut(CheckpointOut &cp, const std::string &name,
100 const T *param, unsigned size);
101
102template <class T>
113 const T *param, unsigned size);
114
115template <class T>
103void arrayParamOut(std::ostream &os, const std::string &name,
116void arrayParamOut(CheckpointOut &cp, const std::string &name,
104 const std::vector<T> &param);
105
106template <class T>
117 const std::vector<T> &param);
118
119template <class T>
107void arrayParamOut(std::ostream &os, const std::string &name,
120void arrayParamOut(CheckpointOut &cp, const std::string &name,
108 const std::list<T> &param);
109
110template <class T>
121 const std::list<T> &param);
122
123template <class T>
111void arrayParamIn(Checkpoint *cp, const std::string &section,
112 const std::string &name, T *param, unsigned size);
124void arrayParamIn(CheckpointIn &cp, const std::string &name,
125 T *param, unsigned size);
113
114template <class T>
126
127template <class T>
115void arrayParamIn(Checkpoint *cp, const std::string &section,
116 const std::string &name, std::vector<T> &param);
128void arrayParamIn(CheckpointIn &cp, const std::string &name,
129 std::vector ¶m);
117
118template <class T>
130
131template <class T>
119void arrayParamIn(Checkpoint *cp, const std::string &section,
120 const std::string &name, std::list<T> &param);
132void arrayParamIn(CheckpointIn &cp, const std::string &name,
133 std::list ¶m);
121
122void
134
135void
123objParamIn(Checkpoint *cp, const std::string &section,
124 const std::string &name, SimObject * &param);
136objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
125
126template <typename T>
127void fromInt(T &t, int i)
128{
129 t = (T)i;
130}
131
132template <typename T>
133void fromSimObject(T &t, SimObject *s)
134{
135 t = dynamic_cast<T>(s);
136}
137
138//
139// These macros are streamlined to use in serialize/unserialize
140// functions. It's assumed that serialize() has a parameter 'os' for
141// the ostream, and unserialize() has parameters 'cp' and 'section'.
137
138template <typename T>
139void fromInt(T &t, int i)
140{
141 t = (T)i;
142}
143
144template <typename T>
145void fromSimObject(T &t, SimObject *s)
146{
147 t = dynamic_cast<T>(s);
148}
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'.
142#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
154#define SERIALIZE_SCALAR(scalar) paramOut(cp, #scalar, scalar)
143
155
144#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
145#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar)
156#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar)
157#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar)
146
147// ENUMs are like SCALARs, but we cast them to ints on the way out
158
159// ENUMs are like SCALARs, but we cast them to ints on the way out
148#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
160#define SERIALIZE_ENUM(scalar) paramOut(cp, #scalar, (int)scalar)
149
150#define UNSERIALIZE_ENUM(scalar) \
151 do { \
152 int tmp; \
161
162#define UNSERIALIZE_ENUM(scalar) \
163 do { \
164 int tmp; \
153 paramIn(cp, section, #scalar, tmp); \
154 fromInt(scalar, tmp); \
165 paramIn(cp, #scalar, tmp); \
166 fromInt(scalar, tmp); \
155 } while (0)
156
157#define SERIALIZE_ARRAY(member, size) \
167 } while (0)
168
169#define SERIALIZE_ARRAY(member, size) \
158 arrayParamOut(os, #member, member, size)
170 arrayParamOut(cp, #member, member, size)
159
160#define UNSERIALIZE_ARRAY(member, size) \
171
172#define UNSERIALIZE_ARRAY(member, size) \
161 arrayParamIn(cp, section, #member, member, size)
173 arrayParamIn(cp, #member, member, size)
162
163#define SERIALIZE_CONTAINER(member) \
174
175#define SERIALIZE_CONTAINER(member) \
164 arrayParamOut(os, #member, member)
176 arrayParamOut(cp, #member, member)
165
166#define UNSERIALIZE_CONTAINER(member) \
177
178#define UNSERIALIZE_CONTAINER(member) \
167 arrayParamIn(cp, section, #member, member)
179 arrayParamIn(cp, #member, member)
168
180
169#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
181#define SERIALIZE_OBJPTR(objptr) paramOut(cp, #objptr, (objptr)->name())
170
171#define UNSERIALIZE_OBJPTR(objptr) \
172 do { \
173 SimObject *sptr; \
182
183#define UNSERIALIZE_OBJPTR(objptr) \
184 do { \
185 SimObject *sptr; \
174 objParamIn(cp, section, #objptr, sptr); \
186 objParamIn(cp, #objptr, sptr); \
175 fromSimObject(objptr, sptr); \
176 } while (0)
177
178/**
179 * Basic support for object serialization.
180 *
187 fromSimObject(objptr, sptr); \
188 } while (0)
189
190/**
191 * Basic support for object serialization.
192 *
193 * Objects that support serialization should derive from this
194 * class. Such objects can largely be divided into two categories: 1)
195 * True SimObjects (deriving from SimObject), and 2) child objects
196 * (non-SimObjects).
197 *
198 * SimObjects are serialized automatically into their own sections
199 * automatically by the SimObject base class (see
200 * SimObject::serializeAll().
201 *
202 * SimObjects can contain other serializable objects that are not
203 * SimObjects. Much like normal serialized members are not serialized
204 * automatically, these objects will not be serialized automatically
205 * and it is expected that the objects owning such serializable
206 * objects call the required serialization/unserialization methods on
207 * child objects. The preferred method to serialize a child object is
208 * to call serializeSection() on the child, which serializes the
209 * object into a new subsection in the current section. Another option
210 * is to call serialize() directly, which serializes the object into
211 * the current section. The latter is not recommended as it can lead
212 * to naming clashes between objects.
213 *
181 * @note Many objects that support serialization need to be put in a
182 * consistent state when serialization takes place. We refer to the
183 * action of forcing an object into a consistent state as
184 * 'draining'. Objects that need draining inherit from Drainable. See
185 * Drainable for more information.
186 */
187class Serializable
188{
189 protected:
214 * @note Many objects that support serialization need to be put in a
215 * consistent state when serialization takes place. We refer to the
216 * action of forcing an object into a consistent state as
217 * 'draining'. Objects that need draining inherit from Drainable. See
218 * Drainable for more information.
219 */
220class Serializable
221{
222 protected:
190 void nameOut(std::ostream &os);
191 void nameOut(std::ostream &os, const std::string &_name);
223 /**
224 * Scoped checkpoint section helper class
225 *
226 * This helper class creates a section within a checkpoint without
227 * the need for a separate serializeable object. It is mainly used
228 * within the Serializable class when serializing or unserializing
229 * section (see serializeSection() and unserializeSection()). It
230 * can also be used to maintain backwards compatibility in
231 * existing code that serializes structs that are not inheriting
232 * from Serializable into subsections.
233 *
234 * When the class is instantiated, it appends a name to the active
235 * path in a checkpoint. The old path is later restored when the
236 * instance is destroyed. For example, serializeSection() could be
237 * implemented by instantiating a ScopedCheckpointSection and then
238 * calling serialize() on an object.
239 */
240 class ScopedCheckpointSection {
241 public:
242 template<class CP>
243 ScopedCheckpointSection(CP &cp, const char *name) {
244 pushName(name);
245 nameOut(cp);
246 }
192
247
248 template<class CP>
249 ScopedCheckpointSection(CP &cp, const std::string &name) {
250 pushName(name.c_str());
251 nameOut(cp);
252 }
253
254 ~ScopedCheckpointSection();
255
256 ScopedCheckpointSection() = delete;
257 ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
258 ScopedCheckpointSection &operator=(
259 const ScopedCheckpointSection &) = delete;
260 ScopedCheckpointSection &operator=(
261 ScopedCheckpointSection &&) = delete;
262
263 private:
264 void pushName(const char *name);
265 void nameOut(CheckpointOut &cp);
266 void nameOut(CheckpointIn &cp) {};
267 };
268
193 public:
194 Serializable();
195 virtual ~Serializable();
196
269 public:
270 Serializable();
271 virtual ~Serializable();
272
197 // manditory virtual function, so objects must provide names
198 virtual const std::string name() const = 0;
273 /**
274 * Serialize an object
275 *
276 * Output an object's state into the current checkpoint section.
277 *
278 * @param cp Checkpoint state
279 */
280 virtual void serialize(CheckpointOut &cp) const = 0;
199
281
200 virtual void serialize(std::ostream &os);
201 virtual void unserialize(Checkpoint *cp, const std::string &section);
282 /**
283 * Unserialize an object
284 *
285 * Read an object's state from the current checkpoint section.
286 *
287 * @param cp Checkpoint state
288 */
289 virtual void unserialize(CheckpointIn &cp) = 0;
202
290
203 static Serializable *create(Checkpoint *cp, const std::string &section);
291 /**
292 * Serialize an object into a new section
293 *
294 * This method creates a new section in a checkpoint and calls
295 * serialize() to serialize the current object into that
296 * section. The name of the section is appended to the current
297 * checkpoint path.
298 *
299 * @param cp Checkpoint state
300 * @param name Name to append to the active path
301 */
302 void serializeSection(CheckpointOut &cp, const char *name) const;
204
303
304 void serializeSection(CheckpointOut &cp, const std::string &name) const {
305 serializeSection(cp, name.c_str());
306 }
307
308 /**
309 * Unserialize an a child object
310 *
311 * This method loads a child object from a checkpoint. The object
312 * name is appended to the active path to form a fully qualified
313 * section name and unserialize() is called.
314 *
315 * @param cp Checkpoint state
316 * @param name Name to append to the active path
317 */
318 void unserializeSection(CheckpointIn &cp, const char *name);
319
320 void unserializeSection(CheckpointIn &cp, const std::string &name) {
321 unserializeSection(cp, name.c_str());
322 }
323
324 /**
325 * @{
326 * @name Legacy interface
327 *
328 * Interface for objects that insist on changing their state when
329 * serializing. Such state change should be done in drain(),
330 * memWriteback(), or memInvalidate() and not in the serialization
331 * method. In general, if state changes occur in serialize, it
332 * complicates testing since it breaks assumptions about draining
333 * and serialization. It potentially also makes components more
334 * fragile since they there are no ordering guarantees when
335 * serializing SimObjects.
336 *
337 * @warn This interface is considered deprecated and should never
338 * be used.
339 */
340
341 virtual void serializeOld(CheckpointOut &cp) {
342 serialize(cp);
343 }
344 void serializeSectionOld(CheckpointOut &cp, const char *name);
345 void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
346 serializeSectionOld(cp, name.c_str());
347 }
348 /** @} */
349
350 /** Get the fully-qualified name of the active section */
351 static const std::string &currentSection();
352
353 static Serializable *create(CheckpointIn &cp, const std::string &section);
354
205 static int ckptCount;
206 static int ckptMaxCount;
207 static int ckptPrevCount;
208 static void serializeAll(const std::string &cpt_dir);
355 static int ckptCount;
356 static int ckptMaxCount;
357 static int ckptPrevCount;
358 static void serializeAll(const std::string &cpt_dir);
209 static void unserializeGlobals(Checkpoint *cp);
359 static void unserializeGlobals(CheckpointIn &cp);
360
361 private:
362 static std::stack<std::string> path;
210};
211
212void debug_serialize(const std::string &cpt_dir);
213
214//
215// A SerializableBuilder serves as an evaluation context for a set of
216// parameters that describe a specific instance of a Serializable. This
217// evaluation context corresponds to a section in the .ini file (as

--- 35 unchanged lines hidden (view full) ---

253 public:
254
255 // Type CreateFunc is a pointer to a function that creates a new
256 // simulation object builder based on a .ini-file parameter
257 // section (specified by the first string argument), a unique name
258 // for the object (specified by the second string argument), and
259 // an optional config hierarchy node (specified by the third
260 // argument). A pointer to the new SerializableBuilder is returned.
363};
364
365void debug_serialize(const std::string &cpt_dir);
366
367//
368// A SerializableBuilder serves as an evaluation context for a set of
369// parameters that describe a specific instance of a Serializable. This
370// evaluation context corresponds to a section in the .ini file (as

--- 35 unchanged lines hidden (view full) ---

406 public:
407
408 // Type CreateFunc is a pointer to a function that creates a new
409 // simulation object builder based on a .ini-file parameter
410 // section (specified by the first string argument), a unique name
411 // for the object (specified by the second string argument), and
412 // an optional config hierarchy node (specified by the third
413 // argument). A pointer to the new SerializableBuilder is returned.
261 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
414 typedef Serializable *(*CreateFunc)(CheckpointIn &cp,
262 const std::string &section);
263
264 static std::map<std::string,CreateFunc> *classMap;
265
266 // Constructor. For example:
267 //
268 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
269 // newBaseCacheSerializableBuilder);
270 //
271 SerializableClass(const std::string &className, CreateFunc createFunc);
272
273 // create Serializable given name of class and pointer to
274 // configuration hierarchy node
415 const std::string &section);
416
417 static std::map<std::string,CreateFunc> *classMap;
418
419 // Constructor. For example:
420 //
421 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
422 // newBaseCacheSerializableBuilder);
423 //
424 SerializableClass(const std::string &className, CreateFunc createFunc);
425
426 // create Serializable given name of class and pointer to
427 // configuration hierarchy node
275 static Serializable *createObject(Checkpoint *cp,
428 static Serializable *createObject(CheckpointIn &cp,
276 const std::string &section);
277};
278
279//
280// Macros to encapsulate the magic of declaring & defining
281// SerializableBuilder and SerializableClass objects
282//
283

--- 8 unchanged lines hidden (view full) ---

292{
293 public:
294 virtual ~SimObjectResolver() { }
295
296 // Find a SimObject given a full path name
297 virtual SimObject *resolveSimObject(const std::string &name) = 0;
298};
299
429 const std::string &section);
430};
431
432//
433// Macros to encapsulate the magic of declaring & defining
434// SerializableBuilder and SerializableClass objects
435//
436

--- 8 unchanged lines hidden (view full) ---

445{
446 public:
447 virtual ~SimObjectResolver() { }
448
449 // Find a SimObject given a full path name
450 virtual SimObject *resolveSimObject(const std::string &name) = 0;
451};
452
300class Checkpoint
453class CheckpointIn
301{
302 private:
303
304 IniFile *db;
305
306 SimObjectResolver &objNameResolver;
307
308 public:
454{
455 private:
456
457 IniFile *db;
458
459 SimObjectResolver &objNameResolver;
460
461 public:
309 Checkpoint(const std::string &cpt_dir, SimObjectResolver &resolver);
310 ~Checkpoint();
462 CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
463 ~CheckpointIn();
311
312 const std::string cptDir;
313
314 bool find(const std::string &section, const std::string &entry,
315 std::string &value);
316
317 bool findObj(const std::string &section, const std::string &entry,
318 SimObject *&value);

--- 31 unchanged lines hidden ---
464
465 const std::string cptDir;
466
467 bool find(const std::string &section, const std::string &entry,
468 std::string &value);
469
470 bool findObj(const std::string &section, const std::string &entry,
471 SimObject *&value);

--- 31 unchanged lines hidden ---