Deleted Added
sdiff udiff text old ( 5992:9c04119e93af ) new ( 6225:ec76e5c7cdd3 )
full compact
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;

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

166void
167Serializable::nameOut(ostream &os, const string &_name)
168{
169 os << "\n[" << _name << "]\n";
170}
171
172template <class T>
173void
174paramOut(ostream &os, const std::string &name, const T &param)
175{
176 os << name << "=";
177 showParam(os, param);
178 os << "\n";
179}
180
181template <class T>
182void
183arrayParamOut(ostream &os, const std::string &name,
184 const std::vector<T> &param)
185{
186 int size = param.size();
187 os << name << "=";
188 if (size > 0)
189 showParam(os, param[0]);
190 for (int i = 1; i < size; ++i) {
191 os << " ";
192 showParam(os, param[i]);
193 }
194 os << "\n";
195}
196
197
198template <class T>
199void
200paramIn(Checkpoint *cp, const std::string &section,
201 const std::string &name, T &param)
202{
203 std::string str;
204 if (!cp->find(section, name, str) || !parseParam(str, param)) {
205 fatal("Can't unserialize '%s:%s'\n", section, name);
206 }
207}
208
209
210template <class T>
211void
212arrayParamOut(ostream &os, const std::string &name,
213 const T *param, int size)
214{
215 os << name << "=";
216 if (size > 0)
217 showParam(os, param[0]);
218 for (int i = 1; i < size; ++i) {
219 os << " ";
220 showParam(os, param[i]);
221 }
222 os << "\n";
223}
224
225
226template <class T>
227void
228arrayParamIn(Checkpoint *cp, const std::string &section,
229 const std::string &name, T *param, int size)
230{
231 std::string str;
232 if (!cp->find(section, name, str)) {
233 fatal("Can't unserialize '%s:%s'\n", section, name);
234 }
235
236 // code below stolen from VectorParam<T>::parse().
237 // it would be nice to unify these somehow...
238
239 vector<string> tokens;

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

264
265 // assign parsed value to vector
266 param[i] = scalar_value;
267 }
268}
269
270template <class T>
271void
272arrayParamIn(Checkpoint *cp, const std::string &section,
273 const std::string &name, std::vector<T> &param)
274{
275 std::string str;
276 if (!cp->find(section, name, str)) {
277 fatal("Can't unserialize '%s:%s'\n", section, name);
278 }
279
280 // code below stolen from VectorParam<T>::parse().
281 // it would be nice to unify these somehow...
282
283 vector<string> tokens;

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

307 // assign parsed value to vector
308 param[i] = scalar_value;
309 }
310}
311
312
313
314void
315objParamIn(Checkpoint *cp, const std::string &section,
316 const std::string &name, SimObject * &param)
317{
318 if (!cp->findObj(section, name, param)) {
319 fatal("Can't unserialize '%s:%s'\n", section, name);
320 }
321}
322
323
324#define INSTANTIATE_PARAM_TEMPLATES(type) \
325template void \
326paramOut(ostream &os, const std::string &name, type const &param); \
327template void \
328paramIn(Checkpoint *cp, const std::string &section, \
329 const std::string &name, type & param); \
330template void \
331arrayParamOut(ostream &os, const std::string &name, \
332 type const *param, int size); \
333template void \
334arrayParamIn(Checkpoint *cp, const std::string &section, \
335 const std::string &name, type *param, int size); \
336template void \
337arrayParamOut(ostream &os, const std::string &name, \
338 const std::vector<type> &param); \
339template void \
340arrayParamIn(Checkpoint *cp, const std::string &section, \
341 const std::string &name, std::vector<type> &param);
342
343INSTANTIATE_PARAM_TEMPLATES(signed char)
344INSTANTIATE_PARAM_TEMPLATES(unsigned char)
345INSTANTIATE_PARAM_TEMPLATES(signed short)
346INSTANTIATE_PARAM_TEMPLATES(unsigned short)
347INSTANTIATE_PARAM_TEMPLATES(signed int)
348INSTANTIATE_PARAM_TEMPLATES(unsigned int)
349INSTANTIATE_PARAM_TEMPLATES(signed long)

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

400{
401}
402
403Serializable::~Serializable()
404{
405}
406
407void
408Serializable::serialize(std::ostream &os)
409{
410}
411
412void
413Serializable::unserialize(Checkpoint *cp, const std::string &section)
414{
415}
416
417void
418Serializable::serializeAll(const std::string &cpt_dir)
419{
420 setCheckpointDir(cpt_dir);
421 string dir = Checkpoint::dir();
422 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST)
423 fatal("couldn't mkdir %s\n", dir);
424
425 string cpt_file = dir + Checkpoint::baseFilename;
426 ofstream outstream(cpt_file.c_str());
427 time_t t = time(NULL);
428 if (!outstream.is_open())
429 fatal("Unable to open file %s for writing\n", cpt_file.c_str());
430 outstream << "// checkpoint generated: " << ctime(&t);
431
432 globals.serialize(outstream);
433 SimObject::serializeAll(outstream);
434}
435
436void
437Serializable::unserializeAll(const std::string &cpt_dir)
438{
439 setCheckpointDir(cpt_dir);
440 string dir = Checkpoint::dir();
441 string cpt_file = dir + Checkpoint::baseFilename;
442 string section = "";
443
444 DPRINTFR(Config, "Loading checkpoint dir '%s'\n",
445 dir);

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

454 globals.unserialize(cp);
455}
456
457const char *Checkpoint::baseFilename = "m5.cpt";
458
459static string checkpointDirBase;
460
461void
462setCheckpointDir(const std::string &name)
463{
464 checkpointDirBase = name;
465 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/')
466 checkpointDirBase += "/";
467}
468
469string
470Checkpoint::dir()
471{
472 // use csprintf to insert curTick into directory name if it
473 // appears to have a format placeholder in it.
474 return (checkpointDirBase.find("%") != string::npos) ?
475 csprintf(checkpointDirBase, curTick) : checkpointDirBase;
476}
477
478void
479debug_serialize(const std::string &cpt_dir)
480{
481 Serializable::serializeAll(cpt_dir);
482}
483
484
485////////////////////////////////////////////////////////////////////////
486//
487// SerializableClass member definitions
488//
489////////////////////////////////////////////////////////////////////////
490
491// Map of class names to SerializableBuilder creation functions.
492// Need to make this a pointer so we can force initialization on the
493// first reference; otherwise, some SerializableClass constructors
494// may be invoked before the classMap constructor.
495map *SerializableClass::classMap = 0;
496
497// SerializableClass constructor: add mapping to classMap
498SerializableClass::SerializableClass(const string &className,
499 CreateFunc createFunc)
500{
501 if (classMap == NULL)
502 classMap = new map();
503
504 if ((*classMap)[className])
505 {
506 cerr << "Error: simulation object class " << className << " redefined"
507 << endl;
508 fatal("");
509 }
510
511 // add className --> createFunc to class map
512 (*classMap)[className] = createFunc;
513}
514
515
516//
517//
518Serializable *
519SerializableClass::createObject(Checkpoint *cp,
520 const std::string &section)
521{
522 string className;
523
524 if (!cp->find(section, "type", className)) {
525 fatal("Serializable::create: no 'type' entry in section '%s'.\n",
526 section);
527 }
528

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

537
538 assert(object != NULL);
539
540 return object;
541}
542
543
544Serializable *
545Serializable::create(Checkpoint *cp, const std::string &section)
546{
547 Serializable *object = SerializableClass::createObject(cp, section);
548 object->unserialize(cp, section);
549 return object;
550}
551
552
553Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path)
554 : db(new IniFile), basePath(path), cptDir(cpt_dir)
555{
556 string filename = cpt_dir + "/" + Checkpoint::baseFilename;
557 if (!db->load(filename)) {
558 fatal("Can't load checkpoint file '%s'\n", filename);
559 }
560}
561
562
563bool
564Checkpoint::find(const std::string &section, const std::string &entry,
565 std::string &value)
566{
567 return db->find(section, entry, value);
568}
569
570
571bool
572Checkpoint::findObj(const std::string &section, const std::string &entry,
573 SimObject *&value)
574{
575 string path;
576
577 if (!db->find(section, entry, path))
578 return false;
579
580 value = resolveSimObject(path);
581 return true;
582}
583
584
585bool
586Checkpoint::sectionExists(const std::string &section)
587{
588 return db->sectionExists(section);
589}