sim: bump checkpoint version for multiple event queues
[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 <iostream>
42 #include <list>
43 #include <map>
44 #include <vector>
45
46 #include "base/types.hh"
47
48 class IniFile;
49 class Serializable;
50 class Checkpoint;
51 class SimObject;
52 class EventQueue;
53
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;
62
63 template <class T>
64 void paramOut(std::ostream &os, const std::string &name, const T &param);
65
66 template <class T>
67 void paramIn(Checkpoint *cp, const std::string &section,
68 const std::string &name, T &param);
69
70 template <class T>
71 bool optParamIn(Checkpoint *cp, const std::string &section,
72 const std::string &name, T &param);
73
74 template <class T>
75 void arrayParamOut(std::ostream &os, const std::string &name,
76 const T *param, unsigned size);
77
78 template <class T>
79 void arrayParamOut(std::ostream &os, const std::string &name,
80 const std::vector<T> &param);
81
82 template <class T>
83 void arrayParamOut(std::ostream &os, const std::string &name,
84 const std::list<T> &param);
85
86 template <class T>
87 void arrayParamIn(Checkpoint *cp, const std::string &section,
88 const std::string &name, T *param, unsigned size);
89
90 template <class T>
91 void arrayParamIn(Checkpoint *cp, const std::string &section,
92 const std::string &name, std::vector<T> &param);
93
94 template <class T>
95 void arrayParamIn(Checkpoint *cp, const std::string &section,
96 const std::string &name, std::list<T> &param);
97
98 void
99 objParamIn(Checkpoint *cp, const std::string &section,
100 const std::string &name, SimObject * &param);
101
102 template <typename T>
103 void fromInt(T &t, int i)
104 {
105 t = (T)i;
106 }
107
108 template <typename T>
109 void fromSimObject(T &t, SimObject *s)
110 {
111 t = dynamic_cast<T>(s);
112 }
113
114 //
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)
119
120 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
121 #define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar)
122
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)
125
126 #define UNSERIALIZE_ENUM(scalar) \
127 do { \
128 int tmp; \
129 paramIn(cp, section, #scalar, tmp); \
130 fromInt(scalar, tmp); \
131 } while (0)
132
133 #define SERIALIZE_ARRAY(member, size) \
134 arrayParamOut(os, #member, member, size)
135
136 #define UNSERIALIZE_ARRAY(member, size) \
137 arrayParamIn(cp, section, #member, member, size)
138
139 #define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
140
141 #define UNSERIALIZE_OBJPTR(objptr) \
142 do { \
143 SimObject *sptr; \
144 objParamIn(cp, section, #objptr, sptr); \
145 fromSimObject(objptr, sptr); \
146 } while (0)
147
148 /**
149 * Basic support for object serialization.
150 *
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.
156 */
157 class Serializable
158 {
159 protected:
160 void nameOut(std::ostream &os);
161 void nameOut(std::ostream &os, const std::string &_name);
162
163 public:
164 Serializable();
165 virtual ~Serializable();
166
167 // manditory virtual function, so objects must provide names
168 virtual const std::string name() const = 0;
169
170 virtual void serialize(std::ostream &os);
171 virtual void unserialize(Checkpoint *cp, const std::string &section);
172
173 static Serializable *create(Checkpoint *cp, const std::string &section);
174
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);
180 };
181
182 void debug_serialize(const std::string &cpt_dir);
183
184 //
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).
193 //
194 // For typical usage, see the definition of
195 // SerializableClass::createObject().
196 //
197 class SerializableBuilder
198 {
199 public:
200
201 SerializableBuilder() {}
202
203 virtual ~SerializableBuilder() {}
204
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;
210 };
211
212 //
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.
217 //
218 // This would be much cleaner in Smalltalk or Objective-C, where types
219 // are first-class objects themselves.
220 //
221 class SerializableClass
222 {
223 public:
224
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 &section);
233
234 static std::map<std::string,CreateFunc> *classMap;
235
236 // Constructor. For example:
237 //
238 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
239 // newBaseCacheSerializableBuilder);
240 //
241 SerializableClass(const std::string &className, CreateFunc createFunc);
242
243 // create Serializable given name of class and pointer to
244 // configuration hierarchy node
245 static Serializable *createObject(Checkpoint *cp,
246 const std::string &section);
247 };
248
249 //
250 // Macros to encapsulate the magic of declaring & defining
251 // SerializableBuilder and SerializableClass objects
252 //
253
254 #define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
255 SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
256 OBJ_CLASS::createForUnserialize);
257
258 class Checkpoint
259 {
260 private:
261
262 IniFile *db;
263
264 public:
265 Checkpoint(const std::string &cpt_dir);
266 ~Checkpoint();
267
268 const std::string cptDir;
269
270 bool find(const std::string &section, const std::string &entry,
271 std::string &value);
272
273 bool findObj(const std::string &section, const std::string &entry,
274 SimObject *&value);
275
276 bool sectionExists(const std::string &section);
277
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
283 // someday
284
285 private:
286 // current directory we're serializing into.
287 static std::string currentDirectory;
288
289 public:
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);
294
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();
300
301 // Filename for base checkpoint file within directory.
302 static const char *baseFilename;
303 };
304
305 #endif // __SERIALIZE_HH__