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 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 string &name, const vector<T> ¶m) |
184{ 185 int size = param.size(); 186 os << name << "="; 187 if (size > 0) 188 showParam(os, param[0]); 189 for (int i = 1; i < size; ++i) { 190 os << " "; 191 showParam(os, param[i]); 192 } 193 os << "\n"; 194} 195 196 197template <class T> 198void |
199paramIn(Checkpoint *cp, const string §ion, const string &name, T ¶m) |
200{ |
201 string str; |
202 if (!cp->find(section, name, str) || !parseParam(str, param)) { 203 fatal("Can't unserialize '%s:%s'\n", section, name); 204 } 205} 206 207 208template <class T> 209void |
210arrayParamOut(ostream &os, const string &name, const T *param, int size) |
211{ 212 os << name << "="; 213 if (size > 0) 214 showParam(os, param[0]); 215 for (int i = 1; i < size; ++i) { 216 os << " "; 217 showParam(os, param[i]); 218 } 219 os << "\n"; 220} 221 222 223template <class T> 224void |
225arrayParamIn(Checkpoint *cp, const string §ion, const string &name, 226 T *param, int size) |
227{ |
228 string str; |
229 if (!cp->find(section, name, str)) { 230 fatal("Can't unserialize '%s:%s'\n", section, name); 231 } 232 233 // code below stolen from VectorParam<T>::parse(). 234 // it would be nice to unify these somehow... 235 236 vector<string> tokens; --- 24 unchanged lines hidden (view full) --- 261 262 // assign parsed value to vector 263 param[i] = scalar_value; 264 } 265} 266 267template <class T> 268void |
269arrayParamIn(Checkpoint *cp, const string §ion, 270 const string &name, vector<T> ¶m) |
271{ |
272 string str; |
273 if (!cp->find(section, name, str)) { 274 fatal("Can't unserialize '%s:%s'\n", section, name); 275 } 276 277 // code below stolen from VectorParam<T>::parse(). 278 // it would be nice to unify these somehow... 279 280 vector<string> tokens; --- 23 unchanged lines hidden (view full) --- 304 // assign parsed value to vector 305 param[i] = scalar_value; 306 } 307} 308 309 310 311void |
312objParamIn(Checkpoint *cp, const string §ion, 313 const string &name, SimObject * ¶m) |
314{ 315 if (!cp->findObj(section, name, param)) { 316 fatal("Can't unserialize '%s:%s'\n", section, name); 317 } 318} 319 320 321#define INSTANTIATE_PARAM_TEMPLATES(type) \ 322template void \ |
323paramOut(ostream &os, const string &name, type const ¶m); \ |
324template void \ |
325paramIn(Checkpoint *cp, const string §ion, \ 326 const string &name, type & param); \ |
327template void \ |
328arrayParamOut(ostream &os, const string &name, \ |
329 type const *param, int size); \ 330template void \ |
331arrayParamIn(Checkpoint *cp, const string §ion, \ 332 const string &name, type *param, int size); \ |
333template void \ |
334arrayParamOut(ostream &os, const string &name, \ 335 const vector<type> ¶m); \ |
336template void \ |
337arrayParamIn(Checkpoint *cp, const string §ion, \ 338 const string &name, vector<type> ¶m); |
339 340INSTANTIATE_PARAM_TEMPLATES(signed char) 341INSTANTIATE_PARAM_TEMPLATES(unsigned char) 342INSTANTIATE_PARAM_TEMPLATES(signed short) 343INSTANTIATE_PARAM_TEMPLATES(unsigned short) 344INSTANTIATE_PARAM_TEMPLATES(signed int) 345INSTANTIATE_PARAM_TEMPLATES(unsigned int) 346INSTANTIATE_PARAM_TEMPLATES(signed long) --- 50 unchanged lines hidden (view full) --- 397{ 398} 399 400Serializable::~Serializable() 401{ 402} 403 404void |
405Serializable::serialize(ostream &os) |
406{ 407} 408 409void |
410Serializable::unserialize(Checkpoint *cp, const string §ion) |
411{ 412} 413 414void |
415Serializable::serializeAll(const string &cpt_dir) |
416{ 417 setCheckpointDir(cpt_dir); 418 string dir = Checkpoint::dir(); 419 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST) 420 fatal("couldn't mkdir %s\n", dir); 421 422 string cpt_file = dir + Checkpoint::baseFilename; 423 ofstream outstream(cpt_file.c_str()); 424 time_t t = time(NULL); 425 if (!outstream.is_open()) 426 fatal("Unable to open file %s for writing\n", cpt_file.c_str()); 427 outstream << "// checkpoint generated: " << ctime(&t); 428 429 globals.serialize(outstream); 430 SimObject::serializeAll(outstream); 431} 432 433void |
434Serializable::unserializeAll(const string &cpt_dir) |
435{ 436 setCheckpointDir(cpt_dir); 437 string dir = Checkpoint::dir(); 438 string cpt_file = dir + Checkpoint::baseFilename; 439 string section = ""; 440 441 DPRINTFR(Config, "Loading checkpoint dir '%s'\n", 442 dir); --- 8 unchanged lines hidden (view full) --- 451 globals.unserialize(cp); 452} 453 454const char *Checkpoint::baseFilename = "m5.cpt"; 455 456static string checkpointDirBase; 457 458void |
459setCheckpointDir(const string &name) |
460{ 461 checkpointDirBase = name; 462 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/') 463 checkpointDirBase += "/"; 464} 465 466string 467Checkpoint::dir() 468{ 469 // use csprintf to insert curTick into directory name if it 470 // appears to have a format placeholder in it. 471 return (checkpointDirBase.find("%") != string::npos) ? 472 csprintf(checkpointDirBase, curTick) : checkpointDirBase; 473} 474 475void |
476debug_serialize(const string &cpt_dir) |
477{ 478 Serializable::serializeAll(cpt_dir); 479} 480 481 482//////////////////////////////////////////////////////////////////////// 483// 484// SerializableClass member definitions 485// 486//////////////////////////////////////////////////////////////////////// 487 488// Map of class names to SerializableBuilder creation functions. 489// Need to make this a pointer so we can force initialization on the 490// first reference; otherwise, some SerializableClass constructors 491// may be invoked before the classMap constructor. |
492map<string, SerializableClass::CreateFunc> *SerializableClass::classMap = 0; |
493 494// SerializableClass constructor: add mapping to classMap 495SerializableClass::SerializableClass(const string &className, |
496 CreateFunc createFunc) |
497{ 498 if (classMap == NULL) |
499 classMap = new map<string, SerializableClass::CreateFunc>(); |
500 501 if ((*classMap)[className]) |
502 fatal("Error: simulation object class %s redefined\n", className); |
503 504 // add className --> createFunc to class map 505 (*classMap)[className] = createFunc; 506} 507 |
508// 509// 510Serializable * |
511SerializableClass::createObject(Checkpoint *cp, const string §ion) |
512{ 513 string className; 514 515 if (!cp->find(section, "type", className)) { 516 fatal("Serializable::create: no 'type' entry in section '%s'.\n", 517 section); 518 } 519 --- 8 unchanged lines hidden (view full) --- 528 529 assert(object != NULL); 530 531 return object; 532} 533 534 535Serializable * |
536Serializable::create(Checkpoint *cp, const string §ion) |
537{ 538 Serializable *object = SerializableClass::createObject(cp, section); 539 object->unserialize(cp, section); 540 return object; 541} 542 543 |
544Checkpoint::Checkpoint(const string &cpt_dir, const string &path) |
545 : db(new IniFile), basePath(path), cptDir(cpt_dir) 546{ 547 string filename = cpt_dir + "/" + Checkpoint::baseFilename; 548 if (!db->load(filename)) { 549 fatal("Can't load checkpoint file '%s'\n", filename); 550 } 551} 552 553 554bool |
555Checkpoint::find(const string §ion, const string &entry, string &value) |
556{ 557 return db->find(section, entry, value); 558} 559 560 561bool |
562Checkpoint::findObj(const string §ion, const string &entry, |
563 SimObject *&value) 564{ 565 string path; 566 567 if (!db->find(section, entry, path)) 568 return false; 569 570 value = resolveSimObject(path); 571 return true; 572} 573 574 575bool |
576Checkpoint::sectionExists(const string §ion) |
577{ 578 return db->sectionExists(section); 579} |