Instead of keeping track of the fraction of time that we're
[gem5.git] / sim / serialize.hh
1 /*
2 * Copyright (c) 2003 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
29 /* @file
30 * Serialization Interface Declarations
31 */
32
33 #ifndef __SERIALIZE_HH__
34 #define __SERIALIZE_HH__
35
36
37 #include <list>
38 #include <iostream>
39 #include <map>
40
41 #include "sim/host.hh"
42 #include "sim/configfile.hh"
43
44 class Serializeable;
45 class Checkpoint;
46
47 template <class T>
48 void paramOut(std::ostream &os, const std::string &name, const T& param);
49
50 template <class T>
51 void paramIn(Checkpoint *cp, const std::string &section,
52 const std::string &name, T& param);
53
54 template <class T>
55 void arrayParamOut(std::ostream &os, const std::string &name,
56 const T *param, int size);
57
58 template <class T>
59 void arrayParamIn(Checkpoint *cp, const std::string &section,
60 const std::string &name, T *param, int size);
61
62 void
63 objParamIn(Checkpoint *cp, const std::string &section,
64 const std::string &name, Serializeable * &param);
65
66
67 //
68 // These macros are streamlined to use in serialize/unserialize
69 // functions. It's assumed that serialize() has a parameter 'os' for
70 // the ostream, and unserialize() has parameters 'cp' and 'section'.
71 #define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
72
73 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
74
75 // ENUMs are like SCALARs, but we cast them to ints on the way out
76 #define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
77
78 #define UNSERIALIZE_ENUM(scalar) \
79 do { \
80 int tmp; \
81 paramIn(cp, section, #scalar, tmp); \
82 scalar = (typeof(scalar))tmp; \
83 } while (0)
84
85 #define SERIALIZE_ARRAY(member, size) \
86 arrayParamOut(os, #member, member, size)
87
88 #define UNSERIALIZE_ARRAY(member, size) \
89 arrayParamIn(cp, section, #member, member, size)
90
91 #define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
92
93 #define UNSERIALIZE_OBJPTR(objptr) \
94 do { \
95 Serializeable *sptr; \
96 objParamIn(cp, section, #objptr, sptr); \
97 objptr = dynamic_cast<typeof(objptr)>(sptr); \
98 } while (0)
99
100 /*
101 * Basic support for object serialization.
102 */
103 class Serializeable
104 {
105 public:
106
107 friend class Serializer;
108
109 protected:
110 bool serialized;
111 static Serializer *serializer;
112
113 void mark();
114 void nameOut(std::ostream& os);
115 void nameOut(std::ostream& os, const std::string &_name);
116
117 public:
118 Serializeable();
119 virtual ~Serializeable();
120
121 // manditory virtual function, so objects must provide names
122 virtual std::string name() const = 0;
123
124 virtual void serialize(std::ostream& os) {}
125 virtual void unserialize(Checkpoint *cp, const std::string &section) {}
126
127 static Serializeable *create(Checkpoint *cp,
128 const std::string &section);
129 };
130
131 class Serializer
132 {
133 friend class Serializeable;
134
135 protected:
136 typedef std::list<Serializeable *> serlist_t;
137 serlist_t objects;
138 std::ostream *output;
139 std::ostream &out() const;
140
141 public:
142 Serializer();
143 virtual ~Serializer();
144
145 private:
146 void add_object(Serializeable *obj);
147 void add_objects();
148
149 public:
150 void serialize();
151 };
152
153 //
154 // A SerializeableBuilder serves as an evaluation context for a set of
155 // parameters that describe a specific instance of a Serializeable. This
156 // evaluation context corresponds to a section in the .ini file (as
157 // with the base ParamContext) plus an optional node in the
158 // configuration hierarchy (the configNode member) for resolving
159 // Serializeable references. SerializeableBuilder is an abstract superclass;
160 // derived classes specialize the class for particular subclasses of
161 // Serializeable (e.g., BaseCache).
162 //
163 // For typical usage, see the definition of
164 // SerializeableClass::createObject().
165 //
166 class SerializeableBuilder
167 {
168 public:
169
170 SerializeableBuilder() {}
171
172 virtual ~SerializeableBuilder() {}
173
174 // Create the actual Serializeable corresponding to the parameter
175 // values in this context. This function is overridden in derived
176 // classes to call a specific constructor for a particular
177 // subclass of Serializeable.
178 virtual Serializeable *create() = 0;
179 };
180
181 //
182 // An instance of SerializeableClass corresponds to a class derived from
183 // Serializeable. The SerializeableClass instance serves to bind the string
184 // name (found in the config file) to a function that creates an
185 // instance of the appropriate derived class.
186 //
187 // This would be much cleaner in Smalltalk or Objective-C, where types
188 // are first-class objects themselves.
189 //
190 class SerializeableClass
191 {
192 public:
193
194 // Type CreateFunc is a pointer to a function that creates a new
195 // simulation object builder based on a .ini-file parameter
196 // section (specified by the first string argument), a unique name
197 // for the object (specified by the second string argument), and
198 // an optional config hierarchy node (specified by the third
199 // argument). A pointer to the new SerializeableBuilder is returned.
200 typedef Serializeable *(*CreateFunc)(Checkpoint *cp,
201 const std::string &section);
202
203 static std::map<std::string,CreateFunc> *classMap;
204
205 // Constructor. For example:
206 //
207 // SerializeableClass baseCacheSerializeableClass("BaseCacheSerializeable",
208 // newBaseCacheSerializeableBuilder);
209 //
210 SerializeableClass(const std::string &className, CreateFunc createFunc);
211
212 // create Serializeable given name of class and pointer to
213 // configuration hierarchy node
214 static Serializeable *createObject(Checkpoint *cp,
215 const std::string &section);
216 };
217
218 //
219 // Macros to encapsulate the magic of declaring & defining
220 // SerializeableBuilder and SerializeableClass objects
221 //
222
223 #define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
224 SerializeableClass the##OBJ_CLASS##Class(CLASS_NAME, \
225 OBJ_CLASS::createForUnserialize);
226
227 class Checkpoint
228 {
229 private:
230
231 IniFile *db;
232 const std::string basePath;
233 const ConfigNode *configNode;
234 std::map<std::string, Serializeable*> objMap;
235
236 public:
237 Checkpoint(const std::string &filename, const std::string &path,
238 const ConfigNode *_configNode);
239
240 bool find(const std::string &section, const std::string &entry,
241 std::string &value);
242
243 bool findObj(const std::string &section, const std::string &entry,
244 Serializeable *&value);
245
246 bool sectionExists(const std::string &section);
247 };
248
249
250 //
251 // Export checkpoint filename param so other objects can derive
252 // filenames from it (e.g., memory).
253 //
254 std::string CheckpointDir();
255 void SetupCheckpoint(Tick when, Tick period = 0);
256
257 #endif // __SERIALIZE_HH__