serialize.hh revision 2287
110259SAndrew.Bardsley@arm.com/* 210259SAndrew.Bardsley@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan 310259SAndrew.Bardsley@arm.com * All rights reserved. 410259SAndrew.Bardsley@arm.com * 510259SAndrew.Bardsley@arm.com * Redistribution and use in source and binary forms, with or without 610259SAndrew.Bardsley@arm.com * modification, are permitted provided that the following conditions are 710259SAndrew.Bardsley@arm.com * met: redistributions of source code must retain the above copyright 810259SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer; 910259SAndrew.Bardsley@arm.com * redistributions in binary form must reproduce the above copyright 1010259SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer in the 1110259SAndrew.Bardsley@arm.com * documentation and/or other materials provided with the distribution; 1210259SAndrew.Bardsley@arm.com * neither the name of the copyright holders nor the names of its 1310259SAndrew.Bardsley@arm.com * contributors may be used to endorse or promote products derived from 1410259SAndrew.Bardsley@arm.com * this software without specific prior written permission. 1510259SAndrew.Bardsley@arm.com * 1610259SAndrew.Bardsley@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1710259SAndrew.Bardsley@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1810259SAndrew.Bardsley@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1910259SAndrew.Bardsley@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2010259SAndrew.Bardsley@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2110259SAndrew.Bardsley@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2210259SAndrew.Bardsley@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2310259SAndrew.Bardsley@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2410259SAndrew.Bardsley@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2510259SAndrew.Bardsley@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2610259SAndrew.Bardsley@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2710259SAndrew.Bardsley@arm.com */ 2810259SAndrew.Bardsley@arm.com 2910259SAndrew.Bardsley@arm.com/* @file 3010259SAndrew.Bardsley@arm.com * Serialization Interface Declarations 3110259SAndrew.Bardsley@arm.com */ 3210259SAndrew.Bardsley@arm.com 3310259SAndrew.Bardsley@arm.com#ifndef __SERIALIZE_HH__ 3410259SAndrew.Bardsley@arm.com#define __SERIALIZE_HH__ 3510259SAndrew.Bardsley@arm.com 3610259SAndrew.Bardsley@arm.com 3710259SAndrew.Bardsley@arm.com#include <list> 3810259SAndrew.Bardsley@arm.com#include <iostream> 3910259SAndrew.Bardsley@arm.com#include <map> 4010259SAndrew.Bardsley@arm.com 4110259SAndrew.Bardsley@arm.com#include "sim/host.hh" 4210259SAndrew.Bardsley@arm.com#include "sim/configfile.hh" 4310259SAndrew.Bardsley@arm.com 4410259SAndrew.Bardsley@arm.comclass Serializable; 4510259SAndrew.Bardsley@arm.comclass Checkpoint; 4610259SAndrew.Bardsley@arm.com 4710259SAndrew.Bardsley@arm.comtemplate <class T> 4810259SAndrew.Bardsley@arm.comvoid paramOut(std::ostream &os, const std::string &name, const T ¶m); 4910259SAndrew.Bardsley@arm.com 5010259SAndrew.Bardsley@arm.comtemplate <class T> 5110259SAndrew.Bardsley@arm.comvoid paramIn(Checkpoint *cp, const std::string §ion, 5210259SAndrew.Bardsley@arm.com const std::string &name, T ¶m); 5310259SAndrew.Bardsley@arm.com 5410259SAndrew.Bardsley@arm.comtemplate <class T> 5510259SAndrew.Bardsley@arm.comvoid arrayParamOut(std::ostream &os, const std::string &name, 5610259SAndrew.Bardsley@arm.com const T *param, int size); 5710259SAndrew.Bardsley@arm.com 5810259SAndrew.Bardsley@arm.comtemplate <class T> 5910259SAndrew.Bardsley@arm.comvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 6010259SAndrew.Bardsley@arm.com const std::string &name, T *param, int size); 6110259SAndrew.Bardsley@arm.com 6210259SAndrew.Bardsley@arm.comvoid 6310259SAndrew.Bardsley@arm.comobjParamIn(Checkpoint *cp, const std::string §ion, 6410259SAndrew.Bardsley@arm.com const std::string &name, Serializable * ¶m); 6510259SAndrew.Bardsley@arm.com 6610259SAndrew.Bardsley@arm.com 6710259SAndrew.Bardsley@arm.com// 6810259SAndrew.Bardsley@arm.com// These macros are streamlined to use in serialize/unserialize 6910259SAndrew.Bardsley@arm.com// functions. It's assumed that serialize() has a parameter 'os' for 7010259SAndrew.Bardsley@arm.com// the ostream, and unserialize() has parameters 'cp' and 'section'. 7110259SAndrew.Bardsley@arm.com#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 7210259SAndrew.Bardsley@arm.com 7310259SAndrew.Bardsley@arm.com#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 7410259SAndrew.Bardsley@arm.com 7510259SAndrew.Bardsley@arm.com// ENUMs are like SCALARs, but we cast them to ints on the way out 7610259SAndrew.Bardsley@arm.com#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 7710259SAndrew.Bardsley@arm.com 7810259SAndrew.Bardsley@arm.com#define UNSERIALIZE_ENUM(scalar) \ 7910259SAndrew.Bardsley@arm.com do { \ 8010259SAndrew.Bardsley@arm.com int tmp; \ 8110259SAndrew.Bardsley@arm.com paramIn(cp, section, #scalar, tmp); \ 8210259SAndrew.Bardsley@arm.com scalar = (typeof(scalar))tmp; \ 8310259SAndrew.Bardsley@arm.com } while (0) 8410259SAndrew.Bardsley@arm.com 8510259SAndrew.Bardsley@arm.com#define SERIALIZE_ARRAY(member, size) \ 8610259SAndrew.Bardsley@arm.com arrayParamOut(os, #member, member, size) 8710259SAndrew.Bardsley@arm.com 8810259SAndrew.Bardsley@arm.com#define UNSERIALIZE_ARRAY(member, size) \ 8910259SAndrew.Bardsley@arm.com arrayParamIn(cp, section, #member, member, size) 9010259SAndrew.Bardsley@arm.com 9110259SAndrew.Bardsley@arm.com#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 9210259SAndrew.Bardsley@arm.com 9310259SAndrew.Bardsley@arm.com#define UNSERIALIZE_OBJPTR(objptr) \ 9410259SAndrew.Bardsley@arm.com do { \ 9510259SAndrew.Bardsley@arm.com Serializable *sptr; \ 9610259SAndrew.Bardsley@arm.com objParamIn(cp, section, #objptr, sptr); \ 9710259SAndrew.Bardsley@arm.com objptr = dynamic_cast<typeof(objptr)>(sptr); \ 9810259SAndrew.Bardsley@arm.com } while (0) 9910259SAndrew.Bardsley@arm.com 10010259SAndrew.Bardsley@arm.com/* 10110259SAndrew.Bardsley@arm.com * Basic support for object serialization. 10210259SAndrew.Bardsley@arm.com */ 10310259SAndrew.Bardsley@arm.comclass Serializable 10410259SAndrew.Bardsley@arm.com{ 10510259SAndrew.Bardsley@arm.com protected: 10610259SAndrew.Bardsley@arm.com void nameOut(std::ostream &os); 10710259SAndrew.Bardsley@arm.com void nameOut(std::ostream &os, const std::string &_name); 10810259SAndrew.Bardsley@arm.com 10910259SAndrew.Bardsley@arm.com public: 11010259SAndrew.Bardsley@arm.com Serializable() {} 11110259SAndrew.Bardsley@arm.com virtual ~Serializable() {} 11210259SAndrew.Bardsley@arm.com 11310259SAndrew.Bardsley@arm.com // manditory virtual function, so objects must provide names 11410259SAndrew.Bardsley@arm.com virtual const std::string name() const = 0; 11510259SAndrew.Bardsley@arm.com 11610259SAndrew.Bardsley@arm.com virtual void serialize(std::ostream &os) {} 11710259SAndrew.Bardsley@arm.com virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 11810259SAndrew.Bardsley@arm.com 11910259SAndrew.Bardsley@arm.com static Serializable *create(Checkpoint *cp, 12010259SAndrew.Bardsley@arm.com const std::string §ion); 12110259SAndrew.Bardsley@arm.com 12210259SAndrew.Bardsley@arm.com static int ckptCount; 12310259SAndrew.Bardsley@arm.com static int ckptMaxCount; 12410259SAndrew.Bardsley@arm.com static int ckptPrevCount; 12510259SAndrew.Bardsley@arm.com static void serializeAll(); 12610259SAndrew.Bardsley@arm.com static void unserializeGlobals(Checkpoint *cp); 12710259SAndrew.Bardsley@arm.com}; 12810259SAndrew.Bardsley@arm.com 12910259SAndrew.Bardsley@arm.com// 13010259SAndrew.Bardsley@arm.com// A SerializableBuilder serves as an evaluation context for a set of 13110259SAndrew.Bardsley@arm.com// parameters that describe a specific instance of a Serializable. This 13210259SAndrew.Bardsley@arm.com// evaluation context corresponds to a section in the .ini file (as 13310259SAndrew.Bardsley@arm.com// with the base ParamContext) plus an optional node in the 13410259SAndrew.Bardsley@arm.com// configuration hierarchy (the configNode member) for resolving 13510259SAndrew.Bardsley@arm.com// Serializable references. SerializableBuilder is an abstract superclass; 13610259SAndrew.Bardsley@arm.com// derived classes specialize the class for particular subclasses of 13710259SAndrew.Bardsley@arm.com// Serializable (e.g., BaseCache). 13810259SAndrew.Bardsley@arm.com// 13910259SAndrew.Bardsley@arm.com// For typical usage, see the definition of 14010259SAndrew.Bardsley@arm.com// SerializableClass::createObject(). 14110259SAndrew.Bardsley@arm.com// 14210259SAndrew.Bardsley@arm.comclass SerializableBuilder 14310259SAndrew.Bardsley@arm.com{ 14410259SAndrew.Bardsley@arm.com public: 14510259SAndrew.Bardsley@arm.com 14610259SAndrew.Bardsley@arm.com SerializableBuilder() {} 14710259SAndrew.Bardsley@arm.com 14810259SAndrew.Bardsley@arm.com virtual ~SerializableBuilder() {} 14910259SAndrew.Bardsley@arm.com 15010259SAndrew.Bardsley@arm.com // Create the actual Serializable corresponding to the parameter 15110259SAndrew.Bardsley@arm.com // values in this context. This function is overridden in derived 15210259SAndrew.Bardsley@arm.com // classes to call a specific constructor for a particular 15310259SAndrew.Bardsley@arm.com // subclass of Serializable. 15410259SAndrew.Bardsley@arm.com virtual Serializable *create() = 0; 15510259SAndrew.Bardsley@arm.com}; 15610259SAndrew.Bardsley@arm.com 15710259SAndrew.Bardsley@arm.com// 15810259SAndrew.Bardsley@arm.com// An instance of SerializableClass corresponds to a class derived from 15910259SAndrew.Bardsley@arm.com// Serializable. The SerializableClass instance serves to bind the string 16010259SAndrew.Bardsley@arm.com// name (found in the config file) to a function that creates an 16110259SAndrew.Bardsley@arm.com// instance of the appropriate derived class. 16210259SAndrew.Bardsley@arm.com// 16310259SAndrew.Bardsley@arm.com// This would be much cleaner in Smalltalk or Objective-C, where types 16410259SAndrew.Bardsley@arm.com// are first-class objects themselves. 16510259SAndrew.Bardsley@arm.com// 16610259SAndrew.Bardsley@arm.comclass SerializableClass 16710259SAndrew.Bardsley@arm.com{ 16810259SAndrew.Bardsley@arm.com public: 16910259SAndrew.Bardsley@arm.com 17010259SAndrew.Bardsley@arm.com // Type CreateFunc is a pointer to a function that creates a new 17110259SAndrew.Bardsley@arm.com // simulation object builder based on a .ini-file parameter 17210259SAndrew.Bardsley@arm.com // section (specified by the first string argument), a unique name 17310259SAndrew.Bardsley@arm.com // for the object (specified by the second string argument), and 17410259SAndrew.Bardsley@arm.com // an optional config hierarchy node (specified by the third 17510259SAndrew.Bardsley@arm.com // argument). A pointer to the new SerializableBuilder is returned. 17610259SAndrew.Bardsley@arm.com typedef Serializable *(*CreateFunc)(Checkpoint *cp, 17710259SAndrew.Bardsley@arm.com const std::string §ion); 17810259SAndrew.Bardsley@arm.com 17910259SAndrew.Bardsley@arm.com static std::map<std::string,CreateFunc> *classMap; 18010259SAndrew.Bardsley@arm.com 18110259SAndrew.Bardsley@arm.com // Constructor. For example: 18210259SAndrew.Bardsley@arm.com // 18310259SAndrew.Bardsley@arm.com // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 18410259SAndrew.Bardsley@arm.com // newBaseCacheSerializableBuilder); 18510259SAndrew.Bardsley@arm.com // 18610259SAndrew.Bardsley@arm.com SerializableClass(const std::string &className, CreateFunc createFunc); 18710259SAndrew.Bardsley@arm.com 18810259SAndrew.Bardsley@arm.com // create Serializable given name of class and pointer to 18910259SAndrew.Bardsley@arm.com // configuration hierarchy node 19010259SAndrew.Bardsley@arm.com static Serializable *createObject(Checkpoint *cp, 19110259SAndrew.Bardsley@arm.com const std::string §ion); 19210259SAndrew.Bardsley@arm.com}; 19310259SAndrew.Bardsley@arm.com 19410259SAndrew.Bardsley@arm.com// 19510913Sandreas.sandberg@arm.com// Macros to encapsulate the magic of declaring & defining 19610913Sandreas.sandberg@arm.com// SerializableBuilder and SerializableClass objects 19710259SAndrew.Bardsley@arm.com// 19810259SAndrew.Bardsley@arm.com 19910259SAndrew.Bardsley@arm.com#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 20010259SAndrew.Bardsley@arm.comSerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 20110259SAndrew.Bardsley@arm.com OBJ_CLASS::createForUnserialize); 20210259SAndrew.Bardsley@arm.com 20310259SAndrew.Bardsley@arm.comclass Checkpoint 20410259SAndrew.Bardsley@arm.com{ 20510259SAndrew.Bardsley@arm.com private: 20610259SAndrew.Bardsley@arm.com 20710259SAndrew.Bardsley@arm.com IniFile *db; 20810913Sandreas.sandberg@arm.com const std::string basePath; 20910259SAndrew.Bardsley@arm.com const ConfigNode *configNode; 21010259SAndrew.Bardsley@arm.com std::map<std::string, Serializable*> objMap; 21110259SAndrew.Bardsley@arm.com 21210259SAndrew.Bardsley@arm.com public: 21310259SAndrew.Bardsley@arm.com Checkpoint(const std::string &cpt_dir, const std::string &path, 21410259SAndrew.Bardsley@arm.com const ConfigNode *_configNode); 21510259SAndrew.Bardsley@arm.com 21610259SAndrew.Bardsley@arm.com const std::string cptDir; 21710259SAndrew.Bardsley@arm.com 21810259SAndrew.Bardsley@arm.com bool find(const std::string §ion, const std::string &entry, 21910259SAndrew.Bardsley@arm.com std::string &value); 22010259SAndrew.Bardsley@arm.com 22110259SAndrew.Bardsley@arm.com bool findObj(const std::string §ion, const std::string &entry, 22210259SAndrew.Bardsley@arm.com Serializable *&value); 22310259SAndrew.Bardsley@arm.com 22410259SAndrew.Bardsley@arm.com bool sectionExists(const std::string §ion); 22510259SAndrew.Bardsley@arm.com 22610259SAndrew.Bardsley@arm.com // The following static functions have to do with checkpoint 22710259SAndrew.Bardsley@arm.com // creation rather than restoration. This class makes a handy 22810259SAndrew.Bardsley@arm.com // namespace for them though. 22910259SAndrew.Bardsley@arm.com 23010259SAndrew.Bardsley@arm.com // Export current checkpoint directory name so other objects can 23110259SAndrew.Bardsley@arm.com // derive filenames from it (e.g., memory). The return value is 23210259SAndrew.Bardsley@arm.com // guaranteed to end in '/' so filenames can be directly appended. 23310259SAndrew.Bardsley@arm.com // This function is only valid while a checkpoint is being created. 23410259SAndrew.Bardsley@arm.com static std::string dir(); 23510259SAndrew.Bardsley@arm.com 23610259SAndrew.Bardsley@arm.com // Filename for base checkpoint file within directory. 23710259SAndrew.Bardsley@arm.com static const char *baseFilename; 23810259SAndrew.Bardsley@arm.com 23910259SAndrew.Bardsley@arm.com // Set up a checkpoint creation event or series of events. 24010259SAndrew.Bardsley@arm.com static void setup(Tick when, Tick period = 0); 24110259SAndrew.Bardsley@arm.com}; 24210259SAndrew.Bardsley@arm.com 24310259SAndrew.Bardsley@arm.com#endif // __SERIALIZE_HH__ 24410259SAndrew.Bardsley@arm.com