serialize.hh revision 7948
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> 737948SAli.Saidi@ARM.comvoid arrayParamOut(std::ostream &os, const std::string &name, 747948SAli.Saidi@ARM.com const std::list<T> ¶m); 757948SAli.Saidi@ARM.com 767948SAli.Saidi@ARM.comtemplate <class T> 77237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string §ion, 786227Snate@binkert.org const std::string &name, T *param, unsigned size); 79217SN/A 804841Ssaidi@eecs.umich.edutemplate <class T> 814841Ssaidi@eecs.umich.eduvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 824841Ssaidi@eecs.umich.edu const std::string &name, std::vector<T> ¶m); 834841Ssaidi@eecs.umich.edu 847948SAli.Saidi@ARM.comtemplate <class T> 857948SAli.Saidi@ARM.comvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 867948SAli.Saidi@ARM.com const std::string &name, std::list<T> ¶m); 877948SAli.Saidi@ARM.com 88237SN/Avoid 89237SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 904000Ssaidi@eecs.umich.edu const std::string &name, SimObject * ¶m); 91237SN/A 92237SN/A 93217SN/A// 94217SN/A// These macros are streamlined to use in serialize/unserialize 95217SN/A// functions. It's assumed that serialize() has a parameter 'os' for 96237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'. 975543Ssaidi@eecs.umich.edu#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 98217SN/A 995543Ssaidi@eecs.umich.edu#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 1006820SLisa.Hsu@amd.com#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar) 101217SN/A 102223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out 1035543Ssaidi@eecs.umich.edu#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 104223SN/A 1055543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar) \ 1065543Ssaidi@eecs.umich.edu do { \ 1075543Ssaidi@eecs.umich.edu int tmp; \ 1085543Ssaidi@eecs.umich.edu paramIn(cp, section, #scalar, tmp); \ 1095543Ssaidi@eecs.umich.edu scalar = (typeof(scalar))tmp; \ 110223SN/A } while (0) 111223SN/A 1125543Ssaidi@eecs.umich.edu#define SERIALIZE_ARRAY(member, size) \ 113217SN/A arrayParamOut(os, #member, member, size) 114217SN/A 1155543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size) \ 116237SN/A arrayParamIn(cp, section, #member, member, size) 117237SN/A 1185543Ssaidi@eecs.umich.edu#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 119237SN/A 1205543Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr) \ 1215543Ssaidi@eecs.umich.edu do { \ 1225543Ssaidi@eecs.umich.edu SimObject *sptr; \ 1235543Ssaidi@eecs.umich.edu objParamIn(cp, section, #objptr, sptr); \ 1245543Ssaidi@eecs.umich.edu objptr = dynamic_cast<typeof(objptr)>(sptr); \ 125237SN/A } while (0) 126217SN/A 1272SN/A/* 1282SN/A * Basic support for object serialization. 1292SN/A */ 130395SN/Aclass Serializable 1312SN/A{ 1322SN/A protected: 133510SN/A void nameOut(std::ostream &os); 134510SN/A void nameOut(std::ostream &os, const std::string &_name); 1352SN/A 1362SN/A public: 1375739Snate@binkert.org Serializable(); 1385739Snate@binkert.org virtual ~Serializable(); 1392SN/A 140265SN/A // manditory virtual function, so objects must provide names 141512SN/A virtual const std::string name() const = 0; 1422SN/A 1435739Snate@binkert.org virtual void serialize(std::ostream &os); 1445739Snate@binkert.org virtual void unserialize(Checkpoint *cp, const std::string §ion); 145237SN/A 1465739Snate@binkert.org static Serializable *create(Checkpoint *cp, const std::string §ion); 1472SN/A 1482287SN/A static int ckptCount; 1492287SN/A static int ckptMaxCount; 1502287SN/A static int ckptPrevCount; 1512868Sktlim@umich.edu static void serializeAll(const std::string &cpt_dir); 152395SN/A static void unserializeGlobals(Checkpoint *cp); 1532SN/A}; 1542SN/A 1552SN/A// 156395SN/A// A SerializableBuilder serves as an evaluation context for a set of 157395SN/A// parameters that describe a specific instance of a Serializable. This 1582SN/A// evaluation context corresponds to a section in the .ini file (as 1592SN/A// with the base ParamContext) plus an optional node in the 1602SN/A// configuration hierarchy (the configNode member) for resolving 161395SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1622SN/A// derived classes specialize the class for particular subclasses of 163395SN/A// Serializable (e.g., BaseCache). 1642SN/A// 1652SN/A// For typical usage, see the definition of 166395SN/A// SerializableClass::createObject(). 1672SN/A// 168395SN/Aclass SerializableBuilder 1692SN/A{ 1702SN/A public: 1712SN/A 172395SN/A SerializableBuilder() {} 1732SN/A 174395SN/A virtual ~SerializableBuilder() {} 1752SN/A 176395SN/A // Create the actual Serializable corresponding to the parameter 1772SN/A // values in this context. This function is overridden in derived 1782SN/A // classes to call a specific constructor for a particular 179395SN/A // subclass of Serializable. 180395SN/A virtual Serializable *create() = 0; 1812SN/A}; 1822SN/A 1832SN/A// 184395SN/A// An instance of SerializableClass corresponds to a class derived from 185395SN/A// Serializable. The SerializableClass instance serves to bind the string 1862SN/A// name (found in the config file) to a function that creates an 1872SN/A// instance of the appropriate derived class. 1882SN/A// 1892SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 1902SN/A// are first-class objects themselves. 1912SN/A// 192395SN/Aclass SerializableClass 1932SN/A{ 1942SN/A public: 1952SN/A 1962SN/A // Type CreateFunc is a pointer to a function that creates a new 1972SN/A // simulation object builder based on a .ini-file parameter 1982SN/A // section (specified by the first string argument), a unique name 1992SN/A // for the object (specified by the second string argument), and 2002SN/A // an optional config hierarchy node (specified by the third 201395SN/A // argument). A pointer to the new SerializableBuilder is returned. 202395SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 2032738Sstever@eecs.umich.edu const std::string §ion); 2042SN/A 2052SN/A static std::map<std::string,CreateFunc> *classMap; 2062SN/A 2072SN/A // Constructor. For example: 2082SN/A // 209395SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 210395SN/A // newBaseCacheSerializableBuilder); 2112SN/A // 212395SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 2132SN/A 214395SN/A // create Serializable given name of class and pointer to 2152SN/A // configuration hierarchy node 216395SN/A static Serializable *createObject(Checkpoint *cp, 2172738Sstever@eecs.umich.edu const std::string §ion); 2182SN/A}; 2192SN/A 2202SN/A// 2212SN/A// Macros to encapsulate the magic of declaring & defining 222395SN/A// SerializableBuilder and SerializableClass objects 2232SN/A// 2242SN/A 2255543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 2265543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 227237SN/A OBJ_CLASS::createForUnserialize); 2282SN/A 229237SN/Aclass Checkpoint 230237SN/A{ 231237SN/A private: 232237SN/A 233237SN/A IniFile *db; 234237SN/A 235237SN/A public: 2367491Ssteve.reinhardt@amd.com Checkpoint(const std::string &cpt_dir); 237237SN/A 238937SN/A const std::string cptDir; 239937SN/A 240237SN/A bool find(const std::string §ion, const std::string &entry, 241237SN/A std::string &value); 242237SN/A 243237SN/A bool findObj(const std::string §ion, const std::string &entry, 2444000Ssaidi@eecs.umich.edu SimObject *&value); 245304SN/A 246304SN/A bool sectionExists(const std::string §ion); 247449SN/A 248449SN/A // The following static functions have to do with checkpoint 249449SN/A // creation rather than restoration. This class makes a handy 2507491Ssteve.reinhardt@amd.com // namespace for them though. Currently no Checkpoint object is 2517491Ssteve.reinhardt@amd.com // created on serialization (only unserialization) so we track the 2527491Ssteve.reinhardt@amd.com // directory name as a global. It would be nice to change this 2537491Ssteve.reinhardt@amd.com // someday 2547491Ssteve.reinhardt@amd.com 2557491Ssteve.reinhardt@amd.com private: 2567491Ssteve.reinhardt@amd.com // current directory we're serializing into. 2577491Ssteve.reinhardt@amd.com static std::string currentDirectory; 2587491Ssteve.reinhardt@amd.com 2597491Ssteve.reinhardt@amd.com public: 2607491Ssteve.reinhardt@amd.com // Set the current directory. This function takes care of 2617823Ssteve.reinhardt@amd.com // inserting curTick() if there's a '%d' in the argument, and 2627491Ssteve.reinhardt@amd.com // appends a '/' if necessary. The final name is returned. 2637491Ssteve.reinhardt@amd.com static std::string setDir(const std::string &base_name); 264449SN/A 265449SN/A // Export current checkpoint directory name so other objects can 266449SN/A // derive filenames from it (e.g., memory). The return value is 267449SN/A // guaranteed to end in '/' so filenames can be directly appended. 268449SN/A // This function is only valid while a checkpoint is being created. 269449SN/A static std::string dir(); 270449SN/A 271449SN/A // Filename for base checkpoint file within directory. 272449SN/A static const char *baseFilename; 273237SN/A}; 2742SN/A 2752SN/A#endif // __SERIALIZE_HH__ 276