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