tick: rename Clock namespace to SimClock
[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/serialize.hh"
50 #include "sim/sim_events.hh"
51 #include "sim/sim_exit.hh"
52 #include "sim/sim_object.hh"
53
54 // For stat reset hack
55 #include "sim/stat_control.hh"
56
57 using namespace std;
58
59 extern SimObject *resolveSimObject(const string &);
60
61 //
62 // The base implementations use to_number for parsing and '<<' for
63 // displaying, suitable for integer types.
64 //
65 template <class T>
66 bool
67 parseParam(const string &s, T &value)
68 {
69 return to_number(s, value);
70 }
71
72 template <class T>
73 void
74 showParam(ostream &os, const T &value)
75 {
76 os << value;
77 }
78
79 //
80 // Template specializations:
81 // - char (8-bit integer)
82 // - floating-point types
83 // - bool
84 // - string
85 //
86
87 // Treat 8-bit ints (chars) as ints on output, not as chars
88 template <>
89 void
90 showParam(ostream &os, const char &value)
91 {
92 os << (int)value;
93 }
94
95
96 template <>
97 void
98 showParam(ostream &os, const unsigned char &value)
99 {
100 os << (unsigned int)value;
101 }
102
103
104 // Use sscanf() for FP types as to_number() only handles integers
105 template <>
106 bool
107 parseParam(const string &s, float &value)
108 {
109 return (sscanf(s.c_str(), "%f", &value) == 1);
110 }
111
112 template <>
113 bool
114 parseParam(const string &s, double &value)
115 {
116 return (sscanf(s.c_str(), "%lf", &value) == 1);
117 }
118
119 template <>
120 bool
121 parseParam(const string &s, bool &value)
122 {
123 const string &ls = to_lower(s);
124
125 if (ls == "true") {
126 value = true;
127 return true;
128 }
129
130 if (ls == "false") {
131 value = false;
132 return true;
133 }
134
135 return false;
136 }
137
138 // Display bools as strings
139 template <>
140 void
141 showParam(ostream &os, const bool &value)
142 {
143 os << (value ? "true" : "false");
144 }
145
146
147 // String requires no processing to speak of
148 template <>
149 bool
150 parseParam(const string &s, string &value)
151 {
152 value = s;
153 return true;
154 }
155
156 int Serializable::ckptMaxCount = 0;
157 int Serializable::ckptCount = 0;
158 int Serializable::ckptPrevCount = -1;
159
160 void
161 Serializable::nameOut(ostream &os)
162 {
163 os << "\n[" << name() << "]\n";
164 }
165
166 void
167 Serializable::nameOut(ostream &os, const string &_name)
168 {
169 os << "\n[" << _name << "]\n";
170 }
171
172 template <class T>
173 void
174 paramOut(ostream &os, const string &name, const T &param)
175 {
176 os << name << "=";
177 showParam(os, param);
178 os << "\n";
179 }
180
181 template <class T>
182 void
183 arrayParamOut(ostream &os, const string &name, const vector<T> &param)
184 {
185 typename vector<T>::size_type size = param.size();
186 os << name << "=";
187 if (size > 0)
188 showParam(os, param[0]);
189 for (typename vector<T>::size_type i = 1; i < size; ++i) {
190 os << " ";
191 showParam(os, param[i]);
192 }
193 os << "\n";
194 }
195
196
197 template <class T>
198 void
199 paramIn(Checkpoint *cp, const string &section, const string &name, T &param)
200 {
201 string str;
202 if (!cp->find(section, name, str) || !parseParam(str, param)) {
203 fatal("Can't unserialize '%s:%s'\n", section, name);
204 }
205 }
206
207 template <class T>
208 bool
209 optParamIn(Checkpoint *cp, const string &section, const string &name, T &param)
210 {
211 string str;
212 if (!cp->find(section, name, str) || !parseParam(str, param)) {
213 warn("optional parameter %s:%s not present\n", section, name);
214 return false;
215 } else {
216 return true;
217 }
218 }
219
220 template <class T>
221 void
222 arrayParamOut(ostream &os, const string &name, const T *param, unsigned size)
223 {
224 os << name << "=";
225 if (size > 0)
226 showParam(os, param[0]);
227 for (unsigned i = 1; i < size; ++i) {
228 os << " ";
229 showParam(os, param[i]);
230 }
231 os << "\n";
232 }
233
234
235 template <class T>
236 void
237 arrayParamIn(Checkpoint *cp, const string &section, const string &name,
238 T *param, unsigned size)
239 {
240 string str;
241 if (!cp->find(section, name, str)) {
242 fatal("Can't unserialize '%s:%s'\n", section, name);
243 }
244
245 // code below stolen from VectorParam<T>::parse().
246 // it would be nice to unify these somehow...
247
248 vector<string> tokens;
249
250 tokenize(tokens, str, ' ');
251
252 // Need this if we were doing a vector
253 // value.resize(tokens.size());
254
255 if (tokens.size() != size) {
256 fatal("Array size mismatch on %s:%s'\n", section, name);
257 }
258
259 for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
260 // need to parse into local variable to handle vector<bool>,
261 // for which operator[] returns a special reference class
262 // that's not the same as 'bool&', (since it's a packed
263 // vector)
264 T scalar_value;
265 if (!parseParam(tokens[i], scalar_value)) {
266 string err("could not parse \"");
267
268 err += str;
269 err += "\"";
270
271 fatal(err);
272 }
273
274 // assign parsed value to vector
275 param[i] = scalar_value;
276 }
277 }
278
279 template <class T>
280 void
281 arrayParamIn(Checkpoint *cp, const string &section,
282 const string &name, vector<T> &param)
283 {
284 string str;
285 if (!cp->find(section, name, str)) {
286 fatal("Can't unserialize '%s:%s'\n", section, name);
287 }
288
289 // code below stolen from VectorParam<T>::parse().
290 // it would be nice to unify these somehow...
291
292 vector<string> tokens;
293
294 tokenize(tokens, str, ' ');
295
296 // Need this if we were doing a vector
297 // value.resize(tokens.size());
298
299 param.resize(tokens.size());
300
301 for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
302 // need to parse into local variable to handle vector<bool>,
303 // for which operator[] returns a special reference class
304 // that's not the same as 'bool&', (since it's a packed
305 // vector)
306 T scalar_value;
307 if (!parseParam(tokens[i], scalar_value)) {
308 string err("could not parse \"");
309
310 err += str;
311 err += "\"";
312
313 fatal(err);
314 }
315
316 // assign parsed value to vector
317 param[i] = scalar_value;
318 }
319 }
320
321 void
322 objParamIn(Checkpoint *cp, const string &section,
323 const string &name, SimObject * &param)
324 {
325 if (!cp->findObj(section, name, param)) {
326 fatal("Can't unserialize '%s:%s'\n", section, name);
327 }
328 }
329
330
331 #define INSTANTIATE_PARAM_TEMPLATES(type) \
332 template void \
333 paramOut(ostream &os, const string &name, type const &param); \
334 template void \
335 paramIn(Checkpoint *cp, const string &section, \
336 const string &name, type & param); \
337 template bool \
338 optParamIn(Checkpoint *cp, const string &section, \
339 const string &name, type & param); \
340 template void \
341 arrayParamOut(ostream &os, const string &name, \
342 type const *param, unsigned size); \
343 template void \
344 arrayParamIn(Checkpoint *cp, const string &section, \
345 const string &name, type *param, unsigned size); \
346 template void \
347 arrayParamOut(ostream &os, const string &name, \
348 const vector<type> &param); \
349 template void \
350 arrayParamIn(Checkpoint *cp, const string &section, \
351 const string &name, vector<type> &param);
352
353 INSTANTIATE_PARAM_TEMPLATES(signed char)
354 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
355 INSTANTIATE_PARAM_TEMPLATES(signed short)
356 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
357 INSTANTIATE_PARAM_TEMPLATES(signed int)
358 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
359 INSTANTIATE_PARAM_TEMPLATES(signed long)
360 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
361 INSTANTIATE_PARAM_TEMPLATES(signed long long)
362 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
363 INSTANTIATE_PARAM_TEMPLATES(bool)
364 INSTANTIATE_PARAM_TEMPLATES(float)
365 INSTANTIATE_PARAM_TEMPLATES(double)
366 INSTANTIATE_PARAM_TEMPLATES(string)
367
368
369 /////////////////////////////
370
371 /// Container for serializing global variables (not associated with
372 /// any serialized object).
373 class Globals : public Serializable
374 {
375 public:
376 const string name() const;
377 void serialize(ostream &os);
378 void unserialize(Checkpoint *cp);
379 };
380
381 /// The one and only instance of the Globals class.
382 Globals globals;
383
384 const string
385 Globals::name() const
386 {
387 return "Globals";
388 }
389
390 void
391 Globals::serialize(ostream &os)
392 {
393 nameOut(os);
394 SERIALIZE_SCALAR(curTick);
395
396 nameOut(os, "MainEventQueue");
397 mainEventQueue.serialize(os);
398 }
399
400 void
401 Globals::unserialize(Checkpoint *cp)
402 {
403 const string &section = name();
404 UNSERIALIZE_SCALAR(curTick);
405
406 mainEventQueue.unserialize(cp, "MainEventQueue");
407 }
408
409 Serializable::Serializable()
410 {
411 }
412
413 Serializable::~Serializable()
414 {
415 }
416
417 void
418 Serializable::serialize(ostream &os)
419 {
420 }
421
422 void
423 Serializable::unserialize(Checkpoint *cp, const string &section)
424 {
425 }
426
427 void
428 Serializable::serializeAll(const string &cpt_dir)
429 {
430 setCheckpointDir(cpt_dir);
431 string dir = Checkpoint::dir();
432 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
433 fatal("couldn't mkdir %s\n", dir);
434
435 string cpt_file = dir + Checkpoint::baseFilename;
436 ofstream outstream(cpt_file.c_str());
437 time_t t = time(NULL);
438 if (!outstream.is_open())
439 fatal("Unable to open file %s for writing\n", cpt_file.c_str());
440 outstream << "## checkpoint generated: " << ctime(&t);
441
442 globals.serialize(outstream);
443 SimObject::serializeAll(outstream);
444 }
445
446 void
447 Serializable::unserializeAll(const string &cpt_dir)
448 {
449 setCheckpointDir(cpt_dir);
450 string dir = Checkpoint::dir();
451 string cpt_file = dir + Checkpoint::baseFilename;
452 string section = "";
453
454 DPRINTFR(Config, "Loading checkpoint dir '%s'\n",
455 dir);
456 Checkpoint *cp = new Checkpoint(dir, section);
457 unserializeGlobals(cp);
458 SimObject::unserializeAll(cp);
459 }
460
461 void
462 Serializable::unserializeGlobals(Checkpoint *cp)
463 {
464 globals.unserialize(cp);
465 }
466
467 const char *Checkpoint::baseFilename = "m5.cpt";
468
469 static string checkpointDirBase;
470
471 void
472 setCheckpointDir(const string &name)
473 {
474 checkpointDirBase = name;
475 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
476 checkpointDirBase += "/";
477 }
478
479 string
480 Checkpoint::dir()
481 {
482 // use csprintf to insert curTick into directory name if it
483 // appears to have a format placeholder in it.
484 return (checkpointDirBase.find("%") != string::npos) ?
485 csprintf(checkpointDirBase, curTick) : checkpointDirBase;
486 }
487
488 void
489 debug_serialize(const string &cpt_dir)
490 {
491 Serializable::serializeAll(cpt_dir);
492 }
493
494
495 ////////////////////////////////////////////////////////////////////////
496 //
497 // SerializableClass member definitions
498 //
499 ////////////////////////////////////////////////////////////////////////
500
501 // Map of class names to SerializableBuilder creation functions.
502 // Need to make this a pointer so we can force initialization on the
503 // first reference; otherwise, some SerializableClass constructors
504 // may be invoked before the classMap constructor.
505 map<string, SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
506
507 // SerializableClass constructor: add mapping to classMap
508 SerializableClass::SerializableClass(const string &className,
509 CreateFunc createFunc)
510 {
511 if (classMap == NULL)
512 classMap = new map<string, SerializableClass::CreateFunc>();
513
514 if ((*classMap)[className])
515 fatal("Error: simulation object class %s redefined\n", className);
516
517 // add className --> createFunc to class map
518 (*classMap)[className] = createFunc;
519 }
520
521 //
522 //
523 Serializable *
524 SerializableClass::createObject(Checkpoint *cp, const string &section)
525 {
526 string className;
527
528 if (!cp->find(section, "type", className)) {
529 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
530 section);
531 }
532
533 CreateFunc createFunc = (*classMap)[className];
534
535 if (createFunc == NULL) {
536 fatal("Serializable::create: no create function for class '%s'.\n",
537 className);
538 }
539
540 Serializable *object = createFunc(cp, section);
541
542 assert(object != NULL);
543
544 return object;
545 }
546
547
548 Serializable *
549 Serializable::create(Checkpoint *cp, const string &section)
550 {
551 Serializable *object = SerializableClass::createObject(cp, section);
552 object->unserialize(cp, section);
553 return object;
554 }
555
556
557 Checkpoint::Checkpoint(const string &cpt_dir, const string &path)
558 : db(new IniFile), basePath(path), cptDir(cpt_dir)
559 {
560 string filename = cpt_dir + "/" + Checkpoint::baseFilename;
561 if (!db->load(filename)) {
562 fatal("Can't load checkpoint file '%s'\n", filename);
563 }
564 }
565
566
567 bool
568 Checkpoint::find(const string &section, const string &entry, string &value)
569 {
570 return db->find(section, entry, value);
571 }
572
573
574 bool
575 Checkpoint::findObj(const string &section, const string &entry,
576 SimObject *&value)
577 {
578 string path;
579
580 if (!db->find(section, entry, path))
581 return false;
582
583 value = resolveSimObject(path);
584 return true;
585 }
586
587
588 bool
589 Checkpoint::sectionExists(const string &section)
590 {
591 return db->sectionExists(section);
592 }