serialize.hh revision 8902
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
53217SN/Atemplate <class T>
54502SN/Avoid paramOut(std::ostream &os, const std::string &name, const T &param);
55217SN/A
56217SN/Atemplate <class T>
57237SN/Avoid paramIn(Checkpoint *cp, const std::string &section,
58502SN/A             const std::string &name, T &param);
59217SN/A
60217SN/Atemplate <class T>
616820SLisa.Hsu@amd.combool optParamIn(Checkpoint *cp, const std::string &section,
626820SLisa.Hsu@amd.com             const std::string &name, T &param);
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> &param);
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> &param);
757948SAli.Saidi@ARM.com
767948SAli.Saidi@ARM.comtemplate <class T>
77237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string &section,
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 &section,
824841Ssaidi@eecs.umich.edu                  const std::string &name, std::vector<T> &param);
834841Ssaidi@eecs.umich.edu
847948SAli.Saidi@ARM.comtemplate <class T>
857948SAli.Saidi@ARM.comvoid arrayParamIn(Checkpoint *cp, const std::string &section,
867948SAli.Saidi@ARM.com                  const std::string &name, std::list<T> &param);
877948SAli.Saidi@ARM.com
88237SN/Avoid
89237SN/AobjParamIn(Checkpoint *cp, const std::string &section,
904000Ssaidi@eecs.umich.edu           const std::string &name, SimObject * &param);
91237SN/A
928902Sandreas.hansson@arm.comtemplate <typename T>
938902Sandreas.hansson@arm.comvoid fromInt(T &t, int i)
948902Sandreas.hansson@arm.com{
958902Sandreas.hansson@arm.com    t = (T)i;
968902Sandreas.hansson@arm.com}
978902Sandreas.hansson@arm.com
988902Sandreas.hansson@arm.comtemplate <typename T>
998902Sandreas.hansson@arm.comvoid fromSimObject(T &t, SimObject *s)
1008902Sandreas.hansson@arm.com{
1018902Sandreas.hansson@arm.com    t = dynamic_cast<T>(s);
1028902Sandreas.hansson@arm.com}
103237SN/A
104217SN/A//
105217SN/A// These macros are streamlined to use in serialize/unserialize
106217SN/A// functions.  It's assumed that serialize() has a parameter 'os' for
107237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'.
1085543Ssaidi@eecs.umich.edu#define SERIALIZE_SCALAR(scalar)        paramOut(os, #scalar, scalar)
109217SN/A
1105543Ssaidi@eecs.umich.edu#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, section, #scalar, scalar)
1116820SLisa.Hsu@amd.com#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, section, #scalar, scalar)
112217SN/A
113223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out
1145543Ssaidi@eecs.umich.edu#define SERIALIZE_ENUM(scalar)          paramOut(os, #scalar, (int)scalar)
115223SN/A
1165543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar)                \
1175543Ssaidi@eecs.umich.edu do {                                           \
1185543Ssaidi@eecs.umich.edu    int tmp;                                    \
1195543Ssaidi@eecs.umich.edu    paramIn(cp, section, #scalar, tmp);         \
1208902Sandreas.hansson@arm.com    fromInt(scalar, tmp);                    \
121223SN/A  } while (0)
122223SN/A
1235543Ssaidi@eecs.umich.edu#define SERIALIZE_ARRAY(member, size)           \
124217SN/A        arrayParamOut(os, #member, member, size)
125217SN/A
1265543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size)         \
127237SN/A        arrayParamIn(cp, section, #member, member, size)
128237SN/A
1295543Ssaidi@eecs.umich.edu#define SERIALIZE_OBJPTR(objptr)        paramOut(os, #objptr, (objptr)->name())
130237SN/A
1315543Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr)                      \
1325543Ssaidi@eecs.umich.edu  do {                                                  \
1335543Ssaidi@eecs.umich.edu    SimObject *sptr;                                    \
1345543Ssaidi@eecs.umich.edu    objParamIn(cp, section, #objptr, sptr);             \
1358902Sandreas.hansson@arm.com    fromSimObject(objptr, sptr);                        \
136237SN/A  } while (0)
137217SN/A
1382SN/A/*
1392SN/A * Basic support for object serialization.
1402SN/A */
141395SN/Aclass Serializable
1422SN/A{
1432SN/A  protected:
144510SN/A    void nameOut(std::ostream &os);
145510SN/A    void nameOut(std::ostream &os, const std::string &_name);
1462SN/A
1472SN/A  public:
1485739Snate@binkert.org    Serializable();
1495739Snate@binkert.org    virtual ~Serializable();
1502SN/A
151265SN/A    // manditory virtual function, so objects must provide names
152512SN/A    virtual const std::string name() const = 0;
1532SN/A
1545739Snate@binkert.org    virtual void serialize(std::ostream &os);
1555739Snate@binkert.org    virtual void unserialize(Checkpoint *cp, const std::string &section);
156237SN/A
1575739Snate@binkert.org    static Serializable *create(Checkpoint *cp, const std::string &section);
1582SN/A
1592287SN/A    static int ckptCount;
1602287SN/A    static int ckptMaxCount;
1612287SN/A    static int ckptPrevCount;
1622868Sktlim@umich.edu    static void serializeAll(const std::string &cpt_dir);
163395SN/A    static void unserializeGlobals(Checkpoint *cp);
1642SN/A};
1652SN/A
1662SN/A//
167395SN/A// A SerializableBuilder serves as an evaluation context for a set of
168395SN/A// parameters that describe a specific instance of a Serializable.  This
1692SN/A// evaluation context corresponds to a section in the .ini file (as
1702SN/A// with the base ParamContext) plus an optional node in the
1712SN/A// configuration hierarchy (the configNode member) for resolving
172395SN/A// Serializable references.  SerializableBuilder is an abstract superclass;
1732SN/A// derived classes specialize the class for particular subclasses of
174395SN/A// Serializable (e.g., BaseCache).
1752SN/A//
1762SN/A// For typical usage, see the definition of
177395SN/A// SerializableClass::createObject().
1782SN/A//
179395SN/Aclass SerializableBuilder
1802SN/A{
1812SN/A  public:
1822SN/A
183395SN/A    SerializableBuilder() {}
1842SN/A
185395SN/A    virtual ~SerializableBuilder() {}
1862SN/A
187395SN/A    // Create the actual Serializable corresponding to the parameter
1882SN/A    // values in this context.  This function is overridden in derived
1892SN/A    // classes to call a specific constructor for a particular
190395SN/A    // subclass of Serializable.
191395SN/A    virtual Serializable *create() = 0;
1922SN/A};
1932SN/A
1942SN/A//
195395SN/A// An instance of SerializableClass corresponds to a class derived from
196395SN/A// Serializable.  The SerializableClass instance serves to bind the string
1972SN/A// name (found in the config file) to a function that creates an
1982SN/A// instance of the appropriate derived class.
1992SN/A//
2002SN/A// This would be much cleaner in Smalltalk or Objective-C, where types
2012SN/A// are first-class objects themselves.
2022SN/A//
203395SN/Aclass SerializableClass
2042SN/A{
2052SN/A  public:
2062SN/A
2072SN/A    // Type CreateFunc is a pointer to a function that creates a new
2082SN/A    // simulation object builder based on a .ini-file parameter
2092SN/A    // section (specified by the first string argument), a unique name
2102SN/A    // for the object (specified by the second string argument), and
2112SN/A    // an optional config hierarchy node (specified by the third
212395SN/A    // argument).  A pointer to the new SerializableBuilder is returned.
213395SN/A    typedef Serializable *(*CreateFunc)(Checkpoint *cp,
2142738Sstever@eecs.umich.edu                                        const std::string &section);
2152SN/A
2162SN/A    static std::map<std::string,CreateFunc> *classMap;
2172SN/A
2182SN/A    // Constructor.  For example:
2192SN/A    //
220395SN/A    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
221395SN/A    //                         newBaseCacheSerializableBuilder);
2222SN/A    //
223395SN/A    SerializableClass(const std::string &className, CreateFunc createFunc);
2242SN/A
225395SN/A    // create Serializable given name of class and pointer to
2262SN/A    // configuration hierarchy node
227395SN/A    static Serializable *createObject(Checkpoint *cp,
2282738Sstever@eecs.umich.edu                                      const std::string &section);
2292SN/A};
2302SN/A
2312SN/A//
2322SN/A// Macros to encapsulate the magic of declaring & defining
233395SN/A// SerializableBuilder and SerializableClass objects
2342SN/A//
2352SN/A
2365543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
2375543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
238237SN/A                                         OBJ_CLASS::createForUnserialize);
2392SN/A
240237SN/Aclass Checkpoint
241237SN/A{
242237SN/A  private:
243237SN/A
244237SN/A    IniFile *db;
245237SN/A
246237SN/A  public:
2477491Ssteve.reinhardt@amd.com    Checkpoint(const std::string &cpt_dir);
248237SN/A
249937SN/A    const std::string cptDir;
250937SN/A
251237SN/A    bool find(const std::string &section, const std::string &entry,
252237SN/A              std::string &value);
253237SN/A
254237SN/A    bool findObj(const std::string &section, const std::string &entry,
2554000Ssaidi@eecs.umich.edu                 SimObject *&value);
256304SN/A
257304SN/A    bool sectionExists(const std::string &section);
258449SN/A
259449SN/A    // The following static functions have to do with checkpoint
260449SN/A    // creation rather than restoration.  This class makes a handy
2617491Ssteve.reinhardt@amd.com    // namespace for them though.  Currently no Checkpoint object is
2627491Ssteve.reinhardt@amd.com    // created on serialization (only unserialization) so we track the
2637491Ssteve.reinhardt@amd.com    // directory name as a global.  It would be nice to change this
2647491Ssteve.reinhardt@amd.com    // someday
2657491Ssteve.reinhardt@amd.com
2667491Ssteve.reinhardt@amd.com  private:
2677491Ssteve.reinhardt@amd.com    // current directory we're serializing into.
2687491Ssteve.reinhardt@amd.com    static std::string currentDirectory;
2697491Ssteve.reinhardt@amd.com
2707491Ssteve.reinhardt@amd.com  public:
2717491Ssteve.reinhardt@amd.com    // Set the current directory.  This function takes care of
2727823Ssteve.reinhardt@amd.com    // inserting curTick() if there's a '%d' in the argument, and
2737491Ssteve.reinhardt@amd.com    // appends a '/' if necessary.  The final name is returned.
2747491Ssteve.reinhardt@amd.com    static std::string setDir(const std::string &base_name);
275449SN/A
276449SN/A    // Export current checkpoint directory name so other objects can
277449SN/A    // derive filenames from it (e.g., memory).  The return value is
278449SN/A    // guaranteed to end in '/' so filenames can be directly appended.
279449SN/A    // This function is only valid while a checkpoint is being created.
280449SN/A    static std::string dir();
281449SN/A
282449SN/A    // Filename for base checkpoint file within directory.
283449SN/A    static const char *baseFilename;
284237SN/A};
2852SN/A
2862SN/A#endif // __SERIALIZE_HH__
287