serialize.cc revision 512
1/* 2 * Copyright (c) 2003 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 29#include <sys/time.h> 30#include <sys/types.h> 31#include <sys/stat.h> 32 33#include <fstream> 34#include <list> 35#include <string> 36#include <vector> 37 38#include "base/inifile.hh" 39#include "base/misc.hh" 40#include "base/str.hh" 41#include "base/trace.hh" 42#include "sim/config_node.hh" 43#include "sim/eventq.hh" 44#include "sim/param.hh" 45#include "sim/serialize.hh" 46#include "sim/sim_events.hh" 47#include "sim/sim_object.hh" 48 49using namespace std; 50 51void 52Serializable::nameOut(ostream &os) 53{ 54 os << "\n[" << name() << "]\n"; 55} 56 57void 58Serializable::nameOut(ostream &os, const string &_name) 59{ 60 os << "\n[" << _name << "]\n"; 61} 62 63template <class T> 64void 65paramOut(ostream &os, const std::string &name, const T ¶m) 66{ 67 os << name << "="; 68 showParam(os, param); 69 os << "\n"; 70} 71 72 73template <class T> 74void 75paramIn(Checkpoint *cp, const std::string §ion, 76 const std::string &name, T ¶m) 77{ 78 std::string str; 79 if (!cp->find(section, name, str) || !parseParam(str, param)) { 80 fatal("Can't unserialize '%s:%s'\n", section, name); 81 } 82} 83 84 85template <class T> 86void 87arrayParamOut(ostream &os, const std::string &name, 88 const T *param, int size) 89{ 90 os << name << "="; 91 if (size > 0) 92 showParam(os, param[0]); 93 for (int i = 1; i < size; ++i) { 94 os << " "; 95 showParam(os, param[i]); 96 } 97 os << "\n"; 98} 99 100 101template <class T> 102void 103arrayParamIn(Checkpoint *cp, const std::string §ion, 104 const std::string &name, T *param, int size) 105{ 106 std::string str; 107 if (!cp->find(section, name, str)) { 108 fatal("Can't unserialize '%s:%s'\n", section, name); 109 } 110 111 // code below stolen from VectorParam<T>::parse(). 112 // it would be nice to unify these somehow... 113 114 vector<string> tokens; 115 116 tokenize(tokens, str, ' '); 117 118 // Need this if we were doing a vector 119 // value.resize(tokens.size()); 120 121 if (tokens.size() != size) { 122 fatal("Array size mismatch on %s:%s'\n", section, name); 123 } 124 125 for (int i = 0; i < tokens.size(); i++) { 126 // need to parse into local variable to handle vector<bool>, 127 // for which operator[] returns a special reference class 128 // that's not the same as 'bool&', (since it's a packed 129 // vector) 130 T scalar_value; 131 if (!parseParam(tokens[i], scalar_value)) { 132 string err("could not parse \""); 133 134 err += str; 135 err += "\""; 136 137 fatal(err); 138 } 139 140 // assign parsed value to vector 141 param[i] = scalar_value; 142 } 143} 144 145 146void 147objParamIn(Checkpoint *cp, const std::string §ion, 148 const std::string &name, Serializable * ¶m) 149{ 150 if (!cp->findObj(section, name, param)) { 151 fatal("Can't unserialize '%s:%s'\n", section, name); 152 } 153} 154 155 156#define INSTANTIATE_PARAM_TEMPLATES(type) \ 157template void \ 158paramOut(ostream &os, const std::string &name, type const ¶m); \ 159template void \ 160paramIn(Checkpoint *cp, const std::string §ion, \ 161 const std::string &name, type & param); \ 162template void \ 163arrayParamOut(ostream &os, const std::string &name, \ 164 type const *param, int size); \ 165template void \ 166arrayParamIn(Checkpoint *cp, const std::string §ion, \ 167 const std::string &name, type *param, int size); 168 169 170INSTANTIATE_PARAM_TEMPLATES(int8_t) 171INSTANTIATE_PARAM_TEMPLATES(uint8_t) 172INSTANTIATE_PARAM_TEMPLATES(int16_t) 173INSTANTIATE_PARAM_TEMPLATES(uint16_t) 174INSTANTIATE_PARAM_TEMPLATES(int32_t) 175INSTANTIATE_PARAM_TEMPLATES(uint32_t) 176INSTANTIATE_PARAM_TEMPLATES(int64_t) 177INSTANTIATE_PARAM_TEMPLATES(uint64_t) 178INSTANTIATE_PARAM_TEMPLATES(bool) 179INSTANTIATE_PARAM_TEMPLATES(string) 180 181 182///////////////////////////// 183 184/// Container for serializing global variables (not associated with 185/// any serialized object). 186class Globals : public Serializable 187{ 188 public: 189 const string name() const; 190 void serialize(ostream& os); 191 void unserialize(Checkpoint *cp); 192}; 193 194/// The one and only instance of the Globals class. 195Globals globals; 196 197const string 198Globals::name() const 199{ 200 return "Globals"; 201} 202 203void 204Globals::serialize(ostream& os) 205{ 206 nameOut(os); 207 SERIALIZE_SCALAR(curTick); 208 209 nameOut(os, "MainEventQueue"); 210 mainEventQueue.serialize(os); 211} 212 213void 214Globals::unserialize(Checkpoint *cp) 215{ 216 const string §ion = name(); 217 UNSERIALIZE_SCALAR(curTick); 218 219 mainEventQueue.unserialize(cp, "MainEventQueue"); 220} 221 222void 223Serializable::serializeAll() 224{ 225 string dir = Checkpoint::dir(); 226 if (mkdir(dir.c_str(), 0775) == -1 && errno != EEXIST) 227 fatal("couldn't mkdir %s\n", dir); 228 229 string cpt_file = dir + Checkpoint::baseFilename; 230 ofstream outstream(cpt_file.c_str()); 231 time_t t = time(NULL); 232 outstream << "// checkpoint generated: " << ctime(&t); 233 234 globals.serialize(outstream); 235 SimObject::serializeAll(outstream); 236} 237 238 239void 240Serializable::unserializeGlobals(Checkpoint *cp) 241{ 242 globals.unserialize(cp); 243} 244 245 246class SerializeEvent : public Event 247{ 248 protected: 249 Tick repeat; 250 251 public: 252 SerializeEvent(Tick _when, Tick _repeat); 253 virtual void process(); 254 virtual void serialize(std::ostream &os) 255 { 256 panic("Cannot serialize the SerializeEvent"); 257 } 258 259}; 260 261SerializeEvent::SerializeEvent(Tick _when, Tick _repeat) 262 : Event(&mainEventQueue, Serialize_Pri), repeat(_repeat) 263{ 264 setFlags(AutoDelete); 265 schedule(_when); 266} 267 268void 269SerializeEvent::process() 270{ 271 Serializable::serializeAll(); 272 if (repeat) 273 schedule(curTick + repeat); 274} 275 276const char *Checkpoint::baseFilename = "m5.cpt"; 277 278static string checkpointDirBase; 279 280string 281Checkpoint::dir() 282{ 283 // use csprintf to insert curTick into directory name if it 284 // appears to have a format placeholder in it. 285 return (checkpointDirBase.find("%") != string::npos) ? 286 csprintf(checkpointDirBase, curTick) : checkpointDirBase; 287} 288 289void 290Checkpoint::setup(Tick when, Tick period) 291{ 292 new SerializeEvent(when, period); 293} 294 295class SerializeParamContext : public ParamContext 296{ 297 private: 298 SerializeEvent *event; 299 300 public: 301 SerializeParamContext(const string §ion); 302 ~SerializeParamContext(); 303 void checkParams(); 304}; 305 306SerializeParamContext serialParams("serialize"); 307 308Param<string> serialize_dir(&serialParams, "dir", 309 "dir to stick checkpoint in " 310 "(sprintf format with cycle #)"); 311 312Param<Counter> serialize_cycle(&serialParams, 313 "cycle", 314 "cycle to serialize", 315 0); 316 317Param<Counter> serialize_period(&serialParams, 318 "period", 319 "period to repeat serializations", 320 0); 321 322 323 324SerializeParamContext::SerializeParamContext(const string §ion) 325 : ParamContext(section), event(NULL) 326{ } 327 328SerializeParamContext::~SerializeParamContext() 329{ 330} 331 332void 333SerializeParamContext::checkParams() 334{ 335 if (serialize_dir.isValid()) { 336 checkpointDirBase = serialize_dir; 337 } else { 338 if (outputDirectory.empty()) 339 checkpointDirBase = "m5.%012d"; 340 else 341 checkpointDirBase = outputDirectory + "cpt.%012d"; 342 } 343 344 // guarantee that directory ends with a '/' 345 if (checkpointDirBase[checkpointDirBase.size() - 1] != '/') 346 checkpointDirBase += "/"; 347 348 if (serialize_cycle > 0) 349 Checkpoint::setup(serialize_cycle, serialize_period); 350} 351 352void 353debug_serialize() 354{ 355 Serializable::serializeAll(); 356} 357 358void 359debug_serialize(Tick when) 360{ 361 new SerializeEvent(when, 0); 362} 363 364//////////////////////////////////////////////////////////////////////// 365// 366// SerializableClass member definitions 367// 368//////////////////////////////////////////////////////////////////////// 369 370// Map of class names to SerializableBuilder creation functions. 371// Need to make this a pointer so we can force initialization on the 372// first reference; otherwise, some SerializableClass constructors 373// may be invoked before the classMap constructor. 374map<string,SerializableClass::CreateFunc> *SerializableClass::classMap = 0; 375 376// SerializableClass constructor: add mapping to classMap 377SerializableClass::SerializableClass(const string &className, 378 CreateFunc createFunc) 379{ 380 if (classMap == NULL) 381 classMap = new map<string,SerializableClass::CreateFunc>(); 382 383 if ((*classMap)[className]) 384 { 385 cerr << "Error: simulation object class " << className << " redefined" 386 << endl; 387 fatal(""); 388 } 389 390 // add className --> createFunc to class map 391 (*classMap)[className] = createFunc; 392} 393 394 395// 396// 397Serializable * 398SerializableClass::createObject(Checkpoint *cp, 399 const std::string §ion) 400{ 401 string className; 402 403 if (!cp->find(section, "type", className)) { 404 fatal("Serializable::create: no 'type' entry in section '%s'.\n", 405 section); 406 } 407 408 CreateFunc createFunc = (*classMap)[className]; 409 410 if (createFunc == NULL) { 411 fatal("Serializable::create: no create function for class '%s'.\n", 412 className); 413 } 414 415 Serializable *object = createFunc(cp, section); 416 417 assert(object != NULL); 418 419 return object; 420} 421 422 423Serializable * 424Serializable::create(Checkpoint *cp, const std::string §ion) 425{ 426 Serializable *object = SerializableClass::createObject(cp, section); 427 object->unserialize(cp, section); 428 return object; 429} 430 431 432Checkpoint::Checkpoint(const std::string &cpt_dir, const std::string &path, 433 const ConfigNode *_configNode) 434 : db(new IniFile), basePath(path), configNode(_configNode) 435{ 436 string filename = cpt_dir + "/" + Checkpoint::baseFilename; 437 if (!db->load(filename)) { 438 fatal("Can't load checkpoint file '%s'\n", filename); 439 } 440} 441 442 443bool 444Checkpoint::find(const std::string §ion, const std::string &entry, 445 std::string &value) 446{ 447 return db->find(section, entry, value); 448} 449 450 451bool 452Checkpoint::findObj(const std::string §ion, const std::string &entry, 453 Serializable *&value) 454{ 455 string path; 456 457 if (!db->find(section, entry, path)) 458 return false; 459 460 if ((value = configNode->resolveSimObject(path)) != NULL) 461 return true; 462 463 if ((value = objMap[path]) != NULL) 464 return true; 465 466 return false; 467} 468 469 470bool 471Checkpoint::sectionExists(const std::string §ion) 472{ 473 return db->sectionExists(section); 474} 475