serialize.hh revision 9048
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 418229Snate@binkert.org#include <iostream> 422SN/A#include <list> 438229Snate@binkert.org#include <map> 444841Ssaidi@eecs.umich.edu#include <vector> 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 539048SAli.Saidi@ARM.com/** The current version of the checkpoint format. 549048SAli.Saidi@ARM.com * This should be incremented by 1 and only 1 for every new version, where a new 559048SAli.Saidi@ARM.com * version is defined as a checkpoint created before this version wont work on 569048SAli.Saidi@ARM.com * the current version until the checkpoint format is updated. Adding a new 579048SAli.Saidi@ARM.com * SimObject shouldn't cause the version number to increase, only changes to 589048SAli.Saidi@ARM.com * existing objects such as serializing/unserializing more stote, changing sizes 599048SAli.Saidi@ARM.com * of serialized arrays, etc. */ 609048SAli.Saidi@ARM.comstatic const uint64_t gem5CheckpointVersion = 0x0000000000000001; 619048SAli.Saidi@ARM.com 62217SN/Atemplate <class T> 63502SN/Avoid paramOut(std::ostream &os, const std::string &name, const T ¶m); 64217SN/A 65217SN/Atemplate <class T> 66237SN/Avoid paramIn(Checkpoint *cp, const std::string §ion, 67502SN/A const std::string &name, T ¶m); 68217SN/A 69217SN/Atemplate <class T> 706820SLisa.Hsu@amd.combool optParamIn(Checkpoint *cp, const std::string §ion, 716820SLisa.Hsu@amd.com const std::string &name, T ¶m); 726820SLisa.Hsu@amd.com 736820SLisa.Hsu@amd.comtemplate <class T> 74217SN/Avoid arrayParamOut(std::ostream &os, const std::string &name, 756227Snate@binkert.org const T *param, unsigned size); 76217SN/A 77217SN/Atemplate <class T> 784841Ssaidi@eecs.umich.eduvoid arrayParamOut(std::ostream &os, const std::string &name, 794841Ssaidi@eecs.umich.edu const std::vector<T> ¶m); 804841Ssaidi@eecs.umich.edu 814841Ssaidi@eecs.umich.edutemplate <class T> 827948SAli.Saidi@ARM.comvoid arrayParamOut(std::ostream &os, const std::string &name, 837948SAli.Saidi@ARM.com const std::list<T> ¶m); 847948SAli.Saidi@ARM.com 857948SAli.Saidi@ARM.comtemplate <class T> 86237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string §ion, 876227Snate@binkert.org const std::string &name, T *param, unsigned size); 88217SN/A 894841Ssaidi@eecs.umich.edutemplate <class T> 904841Ssaidi@eecs.umich.eduvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 914841Ssaidi@eecs.umich.edu const std::string &name, std::vector<T> ¶m); 924841Ssaidi@eecs.umich.edu 937948SAli.Saidi@ARM.comtemplate <class T> 947948SAli.Saidi@ARM.comvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 957948SAli.Saidi@ARM.com const std::string &name, std::list<T> ¶m); 967948SAli.Saidi@ARM.com 97237SN/Avoid 98237SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 994000Ssaidi@eecs.umich.edu const std::string &name, SimObject * ¶m); 100237SN/A 1018902Sandreas.hansson@arm.comtemplate <typename T> 1028902Sandreas.hansson@arm.comvoid fromInt(T &t, int i) 1038902Sandreas.hansson@arm.com{ 1048902Sandreas.hansson@arm.com t = (T)i; 1058902Sandreas.hansson@arm.com} 1068902Sandreas.hansson@arm.com 1078902Sandreas.hansson@arm.comtemplate <typename T> 1088902Sandreas.hansson@arm.comvoid fromSimObject(T &t, SimObject *s) 1098902Sandreas.hansson@arm.com{ 1108902Sandreas.hansson@arm.com t = dynamic_cast<T>(s); 1118902Sandreas.hansson@arm.com} 112237SN/A 113217SN/A// 114217SN/A// These macros are streamlined to use in serialize/unserialize 115217SN/A// functions. It's assumed that serialize() has a parameter 'os' for 116237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'. 1175543Ssaidi@eecs.umich.edu#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 118217SN/A 1195543Ssaidi@eecs.umich.edu#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 1206820SLisa.Hsu@amd.com#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, section, #scalar, scalar) 121217SN/A 122223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out 1235543Ssaidi@eecs.umich.edu#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 124223SN/A 1255543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar) \ 1265543Ssaidi@eecs.umich.edu do { \ 1275543Ssaidi@eecs.umich.edu int tmp; \ 1285543Ssaidi@eecs.umich.edu paramIn(cp, section, #scalar, tmp); \ 1298902Sandreas.hansson@arm.com fromInt(scalar, tmp); \ 130223SN/A } while (0) 131223SN/A 1325543Ssaidi@eecs.umich.edu#define SERIALIZE_ARRAY(member, size) \ 133217SN/A arrayParamOut(os, #member, member, size) 134217SN/A 1355543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size) \ 136237SN/A arrayParamIn(cp, section, #member, member, size) 137237SN/A 1385543Ssaidi@eecs.umich.edu#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 139237SN/A 1405543Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr) \ 1415543Ssaidi@eecs.umich.edu do { \ 1425543Ssaidi@eecs.umich.edu SimObject *sptr; \ 1435543Ssaidi@eecs.umich.edu objParamIn(cp, section, #objptr, sptr); \ 1448902Sandreas.hansson@arm.com fromSimObject(objptr, sptr); \ 145237SN/A } while (0) 146217SN/A 1472SN/A/* 1482SN/A * Basic support for object serialization. 1492SN/A */ 150395SN/Aclass Serializable 1512SN/A{ 1522SN/A protected: 153510SN/A void nameOut(std::ostream &os); 154510SN/A void nameOut(std::ostream &os, const std::string &_name); 1552SN/A 1562SN/A public: 1575739Snate@binkert.org Serializable(); 1585739Snate@binkert.org virtual ~Serializable(); 1592SN/A 160265SN/A // manditory virtual function, so objects must provide names 161512SN/A virtual const std::string name() const = 0; 1622SN/A 1635739Snate@binkert.org virtual void serialize(std::ostream &os); 1645739Snate@binkert.org virtual void unserialize(Checkpoint *cp, const std::string §ion); 165237SN/A 1665739Snate@binkert.org static Serializable *create(Checkpoint *cp, const std::string §ion); 1672SN/A 1682287SN/A static int ckptCount; 1692287SN/A static int ckptMaxCount; 1702287SN/A static int ckptPrevCount; 1712868Sktlim@umich.edu static void serializeAll(const std::string &cpt_dir); 172395SN/A static void unserializeGlobals(Checkpoint *cp); 1732SN/A}; 1742SN/A 1752SN/A// 176395SN/A// A SerializableBuilder serves as an evaluation context for a set of 177395SN/A// parameters that describe a specific instance of a Serializable. This 1782SN/A// evaluation context corresponds to a section in the .ini file (as 1792SN/A// with the base ParamContext) plus an optional node in the 1802SN/A// configuration hierarchy (the configNode member) for resolving 181395SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1822SN/A// derived classes specialize the class for particular subclasses of 183395SN/A// Serializable (e.g., BaseCache). 1842SN/A// 1852SN/A// For typical usage, see the definition of 186395SN/A// SerializableClass::createObject(). 1872SN/A// 188395SN/Aclass SerializableBuilder 1892SN/A{ 1902SN/A public: 1912SN/A 192395SN/A SerializableBuilder() {} 1932SN/A 194395SN/A virtual ~SerializableBuilder() {} 1952SN/A 196395SN/A // Create the actual Serializable corresponding to the parameter 1972SN/A // values in this context. This function is overridden in derived 1982SN/A // classes to call a specific constructor for a particular 199395SN/A // subclass of Serializable. 200395SN/A virtual Serializable *create() = 0; 2012SN/A}; 2022SN/A 2032SN/A// 204395SN/A// An instance of SerializableClass corresponds to a class derived from 205395SN/A// Serializable. The SerializableClass instance serves to bind the string 2062SN/A// name (found in the config file) to a function that creates an 2072SN/A// instance of the appropriate derived class. 2082SN/A// 2092SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 2102SN/A// are first-class objects themselves. 2112SN/A// 212395SN/Aclass SerializableClass 2132SN/A{ 2142SN/A public: 2152SN/A 2162SN/A // Type CreateFunc is a pointer to a function that creates a new 2172SN/A // simulation object builder based on a .ini-file parameter 2182SN/A // section (specified by the first string argument), a unique name 2192SN/A // for the object (specified by the second string argument), and 2202SN/A // an optional config hierarchy node (specified by the third 221395SN/A // argument). A pointer to the new SerializableBuilder is returned. 222395SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 2232738Sstever@eecs.umich.edu const std::string §ion); 2242SN/A 2252SN/A static std::map<std::string,CreateFunc> *classMap; 2262SN/A 2272SN/A // Constructor. For example: 2282SN/A // 229395SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 230395SN/A // newBaseCacheSerializableBuilder); 2312SN/A // 232395SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 2332SN/A 234395SN/A // create Serializable given name of class and pointer to 2352SN/A // configuration hierarchy node 236395SN/A static Serializable *createObject(Checkpoint *cp, 2372738Sstever@eecs.umich.edu const std::string §ion); 2382SN/A}; 2392SN/A 2402SN/A// 2412SN/A// Macros to encapsulate the magic of declaring & defining 242395SN/A// SerializableBuilder and SerializableClass objects 2432SN/A// 2442SN/A 2455543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 2465543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 247237SN/A OBJ_CLASS::createForUnserialize); 2482SN/A 249237SN/Aclass Checkpoint 250237SN/A{ 251237SN/A private: 252237SN/A 253237SN/A IniFile *db; 254237SN/A 255237SN/A public: 2567491Ssteve.reinhardt@amd.com Checkpoint(const std::string &cpt_dir); 257237SN/A 258937SN/A const std::string cptDir; 259937SN/A 260237SN/A bool find(const std::string §ion, const std::string &entry, 261237SN/A std::string &value); 262237SN/A 263237SN/A bool findObj(const std::string §ion, const std::string &entry, 2644000Ssaidi@eecs.umich.edu SimObject *&value); 265304SN/A 266304SN/A bool sectionExists(const std::string §ion); 267449SN/A 268449SN/A // The following static functions have to do with checkpoint 269449SN/A // creation rather than restoration. This class makes a handy 2707491Ssteve.reinhardt@amd.com // namespace for them though. Currently no Checkpoint object is 2717491Ssteve.reinhardt@amd.com // created on serialization (only unserialization) so we track the 2727491Ssteve.reinhardt@amd.com // directory name as a global. It would be nice to change this 2737491Ssteve.reinhardt@amd.com // someday 2747491Ssteve.reinhardt@amd.com 2757491Ssteve.reinhardt@amd.com private: 2767491Ssteve.reinhardt@amd.com // current directory we're serializing into. 2777491Ssteve.reinhardt@amd.com static std::string currentDirectory; 2787491Ssteve.reinhardt@amd.com 2797491Ssteve.reinhardt@amd.com public: 2807491Ssteve.reinhardt@amd.com // Set the current directory. This function takes care of 2817823Ssteve.reinhardt@amd.com // inserting curTick() if there's a '%d' in the argument, and 2827491Ssteve.reinhardt@amd.com // appends a '/' if necessary. The final name is returned. 2837491Ssteve.reinhardt@amd.com static std::string setDir(const std::string &base_name); 284449SN/A 285449SN/A // Export current checkpoint directory name so other objects can 286449SN/A // derive filenames from it (e.g., memory). The return value is 287449SN/A // guaranteed to end in '/' so filenames can be directly appended. 288449SN/A // This function is only valid while a checkpoint is being created. 289449SN/A static std::string dir(); 290449SN/A 291449SN/A // Filename for base checkpoint file within directory. 292449SN/A static const char *baseFilename; 293237SN/A}; 2942SN/A 2952SN/A#endif // __SERIALIZE_HH__ 296