d32bb1142e5fdd13a06570588e5a9add3c5afed3
[gem5.git] / src / sim / serialize.cc
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 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <errno.h>
37
38 #include <fstream>
39 #include <list>
40 #include <string>
41 #include <vector>
42
43 #include "base/inifile.hh"
44 #include "base/misc.hh"
45 #include "base/output.hh"
46 #include "base/str.hh"
47 #include "base/trace.hh"
48 #include "sim/eventq.hh"
49 #include "sim/param.hh"
50 #include "sim/serialize.hh"
51 #include "sim/sim_events.hh"
52 #include "sim/sim_exit.hh"
53 #include "sim/sim_object.hh"
54
55 // For stat reset hack
56 #include "sim/stat_control.hh"
57
58 using namespace std;
59
60 extern SimObject *resolveSimObject(const string &);
61
62 int Serializable::ckptMaxCount = 0;
63 int Serializable::ckptCount = 0;
64 int Serializable::ckptPrevCount = -1;
65
66 void
67 Serializable::nameOut(ostream &os)
68 {
69 os << "\n[" << name() << "]\n";
70 }
71
72 void
73 Serializable::nameOut(ostream &os, const string &_name)
74 {
75 os << "\n[" << _name << "]\n";
76 }
77
78 template <class T>
79 void
80 paramOut(ostream &os, const std::string &name, const T &param)
81 {
82 os << name << "=";
83 showParam(os, param);
84 os << "\n";
85 }
86
87
88 template <class T>
89 void
90 paramIn(Checkpoint *cp, const std::string &section,
91 const std::string &name, T &param)
92 {
93 std::string str;
94 if (!cp->find(section, name, str) || !parseParam(str, param)) {
95 fatal("Can't unserialize '%s:%s'\n", section, name);
96 }
97 }
98
99
100 template <class T>
101 void
102 arrayParamOut(ostream &os, const std::string &name,
103 const T *param, int size)
104 {
105 os << name << "=";
106 if (size > 0)
107 showParam(os, param[0]);
108 for (int i = 1; i < size; ++i) {
109 os << " ";
110 showParam(os, param[i]);
111 }
112 os << "\n";
113 }
114
115
116 template <class T>
117 void
118 arrayParamIn(Checkpoint *cp, const std::string &section,
119 const std::string &name, T *param, int size)
120 {
121 std::string str;
122 if (!cp->find(section, name, str)) {
123 fatal("Can't unserialize '%s:%s'\n", section, name);
124 }
125
126 // code below stolen from VectorParam<T>::parse().
127 // it would be nice to unify these somehow...
128
129 vector<string> tokens;
130
131 tokenize(tokens, str, ' ');
132
133 // Need this if we were doing a vector
134 // value.resize(tokens.size());
135
136 if (tokens.size() != size) {
137 fatal("Array size mismatch on %s:%s'\n", section, name);
138 }
139
140 for (int i = 0; i < tokens.size(); i++) {
141 // need to parse into local variable to handle vector<bool>,
142 // for which operator[] returns a special reference class
143 // that's not the same as 'bool&', (since it's a packed
144 // vector)
145 T scalar_value;
146 if (!parseParam(tokens[i], scalar_value)) {
147 string err("could not parse \"");
148
149 err += str;
150 err += "\"";
151
152 fatal(err);
153 }
154
155 // assign parsed value to vector
156 param[i] = scalar_value;
157 }
158 }
159
160
161 void
162 objParamIn(Checkpoint *cp, const std::string &section,
163 const std::string &name, SimObject * &param)
164 {
165 if (!cp->findObj(section, name, param)) {
166 fatal("Can't unserialize '%s:%s'\n", section, name);
167 }
168 }
169
170
171 #define INSTANTIATE_PARAM_TEMPLATES(type) \
172 template void \
173 paramOut(ostream &os, const std::string &name, type const &param); \
174 template void \
175 paramIn(Checkpoint *cp, const std::string &section, \
176 const std::string &name, type & param); \
177 template void \
178 arrayParamOut(ostream &os, const std::string &name, \
179 type const *param, int size); \
180 template void \
181 arrayParamIn(Checkpoint *cp, const std::string &section, \
182 const std::string &name, type *param, int size);
183
184 INSTANTIATE_PARAM_TEMPLATES(signed char)
185 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
186 INSTANTIATE_PARAM_TEMPLATES(signed short)
187 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
188 INSTANTIATE_PARAM_TEMPLATES(signed int)
189 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
190 INSTANTIATE_PARAM_TEMPLATES(signed long)
191 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
192 INSTANTIATE_PARAM_TEMPLATES(signed long long)
193 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
194 INSTANTIATE_PARAM_TEMPLATES(bool)
195 INSTANTIATE_PARAM_TEMPLATES(string)
196
197
198 /////////////////////////////
199
200 /// Container for serializing global variables (not associated with
201 /// any serialized object).
202 class Globals : public Serializable
203 {
204 public:
205 const string name() const;
206 void serialize(ostream &os);
207 void unserialize(Checkpoint *cp);
208 };
209
210 /// The one and only instance of the Globals class.
211 Globals globals;
212
213 const string
214 Globals::name() const
215 {
216 return "Globals";
217 }
218
219 void
220 Globals::serialize(ostream &os)
221 {
222 nameOut(os);
223 SERIALIZE_SCALAR(curTick);
224
225 nameOut(os, "MainEventQueue");
226 mainEventQueue.serialize(os);
227 }
228
229 void
230 Globals::unserialize(Checkpoint *cp)
231 {
232 const string &section = name();
233 UNSERIALIZE_SCALAR(curTick);
234
235 mainEventQueue.unserialize(cp, "MainEventQueue");
236 }
237
238 void
239 Serializable::serializeAll(const std::string &cpt_dir)
240 {
241 setCheckpointDir(cpt_dir);
242 string dir = Checkpoint::dir();
243 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
244 fatal("couldn't mkdir %s\n", dir);
245
246 string cpt_file = dir + Checkpoint::baseFilename;
247 ofstream outstream(cpt_file.c_str());
248 time_t t = time(NULL);
249 outstream << "// checkpoint generated: " << ctime(&t);
250
251 globals.serialize(outstream);
252 SimObject::serializeAll(outstream);
253 }
254
255 void
256 Serializable::unserializeAll(const std::string &cpt_dir)
257 {
258 setCheckpointDir(cpt_dir);
259 string dir = Checkpoint::dir();
260 string cpt_file = dir + Checkpoint::baseFilename;
261 string section = "";
262
263 DPRINTFR(Config, "Loading checkpoint dir '%s'\n",
264 dir);
265 Checkpoint *cp = new Checkpoint(dir, section);
266 unserializeGlobals(cp);
267
268 SimObject::unserializeAll(cp);
269 }
270
271 void
272 Serializable::unserializeGlobals(Checkpoint *cp)
273 {
274 globals.unserialize(cp);
275 }
276
277 const char *Checkpoint::baseFilename = "m5.cpt";
278
279 static string checkpointDirBase;
280
281 void
282 setCheckpointDir(const std::string &name)
283 {
284 checkpointDirBase = name;
285 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
286 checkpointDirBase += "/";
287 }
288
289 string
290 Checkpoint::dir()
291 {
292 // use csprintf to insert curTick into directory name if it
293 // appears to have a format placeholder in it.
294 return (checkpointDirBase.find("%") != string::npos) ?
295 csprintf(checkpointDirBase, curTick) : checkpointDirBase;
296 }
297
298 void
299 debug_serialize(const std::string &cpt_dir)
300 {
301 Serializable::serializeAll(cpt_dir);
302 }
303
304
305 ////////////////////////////////////////////////////////////////////////
306 //
307 // SerializableClass member definitions
308 //
309 ////////////////////////////////////////////////////////////////////////
310
311 // Map of class names to SerializableBuilder creation functions.
312 // Need to make this a pointer so we can force initialization on the
313 // first reference; otherwise, some SerializableClass constructors
314 // may be invoked before the classMap constructor.
315 map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
316
317 // SerializableClass constructor: add mapping to classMap
318 SerializableClass::SerializableClass(const string &className,
319 CreateFunc createFunc)
320 {
321 if (classMap == NULL)
322 classMap = new map<string,SerializableClass::CreateFunc>();
323
324 if ((*classMap)[className])
325 {
326 cerr << "Error: simulation object class " << className << " redefined"
327 << endl;
328 fatal("");
329 }
330
331 // add className --> createFunc to class map
332 (*classMap)[className] = createFunc;
333 }
334
335
336 //
337 //
338 Serializable *
339 SerializableClass::createObject(Checkpoint *cp,
340 const std::string &section)
341 {
342 string className;
343
344 if (!cp->find(section, "type", className)) {
345 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
346 section);
347 }
348
349 CreateFunc createFunc = (*classMap)[className];
350
351 if (createFunc == NULL) {
352 fatal("Serializable::create: no create function for class '%s'.\n",
353 className);
354 }
355
356 Serializable *object = createFunc(cp, section);
357
358 assert(object != NULL);
359
360 return object;
361 }
362
363
364 Serializable *
365 Serializable::create(Checkpoint *cp, const std::string &section)
366 {
367 Serializable *object = SerializableClass::createObject(cp, section);
368 object->unserialize(cp, section);
369 return object;
370 }
371
372
373 Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path)
374 : db(new IniFile), basePath(path), cptDir(cpt_dir)
375 {
376 string filename = cpt_dir + "/" + Checkpoint::baseFilename;
377 if (!db->load(filename)) {
378 fatal("Can't load checkpoint file '%s'\n", filename);
379 }
380 }
381
382
383 bool
384 Checkpoint::find(const std::string &section, const std::string &entry,
385 std::string &value)
386 {
387 return db->find(section, entry, value);
388 }
389
390
391 bool
392 Checkpoint::findObj(const std::string &section, const std::string &entry,
393 SimObject *&value)
394 {
395 string path;
396
397 if (!db->find(section, entry, path))
398 return false;
399
400 value = resolveSimObject(path);
401 return true;
402 }
403
404
405 bool
406 Checkpoint::sectionExists(const std::string &section)
407 {
408 return db->sectionExists(section);
409 }