Deleted Added
sdiff udiff text old ( 10903:022e5d110a22 ) new ( 10905:a6ca6831e775 )
full compact
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

--- 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
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/bitunion.hh"
47#include "base/types.hh"
48
49class IniFile;
50class Serializable;
51class Checkpoint;
52class SimObject;
53class EventQueue;
54
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>
65void paramOut(std::ostream &os, const std::string &name, const T &param);
66
67template <typename DataType, typename BitUnion>
68void paramOut(std::ostream &os, const std::string &name,
69 const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
70{
71 paramOut(os, name, p.__data);
72}
73
74template <class T>
75void paramIn(Checkpoint *cp, const std::string &section,
76 const std::string &name, T &param);
77
78template <typename DataType, typename BitUnion>
79void paramIn(Checkpoint *cp, const std::string &section,
80 const std::string &name,
81 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
82{
83 paramIn(cp, section, name, p.__data);
84}
85
86template <class T>
87bool optParamIn(Checkpoint *cp, const std::string &section,
88 const std::string &name, T &param);
89
90template <typename DataType, typename BitUnion>
91bool optParamIn(Checkpoint *cp, const std::string &section,
92 const std::string &name,
93 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
94{
95 return optParamIn(cp, section, name, p.__data);
96}
97
98template <class T>
99void arrayParamOut(std::ostream &os, const std::string &name,
100 const T *param, unsigned size);
101
102template <class T>
103void arrayParamOut(std::ostream &os, const std::string &name,
104 const std::vector<T> &param);
105
106template <class T>
107void arrayParamOut(std::ostream &os, const std::string &name,
108 const std::list<T> &param);
109
110template <class T>
111void arrayParamIn(Checkpoint *cp, const std::string &section,
112 const std::string &name, T *param, unsigned size);
113
114template <class T>
115void arrayParamIn(Checkpoint *cp, const std::string &section,
116 const std::string &name, std::vector<T> &param);
117
118template <class T>
119void arrayParamIn(Checkpoint *cp, const std::string &section,
120 const std::string &name, std::list<T> &param);
121
122void
123objParamIn(Checkpoint *cp, const std::string &section,
124 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'.
142#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
143
144#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
145#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar)
146
147// ENUMs are like SCALARs, but we cast them to ints on the way out
148#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
149
150#define UNSERIALIZE_ENUM(scalar) \
151 do { \
152 int tmp; \
153 paramIn(cp, section, #scalar, tmp); \
154 fromInt(scalar, tmp); \
155 } while (0)
156
157#define SERIALIZE_ARRAY(member, size) \
158 arrayParamOut(os, #member, member, size)
159
160#define UNSERIALIZE_ARRAY(member, size) \
161 arrayParamIn(cp, section, #member, member, size)
162
163#define SERIALIZE_CONTAINER(member) \
164 arrayParamOut(os, #member, member)
165
166#define UNSERIALIZE_CONTAINER(member) \
167 arrayParamIn(cp, section, #member, member)
168
169#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
170
171#define UNSERIALIZE_OBJPTR(objptr) \
172 do { \
173 SimObject *sptr; \
174 objParamIn(cp, section, #objptr, sptr); \
175 fromSimObject(objptr, sptr); \
176 } while (0)
177
178/**
179 * Basic support for object serialization.
180 *
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:
190 void nameOut(std::ostream &os);
191 void nameOut(std::ostream &os, const std::string &_name);
192
193 public:
194 Serializable();
195 virtual ~Serializable();
196
197 // manditory virtual function, so objects must provide names
198 virtual const std::string name() const = 0;
199
200 virtual void serialize(std::ostream &os);
201 virtual void unserialize(Checkpoint *cp, const std::string &section);
202
203 static Serializable *create(Checkpoint *cp, const std::string &section);
204
205 static int ckptCount;
206 static int ckptMaxCount;
207 static int ckptPrevCount;
208 static void serializeAll(const std::string &cpt_dir);
209 static void unserializeGlobals(Checkpoint *cp);
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.
261 typedef Serializable *(*CreateFunc)(Checkpoint *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
275 static Serializable *createObject(Checkpoint *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
300class Checkpoint
301{
302 private:
303
304 IniFile *db;
305
306 SimObjectResolver &objNameResolver;
307
308 public:
309 Checkpoint(const std::string &cpt_dir, SimObjectResolver &resolver);
310 ~Checkpoint();
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 ---