2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
34 * Serialization Interface Declarations
37 #ifndef __SERIALIZE_HH__
38 #define __SERIALIZE_HH__
46 #include "base/types.hh"
54 /** The current version of the checkpoint format.
55 * This should be incremented by 1 and only 1 for every new version, where a new
56 * version is defined as a checkpoint created before this version won't work on
57 * the current version until the checkpoint format is updated. Adding a new
58 * SimObject shouldn't cause the version number to increase, only changes to
59 * existing objects such as serializing/unserializing more state, changing sizes
60 * of serialized arrays, etc. */
61 static const uint64_t gem5CheckpointVersion = 0x000000000000000c;
64 void paramOut(std::ostream &os, const std::string &name, const T ¶m);
67 void paramIn(Checkpoint *cp, const std::string §ion,
68 const std::string &name, T ¶m);
71 bool optParamIn(Checkpoint *cp, const std::string §ion,
72 const std::string &name, T ¶m);
75 void arrayParamOut(std::ostream &os, const std::string &name,
76 const T *param, unsigned size);
79 void arrayParamOut(std::ostream &os, const std::string &name,
80 const std::vector<T> ¶m);
83 void arrayParamOut(std::ostream &os, const std::string &name,
84 const std::list<T> ¶m);
87 void arrayParamIn(Checkpoint *cp, const std::string §ion,
88 const std::string &name, T *param, unsigned size);
91 void arrayParamIn(Checkpoint *cp, const std::string §ion,
92 const std::string &name, std::vector<T> ¶m);
95 void arrayParamIn(Checkpoint *cp, const std::string §ion,
96 const std::string &name, std::list<T> ¶m);
99 objParamIn(Checkpoint *cp, const std::string §ion,
100 const std::string &name, SimObject * ¶m);
102 template <typename T>
103 void fromInt(T &t, int i)
108 template <typename T>
109 void fromSimObject(T &t, SimObject *s)
111 t = dynamic_cast<T>(s);
115 // These macros are streamlined to use in serialize/unserialize
116 // functions. It's assumed that serialize() has a parameter 'os' for
117 // the ostream, and unserialize() has parameters 'cp' and 'section'.
118 #define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
120 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
121 #define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar)
123 // ENUMs are like SCALARs, but we cast them to ints on the way out
124 #define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
126 #define UNSERIALIZE_ENUM(scalar) \
129 paramIn(cp, section, #scalar, tmp); \
130 fromInt(scalar, tmp); \
133 #define SERIALIZE_ARRAY(member, size) \
134 arrayParamOut(os, #member, member, size)
136 #define UNSERIALIZE_ARRAY(member, size) \
137 arrayParamIn(cp, section, #member, member, size)
139 #define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
141 #define UNSERIALIZE_OBJPTR(objptr) \
144 objParamIn(cp, section, #objptr, sptr); \
145 fromSimObject(objptr, sptr); \
149 * Basic support for object serialization.
151 * @note Many objects that support serialization need to be put in a
152 * consistent state when serialization takes place. We refer to the
153 * action of forcing an object into a consistent state as
154 * 'draining'. Objects that need draining inherit from Drainable. See
155 * Drainable for more information.
160 void nameOut(std::ostream &os);
161 void nameOut(std::ostream &os, const std::string &_name);
165 virtual ~Serializable();
167 // manditory virtual function, so objects must provide names
168 virtual const std::string name() const = 0;
170 virtual void serialize(std::ostream &os);
171 virtual void unserialize(Checkpoint *cp, const std::string §ion);
173 static Serializable *create(Checkpoint *cp, const std::string §ion);
175 static int ckptCount;
176 static int ckptMaxCount;
177 static int ckptPrevCount;
178 static void serializeAll(const std::string &cpt_dir);
179 static void unserializeGlobals(Checkpoint *cp);
182 void debug_serialize(const std::string &cpt_dir);
185 // A SerializableBuilder serves as an evaluation context for a set of
186 // parameters that describe a specific instance of a Serializable. This
187 // evaluation context corresponds to a section in the .ini file (as
188 // with the base ParamContext) plus an optional node in the
189 // configuration hierarchy (the configNode member) for resolving
190 // Serializable references. SerializableBuilder is an abstract superclass;
191 // derived classes specialize the class for particular subclasses of
192 // Serializable (e.g., BaseCache).
194 // For typical usage, see the definition of
195 // SerializableClass::createObject().
197 class SerializableBuilder
201 SerializableBuilder() {}
203 virtual ~SerializableBuilder() {}
205 // Create the actual Serializable corresponding to the parameter
206 // values in this context. This function is overridden in derived
207 // classes to call a specific constructor for a particular
208 // subclass of Serializable.
209 virtual Serializable *create() = 0;
213 // An instance of SerializableClass corresponds to a class derived from
214 // Serializable. The SerializableClass instance serves to bind the string
215 // name (found in the config file) to a function that creates an
216 // instance of the appropriate derived class.
218 // This would be much cleaner in Smalltalk or Objective-C, where types
219 // are first-class objects themselves.
221 class SerializableClass
225 // Type CreateFunc is a pointer to a function that creates a new
226 // simulation object builder based on a .ini-file parameter
227 // section (specified by the first string argument), a unique name
228 // for the object (specified by the second string argument), and
229 // an optional config hierarchy node (specified by the third
230 // argument). A pointer to the new SerializableBuilder is returned.
231 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
232 const std::string §ion);
234 static std::map<std::string,CreateFunc> *classMap;
236 // Constructor. For example:
238 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
239 // newBaseCacheSerializableBuilder);
241 SerializableClass(const std::string &className, CreateFunc createFunc);
243 // create Serializable given name of class and pointer to
244 // configuration hierarchy node
245 static Serializable *createObject(Checkpoint *cp,
246 const std::string §ion);
250 // Macros to encapsulate the magic of declaring & defining
251 // SerializableBuilder and SerializableClass objects
254 #define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
255 SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
256 OBJ_CLASS::createForUnserialize);
265 Checkpoint(const std::string &cpt_dir);
268 const std::string cptDir;
270 bool find(const std::string §ion, const std::string &entry,
273 bool findObj(const std::string §ion, const std::string &entry,
276 bool sectionExists(const std::string §ion);
278 // The following static functions have to do with checkpoint
279 // creation rather than restoration. This class makes a handy
280 // namespace for them though. Currently no Checkpoint object is
281 // created on serialization (only unserialization) so we track the
282 // directory name as a global. It would be nice to change this
286 // current directory we're serializing into.
287 static std::string currentDirectory;
290 // Set the current directory. This function takes care of
291 // inserting curTick() if there's a '%d' in the argument, and
292 // appends a '/' if necessary. The final name is returned.
293 static std::string setDir(const std::string &base_name);
295 // Export current checkpoint directory name so other objects can
296 // derive filenames from it (e.g., memory). The return value is
297 // guaranteed to end in '/' so filenames can be directly appended.
298 // This function is only valid while a checkpoint is being created.
299 static std::string dir();
301 // Filename for base checkpoint file within directory.
302 static const char *baseFilename;
305 #endif // __SERIALIZE_HH__