serialize.hh revision 9342
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
559056SAli.Saidi@ARM.com * version is defined as a checkpoint created before this version won't 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
589056SAli.Saidi@ARM.com * existing objects such as serializing/unserializing more state, changing sizes
599048SAli.Saidi@ARM.com * of serialized arrays, etc. */
609332Sdam.sunwoo@arm.comstatic const uint64_t gem5CheckpointVersion = 0x0000000000000003;
619048SAli.Saidi@ARM.com
62217SN/Atemplate <class T>
63502SN/Avoid paramOut(std::ostream &os, const std::string &name, const T &param);
64217SN/A
65217SN/Atemplate <class T>
66237SN/Avoid paramIn(Checkpoint *cp, const std::string &section,
67502SN/A             const std::string &name, T &param);
68217SN/A
69217SN/Atemplate <class T>
706820SLisa.Hsu@amd.combool optParamIn(Checkpoint *cp, const std::string &section,
716820SLisa.Hsu@amd.com             const std::string &name, T &param);
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> &param);
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> &param);
847948SAli.Saidi@ARM.com
857948SAli.Saidi@ARM.comtemplate <class T>
86237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string &section,
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 &section,
914841Ssaidi@eecs.umich.edu                  const std::string &name, std::vector<T> &param);
924841Ssaidi@eecs.umich.edu
937948SAli.Saidi@ARM.comtemplate <class T>
947948SAli.Saidi@ARM.comvoid arrayParamIn(Checkpoint *cp, const std::string &section,
957948SAli.Saidi@ARM.com                  const std::string &name, std::list<T> &param);
967948SAli.Saidi@ARM.com
97237SN/Avoid
98237SN/AobjParamIn(Checkpoint *cp, const std::string &section,
994000Ssaidi@eecs.umich.edu           const std::string &name, SimObject * &param);
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
1479342SAndreas.Sandberg@arm.com/**
1482SN/A * Basic support for object serialization.
1499342SAndreas.Sandberg@arm.com *
1509342SAndreas.Sandberg@arm.com * @note Many objects that support serialization need to be put in a
1519342SAndreas.Sandberg@arm.com * consistent state when serialization takes place. We refer to the
1529342SAndreas.Sandberg@arm.com * action of forcing an object into a consistent state as
1539342SAndreas.Sandberg@arm.com * 'draining'. Objects that need draining inherit from Drainable. See
1549342SAndreas.Sandberg@arm.com * Drainable for more information.
1552SN/A */
156395SN/Aclass Serializable
1572SN/A{
1582SN/A  protected:
159510SN/A    void nameOut(std::ostream &os);
160510SN/A    void nameOut(std::ostream &os, const std::string &_name);
1612SN/A
1622SN/A  public:
1635739Snate@binkert.org    Serializable();
1645739Snate@binkert.org    virtual ~Serializable();
1652SN/A
166265SN/A    // manditory virtual function, so objects must provide names
167512SN/A    virtual const std::string name() const = 0;
1682SN/A
1695739Snate@binkert.org    virtual void serialize(std::ostream &os);
1705739Snate@binkert.org    virtual void unserialize(Checkpoint *cp, const std::string &section);
171237SN/A
1725739Snate@binkert.org    static Serializable *create(Checkpoint *cp, const std::string &section);
1732SN/A
1742287SN/A    static int ckptCount;
1752287SN/A    static int ckptMaxCount;
1762287SN/A    static int ckptPrevCount;
1772868Sktlim@umich.edu    static void serializeAll(const std::string &cpt_dir);
178395SN/A    static void unserializeGlobals(Checkpoint *cp);
1792SN/A};
1802SN/A
1812SN/A//
182395SN/A// A SerializableBuilder serves as an evaluation context for a set of
183395SN/A// parameters that describe a specific instance of a Serializable.  This
1842SN/A// evaluation context corresponds to a section in the .ini file (as
1852SN/A// with the base ParamContext) plus an optional node in the
1862SN/A// configuration hierarchy (the configNode member) for resolving
187395SN/A// Serializable references.  SerializableBuilder is an abstract superclass;
1882SN/A// derived classes specialize the class for particular subclasses of
189395SN/A// Serializable (e.g., BaseCache).
1902SN/A//
1912SN/A// For typical usage, see the definition of
192395SN/A// SerializableClass::createObject().
1932SN/A//
194395SN/Aclass SerializableBuilder
1952SN/A{
1962SN/A  public:
1972SN/A
198395SN/A    SerializableBuilder() {}
1992SN/A
200395SN/A    virtual ~SerializableBuilder() {}
2012SN/A
202395SN/A    // Create the actual Serializable corresponding to the parameter
2032SN/A    // values in this context.  This function is overridden in derived
2042SN/A    // classes to call a specific constructor for a particular
205395SN/A    // subclass of Serializable.
206395SN/A    virtual Serializable *create() = 0;
2072SN/A};
2082SN/A
2092SN/A//
210395SN/A// An instance of SerializableClass corresponds to a class derived from
211395SN/A// Serializable.  The SerializableClass instance serves to bind the string
2122SN/A// name (found in the config file) to a function that creates an
2132SN/A// instance of the appropriate derived class.
2142SN/A//
2152SN/A// This would be much cleaner in Smalltalk or Objective-C, where types
2162SN/A// are first-class objects themselves.
2172SN/A//
218395SN/Aclass SerializableClass
2192SN/A{
2202SN/A  public:
2212SN/A
2222SN/A    // Type CreateFunc is a pointer to a function that creates a new
2232SN/A    // simulation object builder based on a .ini-file parameter
2242SN/A    // section (specified by the first string argument), a unique name
2252SN/A    // for the object (specified by the second string argument), and
2262SN/A    // an optional config hierarchy node (specified by the third
227395SN/A    // argument).  A pointer to the new SerializableBuilder is returned.
228395SN/A    typedef Serializable *(*CreateFunc)(Checkpoint *cp,
2292738Sstever@eecs.umich.edu                                        const std::string &section);
2302SN/A
2312SN/A    static std::map<std::string,CreateFunc> *classMap;
2322SN/A
2332SN/A    // Constructor.  For example:
2342SN/A    //
235395SN/A    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
236395SN/A    //                         newBaseCacheSerializableBuilder);
2372SN/A    //
238395SN/A    SerializableClass(const std::string &className, CreateFunc createFunc);
2392SN/A
240395SN/A    // create Serializable given name of class and pointer to
2412SN/A    // configuration hierarchy node
242395SN/A    static Serializable *createObject(Checkpoint *cp,
2432738Sstever@eecs.umich.edu                                      const std::string &section);
2442SN/A};
2452SN/A
2462SN/A//
2472SN/A// Macros to encapsulate the magic of declaring & defining
248395SN/A// SerializableBuilder and SerializableClass objects
2492SN/A//
2502SN/A
2515543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
2525543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
253237SN/A                                         OBJ_CLASS::createForUnserialize);
2542SN/A
255237SN/Aclass Checkpoint
256237SN/A{
257237SN/A  private:
258237SN/A
259237SN/A    IniFile *db;
260237SN/A
261237SN/A  public:
2627491Ssteve.reinhardt@amd.com    Checkpoint(const std::string &cpt_dir);
2639086Sandreas.hansson@arm.com    ~Checkpoint();
264237SN/A
265937SN/A    const std::string cptDir;
266937SN/A
267237SN/A    bool find(const std::string &section, const std::string &entry,
268237SN/A              std::string &value);
269237SN/A
270237SN/A    bool findObj(const std::string &section, const std::string &entry,
2714000Ssaidi@eecs.umich.edu                 SimObject *&value);
272304SN/A
273304SN/A    bool sectionExists(const std::string &section);
274449SN/A
275449SN/A    // The following static functions have to do with checkpoint
276449SN/A    // creation rather than restoration.  This class makes a handy
2777491Ssteve.reinhardt@amd.com    // namespace for them though.  Currently no Checkpoint object is
2787491Ssteve.reinhardt@amd.com    // created on serialization (only unserialization) so we track the
2797491Ssteve.reinhardt@amd.com    // directory name as a global.  It would be nice to change this
2807491Ssteve.reinhardt@amd.com    // someday
2817491Ssteve.reinhardt@amd.com
2827491Ssteve.reinhardt@amd.com  private:
2837491Ssteve.reinhardt@amd.com    // current directory we're serializing into.
2847491Ssteve.reinhardt@amd.com    static std::string currentDirectory;
2857491Ssteve.reinhardt@amd.com
2867491Ssteve.reinhardt@amd.com  public:
2877491Ssteve.reinhardt@amd.com    // Set the current directory.  This function takes care of
2887823Ssteve.reinhardt@amd.com    // inserting curTick() if there's a '%d' in the argument, and
2897491Ssteve.reinhardt@amd.com    // appends a '/' if necessary.  The final name is returned.
2907491Ssteve.reinhardt@amd.com    static std::string setDir(const std::string &base_name);
291449SN/A
292449SN/A    // Export current checkpoint directory name so other objects can
293449SN/A    // derive filenames from it (e.g., memory).  The return value is
294449SN/A    // guaranteed to end in '/' so filenames can be directly appended.
295449SN/A    // This function is only valid while a checkpoint is being created.
296449SN/A    static std::string dir();
297449SN/A
298449SN/A    // Filename for base checkpoint file within directory.
299449SN/A    static const char *baseFilename;
300237SN/A};
3012SN/A
3022SN/A#endif // __SERIALIZE_HH__
303