677a3fd92ec898041dd09916ba1a4e340b42d43e
[gem5.git] / src / sim / serialize.hh
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 <list>
42 #include <vector>
43 #include <iostream>
44 #include <map>
45
46 #include "base/types.hh"
47
48 class IniFile;
49 class Serializable;
50 class Checkpoint;
51 class SimObject;
52
53 template <class T>
54 void paramOut(std::ostream &os, const std::string &name, const T &param);
55
56 template <class T>
57 void paramIn(Checkpoint *cp, const std::string &section,
58 const std::string &name, T &param);
59
60 template <class T>
61 bool optParamIn(Checkpoint *cp, const std::string &section,
62 const std::string &name, T &param);
63
64 template <class T>
65 void arrayParamOut(std::ostream &os, const std::string &name,
66 const T *param, unsigned size);
67
68 template <class T>
69 void arrayParamOut(std::ostream &os, const std::string &name,
70 const std::vector<T> &param);
71
72 template <class T>
73 void arrayParamIn(Checkpoint *cp, const std::string &section,
74 const std::string &name, T *param, unsigned size);
75
76 template <class T>
77 void arrayParamIn(Checkpoint *cp, const std::string &section,
78 const std::string &name, std::vector<T> &param);
79
80 void
81 objParamIn(Checkpoint *cp, const std::string &section,
82 const std::string &name, SimObject * &param);
83
84
85 //
86 // These macros are streamlined to use in serialize/unserialize
87 // functions. It's assumed that serialize() has a parameter 'os' for
88 // the ostream, and unserialize() has parameters 'cp' and 'section'.
89 #define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
90
91 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
92 #define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar)
93
94 // ENUMs are like SCALARs, but we cast them to ints on the way out
95 #define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
96
97 #define UNSERIALIZE_ENUM(scalar) \
98 do { \
99 int tmp; \
100 paramIn(cp, section, #scalar, tmp); \
101 scalar = (typeof(scalar))tmp; \
102 } while (0)
103
104 #define SERIALIZE_ARRAY(member, size) \
105 arrayParamOut(os, #member, member, size)
106
107 #define UNSERIALIZE_ARRAY(member, size) \
108 arrayParamIn(cp, section, #member, member, size)
109
110 #define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
111
112 #define UNSERIALIZE_OBJPTR(objptr) \
113 do { \
114 SimObject *sptr; \
115 objParamIn(cp, section, #objptr, sptr); \
116 objptr = dynamic_cast<typeof(objptr)>(sptr); \
117 } while (0)
118
119 /*
120 * Basic support for object serialization.
121 */
122 class Serializable
123 {
124 protected:
125 void nameOut(std::ostream &os);
126 void nameOut(std::ostream &os, const std::string &_name);
127
128 public:
129 Serializable();
130 virtual ~Serializable();
131
132 // manditory virtual function, so objects must provide names
133 virtual const std::string name() const = 0;
134
135 virtual void serialize(std::ostream &os);
136 virtual void unserialize(Checkpoint *cp, const std::string &section);
137
138 static Serializable *create(Checkpoint *cp, const std::string &section);
139
140 static int ckptCount;
141 static int ckptMaxCount;
142 static int ckptPrevCount;
143 static void serializeAll(const std::string &cpt_dir);
144 static void unserializeAll(const std::string &cpt_dir);
145 static void unserializeGlobals(Checkpoint *cp);
146 };
147
148 //
149 // A SerializableBuilder serves as an evaluation context for a set of
150 // parameters that describe a specific instance of a Serializable. This
151 // evaluation context corresponds to a section in the .ini file (as
152 // with the base ParamContext) plus an optional node in the
153 // configuration hierarchy (the configNode member) for resolving
154 // Serializable references. SerializableBuilder is an abstract superclass;
155 // derived classes specialize the class for particular subclasses of
156 // Serializable (e.g., BaseCache).
157 //
158 // For typical usage, see the definition of
159 // SerializableClass::createObject().
160 //
161 class SerializableBuilder
162 {
163 public:
164
165 SerializableBuilder() {}
166
167 virtual ~SerializableBuilder() {}
168
169 // Create the actual Serializable corresponding to the parameter
170 // values in this context. This function is overridden in derived
171 // classes to call a specific constructor for a particular
172 // subclass of Serializable.
173 virtual Serializable *create() = 0;
174 };
175
176 //
177 // An instance of SerializableClass corresponds to a class derived from
178 // Serializable. The SerializableClass instance serves to bind the string
179 // name (found in the config file) to a function that creates an
180 // instance of the appropriate derived class.
181 //
182 // This would be much cleaner in Smalltalk or Objective-C, where types
183 // are first-class objects themselves.
184 //
185 class SerializableClass
186 {
187 public:
188
189 // Type CreateFunc is a pointer to a function that creates a new
190 // simulation object builder based on a .ini-file parameter
191 // section (specified by the first string argument), a unique name
192 // for the object (specified by the second string argument), and
193 // an optional config hierarchy node (specified by the third
194 // argument). A pointer to the new SerializableBuilder is returned.
195 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
196 const std::string &section);
197
198 static std::map<std::string,CreateFunc> *classMap;
199
200 // Constructor. For example:
201 //
202 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
203 // newBaseCacheSerializableBuilder);
204 //
205 SerializableClass(const std::string &className, CreateFunc createFunc);
206
207 // create Serializable given name of class and pointer to
208 // configuration hierarchy node
209 static Serializable *createObject(Checkpoint *cp,
210 const std::string &section);
211 };
212
213 //
214 // Macros to encapsulate the magic of declaring & defining
215 // SerializableBuilder and SerializableClass objects
216 //
217
218 #define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
219 SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
220 OBJ_CLASS::createForUnserialize);
221
222 class Checkpoint
223 {
224 private:
225
226 IniFile *db;
227
228 public:
229 Checkpoint(const std::string &cpt_dir);
230
231 const std::string cptDir;
232
233 bool find(const std::string &section, const std::string &entry,
234 std::string &value);
235
236 bool findObj(const std::string &section, const std::string &entry,
237 SimObject *&value);
238
239 bool sectionExists(const std::string &section);
240
241 // The following static functions have to do with checkpoint
242 // creation rather than restoration. This class makes a handy
243 // namespace for them though. Currently no Checkpoint object is
244 // created on serialization (only unserialization) so we track the
245 // directory name as a global. It would be nice to change this
246 // someday
247
248 private:
249 // current directory we're serializing into.
250 static std::string currentDirectory;
251
252 public:
253 // Set the current directory. This function takes care of
254 // inserting curTick if there's a '%d' in the argument, and
255 // appends a '/' if necessary. The final name is returned.
256 static std::string setDir(const std::string &base_name);
257
258 // Export current checkpoint directory name so other objects can
259 // derive filenames from it (e.g., memory). The return value is
260 // guaranteed to end in '/' so filenames can be directly appended.
261 // This function is only valid while a checkpoint is being created.
262 static std::string dir();
263
264 // Filename for base checkpoint file within directory.
265 static const char *baseFilename;
266 };
267
268 #endif // __SERIALIZE_HH__