serialize.hh revision 5543
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 4656SN/A#include "sim/host.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> 61217SN/Avoid arrayParamOut(std::ostream &os, const std::string &name, 62217SN/A const T *param, int size); 63217SN/A 64217SN/Atemplate <class T> 654841Ssaidi@eecs.umich.eduvoid arrayParamOut(std::ostream &os, const std::string &name, 664841Ssaidi@eecs.umich.edu const std::vector<T> ¶m); 674841Ssaidi@eecs.umich.edu 684841Ssaidi@eecs.umich.edutemplate <class T> 69237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string §ion, 70217SN/A const std::string &name, T *param, int size); 71217SN/A 724841Ssaidi@eecs.umich.edutemplate <class T> 734841Ssaidi@eecs.umich.eduvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 744841Ssaidi@eecs.umich.edu const std::string &name, std::vector<T> ¶m); 754841Ssaidi@eecs.umich.edu 76237SN/Avoid 77237SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 784000Ssaidi@eecs.umich.edu const std::string &name, SimObject * ¶m); 79237SN/A 80237SN/A 81217SN/A// 82217SN/A// These macros are streamlined to use in serialize/unserialize 83217SN/A// functions. It's assumed that serialize() has a parameter 'os' for 84237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'. 855543Ssaidi@eecs.umich.edu#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 86217SN/A 875543Ssaidi@eecs.umich.edu#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 88217SN/A 89223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out 905543Ssaidi@eecs.umich.edu#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 91223SN/A 925543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar) \ 935543Ssaidi@eecs.umich.edu do { \ 945543Ssaidi@eecs.umich.edu int tmp; \ 955543Ssaidi@eecs.umich.edu paramIn(cp, section, #scalar, tmp); \ 965543Ssaidi@eecs.umich.edu scalar = (typeof(scalar))tmp; \ 97223SN/A } while (0) 98223SN/A 995543Ssaidi@eecs.umich.edu#define SERIALIZE_ARRAY(member, size) \ 100217SN/A arrayParamOut(os, #member, member, size) 101217SN/A 1025543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size) \ 103237SN/A arrayParamIn(cp, section, #member, member, size) 104237SN/A 1055543Ssaidi@eecs.umich.edu#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 106237SN/A 1075543Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr) \ 1085543Ssaidi@eecs.umich.edu do { \ 1095543Ssaidi@eecs.umich.edu SimObject *sptr; \ 1105543Ssaidi@eecs.umich.edu objParamIn(cp, section, #objptr, sptr); \ 1115543Ssaidi@eecs.umich.edu objptr = dynamic_cast<typeof(objptr)>(sptr); \ 112237SN/A } while (0) 113217SN/A 1142SN/A/* 1152SN/A * Basic support for object serialization. 1162SN/A */ 117395SN/Aclass Serializable 1182SN/A{ 1192SN/A protected: 120510SN/A void nameOut(std::ostream &os); 121510SN/A void nameOut(std::ostream &os, const std::string &_name); 1222SN/A 1232SN/A public: 124395SN/A Serializable() {} 125395SN/A virtual ~Serializable() {} 1262SN/A 127265SN/A // manditory virtual function, so objects must provide names 128512SN/A virtual const std::string name() const = 0; 1292SN/A 130510SN/A virtual void serialize(std::ostream &os) {} 131237SN/A virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 132237SN/A 133395SN/A static Serializable *create(Checkpoint *cp, 134237SN/A const std::string §ion); 1352SN/A 1362287SN/A static int ckptCount; 1372287SN/A static int ckptMaxCount; 1382287SN/A static int ckptPrevCount; 1392868Sktlim@umich.edu static void serializeAll(const std::string &cpt_dir); 1402868Sktlim@umich.edu static void unserializeAll(const std::string &cpt_dir); 141395SN/A static void unserializeGlobals(Checkpoint *cp); 1422SN/A}; 1432SN/A 1442SN/A// 145395SN/A// A SerializableBuilder serves as an evaluation context for a set of 146395SN/A// parameters that describe a specific instance of a Serializable. This 1472SN/A// evaluation context corresponds to a section in the .ini file (as 1482SN/A// with the base ParamContext) plus an optional node in the 1492SN/A// configuration hierarchy (the configNode member) for resolving 150395SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1512SN/A// derived classes specialize the class for particular subclasses of 152395SN/A// Serializable (e.g., BaseCache). 1532SN/A// 1542SN/A// For typical usage, see the definition of 155395SN/A// SerializableClass::createObject(). 1562SN/A// 157395SN/Aclass SerializableBuilder 1582SN/A{ 1592SN/A public: 1602SN/A 161395SN/A SerializableBuilder() {} 1622SN/A 163395SN/A virtual ~SerializableBuilder() {} 1642SN/A 165395SN/A // Create the actual Serializable corresponding to the parameter 1662SN/A // values in this context. This function is overridden in derived 1672SN/A // classes to call a specific constructor for a particular 168395SN/A // subclass of Serializable. 169395SN/A virtual Serializable *create() = 0; 1702SN/A}; 1712SN/A 1722SN/A// 173395SN/A// An instance of SerializableClass corresponds to a class derived from 174395SN/A// Serializable. The SerializableClass instance serves to bind the string 1752SN/A// name (found in the config file) to a function that creates an 1762SN/A// instance of the appropriate derived class. 1772SN/A// 1782SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 1792SN/A// are first-class objects themselves. 1802SN/A// 181395SN/Aclass SerializableClass 1822SN/A{ 1832SN/A public: 1842SN/A 1852SN/A // Type CreateFunc is a pointer to a function that creates a new 1862SN/A // simulation object builder based on a .ini-file parameter 1872SN/A // section (specified by the first string argument), a unique name 1882SN/A // for the object (specified by the second string argument), and 1892SN/A // an optional config hierarchy node (specified by the third 190395SN/A // argument). A pointer to the new SerializableBuilder is returned. 191395SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 1922738Sstever@eecs.umich.edu const std::string §ion); 1932SN/A 1942SN/A static std::map<std::string,CreateFunc> *classMap; 1952SN/A 1962SN/A // Constructor. For example: 1972SN/A // 198395SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 199395SN/A // newBaseCacheSerializableBuilder); 2002SN/A // 201395SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 2022SN/A 203395SN/A // create Serializable given name of class and pointer to 2042SN/A // configuration hierarchy node 205395SN/A static Serializable *createObject(Checkpoint *cp, 2062738Sstever@eecs.umich.edu const std::string §ion); 2072SN/A}; 2082SN/A 2092SN/A// 2102SN/A// Macros to encapsulate the magic of declaring & defining 211395SN/A// SerializableBuilder and SerializableClass objects 2122SN/A// 2132SN/A 2145543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 2155543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 216237SN/A OBJ_CLASS::createForUnserialize); 2172SN/A 2182797Sktlim@umich.eduvoid 2192868Sktlim@umich.edusetCheckpointDir(const std::string &name); 2202797Sktlim@umich.edu 221237SN/Aclass Checkpoint 222237SN/A{ 223237SN/A private: 224237SN/A 225237SN/A IniFile *db; 226237SN/A const std::string basePath; 227395SN/A std::map<std::string, Serializable*> objMap; 228237SN/A 229237SN/A public: 2302738Sstever@eecs.umich.edu Checkpoint(const std::string &cpt_dir, const std::string &path); 231237SN/A 232937SN/A const std::string cptDir; 233937SN/A 234237SN/A bool find(const std::string §ion, const std::string &entry, 235237SN/A std::string &value); 236237SN/A 237237SN/A bool findObj(const std::string §ion, const std::string &entry, 2384000Ssaidi@eecs.umich.edu SimObject *&value); 239304SN/A 240304SN/A bool sectionExists(const std::string §ion); 241449SN/A 242449SN/A // The following static functions have to do with checkpoint 243449SN/A // creation rather than restoration. This class makes a handy 244449SN/A // namespace for them though. 245449SN/A 246449SN/A // Export current checkpoint directory name so other objects can 247449SN/A // derive filenames from it (e.g., memory). The return value is 248449SN/A // guaranteed to end in '/' so filenames can be directly appended. 249449SN/A // This function is only valid while a checkpoint is being created. 250449SN/A static std::string dir(); 251449SN/A 252449SN/A // Filename for base checkpoint file within directory. 253449SN/A static const char *baseFilename; 254237SN/A}; 2552SN/A 2562SN/A#endif // __SERIALIZE_HH__ 257