1/*
2 * Copyright (c) 2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2013 Advanced Micro Devices, Inc.
16 * Copyright (c) 2013 Mark D. Hill and David A. Wood
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met: redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Authors: Nathan Binkert
43 * Erik Hallnor
44 * Steve Reinhardt
45 * Andreas Sandberg
46 */
47
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/types.h>
51
52#include <cerrno>
53#include <fstream>
54#include <list>
55#include <string>
56#include <vector>
57
58#include "base/inifile.hh"
59#include "base/misc.hh"
60#include "base/output.hh"
61#include "base/str.hh"
62#include "base/trace.hh"
63#include "debug/Checkpoint.hh"
64#include "sim/eventq.hh"
65#include "sim/serialize.hh"
66#include "sim/sim_events.hh"
67#include "sim/sim_exit.hh"
68#include "sim/sim_object.hh"
69
70// For stat reset hack
71#include "sim/stat_control.hh"

--- 8 unchanged lines hidden (view full) ---

80bool
81parseParam(const string &s, T &value)
82{
83 return to_number(s, value);
84}
85
86template <class T>
87void
74showParam(ostream &os, const T &value)
88showParam(CheckpointOut &os, const T &value)
89{
90 os << value;
91}
92
93//
94// Template specializations:
95// - char (8-bit integer)
96// - floating-point types
97// - bool
98// - string
99//
100
101// Treat 8-bit ints (chars) as ints on output, not as chars
102template <>
103void
90showParam(ostream &os, const char &value)
104showParam(CheckpointOut &os, const char &value)
105{
106 os << (int)value;
107}
108
109
110template <>
111void
98showParam(ostream &os, const signed char &value)
112showParam(CheckpointOut &os, const signed char &value)
113{
114 os << (int)value;
115}
116
117
118template <>
119void
106showParam(ostream &os, const unsigned char &value)
120showParam(CheckpointOut &os, const unsigned char &value)
121{
122 os << (unsigned int)value;
123}
124
125
126template <>
127bool
128parseParam(const string &s, float &value)

--- 13 unchanged lines hidden (view full) ---

142parseParam(const string &s, bool &value)
143{
144 return to_bool(s, value);
145}
146
147// Display bools as strings
148template <>
149void
136showParam(ostream &os, const bool &value)
150showParam(CheckpointOut &os, const bool &value)
151{
152 os << (value ? "true" : "false");
153}
154
155
156// String requires no processing to speak of
157template <>
158bool
159parseParam(const string &s, string &value)
160{
161 value = s;
162 return true;
163}
164
165int Serializable::ckptMaxCount = 0;
166int Serializable::ckptCount = 0;
167int Serializable::ckptPrevCount = -1;
168std::stack<std::string> Serializable::path;
169
155void
156Serializable::nameOut(ostream &os)
157{
158 os << "\n[" << name() << "]\n";
159}
160
161void
162Serializable::nameOut(ostream &os, const string &_name)
163{
164 os << "\n[" << _name << "]\n";
165}
166
170template <class T>
171void
169paramOut(ostream &os, const string &name, const T &param)
172paramOut(CheckpointOut &os, const string &name, const T &param)
173{
174 os << name << "=";
175 showParam(os, param);
176 os << "\n";
177}
178
179template <class T>
180void
178arrayParamOut(ostream &os, const string &name, const vector<T> &param)
181arrayParamOut(CheckpointOut &os, const string &name, const vector<T> &param)
182{
183 typename vector<T>::size_type size = param.size();
184 os << name << "=";
185 if (size > 0)
186 showParam(os, param[0]);
187 for (typename vector<T>::size_type i = 1; i < size; ++i) {
188 os << " ";
189 showParam(os, param[i]);
190 }
191 os << "\n";
192}
193
194template <class T>
195void
193arrayParamOut(ostream &os, const string &name, const list<T> &param)
196arrayParamOut(CheckpointOut &os, const string &name, const list<T> &param)
197{
198 typename list<T>::const_iterator it = param.begin();
199
200 os << name << "=";
201 if (param.size() > 0)
202 showParam(os, *it);
203 it++;
204 while (it != param.end()) {
205 os << " ";
206 showParam(os, *it);
207 it++;
208 }
209 os << "\n";
210}
211
212template <class T>
213void
211paramIn(Checkpoint *cp, const string &section, const string &name, T &param)
214paramIn(CheckpointIn &cp, const string &name, T &param)
215{
216 const string &section(Serializable::currentSection());
217 string str;
214 if (!cp->find(section, name, str) || !parseParam(str, param)) {
218 if (!cp.find(section, name, str) || !parseParam(str, param)) {
219 fatal("Can't unserialize '%s:%s'\n", section, name);
220 }
221}
222
223template <class T>
224bool
221optParamIn(Checkpoint *cp, const string &section, const string &name, T &param)
225optParamIn(CheckpointIn &cp, const string &name, T &param)
226{
227 const string &section(Serializable::currentSection());
228 string str;
224 if (!cp->find(section, name, str) || !parseParam(str, param)) {
229 if (!cp.find(section, name, str) || !parseParam(str, param)) {
230 warn("optional parameter %s:%s not present\n", section, name);
231 return false;
232 } else {
233 return true;
234 }
235}
236
237template <class T>
238void
234arrayParamOut(ostream &os, const string &name, const T *param, unsigned size)
239arrayParamOut(CheckpointOut &os, const string &name,
240 const T *param, unsigned size)
241{
242 os << name << "=";
243 if (size > 0)
244 showParam(os, param[0]);
245 for (unsigned i = 1; i < size; ++i) {
246 os << " ";
247 showParam(os, param[i]);
248 }
249 os << "\n";
250}
251
252
253template <class T>
254void
249arrayParamIn(Checkpoint *cp, const string &section, const string &name,
250 T *param, unsigned size)
255arrayParamIn(CheckpointIn &cp, const string &name, T *param, unsigned size)
256{
257 const string &section(Serializable::currentSection());
258 string str;
253 if (!cp->find(section, name, str)) {
259 if (!cp.find(section, name, str)) {
260 fatal("Can't unserialize '%s:%s'\n", section, name);
261 }
262
263 // code below stolen from VectorParam<T>::parse().
264 // it would be nice to unify these somehow...
265
266 vector<string> tokens;
267

--- 6 unchanged lines hidden (view full) ---

274 fatal("Array size mismatch on %s:%s'\n", section, name);
275 }
276
277 for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
278 // need to parse into local variable to handle vector<bool>,
279 // for which operator[] returns a special reference class
280 // that's not the same as 'bool&', (since it's a packed
281 // vector)
276 T scalar_value = 0;
282 T scalar_value;
283 if (!parseParam(tokens[i], scalar_value)) {
284 string err("could not parse \"");
285
286 err += str;
287 err += "\"";
288
289 fatal(err);
290 }
291
292 // assign parsed value to vector
293 param[i] = scalar_value;
294 }
295}
296
297template <class T>
298void
293arrayParamIn(Checkpoint *cp, const string &section,
294 const string &name, vector<T> &param)
299arrayParamIn(CheckpointIn &cp, const string &name, vector<T> &param)
300{
301 const string &section(Serializable::currentSection());
302 string str;
297 if (!cp->find(section, name, str)) {
303 if (!cp.find(section, name, str)) {
304 fatal("Can't unserialize '%s:%s'\n", section, name);
305 }
306
307 // code below stolen from VectorParam<T>::parse().
308 // it would be nice to unify these somehow...
309
310 vector<string> tokens;
311

--- 4 unchanged lines hidden (view full) ---

316
317 param.resize(tokens.size());
318
319 for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
320 // need to parse into local variable to handle vector<bool>,
321 // for which operator[] returns a special reference class
322 // that's not the same as 'bool&', (since it's a packed
323 // vector)
318 T scalar_value = 0;
324 T scalar_value;
325 if (!parseParam(tokens[i], scalar_value)) {
326 string err("could not parse \"");
327
328 err += str;
329 err += "\"";
330
331 fatal(err);
332 }
333
334 // assign parsed value to vector
335 param[i] = scalar_value;
336 }
337}
338
339template <class T>
340void
335arrayParamIn(Checkpoint *cp, const string &section,
336 const string &name, list<T> &param)
341arrayParamIn(CheckpointIn &cp, const string &name, list<T> &param)
342{
343 const string &section(Serializable::currentSection());
344 string str;
339 if (!cp->find(section, name, str)) {
345 if (!cp.find(section, name, str)) {
346 fatal("Can't unserialize '%s:%s'\n", section, name);
347 }
348 param.clear();
349
350 vector<string> tokens;
351 tokenize(tokens, str, ' ');
352
353 for (vector<string>::size_type i = 0; i < tokens.size(); i++) {
348 T scalar_value = 0;
354 T scalar_value;
355 if (!parseParam(tokens[i], scalar_value)) {
356 string err("could not parse \"");
357
358 err += str;
359 err += "\"";
360
361 fatal(err);
362 }
363
364 // assign parsed value to vector
365 param.push_back(scalar_value);
366 }
367}
368
369
370void
365objParamIn(Checkpoint *cp, const string &section,
366 const string &name, SimObject * &param)
371objParamIn(CheckpointIn &cp, const string &name, SimObject * &param)
372{
368 if (!cp->findObj(section, name, param)) {
373 const string &section(Serializable::currentSection());
374 if (!cp.findObj(section, name, param)) {
375 fatal("Can't unserialize '%s:%s'\n", section, name);
376 }
377}
378
379
380#define INSTANTIATE_PARAM_TEMPLATES(type) \
375template void \
376paramOut(ostream &os, const string &name, type const &param); \
377template void \
378paramIn(Checkpoint *cp, const string &section, \
379 const string &name, type & param); \
380template bool \
381optParamIn(Checkpoint *cp, const string &section, \
382 const string &name, type & param); \
383template void \
384arrayParamOut(ostream &os, const string &name, \
385 type const *param, unsigned size); \
386template void \
387arrayParamIn(Checkpoint *cp, const string &section, \
388 const string &name, type *param, unsigned size); \
389template void \
390arrayParamOut(ostream &os, const string &name, \
391 const vector<type> &param); \
392template void \
393arrayParamIn(Checkpoint *cp, const string &section, \
394 const string &name, vector<type> &param); \
395template void \
396arrayParamOut(ostream &os, const string &name, \
397 const list<type> &param); \
398template void \
399arrayParamIn(Checkpoint *cp, const string &section, \
400 const string &name, list<type> &param);
381 template void \
382 paramOut(CheckpointOut &os, const string &name, type const &param); \
383 template void \
384 paramIn(CheckpointIn &cp, const string &name, type & param); \
385 template bool \
386 optParamIn(CheckpointIn &cp, const string &name, type & param); \
387 template void \
388 arrayParamOut(CheckpointOut &os, const string &name, \
389 type const *param, unsigned size); \
390 template void \
391 arrayParamIn(CheckpointIn &cp, const string &name, \
392 type *param, unsigned size); \
393 template void \
394 arrayParamOut(CheckpointOut &os, const string &name, \
395 const vector<type> &param); \
396 template void \
397 arrayParamIn(CheckpointIn &cp, const string &name, \
398 vector<type> &param); \
399 template void \
400 arrayParamOut(CheckpointOut &os, const string &name, \
401 const list<type> &param); \
402 template void \
403 arrayParamIn(CheckpointIn &cp, const string &name, \
404 list<type> &param);
405
406INSTANTIATE_PARAM_TEMPLATES(char)
407INSTANTIATE_PARAM_TEMPLATES(signed char)
408INSTANTIATE_PARAM_TEMPLATES(unsigned char)
409INSTANTIATE_PARAM_TEMPLATES(signed short)
410INSTANTIATE_PARAM_TEMPLATES(unsigned short)
411INSTANTIATE_PARAM_TEMPLATES(signed int)
412INSTANTIATE_PARAM_TEMPLATES(unsigned int)

--- 9 unchanged lines hidden (view full) ---

422
423/////////////////////////////
424
425/// Container for serializing global variables (not associated with
426/// any serialized object).
427class Globals : public Serializable
428{
429 public:
426 const string name() const;
427 void serialize(ostream &os);
428 void unserialize(Checkpoint *cp, const std::string &section);
430 Globals()
431 : unserializedCurTick(0) {}
432
433 void serialize(CheckpointOut &cp) const M5_ATTR_OVERRIDE;
434 void unserialize(CheckpointIn &cp) M5_ATTR_OVERRIDE;
435
436 Tick unserializedCurTick;
437};
438
439/// The one and only instance of the Globals class.
440Globals globals;
441
434const string
435Globals::name() const
436{
437 return "Globals";
438}
439
442void
441Globals::serialize(ostream &os)
443Globals::serialize(CheckpointOut &cp) const
444{
443 nameOut(os);
444 paramOut(os, "curTick", curTick());
445 paramOut(cp, "curTick", curTick());
446 paramOut(cp, "numMainEventQueues", numMainEventQueues);
447
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 }
448}
449
450void
455Globals::unserialize(Checkpoint *cp, const std::string &section)
451Globals::unserialize(CheckpointIn &cp)
452{
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 }
453 paramIn(cp, "curTick", unserializedCurTick);
454 paramIn(cp, "numMainEventQueues", numMainEventQueues);
455}
456
457Serializable::Serializable()
458{
459}
460
461Serializable::~Serializable()
462{
463}
464
465void
476Serializable::serialize(ostream &os)
466Serializable::serializeSection(CheckpointOut &cp, const char *name) const
467{
468 Serializable::ScopedCheckpointSection sec(cp, name);
469 serialize(cp);
470}
471
472void
481Serializable::unserialize(Checkpoint *cp, const string &section)
473Serializable::serializeSectionOld(CheckpointOut &cp, const char *name)
474{
475 Serializable::ScopedCheckpointSection sec(cp, name);
476 serializeOld(cp);
477}
478
479void
480Serializable::unserializeSection(CheckpointIn &cp, const char *name)
481{
482 Serializable::ScopedCheckpointSection sec(cp, name);
483 unserialize(cp);
484}
485
486void
487Serializable::serializeAll(const string &cpt_dir)
488{
488 string dir = Checkpoint::setDir(cpt_dir);
489 string dir = CheckpointIn::setDir(cpt_dir);
490 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
491 fatal("couldn't mkdir %s\n", dir);
492
492 string cpt_file = dir + Checkpoint::baseFilename;
493 string cpt_file = dir + CheckpointIn::baseFilename;
494 ofstream outstream(cpt_file.c_str());
495 time_t t = time(NULL);
496 if (!outstream.is_open())
497 fatal("Unable to open file %s for writing\n", cpt_file.c_str());
498 outstream << "## checkpoint generated: " << ctime(&t);
499
499 globals.serialize(outstream);
500 globals.serializeSection(outstream, "Globals");
501 for (uint32_t i = 0; i < numMainEventQueues; ++i)
502 mainEventQueue[i]->serializeSection(outstream, "MainEventQueue");
503
504 SimObject::serializeAll(outstream);
505}
506
507void
504Serializable::unserializeGlobals(Checkpoint *cp)
508Serializable::unserializeGlobals(CheckpointIn &cp)
509{
506 globals.unserialize(cp, globals.name());
510 globals.unserializeSection(cp, "Globals");
511
512 for (uint32_t i = 0; i < numMainEventQueues; ++i) {
513 mainEventQueue[i]->setCurTick(globals.unserializedCurTick);
514 mainEventQueue[i]->unserializeSection(cp, "MainEventQueue");
515 }
516}
517
518Serializable::ScopedCheckpointSection::~ScopedCheckpointSection()
519{
520 assert(!path.empty());
521 DPRINTF(Checkpoint, "Popping: %s\n", path.top());
522 path.pop();
523}
524
525void
526Serializable::ScopedCheckpointSection::pushName(const char *obj_name)
527{
528 if (path.empty()) {
529 path.push(obj_name);
530 } else {
531 path.push(csprintf("%s.%s", path.top(), obj_name));
532 }
533 DPRINTF(Checkpoint, "ScopedCheckpointSection::pushName: %s\n", obj_name);
534}
535
536void
537Serializable::ScopedCheckpointSection::nameOut(CheckpointOut &cp)
538{
539 DPRINTF(Checkpoint, "ScopedCheckpointSection::nameOut: %s\n",
540 Serializable::currentSection());
541 cp << "\n[" << Serializable::currentSection() << "]\n";
542}
543
544void
545debug_serialize(const string &cpt_dir)
546{
547 Serializable::serializeAll(cpt_dir);
548}
549
550
551////////////////////////////////////////////////////////////////////////
552//

--- 19 unchanged lines hidden (view full) ---

572
573 // add className --> createFunc to class map
574 (*classMap)[className] = createFunc;
575}
576
577//
578//
579Serializable *
545SerializableClass::createObject(Checkpoint *cp, const string &section)
580SerializableClass::createObject(CheckpointIn &cp, const string &section)
581{
582 string className;
583
549 if (!cp->find(section, "type", className)) {
584 if (!cp.find(section, "type", className)) {
585 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
586 section);
587 }
588
589 CreateFunc createFunc = (*classMap)[className];
590
591 if (createFunc == NULL) {
592 fatal("Serializable::create: no create function for class '%s'.\n",
593 className);
594 }
595
596 Serializable *object = createFunc(cp, section);
597
598 assert(object != NULL);
599
600 return object;
601}
602
603const std::string &
604Serializable::currentSection()
605{
606 assert(!path.empty());
607
608 return path.top();
609}
610
611Serializable *
570Serializable::create(Checkpoint *cp, const string &section)
612Serializable::create(CheckpointIn &cp, const string &section)
613{
614 Serializable *object = SerializableClass::createObject(cp, section);
573 object->unserialize(cp, section);
615 object->unserializeSection(cp, section);
616 return object;
617}
618
619
578const char *Checkpoint::baseFilename = "m5.cpt";
620const char *CheckpointIn::baseFilename = "m5.cpt";
621
580string Checkpoint::currentDirectory;
622string CheckpointIn::currentDirectory;
623
624string
583Checkpoint::setDir(const string &name)
625CheckpointIn::setDir(const string &name)
626{
627 // use csprintf to insert curTick() into directory name if it
628 // appears to have a format placeholder in it.
629 currentDirectory = (name.find("%") != string::npos) ?
630 csprintf(name, curTick()) : name;
631 if (currentDirectory[currentDirectory.size() - 1] != '/')
632 currentDirectory += "/";
633 return currentDirectory;
634}
635
636string
595Checkpoint::dir()
637CheckpointIn::dir()
638{
639 return currentDirectory;
640}
641
642
601Checkpoint::Checkpoint(const string &cpt_dir, SimObjectResolver &resolver)
643CheckpointIn::CheckpointIn(const string &cpt_dir, SimObjectResolver &resolver)
644 : db(new IniFile), objNameResolver(resolver), cptDir(setDir(cpt_dir))
645{
604 string filename = cptDir + "/" + Checkpoint::baseFilename;
646 string filename = cptDir + "/" + CheckpointIn::baseFilename;
647 if (!db->load(filename)) {
648 fatal("Can't load checkpoint file '%s'\n", filename);
649 }
650}
651
610Checkpoint::~Checkpoint()
652CheckpointIn::~CheckpointIn()
653{
654 delete db;
655}
656
657bool
616Checkpoint::find(const string §ion, const string &entry, string &value)
658CheckpointIn::find(const string &section, const string &entry, string &value)
659{
660 return db->find(section, entry, value);
661}
662
663
664bool
623Checkpoint::findObj(const string §ion, const string &entry,
665CheckpointIn::findObj(const string &section, const string &entry,
666 SimObject *&value)
667{
668 string path;
669
670 if (!db->find(section, entry, path))
671 return false;
672
673 value = objNameResolver.resolveSimObject(path);
674 return true;
675}
676
677
678bool
637Checkpoint::sectionExists(const string §ion)
679CheckpointIn::sectionExists(const string &section)
680{
681 return db->sectionExists(section);
682}