Add myself to list of authors
[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 using namespace std;
56
57 int Serializable::ckptMaxCount = 0;
58 int Serializable::ckptCount = 0;
59 int Serializable::ckptPrevCount = -1;
60
61 void
62 Serializable::nameOut(ostream &os)
63 {
64 os << "\n[" << name() << "]\n";
65 }
66
67 void
68 Serializable::nameOut(ostream &os, const string &_name)
69 {
70 os << "\n[" << _name << "]\n";
71 }
72
73 template <class T>
74 void
75 paramOut(ostream &os, const std::string &name, const T &param)
76 {
77 os << name << "=";
78 showParam(os, param);
79 os << "\n";
80 }
81
82
83 template <class T>
84 void
85 paramIn(Checkpoint *cp, const std::string &section,
86 const std::string &name, T &param)
87 {
88 std::string str;
89 if (!cp->find(section, name, str) || !parseParam(str, param)) {
90 fatal("Can't unserialize '%s:%s'\n", section, name);
91 }
92 }
93
94
95 template <class T>
96 void
97 arrayParamOut(ostream &os, const std::string &name,
98 const T *param, int size)
99 {
100 os << name << "=";
101 if (size > 0)
102 showParam(os, param[0]);
103 for (int i = 1; i < size; ++i) {
104 os << " ";
105 showParam(os, param[i]);
106 }
107 os << "\n";
108 }
109
110
111 template <class T>
112 void
113 arrayParamIn(Checkpoint *cp, const std::string &section,
114 const std::string &name, T *param, int size)
115 {
116 std::string str;
117 if (!cp->find(section, name, str)) {
118 fatal("Can't unserialize '%s:%s'\n", section, name);
119 }
120
121 // code below stolen from VectorParam<T>::parse().
122 // it would be nice to unify these somehow...
123
124 vector<string> tokens;
125
126 tokenize(tokens, str, ' ');
127
128 // Need this if we were doing a vector
129 // value.resize(tokens.size());
130
131 if (tokens.size() != size) {
132 fatal("Array size mismatch on %s:%s'\n", section, name);
133 }
134
135 for (int i = 0; i < tokens.size(); i++) {
136 // need to parse into local variable to handle vector<bool>,
137 // for which operator[] returns a special reference class
138 // that's not the same as 'bool&', (since it's a packed
139 // vector)
140 T scalar_value;
141 if (!parseParam(tokens[i], scalar_value)) {
142 string err("could not parse \"");
143
144 err += str;
145 err += "\"";
146
147 fatal(err);
148 }
149
150 // assign parsed value to vector
151 param[i] = scalar_value;
152 }
153 }
154
155
156 void
157 objParamIn(Checkpoint *cp, const std::string &section,
158 const std::string &name, Serializable * &param)
159 {
160 if (!cp->findObj(section, name, param)) {
161 fatal("Can't unserialize '%s:%s'\n", section, name);
162 }
163 }
164
165
166 #define INSTANTIATE_PARAM_TEMPLATES(type) \
167 template void \
168 paramOut(ostream &os, const std::string &name, type const &param); \
169 template void \
170 paramIn(Checkpoint *cp, const std::string &section, \
171 const std::string &name, type & param); \
172 template void \
173 arrayParamOut(ostream &os, const std::string &name, \
174 type const *param, int size); \
175 template void \
176 arrayParamIn(Checkpoint *cp, const std::string &section, \
177 const std::string &name, type *param, int size);
178
179 INSTANTIATE_PARAM_TEMPLATES(signed char)
180 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
181 INSTANTIATE_PARAM_TEMPLATES(signed short)
182 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
183 INSTANTIATE_PARAM_TEMPLATES(signed int)
184 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
185 INSTANTIATE_PARAM_TEMPLATES(signed long)
186 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
187 INSTANTIATE_PARAM_TEMPLATES(signed long long)
188 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
189 INSTANTIATE_PARAM_TEMPLATES(bool)
190 INSTANTIATE_PARAM_TEMPLATES(string)
191
192
193 /////////////////////////////
194
195 /// Container for serializing global variables (not associated with
196 /// any serialized object).
197 class Globals : public Serializable
198 {
199 public:
200 const string name() const;
201 void serialize(ostream &os);
202 void unserialize(Checkpoint *cp);
203 };
204
205 /// The one and only instance of the Globals class.
206 Globals globals;
207
208 const string
209 Globals::name() const
210 {
211 return "Globals";
212 }
213
214 void
215 Globals::serialize(ostream &os)
216 {
217 nameOut(os);
218 SERIALIZE_SCALAR(curTick);
219
220 nameOut(os, "MainEventQueue");
221 mainEventQueue.serialize(os);
222 }
223
224 void
225 Globals::unserialize(Checkpoint *cp)
226 {
227 const string &section = name();
228 UNSERIALIZE_SCALAR(curTick);
229
230 mainEventQueue.unserialize(cp, "MainEventQueue");
231 }
232
233 void
234 Serializable::serializeAll()
235 {
236 string dir = Checkpoint::dir();
237 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
238 fatal("couldn't mkdir %s\n", dir);
239
240 string cpt_file = dir + Checkpoint::baseFilename;
241 ofstream outstream(cpt_file.c_str());
242 time_t t = time(NULL);
243 outstream << "// checkpoint generated: " << ctime(&t);
244
245 globals.serialize(outstream);
246 SimObject::serializeAll(outstream);
247
248 assert(Serializable::ckptPrevCount + 1 == Serializable::ckptCount);
249 Serializable::ckptPrevCount++;
250 if (ckptMaxCount && ++ckptCount >= ckptMaxCount)
251 exitSimLoop(curTick + 1, "Maximum number of checkpoints dropped");
252
253 }
254
255
256 void
257 Serializable::unserializeGlobals(Checkpoint *cp)
258 {
259 globals.unserialize(cp);
260 }
261
262
263 class SerializeEvent : public Event
264 {
265 protected:
266 Tick repeat;
267
268 public:
269 SerializeEvent(Tick _when, Tick _repeat);
270 virtual void process();
271 virtual void serialize(std::ostream &os)
272 {
273 panic("Cannot serialize the SerializeEvent");
274 }
275
276 };
277
278 SerializeEvent::SerializeEvent(Tick _when, Tick _repeat)
279 : Event(&mainEventQueue, Serialize_Pri), repeat(_repeat)
280 {
281 setFlags(AutoDelete);
282 schedule(_when);
283 }
284
285 void
286 SerializeEvent::process()
287 {
288 Serializable::serializeAll();
289 if (repeat)
290 schedule(curTick + repeat);
291 }
292
293 const char *Checkpoint::baseFilename = "m5.cpt";
294
295 static string checkpointDirBase;
296
297 string
298 Checkpoint::dir()
299 {
300 // use csprintf to insert curTick into directory name if it
301 // appears to have a format placeholder in it.
302 return (checkpointDirBase.find("%") != string::npos) ?
303 csprintf(checkpointDirBase, curTick) : checkpointDirBase;
304 }
305
306 void
307 Checkpoint::setup(Tick when, Tick period)
308 {
309 new SerializeEvent(when, period);
310 }
311
312 class SerializeParamContext : public ParamContext
313 {
314 private:
315 SerializeEvent *event;
316
317 public:
318 SerializeParamContext(const string &section);
319 ~SerializeParamContext();
320 void checkParams();
321 };
322
323 SerializeParamContext serialParams("serialize");
324
325 Param<string> serialize_dir(&serialParams, "dir",
326 "dir to stick checkpoint in "
327 "(sprintf format with cycle #)");
328
329 Param<Counter> serialize_cycle(&serialParams,
330 "cycle",
331 "cycle to serialize",
332 0);
333
334 Param<Counter> serialize_period(&serialParams,
335 "period",
336 "period to repeat serializations",
337 0);
338
339 Param<int> serialize_count(&serialParams, "count",
340 "maximum number of checkpoints to drop");
341
342 SerializeParamContext::SerializeParamContext(const string &section)
343 : ParamContext(section), event(NULL)
344 { }
345
346 SerializeParamContext::~SerializeParamContext()
347 {
348 }
349
350 void
351 SerializeParamContext::checkParams()
352 {
353 checkpointDirBase = simout.resolve(serialize_dir);
354
355 // guarantee that directory ends with a '/'
356 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
357 checkpointDirBase += "/";
358
359 if (serialize_cycle > 0)
360 Checkpoint::setup(serialize_cycle, serialize_period);
361
362 Serializable::ckptMaxCount = serialize_count;
363 }
364
365 void
366 debug_serialize()
367 {
368 Serializable::serializeAll();
369 }
370
371 void
372 debug_serialize(Tick when)
373 {
374 new SerializeEvent(when, 0);
375 }
376
377 ////////////////////////////////////////////////////////////////////////
378 //
379 // SerializableClass member definitions
380 //
381 ////////////////////////////////////////////////////////////////////////
382
383 // Map of class names to SerializableBuilder creation functions.
384 // Need to make this a pointer so we can force initialization on the
385 // first reference; otherwise, some SerializableClass constructors
386 // may be invoked before the classMap constructor.
387 map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
388
389 // SerializableClass constructor: add mapping to classMap
390 SerializableClass::SerializableClass(const string &className,
391 CreateFunc createFunc)
392 {
393 if (classMap == NULL)
394 classMap = new map<string,SerializableClass::CreateFunc>();
395
396 if ((*classMap)[className])
397 {
398 cerr << "Error: simulation object class " << className << " redefined"
399 << endl;
400 fatal("");
401 }
402
403 // add className --> createFunc to class map
404 (*classMap)[className] = createFunc;
405 }
406
407
408 //
409 //
410 Serializable *
411 SerializableClass::createObject(Checkpoint *cp,
412 const std::string &section)
413 {
414 string className;
415
416 if (!cp->find(section, "type", className)) {
417 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
418 section);
419 }
420
421 CreateFunc createFunc = (*classMap)[className];
422
423 if (createFunc == NULL) {
424 fatal("Serializable::create: no create function for class '%s'.\n",
425 className);
426 }
427
428 Serializable *object = createFunc(cp, section);
429
430 assert(object != NULL);
431
432 return object;
433 }
434
435
436 Serializable *
437 Serializable::create(Checkpoint *cp, const std::string &section)
438 {
439 Serializable *object = SerializableClass::createObject(cp, section);
440 object->unserialize(cp, section);
441 return object;
442 }
443
444
445 Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path)
446 : db(new IniFile), basePath(path), cptDir(cpt_dir)
447 {
448 string filename = cpt_dir + "/" + Checkpoint::baseFilename;
449 if (!db->load(filename)) {
450 fatal("Can't load checkpoint file '%s'\n", filename);
451 }
452 }
453
454
455 bool
456 Checkpoint::find(const std::string &section, const std::string &entry,
457 std::string &value)
458 {
459 return db->find(section, entry, value);
460 }
461
462
463 bool
464 Checkpoint::findObj(const std::string &section, const std::string &entry,
465 Serializable *&value)
466 {
467 string path;
468
469 if (!db->find(section, entry, path))
470 return false;
471
472 if ((value = objMap[path]) != NULL)
473 return true;
474
475 return false;
476 }
477
478
479 bool
480 Checkpoint::sectionExists(const std::string &section)
481 {
482 return db->sectionExists(section);
483 }