serialize.hh revision 9554
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. */
609431SAndreas.Sandberg@ARM.comstatic const uint64_t gem5CheckpointVersion = 0x0000000000000005;
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
1819554Sandreas.hansson@arm.comvoid debug_serialize(const std::string &cpt_dir);
1829554Sandreas.hansson@arm.com
1832SN/A//
184395SN/A// A SerializableBuilder serves as an evaluation context for a set of
185395SN/A// parameters that describe a specific instance of a Serializable.  This
1862SN/A// evaluation context corresponds to a section in the .ini file (as
1872SN/A// with the base ParamContext) plus an optional node in the
1882SN/A// configuration hierarchy (the configNode member) for resolving
189395SN/A// Serializable references.  SerializableBuilder is an abstract superclass;
1902SN/A// derived classes specialize the class for particular subclasses of
191395SN/A// Serializable (e.g., BaseCache).
1922SN/A//
1932SN/A// For typical usage, see the definition of
194395SN/A// SerializableClass::createObject().
1952SN/A//
196395SN/Aclass SerializableBuilder
1972SN/A{
1982SN/A  public:
1992SN/A
200395SN/A    SerializableBuilder() {}
2012SN/A
202395SN/A    virtual ~SerializableBuilder() {}
2032SN/A
204395SN/A    // Create the actual Serializable corresponding to the parameter
2052SN/A    // values in this context.  This function is overridden in derived
2062SN/A    // classes to call a specific constructor for a particular
207395SN/A    // subclass of Serializable.
208395SN/A    virtual Serializable *create() = 0;
2092SN/A};
2102SN/A
2112SN/A//
212395SN/A// An instance of SerializableClass corresponds to a class derived from
213395SN/A// Serializable.  The SerializableClass instance serves to bind the string
2142SN/A// name (found in the config file) to a function that creates an
2152SN/A// instance of the appropriate derived class.
2162SN/A//
2172SN/A// This would be much cleaner in Smalltalk or Objective-C, where types
2182SN/A// are first-class objects themselves.
2192SN/A//
220395SN/Aclass SerializableClass
2212SN/A{
2222SN/A  public:
2232SN/A
2242SN/A    // Type CreateFunc is a pointer to a function that creates a new
2252SN/A    // simulation object builder based on a .ini-file parameter
2262SN/A    // section (specified by the first string argument), a unique name
2272SN/A    // for the object (specified by the second string argument), and
2282SN/A    // an optional config hierarchy node (specified by the third
229395SN/A    // argument).  A pointer to the new SerializableBuilder is returned.
230395SN/A    typedef Serializable *(*CreateFunc)(Checkpoint *cp,
2312738Sstever@eecs.umich.edu                                        const std::string &section);
2322SN/A
2332SN/A    static std::map<std::string,CreateFunc> *classMap;
2342SN/A
2352SN/A    // Constructor.  For example:
2362SN/A    //
237395SN/A    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
238395SN/A    //                         newBaseCacheSerializableBuilder);
2392SN/A    //
240395SN/A    SerializableClass(const std::string &className, CreateFunc createFunc);
2412SN/A
242395SN/A    // create Serializable given name of class and pointer to
2432SN/A    // configuration hierarchy node
244395SN/A    static Serializable *createObject(Checkpoint *cp,
2452738Sstever@eecs.umich.edu                                      const std::string &section);
2462SN/A};
2472SN/A
2482SN/A//
2492SN/A// Macros to encapsulate the magic of declaring & defining
250395SN/A// SerializableBuilder and SerializableClass objects
2512SN/A//
2522SN/A
2535543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
2545543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
255237SN/A                                         OBJ_CLASS::createForUnserialize);
2562SN/A
257237SN/Aclass Checkpoint
258237SN/A{
259237SN/A  private:
260237SN/A
261237SN/A    IniFile *db;
262237SN/A
263237SN/A  public:
2647491Ssteve.reinhardt@amd.com    Checkpoint(const std::string &cpt_dir);
2659086Sandreas.hansson@arm.com    ~Checkpoint();
266237SN/A
267937SN/A    const std::string cptDir;
268937SN/A
269237SN/A    bool find(const std::string &section, const std::string &entry,
270237SN/A              std::string &value);
271237SN/A
272237SN/A    bool findObj(const std::string &section, const std::string &entry,
2734000Ssaidi@eecs.umich.edu                 SimObject *&value);
274304SN/A
275304SN/A    bool sectionExists(const std::string &section);
276449SN/A
277449SN/A    // The following static functions have to do with checkpoint
278449SN/A    // creation rather than restoration.  This class makes a handy
2797491Ssteve.reinhardt@amd.com    // namespace for them though.  Currently no Checkpoint object is
2807491Ssteve.reinhardt@amd.com    // created on serialization (only unserialization) so we track the
2817491Ssteve.reinhardt@amd.com    // directory name as a global.  It would be nice to change this
2827491Ssteve.reinhardt@amd.com    // someday
2837491Ssteve.reinhardt@amd.com
2847491Ssteve.reinhardt@amd.com  private:
2857491Ssteve.reinhardt@amd.com    // current directory we're serializing into.
2867491Ssteve.reinhardt@amd.com    static std::string currentDirectory;
2877491Ssteve.reinhardt@amd.com
2887491Ssteve.reinhardt@amd.com  public:
2897491Ssteve.reinhardt@amd.com    // Set the current directory.  This function takes care of
2907823Ssteve.reinhardt@amd.com    // inserting curTick() if there's a '%d' in the argument, and
2917491Ssteve.reinhardt@amd.com    // appends a '/' if necessary.  The final name is returned.
2927491Ssteve.reinhardt@amd.com    static std::string setDir(const std::string &base_name);
293449SN/A
294449SN/A    // Export current checkpoint directory name so other objects can
295449SN/A    // derive filenames from it (e.g., memory).  The return value is
296449SN/A    // guaranteed to end in '/' so filenames can be directly appended.
297449SN/A    // This function is only valid while a checkpoint is being created.
298449SN/A    static std::string dir();
299449SN/A
300449SN/A    // Filename for base checkpoint file within directory.
301449SN/A    static const char *baseFilename;
302237SN/A};
3032SN/A
3042SN/A#endif // __SERIALIZE_HH__
305