syscall_emul: fix warn_once behavior
[gem5.git] / src / sim / serialize.cc
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * Copyright (c) 2013 Mark D. Hill and David A. Wood
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution;
14 * neither the name of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Authors: Nathan Binkert
31 * Erik Hallnor
32 * Steve Reinhardt
33 */
34
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38
39 #include <cerrno>
40 #include <fstream>
41 #include <list>
42 #include <string>
43 #include <vector>
44
45 #include "base/inifile.hh"
46 #include "base/misc.hh"
47 #include "base/output.hh"
48 #include "base/str.hh"
49 #include "base/trace.hh"
50 #include "sim/eventq.hh"
51 #include "sim/serialize.hh"
52 #include "sim/sim_events.hh"
53 #include "sim/sim_exit.hh"
54 #include "sim/sim_object.hh"
55
56 // For stat reset hack
57 #include "sim/stat_control.hh"
58
59 using namespace std;
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 template <>
113 bool
114 parseParam(const string &s, float &value)
115 {
116 return to_number(s, value);
117 }
118
119 template <>
120 bool
121 parseParam(const string &s, double &value)
122 {
123 return to_number(s, value);
124 }
125
126 template <>
127 bool
128 parseParam(const string &s, bool &value)
129 {
130 return to_bool(s, value);
131 }
132
133 // Display bools as strings
134 template <>
135 void
136 showParam(ostream &os, const bool &value)
137 {
138 os << (value ? "true" : "false");
139 }
140
141
142 // String requires no processing to speak of
143 template <>
144 bool
145 parseParam(const string &s, string &value)
146 {
147 value = s;
148 return true;
149 }
150
151 int Serializable::ckptMaxCount = 0;
152 int Serializable::ckptCount = 0;
153 int Serializable::ckptPrevCount = -1;
154
155 void
156 Serializable::nameOut(ostream &os)
157 {
158 os << "\n[" << name() << "]\n";
159 }
160
161 void
162 Serializable::nameOut(ostream &os, const string &_name)
163 {
164 os << "\n[" << _name << "]\n";
165 }
166
167 template <class T>
168 void
169 paramOut(ostream &os, const string &name, const T &param)
170 {
171 os << name << "=";
172 showParam(os, param);
173 os << "\n";
174 }
175
176 template <class T>
177 void
178 arrayParamOut(ostream &os, const string &name, const vector<T> &param)
179 {
180 typename vector<T>::size_type size = param.size();
181 os << name << "=";
182 if (size > 0)
183 showParam(os, param[0]);
184 for (typename vector<T>::size_type i = 1; i < size; ++i) {
185 os << " ";
186 showParam(os, param[i]);
187 }
188 os << "\n";
189 }
190
191 template <class T>
192 void
193 arrayParamOut(ostream &os, const string &name, const list<T> &param)
194 {
195 typename list<T>::const_iterator it = param.begin();
196
197 os << name << "=";
198 if (param.size() > 0)
199 showParam(os, *it);
200 it++;
201 while (it != param.end()) {
202 os << " ";
203 showParam(os, *it);
204 it++;
205 }
206 os << "\n";
207 }
208
209 template <class T>
210 void
211 paramIn(Checkpoint *cp, const string &section, const string &name, T &param)
212 {
213 string str;
214 if (!cp->find(section, name, str) || !parseParam(str, param)) {
215 fatal("Can't unserialize '%s:%s'\n", section, name);
216 }
217 }
218
219 template <class T>
220 bool
221 optParamIn(Checkpoint *cp, const string &section, const string &name, T &param)
222 {
223 string str;
224 if (!cp->find(section, name, str) || !parseParam(str, param)) {
225 warn("optional parameter %s:%s not present\n", section, name);
226 return false;
227 } else {
228 return true;
229 }
230 }
231
232 template <class T>
233 void
234 arrayParamOut(ostream &os, const string &name, const T *param, unsigned size)
235 {
236 os << name << "=";
237 if (size > 0)
238 showParam(os, param[0]);
239 for (unsigned i = 1; i < size; ++i) {
240 os << " ";
241 showParam(os, param[i]);
242 }
243 os << "\n";
244 }
245
246
247 template <class T>
248 void
249 arrayParamIn(Checkpoint *cp, const string &section, const string &name,
250 T *param, unsigned size)
251 {
252 string str;
253 if (!cp->find(section, name, str)) {
254 fatal("Can't unserialize '%s:%s'\n", section, name);
255 }
256
257 // code below stolen from VectorParam<T>::parse().
258 // it would be nice to unify these somehow...
259
260 vector<string> tokens;
261
262 tokenize(tokens, str, ' ');
263
264 // Need this if we were doing a vector
265 // value.resize(tokens.size());
266
267 if (tokens.size() != size) {
268 fatal("Array size mismatch on %s:%s'\n", section, name);
269 }
270
271 for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
272 // need to parse into local variable to handle vector<bool>,
273 // for which operator[] returns a special reference class
274 // that's not the same as 'bool&', (since it's a packed
275 // vector)
276 T scalar_value = 0;
277 if (!parseParam(tokens[i], scalar_value)) {
278 string err("could not parse \"");
279
280 err += str;
281 err += "\"";
282
283 fatal(err);
284 }
285
286 // assign parsed value to vector
287 param[i] = scalar_value;
288 }
289 }
290
291 template <class T>
292 void
293 arrayParamIn(Checkpoint *cp, const string &section,
294 const string &name, vector<T> &param)
295 {
296 string str;
297 if (!cp->find(section, name, str)) {
298 fatal("Can't unserialize '%s:%s'\n", section, name);
299 }
300
301 // code below stolen from VectorParam<T>::parse().
302 // it would be nice to unify these somehow...
303
304 vector<string> tokens;
305
306 tokenize(tokens, str, ' ');
307
308 // Need this if we were doing a vector
309 // value.resize(tokens.size());
310
311 param.resize(tokens.size());
312
313 for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
314 // need to parse into local variable to handle vector<bool>,
315 // for which operator[] returns a special reference class
316 // that's not the same as 'bool&', (since it's a packed
317 // vector)
318 T scalar_value = 0;
319 if (!parseParam(tokens[i], scalar_value)) {
320 string err("could not parse \"");
321
322 err += str;
323 err += "\"";
324
325 fatal(err);
326 }
327
328 // assign parsed value to vector
329 param[i] = scalar_value;
330 }
331 }
332
333 template <class T>
334 void
335 arrayParamIn(Checkpoint *cp, const string &section,
336 const string &name, list<T> &param)
337 {
338 string str;
339 if (!cp->find(section, name, str)) {
340 fatal("Can't unserialize '%s:%s'\n", section, name);
341 }
342 param.clear();
343
344 vector<string> tokens;
345 tokenize(tokens, str, ' ');
346
347 for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
348 T scalar_value = 0;
349 if (!parseParam(tokens[i], scalar_value)) {
350 string err("could not parse \"");
351
352 err += str;
353 err += "\"";
354
355 fatal(err);
356 }
357
358 // assign parsed value to vector
359 param.push_back(scalar_value);
360 }
361 }
362
363
364 void
365 objParamIn(Checkpoint *cp, const string &section,
366 const string &name, SimObject * &param)
367 {
368 if (!cp->findObj(section, name, param)) {
369 fatal("Can't unserialize '%s:%s'\n", section, name);
370 }
371 }
372
373
374 #define INSTANTIATE_PARAM_TEMPLATES(type) \
375 template void \
376 paramOut(ostream &os, const string &name, type const &param); \
377 template void \
378 paramIn(Checkpoint *cp, const string &section, \
379 const string &name, type & param); \
380 template bool \
381 optParamIn(Checkpoint *cp, const string &section, \
382 const string &name, type & param); \
383 template void \
384 arrayParamOut(ostream &os, const string &name, \
385 type const *param, unsigned size); \
386 template void \
387 arrayParamIn(Checkpoint *cp, const string &section, \
388 const string &name, type *param, unsigned size); \
389 template void \
390 arrayParamOut(ostream &os, const string &name, \
391 const vector<type> &param); \
392 template void \
393 arrayParamIn(Checkpoint *cp, const string &section, \
394 const string &name, vector<type> &param); \
395 template void \
396 arrayParamOut(ostream &os, const string &name, \
397 const list<type> &param); \
398 template void \
399 arrayParamIn(Checkpoint *cp, const string &section, \
400 const string &name, list<type> &param);
401
402 INSTANTIATE_PARAM_TEMPLATES(char)
403 INSTANTIATE_PARAM_TEMPLATES(signed char)
404 INSTANTIATE_PARAM_TEMPLATES(unsigned char)
405 INSTANTIATE_PARAM_TEMPLATES(signed short)
406 INSTANTIATE_PARAM_TEMPLATES(unsigned short)
407 INSTANTIATE_PARAM_TEMPLATES(signed int)
408 INSTANTIATE_PARAM_TEMPLATES(unsigned int)
409 INSTANTIATE_PARAM_TEMPLATES(signed long)
410 INSTANTIATE_PARAM_TEMPLATES(unsigned long)
411 INSTANTIATE_PARAM_TEMPLATES(signed long long)
412 INSTANTIATE_PARAM_TEMPLATES(unsigned long long)
413 INSTANTIATE_PARAM_TEMPLATES(bool)
414 INSTANTIATE_PARAM_TEMPLATES(float)
415 INSTANTIATE_PARAM_TEMPLATES(double)
416 INSTANTIATE_PARAM_TEMPLATES(string)
417
418
419 /////////////////////////////
420
421 /// Container for serializing global variables (not associated with
422 /// any serialized object).
423 class Globals : public Serializable
424 {
425 public:
426 const string name() const;
427 void serialize(ostream &os);
428 void unserialize(Checkpoint *cp, const std::string &section);
429 };
430
431 /// The one and only instance of the Globals class.
432 Globals globals;
433
434 const string
435 Globals::name() const
436 {
437 return "Globals";
438 }
439
440 void
441 Globals::serialize(ostream &os)
442 {
443 nameOut(os);
444 paramOut(os, "curTick", curTick());
445
446 paramOut(os, "numMainEventQueues", numMainEventQueues);
447
448 for (uint32_t i = 0; i < numMainEventQueues; ++i) {
449 nameOut(os, "MainEventQueue");
450 mainEventQueue[i]->serialize(os);
451 }
452 }
453
454 void
455 Globals::unserialize(Checkpoint *cp, const std::string &section)
456 {
457 Tick tick;
458 paramIn(cp, section, "curTick", tick);
459 paramIn(cp, section, "numMainEventQueues", numMainEventQueues);
460
461 for (uint32_t i = 0; i < numMainEventQueues; ++i) {
462 mainEventQueue[i]->setCurTick(tick);
463 mainEventQueue[i]->unserialize(cp, "MainEventQueue");
464 }
465 }
466
467 Serializable::Serializable()
468 {
469 }
470
471 Serializable::~Serializable()
472 {
473 }
474
475 void
476 Serializable::serialize(ostream &os)
477 {
478 }
479
480 void
481 Serializable::unserialize(Checkpoint *cp, const string &section)
482 {
483 }
484
485 void
486 Serializable::serializeAll(const string &cpt_dir)
487 {
488 string dir = Checkpoint::setDir(cpt_dir);
489 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
490 fatal("couldn't mkdir %s\n", dir);
491
492 string cpt_file = dir + Checkpoint::baseFilename;
493 ofstream outstream(cpt_file.c_str());
494 time_t t = time(NULL);
495 if (!outstream.is_open())
496 fatal("Unable to open file %s for writing\n", cpt_file.c_str());
497 outstream << "## checkpoint generated: " << ctime(&t);
498
499 globals.serialize(outstream);
500 SimObject::serializeAll(outstream);
501 }
502
503 void
504 Serializable::unserializeGlobals(Checkpoint *cp)
505 {
506 globals.unserialize(cp, globals.name());
507 }
508
509 void
510 debug_serialize(const string &cpt_dir)
511 {
512 Serializable::serializeAll(cpt_dir);
513 }
514
515
516 ////////////////////////////////////////////////////////////////////////
517 //
518 // SerializableClass member definitions
519 //
520 ////////////////////////////////////////////////////////////////////////
521
522 // Map of class names to SerializableBuilder creation functions.
523 // Need to make this a pointer so we can force initialization on the
524 // first reference; otherwise, some SerializableClass constructors
525 // may be invoked before the classMap constructor.
526 map<string, SerializableClass::CreateFunc> *SerializableClass::classMap = 0;
527
528 // SerializableClass constructor: add mapping to classMap
529 SerializableClass::SerializableClass(const string &className,
530 CreateFunc createFunc)
531 {
532 if (classMap == NULL)
533 classMap = new map<string, SerializableClass::CreateFunc>();
534
535 if ((*classMap)[className])
536 fatal("Error: simulation object class %s redefined\n", className);
537
538 // add className --> createFunc to class map
539 (*classMap)[className] = createFunc;
540 }
541
542 //
543 //
544 Serializable *
545 SerializableClass::createObject(Checkpoint *cp, const string &section)
546 {
547 string className;
548
549 if (!cp->find(section, "type", className)) {
550 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
551 section);
552 }
553
554 CreateFunc createFunc = (*classMap)[className];
555
556 if (createFunc == NULL) {
557 fatal("Serializable::create: no create function for class '%s'.\n",
558 className);
559 }
560
561 Serializable *object = createFunc(cp, section);
562
563 assert(object != NULL);
564
565 return object;
566 }
567
568
569 Serializable *
570 Serializable::create(Checkpoint *cp, const string &section)
571 {
572 Serializable *object = SerializableClass::createObject(cp, section);
573 object->unserialize(cp, section);
574 return object;
575 }
576
577
578 const char *Checkpoint::baseFilename = "m5.cpt";
579
580 string Checkpoint::currentDirectory;
581
582 string
583 Checkpoint::setDir(const string &name)
584 {
585 // use csprintf to insert curTick() into directory name if it
586 // appears to have a format placeholder in it.
587 currentDirectory = (name.find("%") != string::npos) ?
588 csprintf(name, curTick()) : name;
589 if (currentDirectory[currentDirectory.size() - 1] != '/')
590 currentDirectory += "/";
591 return currentDirectory;
592 }
593
594 string
595 Checkpoint::dir()
596 {
597 return currentDirectory;
598 }
599
600
601 Checkpoint::Checkpoint(const string &cpt_dir, SimObjectResolver &resolver)
602 : db(new IniFile), objNameResolver(resolver), cptDir(setDir(cpt_dir))
603 {
604 string filename = cptDir + "/" + Checkpoint::baseFilename;
605 if (!db->load(filename)) {
606 fatal("Can't load checkpoint file '%s'\n", filename);
607 }
608 }
609
610 Checkpoint::~Checkpoint()
611 {
612 delete db;
613 }
614
615 bool
616 Checkpoint::find(const string &section, const string &entry, string &value)
617 {
618 return db->find(section, entry, value);
619 }
620
621
622 bool
623 Checkpoint::findObj(const string &section, const string &entry,
624 SimObject *&value)
625 {
626 string path;
627
628 if (!db->find(section, entry, path))
629 return false;
630
631 value = objNameResolver.resolveSimObject(path);
632 return true;
633 }
634
635
636 bool
637 Checkpoint::sectionExists(const string &section)
638 {
639 return db->sectionExists(section);
640 }