serialize.hh revision 5739
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: 1245739Snate@binkert.org Serializable(); 1255739Snate@binkert.org 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 1305739Snate@binkert.org virtual void serialize(std::ostream &os); 1315739Snate@binkert.org virtual void unserialize(Checkpoint *cp, const std::string §ion); 132237SN/A 1335739Snate@binkert.org static Serializable *create(Checkpoint *cp, const std::string §ion); 1342SN/A 1352287SN/A static int ckptCount; 1362287SN/A static int ckptMaxCount; 1372287SN/A static int ckptPrevCount; 1382868Sktlim@umich.edu static void serializeAll(const std::string &cpt_dir); 1392868Sktlim@umich.edu static void unserializeAll(const std::string &cpt_dir); 140395SN/A static void unserializeGlobals(Checkpoint *cp); 1412SN/A}; 1422SN/A 1432SN/A// 144395SN/A// A SerializableBuilder serves as an evaluation context for a set of 145395SN/A// parameters that describe a specific instance of a Serializable. This 1462SN/A// evaluation context corresponds to a section in the .ini file (as 1472SN/A// with the base ParamContext) plus an optional node in the 1482SN/A// configuration hierarchy (the configNode member) for resolving 149395SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1502SN/A// derived classes specialize the class for particular subclasses of 151395SN/A// Serializable (e.g., BaseCache). 1522SN/A// 1532SN/A// For typical usage, see the definition of 154395SN/A// SerializableClass::createObject(). 1552SN/A// 156395SN/Aclass SerializableBuilder 1572SN/A{ 1582SN/A public: 1592SN/A 160395SN/A SerializableBuilder() {} 1612SN/A 162395SN/A virtual ~SerializableBuilder() {} 1632SN/A 164395SN/A // Create the actual Serializable corresponding to the parameter 1652SN/A // values in this context. This function is overridden in derived 1662SN/A // classes to call a specific constructor for a particular 167395SN/A // subclass of Serializable. 168395SN/A virtual Serializable *create() = 0; 1692SN/A}; 1702SN/A 1712SN/A// 172395SN/A// An instance of SerializableClass corresponds to a class derived from 173395SN/A// Serializable. The SerializableClass instance serves to bind the string 1742SN/A// name (found in the config file) to a function that creates an 1752SN/A// instance of the appropriate derived class. 1762SN/A// 1772SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 1782SN/A// are first-class objects themselves. 1792SN/A// 180395SN/Aclass SerializableClass 1812SN/A{ 1822SN/A public: 1832SN/A 1842SN/A // Type CreateFunc is a pointer to a function that creates a new 1852SN/A // simulation object builder based on a .ini-file parameter 1862SN/A // section (specified by the first string argument), a unique name 1872SN/A // for the object (specified by the second string argument), and 1882SN/A // an optional config hierarchy node (specified by the third 189395SN/A // argument). A pointer to the new SerializableBuilder is returned. 190395SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 1912738Sstever@eecs.umich.edu const std::string §ion); 1922SN/A 1932SN/A static std::map<std::string,CreateFunc> *classMap; 1942SN/A 1952SN/A // Constructor. For example: 1962SN/A // 197395SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 198395SN/A // newBaseCacheSerializableBuilder); 1992SN/A // 200395SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 2012SN/A 202395SN/A // create Serializable given name of class and pointer to 2032SN/A // configuration hierarchy node 204395SN/A static Serializable *createObject(Checkpoint *cp, 2052738Sstever@eecs.umich.edu const std::string §ion); 2062SN/A}; 2072SN/A 2082SN/A// 2092SN/A// Macros to encapsulate the magic of declaring & defining 210395SN/A// SerializableBuilder and SerializableClass objects 2112SN/A// 2122SN/A 2135543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 2145543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 215237SN/A OBJ_CLASS::createForUnserialize); 2162SN/A 2172797Sktlim@umich.eduvoid 2182868Sktlim@umich.edusetCheckpointDir(const std::string &name); 2192797Sktlim@umich.edu 220237SN/Aclass Checkpoint 221237SN/A{ 222237SN/A private: 223237SN/A 224237SN/A IniFile *db; 225237SN/A const std::string basePath; 226395SN/A std::map<std::string, Serializable*> objMap; 227237SN/A 228237SN/A public: 2292738Sstever@eecs.umich.edu Checkpoint(const std::string &cpt_dir, const std::string &path); 230237SN/A 231937SN/A const std::string cptDir; 232937SN/A 233237SN/A bool find(const std::string §ion, const std::string &entry, 234237SN/A std::string &value); 235237SN/A 236237SN/A bool findObj(const std::string §ion, const std::string &entry, 2374000Ssaidi@eecs.umich.edu SimObject *&value); 238304SN/A 239304SN/A bool sectionExists(const std::string §ion); 240449SN/A 241449SN/A // The following static functions have to do with checkpoint 242449SN/A // creation rather than restoration. This class makes a handy 243449SN/A // namespace for them though. 244449SN/A 245449SN/A // Export current checkpoint directory name so other objects can 246449SN/A // derive filenames from it (e.g., memory). The return value is 247449SN/A // guaranteed to end in '/' so filenames can be directly appended. 248449SN/A // This function is only valid while a checkpoint is being created. 249449SN/A static std::string dir(); 250449SN/A 251449SN/A // Filename for base checkpoint file within directory. 252449SN/A static const char *baseFilename; 253237SN/A}; 2542SN/A 2552SN/A#endif // __SERIALIZE_HH__ 256