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/* @file 34 * Serialization Interface Declarations 35 */ 36 37#ifndef __SERIALIZE_HH__ 38#define __SERIALIZE_HH__ 39 40 41#include <iostream> 42#include <list> 43#include <map> 44#include <vector> 45 46#include "base/bitunion.hh" 47#include "base/types.hh" 48 49class IniFile; 50class Serializable; 51class Checkpoint; 52class SimObject; 53class EventQueue; 54 55/** The current version of the checkpoint format. 56 * This should be incremented by 1 and only 1 for every new version, where a new 57 * version is defined as a checkpoint created before this version won't work on 58 * the current version until the checkpoint format is updated. Adding a new 59 * SimObject shouldn't cause the version number to increase, only changes to 60 * existing objects such as serializing/unserializing more state, changing sizes 61 * of serialized arrays, etc. */ 62static const uint64_t gem5CheckpointVersion = 0x000000000000000e; 63 64template <class T> 65void paramOut(std::ostream &os, const std::string &name, const T ¶m); 66 67template <typename DataType, typename BitUnion> 68void paramOut(std::ostream &os, const std::string &name, 69 const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p) 70{ 71 paramOut(os, name, p.__data); 72} 73 74template <class T> 75void paramIn(Checkpoint *cp, const std::string §ion, 76 const std::string &name, T ¶m); 77 78template <typename DataType, typename BitUnion> 79void paramIn(Checkpoint *cp, const std::string §ion, 80 const std::string &name, 81 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p) 82{ 83 paramIn(cp, section, name, p.__data); 84} 85 86template <class T> 87bool optParamIn(Checkpoint *cp, const std::string §ion, 88 const std::string &name, T ¶m); 89 90template <typename DataType, typename BitUnion> 91bool optParamIn(Checkpoint *cp, const std::string §ion, 92 const std::string &name, 93 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p) 94{ 95 return optParamIn(cp, section, name, p.__data); 96} 97 98template <class T> 99void arrayParamOut(std::ostream &os, const std::string &name, 100 const T *param, unsigned size); 101 102template <class T> 103void arrayParamOut(std::ostream &os, const std::string &name, 104 const std::vector<T> ¶m); 105 106template <class T> 107void arrayParamOut(std::ostream &os, const std::string &name, 108 const std::list<T> ¶m); 109 110template <class T> 111void arrayParamIn(Checkpoint *cp, const std::string §ion, 112 const std::string &name, T *param, unsigned size); 113 114template <class T> 115void arrayParamIn(Checkpoint *cp, const std::string §ion, 116 const std::string &name, std::vector<T> ¶m); 117 118template <class T> 119void arrayParamIn(Checkpoint *cp, const std::string §ion, 120 const std::string &name, std::list<T> ¶m); 121 122void 123objParamIn(Checkpoint *cp, const std::string §ion, 124 const std::string &name, SimObject * ¶m); 125 126template <typename T> 127void fromInt(T &t, int i) 128{ 129 t = (T)i; 130} 131 132template <typename T> 133void fromSimObject(T &t, SimObject *s) 134{ 135 t = dynamic_cast<T>(s); 136} 137 138// 139// These macros are streamlined to use in serialize/unserialize 140// functions. It's assumed that serialize() has a parameter 'os' for 141// the ostream, and unserialize() has parameters 'cp' and 'section'. 142#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 143 144#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 145#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar) 146 147// ENUMs are like SCALARs, but we cast them to ints on the way out 148#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 149 150#define UNSERIALIZE_ENUM(scalar) \ 151 do { \ 152 int tmp; \ 153 paramIn(cp, section, #scalar, tmp); \ 154 fromInt(scalar, tmp); \ 155 } while (0) 156 157#define SERIALIZE_ARRAY(member, size) \ 158 arrayParamOut(os, #member, member, size) 159 160#define UNSERIALIZE_ARRAY(member, size) \ 161 arrayParamIn(cp, section, #member, member, size) 162
|
169#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 170 171#define UNSERIALIZE_OBJPTR(objptr) \ 172 do { \ 173 SimObject *sptr; \ 174 objParamIn(cp, section, #objptr, sptr); \ 175 fromSimObject(objptr, sptr); \ 176 } while (0) 177 178/** 179 * Basic support for object serialization. 180 * 181 * @note Many objects that support serialization need to be put in a 182 * consistent state when serialization takes place. We refer to the 183 * action of forcing an object into a consistent state as 184 * 'draining'. Objects that need draining inherit from Drainable. See 185 * Drainable for more information. 186 */ 187class Serializable 188{ 189 protected: 190 void nameOut(std::ostream &os); 191 void nameOut(std::ostream &os, const std::string &_name); 192 193 public: 194 Serializable(); 195 virtual ~Serializable(); 196 197 // manditory virtual function, so objects must provide names 198 virtual const std::string name() const = 0; 199 200 virtual void serialize(std::ostream &os); 201 virtual void unserialize(Checkpoint *cp, const std::string §ion); 202 203 static Serializable *create(Checkpoint *cp, const std::string §ion); 204 205 static int ckptCount; 206 static int ckptMaxCount; 207 static int ckptPrevCount; 208 static void serializeAll(const std::string &cpt_dir); 209 static void unserializeGlobals(Checkpoint *cp); 210}; 211 212void debug_serialize(const std::string &cpt_dir); 213 214// 215// A SerializableBuilder serves as an evaluation context for a set of 216// parameters that describe a specific instance of a Serializable. This 217// evaluation context corresponds to a section in the .ini file (as 218// with the base ParamContext) plus an optional node in the 219// configuration hierarchy (the configNode member) for resolving 220// Serializable references. SerializableBuilder is an abstract superclass; 221// derived classes specialize the class for particular subclasses of 222// Serializable (e.g., BaseCache). 223// 224// For typical usage, see the definition of 225// SerializableClass::createObject(). 226// 227class SerializableBuilder 228{ 229 public: 230 231 SerializableBuilder() {} 232 233 virtual ~SerializableBuilder() {} 234 235 // Create the actual Serializable corresponding to the parameter 236 // values in this context. This function is overridden in derived 237 // classes to call a specific constructor for a particular 238 // subclass of Serializable. 239 virtual Serializable *create() = 0; 240}; 241 242// 243// An instance of SerializableClass corresponds to a class derived from 244// Serializable. The SerializableClass instance serves to bind the string 245// name (found in the config file) to a function that creates an 246// instance of the appropriate derived class. 247// 248// This would be much cleaner in Smalltalk or Objective-C, where types 249// are first-class objects themselves. 250// 251class SerializableClass 252{ 253 public: 254 255 // Type CreateFunc is a pointer to a function that creates a new 256 // simulation object builder based on a .ini-file parameter 257 // section (specified by the first string argument), a unique name 258 // for the object (specified by the second string argument), and 259 // an optional config hierarchy node (specified by the third 260 // argument). A pointer to the new SerializableBuilder is returned. 261 typedef Serializable *(*CreateFunc)(Checkpoint *cp, 262 const std::string §ion); 263 264 static std::map<std::string,CreateFunc> *classMap; 265 266 // Constructor. For example: 267 // 268 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 269 // newBaseCacheSerializableBuilder); 270 // 271 SerializableClass(const std::string &className, CreateFunc createFunc); 272 273 // create Serializable given name of class and pointer to 274 // configuration hierarchy node 275 static Serializable *createObject(Checkpoint *cp, 276 const std::string §ion); 277}; 278 279// 280// Macros to encapsulate the magic of declaring & defining 281// SerializableBuilder and SerializableClass objects 282// 283 284#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 285SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 286 OBJ_CLASS::createForUnserialize); 287 288// Base class to wrap object resolving functionality. This can be 289// provided to Checkpoint to allow it to map object names onto 290// object C++ objects in which to unserialize 291class SimObjectResolver 292{ 293 public: 294 virtual ~SimObjectResolver() { } 295 296 // Find a SimObject given a full path name 297 virtual SimObject *resolveSimObject(const std::string &name) = 0; 298}; 299 300class Checkpoint 301{ 302 private: 303 304 IniFile *db; 305 306 SimObjectResolver &objNameResolver; 307 308 public: 309 Checkpoint(const std::string &cpt_dir, SimObjectResolver &resolver); 310 ~Checkpoint(); 311 312 const std::string cptDir; 313 314 bool find(const std::string §ion, const std::string &entry, 315 std::string &value); 316 317 bool findObj(const std::string §ion, const std::string &entry, 318 SimObject *&value); 319 320 bool sectionExists(const std::string §ion); 321 322 // The following static functions have to do with checkpoint 323 // creation rather than restoration. This class makes a handy 324 // namespace for them though. Currently no Checkpoint object is 325 // created on serialization (only unserialization) so we track the 326 // directory name as a global. It would be nice to change this 327 // someday 328 329 private: 330 // current directory we're serializing into. 331 static std::string currentDirectory; 332 333 public: 334 // Set the current directory. This function takes care of 335 // inserting curTick() if there's a '%d' in the argument, and 336 // appends a '/' if necessary. The final name is returned. 337 static std::string setDir(const std::string &base_name); 338 339 // Export current checkpoint directory name so other objects can 340 // derive filenames from it (e.g., memory). The return value is 341 // guaranteed to end in '/' so filenames can be directly appended. 342 // This function is only valid while a checkpoint is being created. 343 static std::string dir(); 344 345 // Filename for base checkpoint file within directory. 346 static const char *baseFilename; 347}; 348 349#endif // __SERIALIZE_HH__
|