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 57using namespace std; 58 59extern SimObject *resolveSimObject(const string &); 60 61// 62// The base implementations use to_number for parsing and '<<' for 63// displaying, suitable for integer types. 64// 65template <class T> 66bool 67parseParam(const string &s, T &value) 68{ 69 return to_number(s, value); 70} 71 72template <class T> 73void 74showParam(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 88template <> 89void 90showParam(ostream &os, const char &value) 91{ 92 os << (int)value; 93} 94 95 96template <> 97void 98showParam(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 105template <> 106bool 107parseParam(const string &s, float &value) 108{ 109 return (sscanf(s.c_str(), "%f", &value) == 1); 110} 111 112template <> 113bool 114parseParam(const string &s, double &value) 115{ 116 return (sscanf(s.c_str(), "%lf", &value) == 1); 117} 118 119template <> 120bool 121parseParam(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 139template <> 140void 141showParam(ostream &os, const bool &value) 142{ 143 os << (value ? "true" : "false"); 144} 145 146 147// String requires no processing to speak of 148template <> 149bool 150parseParam(const string &s, string &value) 151{ 152 value = s; 153 return true; 154} 155 156int Serializable::ckptMaxCount = 0; 157int Serializable::ckptCount = 0; 158int Serializable::ckptPrevCount = -1; 159 160void 161Serializable::nameOut(ostream &os) 162{ 163 os << "\n[" << name() << "]\n"; 164} 165 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 ¶m) 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> ¶m) 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 §ion, 201 const std::string &name, T ¶m) 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 §ion, 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; 240 241 tokenize(tokens, str, ' '); 242 243 // Need this if we were doing a vector 244 // value.resize(tokens.size()); 245 246 if (tokens.size() != size) { 247 fatal("Array size mismatch on %s:%s'\n", section, name); 248 } 249 250 for (int i = 0; i < tokens.size(); i++) { 251 // need to parse into local variable to handle vector<bool>, 252 // for which operator[] returns a special reference class 253 // that's not the same as 'bool&', (since it's a packed 254 // vector) 255 T scalar_value; 256 if (!parseParam(tokens[i], scalar_value)) { 257 string err("could not parse \""); 258 259 err += str; 260 err += "\""; 261 262 fatal(err); 263 } 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 §ion, 273 const std::string &name, std::vector<T> ¶m) 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; 284 285 tokenize(tokens, str, ' '); 286 287 // Need this if we were doing a vector 288 // value.resize(tokens.size()); 289 290 param.resize(tokens.size()); 291 292 for (int i = 0; i < tokens.size(); i++) { 293 // need to parse into local variable to handle vector<bool>, 294 // for which operator[] returns a special reference class 295 // that's not the same as 'bool&', (since it's a packed 296 // vector) 297 T scalar_value; 298 if (!parseParam(tokens[i], scalar_value)) { 299 string err("could not parse \""); 300 301 err += str; 302 err += "\""; 303 304 fatal(err); 305 } 306 307 // assign parsed value to vector 308 param[i] = scalar_value; 309 } 310} 311 312 313 314void 315objParamIn(Checkpoint *cp, const std::string §ion, 316 const std::string &name, SimObject * ¶m) 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 ¶m); \ 327template void \ 328paramIn(Checkpoint *cp, const std::string §ion, \ 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 §ion, \ 335 const std::string &name, type *param, int size); \ 336template void \ 337arrayParamOut(ostream &os, const std::string &name, \ 338 const std::vector<type> ¶m); \ 339template void \ 340arrayParamIn(Checkpoint *cp, const std::string §ion, \ 341 const std::string &name, std::vector<type> ¶m); 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) 350INSTANTIATE_PARAM_TEMPLATES(unsigned long) 351INSTANTIATE_PARAM_TEMPLATES(signed long long) 352INSTANTIATE_PARAM_TEMPLATES(unsigned long long) 353INSTANTIATE_PARAM_TEMPLATES(bool) 354INSTANTIATE_PARAM_TEMPLATES(string) 355 356 357///////////////////////////// 358 359/// Container for serializing global variables (not associated with 360/// any serialized object). 361class Globals : public Serializable 362{ 363 public: 364 const string name() const; 365 void serialize(ostream &os); 366 void unserialize(Checkpoint *cp); 367}; 368 369/// The one and only instance of the Globals class. 370Globals globals; 371 372const string 373Globals::name() const 374{ 375 return "Globals"; 376} 377 378void 379Globals::serialize(ostream &os) 380{ 381 nameOut(os); 382 SERIALIZE_SCALAR(curTick); 383 384 nameOut(os, "MainEventQueue"); 385 mainEventQueue.serialize(os); 386} 387 388void 389Globals::unserialize(Checkpoint *cp) 390{ 391 const string §ion = name(); 392 UNSERIALIZE_SCALAR(curTick); 393 394 mainEventQueue.unserialize(cp, "MainEventQueue"); 395} 396 397void 398Serializable::serializeAll(const std::string &cpt_dir) 399{ 400 setCheckpointDir(cpt_dir); 401 string dir = Checkpoint::dir(); 402 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST) 403 fatal("couldn't mkdir %s\n", dir); 404 405 string cpt_file = dir + Checkpoint::baseFilename; 406 ofstream outstream(cpt_file.c_str()); 407 time_t t = time(NULL);
| 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 57using namespace std; 58 59extern SimObject *resolveSimObject(const string &); 60 61// 62// The base implementations use to_number for parsing and '<<' for 63// displaying, suitable for integer types. 64// 65template <class T> 66bool 67parseParam(const string &s, T &value) 68{ 69 return to_number(s, value); 70} 71 72template <class T> 73void 74showParam(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 88template <> 89void 90showParam(ostream &os, const char &value) 91{ 92 os << (int)value; 93} 94 95 96template <> 97void 98showParam(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 105template <> 106bool 107parseParam(const string &s, float &value) 108{ 109 return (sscanf(s.c_str(), "%f", &value) == 1); 110} 111 112template <> 113bool 114parseParam(const string &s, double &value) 115{ 116 return (sscanf(s.c_str(), "%lf", &value) == 1); 117} 118 119template <> 120bool 121parseParam(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 139template <> 140void 141showParam(ostream &os, const bool &value) 142{ 143 os << (value ? "true" : "false"); 144} 145 146 147// String requires no processing to speak of 148template <> 149bool 150parseParam(const string &s, string &value) 151{ 152 value = s; 153 return true; 154} 155 156int Serializable::ckptMaxCount = 0; 157int Serializable::ckptCount = 0; 158int Serializable::ckptPrevCount = -1; 159 160void 161Serializable::nameOut(ostream &os) 162{ 163 os << "\n[" << name() << "]\n"; 164} 165 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 ¶m) 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> ¶m) 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 §ion, 201 const std::string &name, T ¶m) 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 §ion, 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; 240 241 tokenize(tokens, str, ' '); 242 243 // Need this if we were doing a vector 244 // value.resize(tokens.size()); 245 246 if (tokens.size() != size) { 247 fatal("Array size mismatch on %s:%s'\n", section, name); 248 } 249 250 for (int i = 0; i < tokens.size(); i++) { 251 // need to parse into local variable to handle vector<bool>, 252 // for which operator[] returns a special reference class 253 // that's not the same as 'bool&', (since it's a packed 254 // vector) 255 T scalar_value; 256 if (!parseParam(tokens[i], scalar_value)) { 257 string err("could not parse \""); 258 259 err += str; 260 err += "\""; 261 262 fatal(err); 263 } 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 §ion, 273 const std::string &name, std::vector<T> ¶m) 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; 284 285 tokenize(tokens, str, ' '); 286 287 // Need this if we were doing a vector 288 // value.resize(tokens.size()); 289 290 param.resize(tokens.size()); 291 292 for (int i = 0; i < tokens.size(); i++) { 293 // need to parse into local variable to handle vector<bool>, 294 // for which operator[] returns a special reference class 295 // that's not the same as 'bool&', (since it's a packed 296 // vector) 297 T scalar_value; 298 if (!parseParam(tokens[i], scalar_value)) { 299 string err("could not parse \""); 300 301 err += str; 302 err += "\""; 303 304 fatal(err); 305 } 306 307 // assign parsed value to vector 308 param[i] = scalar_value; 309 } 310} 311 312 313 314void 315objParamIn(Checkpoint *cp, const std::string §ion, 316 const std::string &name, SimObject * ¶m) 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 ¶m); \ 327template void \ 328paramIn(Checkpoint *cp, const std::string §ion, \ 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 §ion, \ 335 const std::string &name, type *param, int size); \ 336template void \ 337arrayParamOut(ostream &os, const std::string &name, \ 338 const std::vector<type> ¶m); \ 339template void \ 340arrayParamIn(Checkpoint *cp, const std::string §ion, \ 341 const std::string &name, std::vector<type> ¶m); 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) 350INSTANTIATE_PARAM_TEMPLATES(unsigned long) 351INSTANTIATE_PARAM_TEMPLATES(signed long long) 352INSTANTIATE_PARAM_TEMPLATES(unsigned long long) 353INSTANTIATE_PARAM_TEMPLATES(bool) 354INSTANTIATE_PARAM_TEMPLATES(string) 355 356 357///////////////////////////// 358 359/// Container for serializing global variables (not associated with 360/// any serialized object). 361class Globals : public Serializable 362{ 363 public: 364 const string name() const; 365 void serialize(ostream &os); 366 void unserialize(Checkpoint *cp); 367}; 368 369/// The one and only instance of the Globals class. 370Globals globals; 371 372const string 373Globals::name() const 374{ 375 return "Globals"; 376} 377 378void 379Globals::serialize(ostream &os) 380{ 381 nameOut(os); 382 SERIALIZE_SCALAR(curTick); 383 384 nameOut(os, "MainEventQueue"); 385 mainEventQueue.serialize(os); 386} 387 388void 389Globals::unserialize(Checkpoint *cp) 390{ 391 const string §ion = name(); 392 UNSERIALIZE_SCALAR(curTick); 393 394 mainEventQueue.unserialize(cp, "MainEventQueue"); 395} 396 397void 398Serializable::serializeAll(const std::string &cpt_dir) 399{ 400 setCheckpointDir(cpt_dir); 401 string dir = Checkpoint::dir(); 402 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST) 403 fatal("couldn't mkdir %s\n", dir); 404 405 string cpt_file = dir + Checkpoint::baseFilename; 406 ofstream outstream(cpt_file.c_str()); 407 time_t t = time(NULL);
|
408 outstream << "// checkpoint generated: " << ctime(&t); 409 410 globals.serialize(outstream); 411 SimObject::serializeAll(outstream); 412} 413 414void 415Serializable::unserializeAll(const std::string &cpt_dir) 416{ 417 setCheckpointDir(cpt_dir); 418 string dir = Checkpoint::dir(); 419 string cpt_file = dir + Checkpoint::baseFilename; 420 string section = ""; 421 422 DPRINTFR(Config, "Loading checkpoint dir '%s'\n", 423 dir); 424 Checkpoint *cp = new Checkpoint(dir, section); 425 unserializeGlobals(cp); 426 SimObject::unserializeAll(cp); 427} 428 429void 430Serializable::unserializeGlobals(Checkpoint *cp) 431{ 432 globals.unserialize(cp); 433} 434 435const char *Checkpoint::baseFilename = "m5.cpt"; 436 437static string checkpointDirBase; 438 439void 440setCheckpointDir(const std::string &name) 441{ 442 checkpointDirBase = name; 443 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/') 444 checkpointDirBase += "/"; 445} 446 447string 448Checkpoint::dir() 449{ 450 // use csprintf to insert curTick into directory name if it 451 // appears to have a format placeholder in it. 452 return (checkpointDirBase.find("%") != string::npos) ? 453 csprintf(checkpointDirBase, curTick) : checkpointDirBase; 454} 455 456void 457debug_serialize(const std::string &cpt_dir) 458{ 459 Serializable::serializeAll(cpt_dir); 460} 461 462 463//////////////////////////////////////////////////////////////////////// 464// 465// SerializableClass member definitions 466// 467//////////////////////////////////////////////////////////////////////// 468 469// Map of class names to SerializableBuilder creation functions. 470// Need to make this a pointer so we can force initialization on the 471// first reference; otherwise, some SerializableClass constructors 472// may be invoked before the classMap constructor. 473map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0; 474 475// SerializableClass constructor: add mapping to classMap 476SerializableClass::SerializableClass(const string &className, 477 CreateFunc createFunc) 478{ 479 if (classMap == NULL) 480 classMap = new map<string,SerializableClass::CreateFunc>(); 481 482 if ((*classMap)[className]) 483 { 484 cerr << "Error: simulation object class " << className << " redefined" 485 << endl; 486 fatal(""); 487 } 488 489 // add className --> createFunc to class map 490 (*classMap)[className] = createFunc; 491} 492 493 494// 495// 496Serializable * 497SerializableClass::createObject(Checkpoint *cp, 498 const std::string §ion) 499{ 500 string className; 501 502 if (!cp->find(section, "type", className)) { 503 fatal("Serializable::create: no 'type' entry in section '%s'.\n", 504 section); 505 } 506 507 CreateFunc createFunc = (*classMap)[className]; 508 509 if (createFunc == NULL) { 510 fatal("Serializable::create: no create function for class '%s'.\n", 511 className); 512 } 513 514 Serializable *object = createFunc(cp, section); 515 516 assert(object != NULL); 517 518 return object; 519} 520 521 522Serializable * 523Serializable::create(Checkpoint *cp, const std::string §ion) 524{ 525 Serializable *object = SerializableClass::createObject(cp, section); 526 object->unserialize(cp, section); 527 return object; 528} 529 530 531Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path) 532 : db(new IniFile), basePath(path), cptDir(cpt_dir) 533{ 534 string filename = cpt_dir + "/" + Checkpoint::baseFilename; 535 if (!db->load(filename)) { 536 fatal("Can't load checkpoint file '%s'\n", filename); 537 } 538} 539 540 541bool 542Checkpoint::find(const std::string §ion, const std::string &entry, 543 std::string &value) 544{ 545 return db->find(section, entry, value); 546} 547 548 549bool 550Checkpoint::findObj(const std::string §ion, const std::string &entry, 551 SimObject *&value) 552{ 553 string path; 554 555 if (!db->find(section, entry, path)) 556 return false; 557 558 value = resolveSimObject(path); 559 return true; 560} 561 562 563bool 564Checkpoint::sectionExists(const std::string §ion) 565{ 566 return db->sectionExists(section); 567}
| 410 outstream << "// checkpoint generated: " << ctime(&t); 411 412 globals.serialize(outstream); 413 SimObject::serializeAll(outstream); 414} 415 416void 417Serializable::unserializeAll(const std::string &cpt_dir) 418{ 419 setCheckpointDir(cpt_dir); 420 string dir = Checkpoint::dir(); 421 string cpt_file = dir + Checkpoint::baseFilename; 422 string section = ""; 423 424 DPRINTFR(Config, "Loading checkpoint dir '%s'\n", 425 dir); 426 Checkpoint *cp = new Checkpoint(dir, section); 427 unserializeGlobals(cp); 428 SimObject::unserializeAll(cp); 429} 430 431void 432Serializable::unserializeGlobals(Checkpoint *cp) 433{ 434 globals.unserialize(cp); 435} 436 437const char *Checkpoint::baseFilename = "m5.cpt"; 438 439static string checkpointDirBase; 440 441void 442setCheckpointDir(const std::string &name) 443{ 444 checkpointDirBase = name; 445 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/') 446 checkpointDirBase += "/"; 447} 448 449string 450Checkpoint::dir() 451{ 452 // use csprintf to insert curTick into directory name if it 453 // appears to have a format placeholder in it. 454 return (checkpointDirBase.find("%") != string::npos) ? 455 csprintf(checkpointDirBase, curTick) : checkpointDirBase; 456} 457 458void 459debug_serialize(const std::string &cpt_dir) 460{ 461 Serializable::serializeAll(cpt_dir); 462} 463 464 465//////////////////////////////////////////////////////////////////////// 466// 467// SerializableClass member definitions 468// 469//////////////////////////////////////////////////////////////////////// 470 471// Map of class names to SerializableBuilder creation functions. 472// Need to make this a pointer so we can force initialization on the 473// first reference; otherwise, some SerializableClass constructors 474// may be invoked before the classMap constructor. 475map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0; 476 477// SerializableClass constructor: add mapping to classMap 478SerializableClass::SerializableClass(const string &className, 479 CreateFunc createFunc) 480{ 481 if (classMap == NULL) 482 classMap = new map<string,SerializableClass::CreateFunc>(); 483 484 if ((*classMap)[className]) 485 { 486 cerr << "Error: simulation object class " << className << " redefined" 487 << endl; 488 fatal(""); 489 } 490 491 // add className --> createFunc to class map 492 (*classMap)[className] = createFunc; 493} 494 495 496// 497// 498Serializable * 499SerializableClass::createObject(Checkpoint *cp, 500 const std::string §ion) 501{ 502 string className; 503 504 if (!cp->find(section, "type", className)) { 505 fatal("Serializable::create: no 'type' entry in section '%s'.\n", 506 section); 507 } 508 509 CreateFunc createFunc = (*classMap)[className]; 510 511 if (createFunc == NULL) { 512 fatal("Serializable::create: no create function for class '%s'.\n", 513 className); 514 } 515 516 Serializable *object = createFunc(cp, section); 517 518 assert(object != NULL); 519 520 return object; 521} 522 523 524Serializable * 525Serializable::create(Checkpoint *cp, const std::string §ion) 526{ 527 Serializable *object = SerializableClass::createObject(cp, section); 528 object->unserialize(cp, section); 529 return object; 530} 531 532 533Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path) 534 : db(new IniFile), basePath(path), cptDir(cpt_dir) 535{ 536 string filename = cpt_dir + "/" + Checkpoint::baseFilename; 537 if (!db->load(filename)) { 538 fatal("Can't load checkpoint file '%s'\n", filename); 539 } 540} 541 542 543bool 544Checkpoint::find(const std::string §ion, const std::string &entry, 545 std::string &value) 546{ 547 return db->find(section, entry, value); 548} 549 550 551bool 552Checkpoint::findObj(const std::string §ion, const std::string &entry, 553 SimObject *&value) 554{ 555 string path; 556 557 if (!db->find(section, entry, path)) 558 return false; 559 560 value = resolveSimObject(path); 561 return true; 562} 563 564 565bool 566Checkpoint::sectionExists(const std::string §ion) 567{ 568 return db->sectionExists(section); 569}
|