serialize.hh revision 7491
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 &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>
73237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string &section,
746227Snate@binkert.org                  const std::string &name, T *param, unsigned size);
75217SN/A
764841Ssaidi@eecs.umich.edutemplate <class T>
774841Ssaidi@eecs.umich.eduvoid arrayParamIn(Checkpoint *cp, const std::string &section,
784841Ssaidi@eecs.umich.edu                  const std::string &name, std::vector<T> &param);
794841Ssaidi@eecs.umich.edu
80237SN/Avoid
81237SN/AobjParamIn(Checkpoint *cp, const std::string &section,
824000Ssaidi@eecs.umich.edu           const std::string &name, SimObject * &param);
83237SN/A
84237SN/A
85217SN/A//
86217SN/A// These macros are streamlined to use in serialize/unserialize
87217SN/A// functions.  It's assumed that serialize() has a parameter 'os' for
88237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'.
895543Ssaidi@eecs.umich.edu#define SERIALIZE_SCALAR(scalar)        paramOut(os, #scalar, scalar)
90217SN/A
915543Ssaidi@eecs.umich.edu#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, section, #scalar, scalar)
926820SLisa.Hsu@amd.com#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, section, #scalar, scalar)
93217SN/A
94223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out
955543Ssaidi@eecs.umich.edu#define SERIALIZE_ENUM(scalar)          paramOut(os, #scalar, (int)scalar)
96223SN/A
975543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar)                \
985543Ssaidi@eecs.umich.edu do {                                           \
995543Ssaidi@eecs.umich.edu    int tmp;                                    \
1005543Ssaidi@eecs.umich.edu    paramIn(cp, section, #scalar, tmp);         \
1015543Ssaidi@eecs.umich.edu    scalar = (typeof(scalar))tmp;               \
102223SN/A  } while (0)
103223SN/A
1045543Ssaidi@eecs.umich.edu#define SERIALIZE_ARRAY(member, size)           \
105217SN/A        arrayParamOut(os, #member, member, size)
106217SN/A
1075543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size)         \
108237SN/A        arrayParamIn(cp, section, #member, member, size)
109237SN/A
1105543Ssaidi@eecs.umich.edu#define SERIALIZE_OBJPTR(objptr)        paramOut(os, #objptr, (objptr)->name())
111237SN/A
1125543Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr)                      \
1135543Ssaidi@eecs.umich.edu  do {                                                  \
1145543Ssaidi@eecs.umich.edu    SimObject *sptr;                                    \
1155543Ssaidi@eecs.umich.edu    objParamIn(cp, section, #objptr, sptr);             \
1165543Ssaidi@eecs.umich.edu    objptr = dynamic_cast<typeof(objptr)>(sptr);        \
117237SN/A  } while (0)
118217SN/A
1192SN/A/*
1202SN/A * Basic support for object serialization.
1212SN/A */
122395SN/Aclass Serializable
1232SN/A{
1242SN/A  protected:
125510SN/A    void nameOut(std::ostream &os);
126510SN/A    void nameOut(std::ostream &os, const std::string &_name);
1272SN/A
1282SN/A  public:
1295739Snate@binkert.org    Serializable();
1305739Snate@binkert.org    virtual ~Serializable();
1312SN/A
132265SN/A    // manditory virtual function, so objects must provide names
133512SN/A    virtual const std::string name() const = 0;
1342SN/A
1355739Snate@binkert.org    virtual void serialize(std::ostream &os);
1365739Snate@binkert.org    virtual void unserialize(Checkpoint *cp, const std::string &section);
137237SN/A
1385739Snate@binkert.org    static Serializable *create(Checkpoint *cp, const std::string &section);
1392SN/A
1402287SN/A    static int ckptCount;
1412287SN/A    static int ckptMaxCount;
1422287SN/A    static int ckptPrevCount;
1432868Sktlim@umich.edu    static void serializeAll(const std::string &cpt_dir);
1442868Sktlim@umich.edu    static void unserializeAll(const std::string &cpt_dir);
145395SN/A    static void unserializeGlobals(Checkpoint *cp);
1462SN/A};
1472SN/A
1482SN/A//
149395SN/A// A SerializableBuilder serves as an evaluation context for a set of
150395SN/A// parameters that describe a specific instance of a Serializable.  This
1512SN/A// evaluation context corresponds to a section in the .ini file (as
1522SN/A// with the base ParamContext) plus an optional node in the
1532SN/A// configuration hierarchy (the configNode member) for resolving
154395SN/A// Serializable references.  SerializableBuilder is an abstract superclass;
1552SN/A// derived classes specialize the class for particular subclasses of
156395SN/A// Serializable (e.g., BaseCache).
1572SN/A//
1582SN/A// For typical usage, see the definition of
159395SN/A// SerializableClass::createObject().
1602SN/A//
161395SN/Aclass SerializableBuilder
1622SN/A{
1632SN/A  public:
1642SN/A
165395SN/A    SerializableBuilder() {}
1662SN/A
167395SN/A    virtual ~SerializableBuilder() {}
1682SN/A
169395SN/A    // Create the actual Serializable corresponding to the parameter
1702SN/A    // values in this context.  This function is overridden in derived
1712SN/A    // classes to call a specific constructor for a particular
172395SN/A    // subclass of Serializable.
173395SN/A    virtual Serializable *create() = 0;
1742SN/A};
1752SN/A
1762SN/A//
177395SN/A// An instance of SerializableClass corresponds to a class derived from
178395SN/A// Serializable.  The SerializableClass instance serves to bind the string
1792SN/A// name (found in the config file) to a function that creates an
1802SN/A// instance of the appropriate derived class.
1812SN/A//
1822SN/A// This would be much cleaner in Smalltalk or Objective-C, where types
1832SN/A// are first-class objects themselves.
1842SN/A//
185395SN/Aclass SerializableClass
1862SN/A{
1872SN/A  public:
1882SN/A
1892SN/A    // Type CreateFunc is a pointer to a function that creates a new
1902SN/A    // simulation object builder based on a .ini-file parameter
1912SN/A    // section (specified by the first string argument), a unique name
1922SN/A    // for the object (specified by the second string argument), and
1932SN/A    // an optional config hierarchy node (specified by the third
194395SN/A    // argument).  A pointer to the new SerializableBuilder is returned.
195395SN/A    typedef Serializable *(*CreateFunc)(Checkpoint *cp,
1962738Sstever@eecs.umich.edu                                        const std::string &section);
1972SN/A
1982SN/A    static std::map<std::string,CreateFunc> *classMap;
1992SN/A
2002SN/A    // Constructor.  For example:
2012SN/A    //
202395SN/A    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
203395SN/A    //                         newBaseCacheSerializableBuilder);
2042SN/A    //
205395SN/A    SerializableClass(const std::string &className, CreateFunc createFunc);
2062SN/A
207395SN/A    // create Serializable given name of class and pointer to
2082SN/A    // configuration hierarchy node
209395SN/A    static Serializable *createObject(Checkpoint *cp,
2102738Sstever@eecs.umich.edu                                      const std::string &section);
2112SN/A};
2122SN/A
2132SN/A//
2142SN/A// Macros to encapsulate the magic of declaring & defining
215395SN/A// SerializableBuilder and SerializableClass objects
2162SN/A//
2172SN/A
2185543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
2195543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
220237SN/A                                         OBJ_CLASS::createForUnserialize);
2212SN/A
222237SN/Aclass Checkpoint
223237SN/A{
224237SN/A  private:
225237SN/A
226237SN/A    IniFile *db;
227237SN/A
228237SN/A  public:
2297491Ssteve.reinhardt@amd.com    Checkpoint(const std::string &cpt_dir);
230237SN/A
231937SN/A    const std::string cptDir;
232937SN/A
233237SN/A    bool find(const std::string &section, const std::string &entry,
234237SN/A              std::string &value);
235237SN/A
236237SN/A    bool findObj(const std::string &section, const std::string &entry,
2374000Ssaidi@eecs.umich.edu                 SimObject *&value);
238304SN/A
239304SN/A    bool sectionExists(const std::string &section);
240449SN/A
241449SN/A    // The following static functions have to do with checkpoint
242449SN/A    // creation rather than restoration.  This class makes a handy
2437491Ssteve.reinhardt@amd.com    // namespace for them though.  Currently no Checkpoint object is
2447491Ssteve.reinhardt@amd.com    // created on serialization (only unserialization) so we track the
2457491Ssteve.reinhardt@amd.com    // directory name as a global.  It would be nice to change this
2467491Ssteve.reinhardt@amd.com    // someday
2477491Ssteve.reinhardt@amd.com
2487491Ssteve.reinhardt@amd.com  private:
2497491Ssteve.reinhardt@amd.com    // current directory we're serializing into.
2507491Ssteve.reinhardt@amd.com    static std::string currentDirectory;
2517491Ssteve.reinhardt@amd.com
2527491Ssteve.reinhardt@amd.com  public:
2537491Ssteve.reinhardt@amd.com    // Set the current directory.  This function takes care of
2547491Ssteve.reinhardt@amd.com    // inserting curTick if there's a '%d' in the argument, and
2557491Ssteve.reinhardt@amd.com    // appends a '/' if necessary.  The final name is returned.
2567491Ssteve.reinhardt@amd.com    static std::string setDir(const std::string &base_name);
257449SN/A
258449SN/A    // Export current checkpoint directory name so other objects can
259449SN/A    // derive filenames from it (e.g., memory).  The return value is
260449SN/A    // guaranteed to end in '/' so filenames can be directly appended.
261449SN/A    // This function is only valid while a checkpoint is being created.
262449SN/A    static std::string dir();
263449SN/A
264449SN/A    // Filename for base checkpoint file within directory.
265449SN/A    static const char *baseFilename;
266237SN/A};
2672SN/A
2682SN/A#endif // __SERIALIZE_HH__
269