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