serialize.hh revision 4000
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> 422SN/A#include <iostream> 43217SN/A#include <map> 442SN/A 4556SN/A#include "sim/host.hh" 462SN/A 472738Sstever@eecs.umich.educlass IniFile; 48395SN/Aclass Serializable; 49237SN/Aclass Checkpoint; 504000Ssaidi@eecs.umich.educlass SimObject; 512SN/A 52217SN/Atemplate <class T> 53502SN/Avoid paramOut(std::ostream &os, const std::string &name, const T ¶m); 54217SN/A 55217SN/Atemplate <class T> 56237SN/Avoid paramIn(Checkpoint *cp, const std::string §ion, 57502SN/A const std::string &name, T ¶m); 58217SN/A 59217SN/Atemplate <class T> 60217SN/Avoid arrayParamOut(std::ostream &os, const std::string &name, 61217SN/A const T *param, int size); 62217SN/A 63217SN/Atemplate <class T> 64237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string §ion, 65217SN/A const std::string &name, T *param, int size); 66217SN/A 67237SN/Avoid 68237SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 694000Ssaidi@eecs.umich.edu const std::string &name, SimObject * ¶m); 70237SN/A 71237SN/A 72217SN/A// 73217SN/A// These macros are streamlined to use in serialize/unserialize 74217SN/A// functions. It's assumed that serialize() has a parameter 'os' for 75237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'. 76222SN/A#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 77217SN/A 78237SN/A#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 79217SN/A 80223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out 81223SN/A#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 82223SN/A 83223SN/A#define UNSERIALIZE_ENUM(scalar) \ 84223SN/A do { \ 85223SN/A int tmp; \ 86237SN/A paramIn(cp, section, #scalar, tmp); \ 87223SN/A scalar = (typeof(scalar))tmp; \ 88223SN/A } while (0) 89223SN/A 90217SN/A#define SERIALIZE_ARRAY(member, size) \ 91217SN/A arrayParamOut(os, #member, member, size) 92217SN/A 93217SN/A#define UNSERIALIZE_ARRAY(member, size) \ 94237SN/A arrayParamIn(cp, section, #member, member, size) 95237SN/A 96237SN/A#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 97237SN/A 98237SN/A#define UNSERIALIZE_OBJPTR(objptr) \ 99237SN/A do { \ 1004000Ssaidi@eecs.umich.edu SimObject *sptr; \ 101237SN/A objParamIn(cp, section, #objptr, sptr); \ 102237SN/A objptr = dynamic_cast<typeof(objptr)>(sptr); \ 103237SN/A } while (0) 104217SN/A 1052SN/A/* 1062SN/A * Basic support for object serialization. 1072SN/A */ 108395SN/Aclass Serializable 1092SN/A{ 1102SN/A protected: 111510SN/A void nameOut(std::ostream &os); 112510SN/A void nameOut(std::ostream &os, const std::string &_name); 1132SN/A 1142SN/A public: 115395SN/A Serializable() {} 116395SN/A virtual ~Serializable() {} 1172SN/A 118265SN/A // manditory virtual function, so objects must provide names 119512SN/A virtual const std::string name() const = 0; 1202SN/A 121510SN/A virtual void serialize(std::ostream &os) {} 122237SN/A virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 123237SN/A 124395SN/A static Serializable *create(Checkpoint *cp, 125237SN/A const std::string §ion); 1262SN/A 1272287SN/A static int ckptCount; 1282287SN/A static int ckptMaxCount; 1292287SN/A static int ckptPrevCount; 1302868Sktlim@umich.edu static void serializeAll(const std::string &cpt_dir); 1312868Sktlim@umich.edu static void unserializeAll(const std::string &cpt_dir); 132395SN/A static void unserializeGlobals(Checkpoint *cp); 1332SN/A}; 1342SN/A 1352SN/A// 136395SN/A// A SerializableBuilder serves as an evaluation context for a set of 137395SN/A// parameters that describe a specific instance of a Serializable. This 1382SN/A// evaluation context corresponds to a section in the .ini file (as 1392SN/A// with the base ParamContext) plus an optional node in the 1402SN/A// configuration hierarchy (the configNode member) for resolving 141395SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1422SN/A// derived classes specialize the class for particular subclasses of 143395SN/A// Serializable (e.g., BaseCache). 1442SN/A// 1452SN/A// For typical usage, see the definition of 146395SN/A// SerializableClass::createObject(). 1472SN/A// 148395SN/Aclass SerializableBuilder 1492SN/A{ 1502SN/A public: 1512SN/A 152395SN/A SerializableBuilder() {} 1532SN/A 154395SN/A virtual ~SerializableBuilder() {} 1552SN/A 156395SN/A // Create the actual Serializable corresponding to the parameter 1572SN/A // values in this context. This function is overridden in derived 1582SN/A // classes to call a specific constructor for a particular 159395SN/A // subclass of Serializable. 160395SN/A virtual Serializable *create() = 0; 1612SN/A}; 1622SN/A 1632SN/A// 164395SN/A// An instance of SerializableClass corresponds to a class derived from 165395SN/A// Serializable. The SerializableClass instance serves to bind the string 1662SN/A// name (found in the config file) to a function that creates an 1672SN/A// instance of the appropriate derived class. 1682SN/A// 1692SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 1702SN/A// are first-class objects themselves. 1712SN/A// 172395SN/Aclass SerializableClass 1732SN/A{ 1742SN/A public: 1752SN/A 1762SN/A // Type CreateFunc is a pointer to a function that creates a new 1772SN/A // simulation object builder based on a .ini-file parameter 1782SN/A // section (specified by the first string argument), a unique name 1792SN/A // for the object (specified by the second string argument), and 1802SN/A // an optional config hierarchy node (specified by the third 181395SN/A // argument). A pointer to the new SerializableBuilder is returned. 182395SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 1832738Sstever@eecs.umich.edu const std::string §ion); 1842SN/A 1852SN/A static std::map<std::string,CreateFunc> *classMap; 1862SN/A 1872SN/A // Constructor. For example: 1882SN/A // 189395SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 190395SN/A // newBaseCacheSerializableBuilder); 1912SN/A // 192395SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 1932SN/A 194395SN/A // create Serializable given name of class and pointer to 1952SN/A // configuration hierarchy node 196395SN/A static Serializable *createObject(Checkpoint *cp, 1972738Sstever@eecs.umich.edu const std::string §ion); 1982SN/A}; 1992SN/A 2002SN/A// 2012SN/A// Macros to encapsulate the magic of declaring & defining 202395SN/A// SerializableBuilder and SerializableClass objects 2032SN/A// 2042SN/A 205237SN/A#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 206395SN/ASerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 207237SN/A OBJ_CLASS::createForUnserialize); 2082SN/A 2092797Sktlim@umich.eduvoid 2102868Sktlim@umich.edusetCheckpointDir(const std::string &name); 2112797Sktlim@umich.edu 212237SN/Aclass Checkpoint 213237SN/A{ 214237SN/A private: 215237SN/A 216237SN/A IniFile *db; 217237SN/A const std::string basePath; 218395SN/A std::map<std::string, Serializable*> objMap; 219237SN/A 220237SN/A public: 2212738Sstever@eecs.umich.edu Checkpoint(const std::string &cpt_dir, const std::string &path); 222237SN/A 223937SN/A const std::string cptDir; 224937SN/A 225237SN/A bool find(const std::string §ion, const std::string &entry, 226237SN/A std::string &value); 227237SN/A 228237SN/A bool findObj(const std::string §ion, const std::string &entry, 2294000Ssaidi@eecs.umich.edu SimObject *&value); 230304SN/A 231304SN/A bool sectionExists(const std::string §ion); 232449SN/A 233449SN/A // The following static functions have to do with checkpoint 234449SN/A // creation rather than restoration. This class makes a handy 235449SN/A // namespace for them though. 236449SN/A 237449SN/A // Export current checkpoint directory name so other objects can 238449SN/A // derive filenames from it (e.g., memory). The return value is 239449SN/A // guaranteed to end in '/' so filenames can be directly appended. 240449SN/A // This function is only valid while a checkpoint is being created. 241449SN/A static std::string dir(); 242449SN/A 243449SN/A // Filename for base checkpoint file within directory. 244449SN/A static const char *baseFilename; 245237SN/A}; 2462SN/A 2472SN/A#endif // __SERIALIZE_HH__ 248