serialize.hh revision 7823
12SN/A/* 21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282760Sbinkertn@umich.edu * Authors: Nathan Binkert 292760Sbinkertn@umich.edu * Erik Hallnor 302665Ssaidi@eecs.umich.edu * Steve Reinhardt 312SN/A */ 322SN/A 332SN/A/* @file 342SN/A * Serialization Interface Declarations 352SN/A */ 362SN/A 372SN/A#ifndef __SERIALIZE_HH__ 382SN/A#define __SERIALIZE_HH__ 392SN/A 402SN/A 412SN/A#include <list> 424841Ssaidi@eecs.umich.edu#include <vector> 432SN/A#include <iostream> 44217SN/A#include <map> 452SN/A 466214Snate@binkert.org#include "base/types.hh" 472SN/A 482738Sstever@eecs.umich.educlass IniFile; 49395SN/Aclass Serializable; 50237SN/Aclass Checkpoint; 514000Ssaidi@eecs.umich.educlass SimObject; 522SN/A 53217SN/Atemplate <class T> 54502SN/Avoid paramOut(std::ostream &os, const std::string &name, const T ¶m); 55217SN/A 56217SN/Atemplate <class T> 57237SN/Avoid paramIn(Checkpoint *cp, const std::string §ion, 58502SN/A const std::string &name, T ¶m); 59217SN/A 60217SN/Atemplate <class T> 616820SLisa.Hsu@amd.combool optParamIn(Checkpoint *cp, const std::string §ion, 626820SLisa.Hsu@amd.com const std::string &name, T ¶m); 636820SLisa.Hsu@amd.com 646820SLisa.Hsu@amd.comtemplate <class T> 65217SN/Avoid arrayParamOut(std::ostream &os, const std::string &name, 666227Snate@binkert.org const T *param, unsigned size); 67217SN/A 68217SN/Atemplate <class T> 694841Ssaidi@eecs.umich.eduvoid arrayParamOut(std::ostream &os, const std::string &name, 704841Ssaidi@eecs.umich.edu const std::vector<T> ¶m); 714841Ssaidi@eecs.umich.edu 724841Ssaidi@eecs.umich.edutemplate <class T> 73237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string §ion, 746227Snate@binkert.org const std::string &name, T *param, unsigned size); 75217SN/A 764841Ssaidi@eecs.umich.edutemplate <class T> 774841Ssaidi@eecs.umich.eduvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 784841Ssaidi@eecs.umich.edu const std::string &name, std::vector<T> ¶m); 794841Ssaidi@eecs.umich.edu 80237SN/Avoid 81237SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 824000Ssaidi@eecs.umich.edu const std::string &name, SimObject * ¶m); 83237SN/A 84237SN/A 85217SN/A// 86217SN/A// These macros are streamlined to use in serialize/unserialize 87217SN/A// functions. It's assumed that serialize() has a parameter 'os' for 88237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'. 895543Ssaidi@eecs.umich.edu#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 90217SN/A 915543Ssaidi@eecs.umich.edu#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 926820SLisa.Hsu@amd.com#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar) 93217SN/A 94223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out 955543Ssaidi@eecs.umich.edu#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 96223SN/A 975543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar) \ 985543Ssaidi@eecs.umich.edu do { \ 995543Ssaidi@eecs.umich.edu int tmp; \ 1005543Ssaidi@eecs.umich.edu paramIn(cp, section, #scalar, tmp); \ 1015543Ssaidi@eecs.umich.edu scalar = (typeof(scalar))tmp; \ 102223SN/A } while (0) 103223SN/A 1045543Ssaidi@eecs.umich.edu#define SERIALIZE_ARRAY(member, size) \ 105217SN/A arrayParamOut(os, #member, member, size) 106217SN/A 1075543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size) \ 108237SN/A arrayParamIn(cp, section, #member, member, size) 109237SN/A 1105543Ssaidi@eecs.umich.edu#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 111237SN/A 1125543Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr) \ 1135543Ssaidi@eecs.umich.edu do { \ 1145543Ssaidi@eecs.umich.edu SimObject *sptr; \ 1155543Ssaidi@eecs.umich.edu objParamIn(cp, section, #objptr, sptr); \ 1165543Ssaidi@eecs.umich.edu objptr = dynamic_cast<typeof(objptr)>(sptr); \ 117237SN/A } while (0) 118217SN/A 1192SN/A/* 1202SN/A * Basic support for object serialization. 1212SN/A */ 122395SN/Aclass Serializable 1232SN/A{ 1242SN/A protected: 125510SN/A void nameOut(std::ostream &os); 126510SN/A void nameOut(std::ostream &os, const std::string &_name); 1272SN/A 1282SN/A public: 1295739Snate@binkert.org Serializable(); 1305739Snate@binkert.org virtual ~Serializable(); 1312SN/A 132265SN/A // manditory virtual function, so objects must provide names 133512SN/A virtual const std::string name() const = 0; 1342SN/A 1355739Snate@binkert.org virtual void serialize(std::ostream &os); 1365739Snate@binkert.org virtual void unserialize(Checkpoint *cp, const std::string §ion); 137237SN/A 1385739Snate@binkert.org static Serializable *create(Checkpoint *cp, const std::string §ion); 1392SN/A 1402287SN/A static int ckptCount; 1412287SN/A static int ckptMaxCount; 1422287SN/A static int ckptPrevCount; 1432868Sktlim@umich.edu static void serializeAll(const std::string &cpt_dir); 144395SN/A static void unserializeGlobals(Checkpoint *cp); 1452SN/A}; 1462SN/A 1472SN/A// 148395SN/A// A SerializableBuilder serves as an evaluation context for a set of 149395SN/A// parameters that describe a specific instance of a Serializable. This 1502SN/A// evaluation context corresponds to a section in the .ini file (as 1512SN/A// with the base ParamContext) plus an optional node in the 1522SN/A// configuration hierarchy (the configNode member) for resolving 153395SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1542SN/A// derived classes specialize the class for particular subclasses of 155395SN/A// Serializable (e.g., BaseCache). 1562SN/A// 1572SN/A// For typical usage, see the definition of 158395SN/A// SerializableClass::createObject(). 1592SN/A// 160395SN/Aclass SerializableBuilder 1612SN/A{ 1622SN/A public: 1632SN/A 164395SN/A SerializableBuilder() {} 1652SN/A 166395SN/A virtual ~SerializableBuilder() {} 1672SN/A 168395SN/A // Create the actual Serializable corresponding to the parameter 1692SN/A // values in this context. This function is overridden in derived 1702SN/A // classes to call a specific constructor for a particular 171395SN/A // subclass of Serializable. 172395SN/A virtual Serializable *create() = 0; 1732SN/A}; 1742SN/A 1752SN/A// 176395SN/A// An instance of SerializableClass corresponds to a class derived from 177395SN/A// Serializable. The SerializableClass instance serves to bind the string 1782SN/A// name (found in the config file) to a function that creates an 1792SN/A// instance of the appropriate derived class. 1802SN/A// 1812SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 1822SN/A// are first-class objects themselves. 1832SN/A// 184395SN/Aclass SerializableClass 1852SN/A{ 1862SN/A public: 1872SN/A 1882SN/A // Type CreateFunc is a pointer to a function that creates a new 1892SN/A // simulation object builder based on a .ini-file parameter 1902SN/A // section (specified by the first string argument), a unique name 1912SN/A // for the object (specified by the second string argument), and 1922SN/A // an optional config hierarchy node (specified by the third 193395SN/A // argument). A pointer to the new SerializableBuilder is returned. 194395SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 1952738Sstever@eecs.umich.edu const std::string §ion); 1962SN/A 1972SN/A static std::map<std::string,CreateFunc> *classMap; 1982SN/A 1992SN/A // Constructor. For example: 2002SN/A // 201395SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 202395SN/A // newBaseCacheSerializableBuilder); 2032SN/A // 204395SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 2052SN/A 206395SN/A // create Serializable given name of class and pointer to 2072SN/A // configuration hierarchy node 208395SN/A static Serializable *createObject(Checkpoint *cp, 2092738Sstever@eecs.umich.edu const std::string §ion); 2102SN/A}; 2112SN/A 2122SN/A// 2132SN/A// Macros to encapsulate the magic of declaring & defining 214395SN/A// SerializableBuilder and SerializableClass objects 2152SN/A// 2162SN/A 2175543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 2185543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 219237SN/A OBJ_CLASS::createForUnserialize); 2202SN/A 221237SN/Aclass Checkpoint 222237SN/A{ 223237SN/A private: 224237SN/A 225237SN/A IniFile *db; 226237SN/A 227237SN/A public: 2287491Ssteve.reinhardt@amd.com Checkpoint(const std::string &cpt_dir); 229237SN/A 230937SN/A const std::string cptDir; 231937SN/A 232237SN/A bool find(const std::string §ion, const std::string &entry, 233237SN/A std::string &value); 234237SN/A 235237SN/A bool findObj(const std::string §ion, const std::string &entry, 2364000Ssaidi@eecs.umich.edu SimObject *&value); 237304SN/A 238304SN/A bool sectionExists(const std::string §ion); 239449SN/A 240449SN/A // The following static functions have to do with checkpoint 241449SN/A // creation rather than restoration. This class makes a handy 2427491Ssteve.reinhardt@amd.com // namespace for them though. Currently no Checkpoint object is 2437491Ssteve.reinhardt@amd.com // created on serialization (only unserialization) so we track the 2447491Ssteve.reinhardt@amd.com // directory name as a global. It would be nice to change this 2457491Ssteve.reinhardt@amd.com // someday 2467491Ssteve.reinhardt@amd.com 2477491Ssteve.reinhardt@amd.com private: 2487491Ssteve.reinhardt@amd.com // current directory we're serializing into. 2497491Ssteve.reinhardt@amd.com static std::string currentDirectory; 2507491Ssteve.reinhardt@amd.com 2517491Ssteve.reinhardt@amd.com public: 2527491Ssteve.reinhardt@amd.com // Set the current directory. This function takes care of 2537823Ssteve.reinhardt@amd.com // inserting curTick() if there's a '%d' in the argument, and 2547491Ssteve.reinhardt@amd.com // appends a '/' if necessary. The final name is returned. 2557491Ssteve.reinhardt@amd.com static std::string setDir(const std::string &base_name); 256449SN/A 257449SN/A // Export current checkpoint directory name so other objects can 258449SN/A // derive filenames from it (e.g., memory). The return value is 259449SN/A // guaranteed to end in '/' so filenames can be directly appended. 260449SN/A // This function is only valid while a checkpoint is being created. 261449SN/A static std::string dir(); 262449SN/A 263449SN/A // Filename for base checkpoint file within directory. 264449SN/A static const char *baseFilename; 265237SN/A}; 2662SN/A 2672SN/A#endif // __SERIALIZE_HH__ 268