serialize.hh (9048:950298f29140) serialize.hh (9056:0e38b529c387)
1/*
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
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
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#include <vector>
45
46#include "base/types.hh"
47
48class IniFile;
49class Serializable;
50class Checkpoint;
51class SimObject;
52
53/** The current version of the checkpoint format.
54 * This should be incremented by 1 and only 1 for every new version, where a new
1/*
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
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
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#include <vector>
45
46#include "base/types.hh"
47
48class IniFile;
49class Serializable;
50class Checkpoint;
51class SimObject;
52
53/** The current version of the checkpoint format.
54 * This should be incremented by 1 and only 1 for every new version, where a new
55 * version is defined as a checkpoint created before this version wont work on
55 * version is defined as a checkpoint created before this version won't work on
56 * the current version until the checkpoint format is updated. Adding a new
57 * SimObject shouldn't cause the version number to increase, only changes to
56 * the current version until the checkpoint format is updated. Adding a new
57 * SimObject shouldn't cause the version number to increase, only changes to
58 * existing objects such as serializing/unserializing more stote, changing sizes
58 * existing objects such as serializing/unserializing more state, changing sizes
59 * of serialized arrays, etc. */
60static const uint64_t gem5CheckpointVersion = 0x0000000000000001;
61
62template <class T>
63void paramOut(std::ostream &os, const std::string &name, const T &param);
64
65template <class T>
66void paramIn(Checkpoint *cp, const std::string &section,
67 const std::string &name, T &param);
68
69template <class T>
70bool optParamIn(Checkpoint *cp, const std::string &section,
71 const std::string &name, T &param);
72
73template <class T>
74void arrayParamOut(std::ostream &os, const std::string &name,
75 const T *param, unsigned size);
76
77template <class T>
78void arrayParamOut(std::ostream &os, const std::string &name,
79 const std::vector<T> &param);
80
81template <class T>
82void arrayParamOut(std::ostream &os, const std::string &name,
83 const std::list<T> &param);
84
85template <class T>
86void arrayParamIn(Checkpoint *cp, const std::string &section,
87 const std::string &name, T *param, unsigned size);
88
89template <class T>
90void arrayParamIn(Checkpoint *cp, const std::string &section,
91 const std::string &name, std::vector<T> &param);
92
93template <class T>
94void arrayParamIn(Checkpoint *cp, const std::string &section,
95 const std::string &name, std::list<T> &param);
96
97void
98objParamIn(Checkpoint *cp, const std::string &section,
99 const std::string &name, SimObject * &param);
100
101template <typename T>
102void fromInt(T &t, int i)
103{
104 t = (T)i;
105}
106
107template <typename T>
108void fromSimObject(T &t, SimObject *s)
109{
110 t = dynamic_cast<T>(s);
111}
112
113//
114// These macros are streamlined to use in serialize/unserialize
115// functions. It's assumed that serialize() has a parameter 'os' for
116// the ostream, and unserialize() has parameters 'cp' and 'section'.
117#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
118
119#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
120#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar)
121
122// ENUMs are like SCALARs, but we cast them to ints on the way out
123#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
124
125#define UNSERIALIZE_ENUM(scalar) \
126 do { \
127 int tmp; \
128 paramIn(cp, section, #scalar, tmp); \
129 fromInt(scalar, tmp); \
130 } while (0)
131
132#define SERIALIZE_ARRAY(member, size) \
133 arrayParamOut(os, #member, member, size)
134
135#define UNSERIALIZE_ARRAY(member, size) \
136 arrayParamIn(cp, section, #member, member, size)
137
138#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
139
140#define UNSERIALIZE_OBJPTR(objptr) \
141 do { \
142 SimObject *sptr; \
143 objParamIn(cp, section, #objptr, sptr); \
144 fromSimObject(objptr, sptr); \
145 } while (0)
146
147/*
148 * Basic support for object serialization.
149 */
150class Serializable
151{
152 protected:
153 void nameOut(std::ostream &os);
154 void nameOut(std::ostream &os, const std::string &_name);
155
156 public:
157 Serializable();
158 virtual ~Serializable();
159
160 // manditory virtual function, so objects must provide names
161 virtual const std::string name() const = 0;
162
163 virtual void serialize(std::ostream &os);
164 virtual void unserialize(Checkpoint *cp, const std::string &section);
165
166 static Serializable *create(Checkpoint *cp, const std::string &section);
167
168 static int ckptCount;
169 static int ckptMaxCount;
170 static int ckptPrevCount;
171 static void serializeAll(const std::string &cpt_dir);
172 static void unserializeGlobals(Checkpoint *cp);
173};
174
175//
176// A SerializableBuilder serves as an evaluation context for a set of
177// parameters that describe a specific instance of a Serializable. This
178// evaluation context corresponds to a section in the .ini file (as
179// with the base ParamContext) plus an optional node in the
180// configuration hierarchy (the configNode member) for resolving
181// Serializable references. SerializableBuilder is an abstract superclass;
182// derived classes specialize the class for particular subclasses of
183// Serializable (e.g., BaseCache).
184//
185// For typical usage, see the definition of
186// SerializableClass::createObject().
187//
188class SerializableBuilder
189{
190 public:
191
192 SerializableBuilder() {}
193
194 virtual ~SerializableBuilder() {}
195
196 // Create the actual Serializable corresponding to the parameter
197 // values in this context. This function is overridden in derived
198 // classes to call a specific constructor for a particular
199 // subclass of Serializable.
200 virtual Serializable *create() = 0;
201};
202
203//
204// An instance of SerializableClass corresponds to a class derived from
205// Serializable. The SerializableClass instance serves to bind the string
206// name (found in the config file) to a function that creates an
207// instance of the appropriate derived class.
208//
209// This would be much cleaner in Smalltalk or Objective-C, where types
210// are first-class objects themselves.
211//
212class SerializableClass
213{
214 public:
215
216 // Type CreateFunc is a pointer to a function that creates a new
217 // simulation object builder based on a .ini-file parameter
218 // section (specified by the first string argument), a unique name
219 // for the object (specified by the second string argument), and
220 // an optional config hierarchy node (specified by the third
221 // argument). A pointer to the new SerializableBuilder is returned.
222 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
223 const std::string &section);
224
225 static std::map<std::string,CreateFunc> *classMap;
226
227 // Constructor. For example:
228 //
229 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
230 // newBaseCacheSerializableBuilder);
231 //
232 SerializableClass(const std::string &className, CreateFunc createFunc);
233
234 // create Serializable given name of class and pointer to
235 // configuration hierarchy node
236 static Serializable *createObject(Checkpoint *cp,
237 const std::string &section);
238};
239
240//
241// Macros to encapsulate the magic of declaring & defining
242// SerializableBuilder and SerializableClass objects
243//
244
245#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
246SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
247 OBJ_CLASS::createForUnserialize);
248
249class Checkpoint
250{
251 private:
252
253 IniFile *db;
254
255 public:
256 Checkpoint(const std::string &cpt_dir);
257
258 const std::string cptDir;
259
260 bool find(const std::string &section, const std::string &entry,
261 std::string &value);
262
263 bool findObj(const std::string &section, const std::string &entry,
264 SimObject *&value);
265
266 bool sectionExists(const std::string &section);
267
268 // The following static functions have to do with checkpoint
269 // creation rather than restoration. This class makes a handy
270 // namespace for them though. Currently no Checkpoint object is
271 // created on serialization (only unserialization) so we track the
272 // directory name as a global. It would be nice to change this
273 // someday
274
275 private:
276 // current directory we're serializing into.
277 static std::string currentDirectory;
278
279 public:
280 // Set the current directory. This function takes care of
281 // inserting curTick() if there's a '%d' in the argument, and
282 // appends a '/' if necessary. The final name is returned.
283 static std::string setDir(const std::string &base_name);
284
285 // Export current checkpoint directory name so other objects can
286 // derive filenames from it (e.g., memory). The return value is
287 // guaranteed to end in '/' so filenames can be directly appended.
288 // This function is only valid while a checkpoint is being created.
289 static std::string dir();
290
291 // Filename for base checkpoint file within directory.
292 static const char *baseFilename;
293};
294
295#endif // __SERIALIZE_HH__
59 * of serialized arrays, etc. */
60static const uint64_t gem5CheckpointVersion = 0x0000000000000001;
61
62template <class T>
63void paramOut(std::ostream &os, const std::string &name, const T &param);
64
65template <class T>
66void paramIn(Checkpoint *cp, const std::string &section,
67 const std::string &name, T &param);
68
69template <class T>
70bool optParamIn(Checkpoint *cp, const std::string &section,
71 const std::string &name, T &param);
72
73template <class T>
74void arrayParamOut(std::ostream &os, const std::string &name,
75 const T *param, unsigned size);
76
77template <class T>
78void arrayParamOut(std::ostream &os, const std::string &name,
79 const std::vector<T> &param);
80
81template <class T>
82void arrayParamOut(std::ostream &os, const std::string &name,
83 const std::list<T> &param);
84
85template <class T>
86void arrayParamIn(Checkpoint *cp, const std::string &section,
87 const std::string &name, T *param, unsigned size);
88
89template <class T>
90void arrayParamIn(Checkpoint *cp, const std::string &section,
91 const std::string &name, std::vector<T> &param);
92
93template <class T>
94void arrayParamIn(Checkpoint *cp, const std::string &section,
95 const std::string &name, std::list<T> &param);
96
97void
98objParamIn(Checkpoint *cp, const std::string &section,
99 const std::string &name, SimObject * &param);
100
101template <typename T>
102void fromInt(T &t, int i)
103{
104 t = (T)i;
105}
106
107template <typename T>
108void fromSimObject(T &t, SimObject *s)
109{
110 t = dynamic_cast<T>(s);
111}
112
113//
114// These macros are streamlined to use in serialize/unserialize
115// functions. It's assumed that serialize() has a parameter 'os' for
116// the ostream, and unserialize() has parameters 'cp' and 'section'.
117#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
118
119#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
120#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar)
121
122// ENUMs are like SCALARs, but we cast them to ints on the way out
123#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
124
125#define UNSERIALIZE_ENUM(scalar) \
126 do { \
127 int tmp; \
128 paramIn(cp, section, #scalar, tmp); \
129 fromInt(scalar, tmp); \
130 } while (0)
131
132#define SERIALIZE_ARRAY(member, size) \
133 arrayParamOut(os, #member, member, size)
134
135#define UNSERIALIZE_ARRAY(member, size) \
136 arrayParamIn(cp, section, #member, member, size)
137
138#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
139
140#define UNSERIALIZE_OBJPTR(objptr) \
141 do { \
142 SimObject *sptr; \
143 objParamIn(cp, section, #objptr, sptr); \
144 fromSimObject(objptr, sptr); \
145 } while (0)
146
147/*
148 * Basic support for object serialization.
149 */
150class Serializable
151{
152 protected:
153 void nameOut(std::ostream &os);
154 void nameOut(std::ostream &os, const std::string &_name);
155
156 public:
157 Serializable();
158 virtual ~Serializable();
159
160 // manditory virtual function, so objects must provide names
161 virtual const std::string name() const = 0;
162
163 virtual void serialize(std::ostream &os);
164 virtual void unserialize(Checkpoint *cp, const std::string &section);
165
166 static Serializable *create(Checkpoint *cp, const std::string &section);
167
168 static int ckptCount;
169 static int ckptMaxCount;
170 static int ckptPrevCount;
171 static void serializeAll(const std::string &cpt_dir);
172 static void unserializeGlobals(Checkpoint *cp);
173};
174
175//
176// A SerializableBuilder serves as an evaluation context for a set of
177// parameters that describe a specific instance of a Serializable. This
178// evaluation context corresponds to a section in the .ini file (as
179// with the base ParamContext) plus an optional node in the
180// configuration hierarchy (the configNode member) for resolving
181// Serializable references. SerializableBuilder is an abstract superclass;
182// derived classes specialize the class for particular subclasses of
183// Serializable (e.g., BaseCache).
184//
185// For typical usage, see the definition of
186// SerializableClass::createObject().
187//
188class SerializableBuilder
189{
190 public:
191
192 SerializableBuilder() {}
193
194 virtual ~SerializableBuilder() {}
195
196 // Create the actual Serializable corresponding to the parameter
197 // values in this context. This function is overridden in derived
198 // classes to call a specific constructor for a particular
199 // subclass of Serializable.
200 virtual Serializable *create() = 0;
201};
202
203//
204// An instance of SerializableClass corresponds to a class derived from
205// Serializable. The SerializableClass instance serves to bind the string
206// name (found in the config file) to a function that creates an
207// instance of the appropriate derived class.
208//
209// This would be much cleaner in Smalltalk or Objective-C, where types
210// are first-class objects themselves.
211//
212class SerializableClass
213{
214 public:
215
216 // Type CreateFunc is a pointer to a function that creates a new
217 // simulation object builder based on a .ini-file parameter
218 // section (specified by the first string argument), a unique name
219 // for the object (specified by the second string argument), and
220 // an optional config hierarchy node (specified by the third
221 // argument). A pointer to the new SerializableBuilder is returned.
222 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
223 const std::string &section);
224
225 static std::map<std::string,CreateFunc> *classMap;
226
227 // Constructor. For example:
228 //
229 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
230 // newBaseCacheSerializableBuilder);
231 //
232 SerializableClass(const std::string &className, CreateFunc createFunc);
233
234 // create Serializable given name of class and pointer to
235 // configuration hierarchy node
236 static Serializable *createObject(Checkpoint *cp,
237 const std::string &section);
238};
239
240//
241// Macros to encapsulate the magic of declaring & defining
242// SerializableBuilder and SerializableClass objects
243//
244
245#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
246SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
247 OBJ_CLASS::createForUnserialize);
248
249class Checkpoint
250{
251 private:
252
253 IniFile *db;
254
255 public:
256 Checkpoint(const std::string &cpt_dir);
257
258 const std::string cptDir;
259
260 bool find(const std::string &section, const std::string &entry,
261 std::string &value);
262
263 bool findObj(const std::string &section, const std::string &entry,
264 SimObject *&value);
265
266 bool sectionExists(const std::string &section);
267
268 // The following static functions have to do with checkpoint
269 // creation rather than restoration. This class makes a handy
270 // namespace for them though. Currently no Checkpoint object is
271 // created on serialization (only unserialization) so we track the
272 // directory name as a global. It would be nice to change this
273 // someday
274
275 private:
276 // current directory we're serializing into.
277 static std::string currentDirectory;
278
279 public:
280 // Set the current directory. This function takes care of
281 // inserting curTick() if there's a '%d' in the argument, and
282 // appends a '/' if necessary. The final name is returned.
283 static std::string setDir(const std::string &base_name);
284
285 // Export current checkpoint directory name so other objects can
286 // derive filenames from it (e.g., memory). The return value is
287 // guaranteed to end in '/' so filenames can be directly appended.
288 // This function is only valid while a checkpoint is being created.
289 static std::string dir();
290
291 // Filename for base checkpoint file within directory.
292 static const char *baseFilename;
293};
294
295#endif // __SERIALIZE_HH__