serialize.hh revision 11067
12SN/A/*
210905Sandreas.sandberg@arm.com * Copyright (c) 2015 ARM Limited
310905Sandreas.sandberg@arm.com * All rights reserved
410905Sandreas.sandberg@arm.com *
510905Sandreas.sandberg@arm.com * The license below extends only to copyright in the software and shall
610905Sandreas.sandberg@arm.com * not be construed as granting a license to any other intellectual
710905Sandreas.sandberg@arm.com * property including but not limited to intellectual property relating
810905Sandreas.sandberg@arm.com * to a hardware implementation of the functionality of the software
910905Sandreas.sandberg@arm.com * licensed hereunder.  You may use the software subject to the license
1010905Sandreas.sandberg@arm.com * terms below provided that you ensure that this notice is replicated
1110905Sandreas.sandberg@arm.com * unmodified and in its entirety in all distributions of the software,
1210905Sandreas.sandberg@arm.com * modified or unmodified, in source code or in binary form.
1310905Sandreas.sandberg@arm.com *
141762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
152SN/A * All rights reserved.
162SN/A *
172SN/A * Redistribution and use in source and binary forms, with or without
182SN/A * modification, are permitted provided that the following conditions are
192SN/A * met: redistributions of source code must retain the above copyright
202SN/A * notice, this list of conditions and the following disclaimer;
212SN/A * redistributions in binary form must reproduce the above copyright
222SN/A * notice, this list of conditions and the following disclaimer in the
232SN/A * documentation and/or other materials provided with the distribution;
242SN/A * neither the name of the copyright holders nor the names of its
252SN/A * contributors may be used to endorse or promote products derived from
262SN/A * this software without specific prior written permission.
272SN/A *
282SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402760Sbinkertn@umich.edu * Authors: Nathan Binkert
412760Sbinkertn@umich.edu *          Erik Hallnor
422665Ssaidi@eecs.umich.edu *          Steve Reinhardt
4310905Sandreas.sandberg@arm.com *          Andreas Sandberg
442SN/A */
452SN/A
462SN/A/* @file
472SN/A * Serialization Interface Declarations
482SN/A */
492SN/A
502SN/A#ifndef __SERIALIZE_HH__
512SN/A#define __SERIALIZE_HH__
522SN/A
532SN/A
548229Snate@binkert.org#include <iostream>
552SN/A#include <list>
568229Snate@binkert.org#include <map>
5710905Sandreas.sandberg@arm.com#include <stack>
584841Ssaidi@eecs.umich.edu#include <vector>
592SN/A
6010459SAndreas.Sandberg@ARM.com#include "base/bitunion.hh"
616214Snate@binkert.org#include "base/types.hh"
622SN/A
632738Sstever@eecs.umich.educlass IniFile;
64395SN/Aclass Serializable;
6510905Sandreas.sandberg@arm.comclass CheckpointIn;
664000Ssaidi@eecs.umich.educlass SimObject;
6711067Sandreas.sandberg@arm.comclass SimObjectResolver;
689983Sstever@gmail.comclass EventQueue;
692SN/A
7010905Sandreas.sandberg@arm.comtypedef std::ostream CheckpointOut;
7110905Sandreas.sandberg@arm.com
7210905Sandreas.sandberg@arm.com
739048SAli.Saidi@ARM.com/** The current version of the checkpoint format.
749048SAli.Saidi@ARM.com * This should be incremented by 1 and only 1 for every new version, where a new
759056SAli.Saidi@ARM.com * version is defined as a checkpoint created before this version won't work on
769048SAli.Saidi@ARM.com * the current version until the checkpoint format is updated. Adding a new
779048SAli.Saidi@ARM.com * SimObject shouldn't cause the version number to increase, only changes to
789056SAli.Saidi@ARM.com * existing objects such as serializing/unserializing more state, changing sizes
799048SAli.Saidi@ARM.com * of serialized arrays, etc. */
8010930Sbrandon.potter@amd.comstatic const uint64_t gem5CheckpointVersion = 0x000000000000000f;
819048SAli.Saidi@ARM.com
82217SN/Atemplate <class T>
8310905Sandreas.sandberg@arm.comvoid paramOut(CheckpointOut &cp, const std::string &name, const T &param);
84217SN/A
8510459SAndreas.Sandberg@ARM.comtemplate <typename DataType, typename BitUnion>
8610905Sandreas.sandberg@arm.comvoid paramOut(CheckpointOut &cp, const std::string &name,
8710459SAndreas.Sandberg@ARM.com              const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
8810459SAndreas.Sandberg@ARM.com{
8910905Sandreas.sandberg@arm.com    paramOut(cp, name, p.__data);
9010459SAndreas.Sandberg@ARM.com}
9110459SAndreas.Sandberg@ARM.com
92217SN/Atemplate <class T>
9310905Sandreas.sandberg@arm.comvoid paramIn(CheckpointIn &cp, const std::string &name, T &param);
94217SN/A
9510459SAndreas.Sandberg@ARM.comtemplate <typename DataType, typename BitUnion>
9610905Sandreas.sandberg@arm.comvoid paramIn(CheckpointIn &cp, const std::string &name,
9710459SAndreas.Sandberg@ARM.com             BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
9810459SAndreas.Sandberg@ARM.com{
9910905Sandreas.sandberg@arm.com    paramIn(cp, name, p.__data);
10010459SAndreas.Sandberg@ARM.com}
10110459SAndreas.Sandberg@ARM.com
102217SN/Atemplate <class T>
10310905Sandreas.sandberg@arm.combool optParamIn(CheckpointIn &cp, const std::string &name, T &param);
1046820SLisa.Hsu@amd.com
10510459SAndreas.Sandberg@ARM.comtemplate <typename DataType, typename BitUnion>
10610905Sandreas.sandberg@arm.combool optParamIn(CheckpointIn &cp, const std::string &name,
10710459SAndreas.Sandberg@ARM.com                BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
10810459SAndreas.Sandberg@ARM.com{
10910905Sandreas.sandberg@arm.com    return optParamIn(cp, name, p.__data);
11010459SAndreas.Sandberg@ARM.com}
11110459SAndreas.Sandberg@ARM.com
1126820SLisa.Hsu@amd.comtemplate <class T>
11310905Sandreas.sandberg@arm.comvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1146227Snate@binkert.org                   const T *param, unsigned size);
115217SN/A
116217SN/Atemplate <class T>
11710905Sandreas.sandberg@arm.comvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1184841Ssaidi@eecs.umich.edu                   const std::vector<T> &param);
1194841Ssaidi@eecs.umich.edu
1204841Ssaidi@eecs.umich.edutemplate <class T>
12110905Sandreas.sandberg@arm.comvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1227948SAli.Saidi@ARM.com                   const std::list<T> &param);
1237948SAli.Saidi@ARM.com
1247948SAli.Saidi@ARM.comtemplate <class T>
12510905Sandreas.sandberg@arm.comvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
12610905Sandreas.sandberg@arm.com                  T *param, unsigned size);
127217SN/A
1284841Ssaidi@eecs.umich.edutemplate <class T>
12910905Sandreas.sandberg@arm.comvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
13010905Sandreas.sandberg@arm.com                  std::vector<T> &param);
1314841Ssaidi@eecs.umich.edu
1327948SAli.Saidi@ARM.comtemplate <class T>
13310905Sandreas.sandberg@arm.comvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
13410905Sandreas.sandberg@arm.com                  std::list<T> &param);
1357948SAli.Saidi@ARM.com
136237SN/Avoid
13710905Sandreas.sandberg@arm.comobjParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
138237SN/A
1398902Sandreas.hansson@arm.comtemplate <typename T>
1408902Sandreas.hansson@arm.comvoid fromInt(T &t, int i)
1418902Sandreas.hansson@arm.com{
1428902Sandreas.hansson@arm.com    t = (T)i;
1438902Sandreas.hansson@arm.com}
1448902Sandreas.hansson@arm.com
1458902Sandreas.hansson@arm.comtemplate <typename T>
1468902Sandreas.hansson@arm.comvoid fromSimObject(T &t, SimObject *s)
1478902Sandreas.hansson@arm.com{
1488902Sandreas.hansson@arm.com    t = dynamic_cast<T>(s);
1498902Sandreas.hansson@arm.com}
150237SN/A
151217SN/A//
152217SN/A// These macros are streamlined to use in serialize/unserialize
153217SN/A// functions.  It's assumed that serialize() has a parameter 'os' for
154237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'.
15510905Sandreas.sandberg@arm.com#define SERIALIZE_SCALAR(scalar)        paramOut(cp, #scalar, scalar)
156217SN/A
15710905Sandreas.sandberg@arm.com#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, #scalar, scalar)
15810905Sandreas.sandberg@arm.com#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, #scalar, scalar)
159217SN/A
160223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out
16110905Sandreas.sandberg@arm.com#define SERIALIZE_ENUM(scalar)          paramOut(cp, #scalar, (int)scalar)
162223SN/A
1635543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar)                \
1645543Ssaidi@eecs.umich.edu do {                                           \
1655543Ssaidi@eecs.umich.edu    int tmp;                                    \
16610905Sandreas.sandberg@arm.com    paramIn(cp, #scalar, tmp);                  \
16710905Sandreas.sandberg@arm.com    fromInt(scalar, tmp);                       \
168223SN/A  } while (0)
169223SN/A
1705543Ssaidi@eecs.umich.edu#define SERIALIZE_ARRAY(member, size)           \
17110905Sandreas.sandberg@arm.com        arrayParamOut(cp, #member, member, size)
172217SN/A
1735543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size)         \
17410905Sandreas.sandberg@arm.com        arrayParamIn(cp, #member, member, size)
175237SN/A
17610903Sandreas.sandberg@arm.com#define SERIALIZE_CONTAINER(member)             \
17710905Sandreas.sandberg@arm.com        arrayParamOut(cp, #member, member)
17810903Sandreas.sandberg@arm.com
17910903Sandreas.sandberg@arm.com#define UNSERIALIZE_CONTAINER(member)           \
18010905Sandreas.sandberg@arm.com        arrayParamIn(cp, #member, member)
18110903Sandreas.sandberg@arm.com
18210906Sandreas.sandberg@arm.com#define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
18310906Sandreas.sandberg@arm.com
18410906Sandreas.sandberg@arm.com#define UNSERIALIZE_EVENT(event)                        \
18510906Sandreas.sandberg@arm.com    do {                                                \
18610906Sandreas.sandberg@arm.com        event.unserializeSection(cp, #event);           \
18710906Sandreas.sandberg@arm.com        eventQueue()->checkpointReschedule(&event);     \
18810906Sandreas.sandberg@arm.com    } while(0)
18910906Sandreas.sandberg@arm.com
19010908Sandreas.sandberg@arm.com#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
19110908Sandreas.sandberg@arm.com#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
19210906Sandreas.sandberg@arm.com
19310905Sandreas.sandberg@arm.com#define SERIALIZE_OBJPTR(objptr)        paramOut(cp, #objptr, (objptr)->name())
194237SN/A
1955543Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr)                      \
1965543Ssaidi@eecs.umich.edu  do {                                                  \
1975543Ssaidi@eecs.umich.edu    SimObject *sptr;                                    \
19810905Sandreas.sandberg@arm.com    objParamIn(cp, #objptr, sptr);                      \
1998902Sandreas.hansson@arm.com    fromSimObject(objptr, sptr);                        \
200237SN/A  } while (0)
201217SN/A
2029342SAndreas.Sandberg@arm.com/**
2032SN/A * Basic support for object serialization.
2049342SAndreas.Sandberg@arm.com *
20510905Sandreas.sandberg@arm.com * Objects that support serialization should derive from this
20610905Sandreas.sandberg@arm.com * class. Such objects can largely be divided into two categories: 1)
20710905Sandreas.sandberg@arm.com * True SimObjects (deriving from SimObject), and 2) child objects
20810905Sandreas.sandberg@arm.com * (non-SimObjects).
20910905Sandreas.sandberg@arm.com *
21010905Sandreas.sandberg@arm.com * SimObjects are serialized automatically into their own sections
21110905Sandreas.sandberg@arm.com * automatically by the SimObject base class (see
21210905Sandreas.sandberg@arm.com * SimObject::serializeAll().
21310905Sandreas.sandberg@arm.com *
21410905Sandreas.sandberg@arm.com * SimObjects can contain other serializable objects that are not
21510905Sandreas.sandberg@arm.com * SimObjects. Much like normal serialized members are not serialized
21610905Sandreas.sandberg@arm.com * automatically, these objects will not be serialized automatically
21710905Sandreas.sandberg@arm.com * and it is expected that the objects owning such serializable
21810905Sandreas.sandberg@arm.com * objects call the required serialization/unserialization methods on
21910905Sandreas.sandberg@arm.com * child objects. The preferred method to serialize a child object is
22010905Sandreas.sandberg@arm.com * to call serializeSection() on the child, which serializes the
22110905Sandreas.sandberg@arm.com * object into a new subsection in the current section. Another option
22210905Sandreas.sandberg@arm.com * is to call serialize() directly, which serializes the object into
22310905Sandreas.sandberg@arm.com * the current section. The latter is not recommended as it can lead
22410905Sandreas.sandberg@arm.com * to naming clashes between objects.
22510905Sandreas.sandberg@arm.com *
2269342SAndreas.Sandberg@arm.com * @note Many objects that support serialization need to be put in a
2279342SAndreas.Sandberg@arm.com * consistent state when serialization takes place. We refer to the
2289342SAndreas.Sandberg@arm.com * action of forcing an object into a consistent state as
2299342SAndreas.Sandberg@arm.com * 'draining'. Objects that need draining inherit from Drainable. See
2309342SAndreas.Sandberg@arm.com * Drainable for more information.
2312SN/A */
232395SN/Aclass Serializable
2332SN/A{
2342SN/A  protected:
23510905Sandreas.sandberg@arm.com    /**
23610905Sandreas.sandberg@arm.com     * Scoped checkpoint section helper class
23710905Sandreas.sandberg@arm.com     *
23810905Sandreas.sandberg@arm.com     * This helper class creates a section within a checkpoint without
23910905Sandreas.sandberg@arm.com     * the need for a separate serializeable object. It is mainly used
24010905Sandreas.sandberg@arm.com     * within the Serializable class when serializing or unserializing
24110905Sandreas.sandberg@arm.com     * section (see serializeSection() and unserializeSection()). It
24210905Sandreas.sandberg@arm.com     * can also be used to maintain backwards compatibility in
24310905Sandreas.sandberg@arm.com     * existing code that serializes structs that are not inheriting
24410905Sandreas.sandberg@arm.com     * from Serializable into subsections.
24510905Sandreas.sandberg@arm.com     *
24610905Sandreas.sandberg@arm.com     * When the class is instantiated, it appends a name to the active
24710905Sandreas.sandberg@arm.com     * path in a checkpoint. The old path is later restored when the
24810905Sandreas.sandberg@arm.com     * instance is destroyed. For example, serializeSection() could be
24910905Sandreas.sandberg@arm.com     * implemented by instantiating a ScopedCheckpointSection and then
25010905Sandreas.sandberg@arm.com     * calling serialize() on an object.
25110905Sandreas.sandberg@arm.com     */
25210905Sandreas.sandberg@arm.com    class ScopedCheckpointSection {
25310905Sandreas.sandberg@arm.com      public:
25410905Sandreas.sandberg@arm.com        template<class CP>
25510905Sandreas.sandberg@arm.com        ScopedCheckpointSection(CP &cp, const char *name) {
25610905Sandreas.sandberg@arm.com            pushName(name);
25710905Sandreas.sandberg@arm.com            nameOut(cp);
25810905Sandreas.sandberg@arm.com        }
25910905Sandreas.sandberg@arm.com
26010905Sandreas.sandberg@arm.com        template<class CP>
26110905Sandreas.sandberg@arm.com        ScopedCheckpointSection(CP &cp, const std::string &name) {
26210905Sandreas.sandberg@arm.com            pushName(name.c_str());
26310905Sandreas.sandberg@arm.com            nameOut(cp);
26410905Sandreas.sandberg@arm.com        }
26510905Sandreas.sandberg@arm.com
26610905Sandreas.sandberg@arm.com        ~ScopedCheckpointSection();
26710905Sandreas.sandberg@arm.com
26810905Sandreas.sandberg@arm.com        ScopedCheckpointSection() = delete;
26910905Sandreas.sandberg@arm.com        ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
27010905Sandreas.sandberg@arm.com        ScopedCheckpointSection &operator=(
27110905Sandreas.sandberg@arm.com            const ScopedCheckpointSection &) = delete;
27210905Sandreas.sandberg@arm.com        ScopedCheckpointSection &operator=(
27310905Sandreas.sandberg@arm.com            ScopedCheckpointSection &&) = delete;
27410905Sandreas.sandberg@arm.com
27510905Sandreas.sandberg@arm.com      private:
27610905Sandreas.sandberg@arm.com        void pushName(const char *name);
27710905Sandreas.sandberg@arm.com        void nameOut(CheckpointOut &cp);
27810905Sandreas.sandberg@arm.com        void nameOut(CheckpointIn &cp) {};
27910905Sandreas.sandberg@arm.com    };
2802SN/A
2812SN/A  public:
2825739Snate@binkert.org    Serializable();
2835739Snate@binkert.org    virtual ~Serializable();
2842SN/A
28510905Sandreas.sandberg@arm.com    /**
28610905Sandreas.sandberg@arm.com     * Serialize an object
28710905Sandreas.sandberg@arm.com     *
28810905Sandreas.sandberg@arm.com     * Output an object's state into the current checkpoint section.
28910905Sandreas.sandberg@arm.com     *
29010905Sandreas.sandberg@arm.com     * @param cp Checkpoint state
29110905Sandreas.sandberg@arm.com     */
29210905Sandreas.sandberg@arm.com    virtual void serialize(CheckpointOut &cp) const = 0;
2932SN/A
29410905Sandreas.sandberg@arm.com    /**
29510905Sandreas.sandberg@arm.com     * Unserialize an object
29610905Sandreas.sandberg@arm.com     *
29710905Sandreas.sandberg@arm.com     * Read an object's state from the current checkpoint section.
29810905Sandreas.sandberg@arm.com     *
29910905Sandreas.sandberg@arm.com     * @param cp Checkpoint state
30010905Sandreas.sandberg@arm.com     */
30110905Sandreas.sandberg@arm.com    virtual void unserialize(CheckpointIn &cp) = 0;
302237SN/A
30310905Sandreas.sandberg@arm.com    /**
30410905Sandreas.sandberg@arm.com     * Serialize an object into a new section
30510905Sandreas.sandberg@arm.com     *
30610905Sandreas.sandberg@arm.com     * This method creates a new section in a checkpoint and calls
30710905Sandreas.sandberg@arm.com     * serialize() to serialize the current object into that
30810905Sandreas.sandberg@arm.com     * section. The name of the section is appended to the current
30910905Sandreas.sandberg@arm.com     * checkpoint path.
31010905Sandreas.sandberg@arm.com     *
31110905Sandreas.sandberg@arm.com     * @param cp Checkpoint state
31210905Sandreas.sandberg@arm.com     * @param name Name to append to the active path
31310905Sandreas.sandberg@arm.com     */
31410905Sandreas.sandberg@arm.com    void serializeSection(CheckpointOut &cp, const char *name) const;
31510905Sandreas.sandberg@arm.com
31610905Sandreas.sandberg@arm.com    void serializeSection(CheckpointOut &cp, const std::string &name) const {
31710905Sandreas.sandberg@arm.com        serializeSection(cp, name.c_str());
31810905Sandreas.sandberg@arm.com    }
31910905Sandreas.sandberg@arm.com
32010905Sandreas.sandberg@arm.com    /**
32110905Sandreas.sandberg@arm.com     * Unserialize an a child object
32210905Sandreas.sandberg@arm.com     *
32310905Sandreas.sandberg@arm.com     * This method loads a child object from a checkpoint. The object
32410905Sandreas.sandberg@arm.com     * name is appended to the active path to form a fully qualified
32510905Sandreas.sandberg@arm.com     * section name and unserialize() is called.
32610905Sandreas.sandberg@arm.com     *
32710905Sandreas.sandberg@arm.com     * @param cp Checkpoint state
32810905Sandreas.sandberg@arm.com     * @param name Name to append to the active path
32910905Sandreas.sandberg@arm.com     */
33010905Sandreas.sandberg@arm.com    void unserializeSection(CheckpointIn &cp, const char *name);
33110905Sandreas.sandberg@arm.com
33210905Sandreas.sandberg@arm.com    void unserializeSection(CheckpointIn &cp, const std::string &name) {
33310905Sandreas.sandberg@arm.com        unserializeSection(cp, name.c_str());
33410905Sandreas.sandberg@arm.com    }
33510905Sandreas.sandberg@arm.com
33610905Sandreas.sandberg@arm.com    /**
33710905Sandreas.sandberg@arm.com     * @{
33810905Sandreas.sandberg@arm.com     * @name Legacy interface
33910905Sandreas.sandberg@arm.com     *
34010905Sandreas.sandberg@arm.com     * Interface for objects that insist on changing their state when
34110905Sandreas.sandberg@arm.com     * serializing. Such state change should be done in drain(),
34210905Sandreas.sandberg@arm.com     * memWriteback(), or memInvalidate() and not in the serialization
34310905Sandreas.sandberg@arm.com     * method. In general, if state changes occur in serialize, it
34410905Sandreas.sandberg@arm.com     * complicates testing since it breaks assumptions about draining
34510905Sandreas.sandberg@arm.com     * and serialization. It potentially also makes components more
34610905Sandreas.sandberg@arm.com     * fragile since they there are no ordering guarantees when
34710905Sandreas.sandberg@arm.com     * serializing SimObjects.
34810905Sandreas.sandberg@arm.com     *
34910905Sandreas.sandberg@arm.com     * @warn This interface is considered deprecated and should never
35010905Sandreas.sandberg@arm.com     * be used.
35110905Sandreas.sandberg@arm.com     */
35210905Sandreas.sandberg@arm.com
35310905Sandreas.sandberg@arm.com    virtual void serializeOld(CheckpointOut &cp) {
35410905Sandreas.sandberg@arm.com        serialize(cp);
35510905Sandreas.sandberg@arm.com    }
35610905Sandreas.sandberg@arm.com    void serializeSectionOld(CheckpointOut &cp, const char *name);
35710905Sandreas.sandberg@arm.com    void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
35810905Sandreas.sandberg@arm.com        serializeSectionOld(cp, name.c_str());
35910905Sandreas.sandberg@arm.com    }
36010905Sandreas.sandberg@arm.com    /** @} */
36110905Sandreas.sandberg@arm.com
36210905Sandreas.sandberg@arm.com    /** Get the fully-qualified name of the active section */
36310905Sandreas.sandberg@arm.com    static const std::string &currentSection();
36410905Sandreas.sandberg@arm.com
36510905Sandreas.sandberg@arm.com    static Serializable *create(CheckpointIn &cp, const std::string &section);
3662SN/A
3672287SN/A    static int ckptCount;
3682287SN/A    static int ckptMaxCount;
3692287SN/A    static int ckptPrevCount;
3702868Sktlim@umich.edu    static void serializeAll(const std::string &cpt_dir);
37110905Sandreas.sandberg@arm.com    static void unserializeGlobals(CheckpointIn &cp);
37210905Sandreas.sandberg@arm.com
37310905Sandreas.sandberg@arm.com  private:
37410905Sandreas.sandberg@arm.com    static std::stack<std::string> path;
3752SN/A};
3762SN/A
3779554Sandreas.hansson@arm.comvoid debug_serialize(const std::string &cpt_dir);
3789554Sandreas.hansson@arm.com
3792SN/A//
380395SN/A// A SerializableBuilder serves as an evaluation context for a set of
381395SN/A// parameters that describe a specific instance of a Serializable.  This
3822SN/A// evaluation context corresponds to a section in the .ini file (as
3832SN/A// with the base ParamContext) plus an optional node in the
3842SN/A// configuration hierarchy (the configNode member) for resolving
385395SN/A// Serializable references.  SerializableBuilder is an abstract superclass;
3862SN/A// derived classes specialize the class for particular subclasses of
387395SN/A// Serializable (e.g., BaseCache).
3882SN/A//
3892SN/A// For typical usage, see the definition of
390395SN/A// SerializableClass::createObject().
3912SN/A//
392395SN/Aclass SerializableBuilder
3932SN/A{
3942SN/A  public:
3952SN/A
396395SN/A    SerializableBuilder() {}
3972SN/A
398395SN/A    virtual ~SerializableBuilder() {}
3992SN/A
400395SN/A    // Create the actual Serializable corresponding to the parameter
4012SN/A    // values in this context.  This function is overridden in derived
4022SN/A    // classes to call a specific constructor for a particular
403395SN/A    // subclass of Serializable.
404395SN/A    virtual Serializable *create() = 0;
4052SN/A};
4062SN/A
4072SN/A//
408395SN/A// An instance of SerializableClass corresponds to a class derived from
409395SN/A// Serializable.  The SerializableClass instance serves to bind the string
4102SN/A// name (found in the config file) to a function that creates an
4112SN/A// instance of the appropriate derived class.
4122SN/A//
4132SN/A// This would be much cleaner in Smalltalk or Objective-C, where types
4142SN/A// are first-class objects themselves.
4152SN/A//
416395SN/Aclass SerializableClass
4172SN/A{
4182SN/A  public:
4192SN/A
4202SN/A    // Type CreateFunc is a pointer to a function that creates a new
4212SN/A    // simulation object builder based on a .ini-file parameter
4222SN/A    // section (specified by the first string argument), a unique name
4232SN/A    // for the object (specified by the second string argument), and
4242SN/A    // an optional config hierarchy node (specified by the third
425395SN/A    // argument).  A pointer to the new SerializableBuilder is returned.
42610905Sandreas.sandberg@arm.com    typedef Serializable *(*CreateFunc)(CheckpointIn &cp,
4272738Sstever@eecs.umich.edu                                        const std::string &section);
4282SN/A
4292SN/A    static std::map<std::string,CreateFunc> *classMap;
4302SN/A
4312SN/A    // Constructor.  For example:
4322SN/A    //
433395SN/A    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
434395SN/A    //                         newBaseCacheSerializableBuilder);
4352SN/A    //
436395SN/A    SerializableClass(const std::string &className, CreateFunc createFunc);
4372SN/A
438395SN/A    // create Serializable given name of class and pointer to
4392SN/A    // configuration hierarchy node
44010905Sandreas.sandberg@arm.com    static Serializable *createObject(CheckpointIn &cp,
4412738Sstever@eecs.umich.edu                                      const std::string &section);
4422SN/A};
4432SN/A
4442SN/A//
4452SN/A// Macros to encapsulate the magic of declaring & defining
446395SN/A// SerializableBuilder and SerializableClass objects
4472SN/A//
4482SN/A
4495543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
4505543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
451237SN/A                                         OBJ_CLASS::createForUnserialize);
4522SN/A
45310453SAndrew.Bardsley@arm.com
45410905Sandreas.sandberg@arm.comclass CheckpointIn
455237SN/A{
456237SN/A  private:
457237SN/A
458237SN/A    IniFile *db;
459237SN/A
46010453SAndrew.Bardsley@arm.com    SimObjectResolver &objNameResolver;
46110453SAndrew.Bardsley@arm.com
462237SN/A  public:
46310905Sandreas.sandberg@arm.com    CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
46410905Sandreas.sandberg@arm.com    ~CheckpointIn();
465237SN/A
466937SN/A    const std::string cptDir;
467937SN/A
468237SN/A    bool find(const std::string &section, const std::string &entry,
469237SN/A              std::string &value);
470237SN/A
471237SN/A    bool findObj(const std::string &section, const std::string &entry,
4724000Ssaidi@eecs.umich.edu                 SimObject *&value);
473304SN/A
474304SN/A    bool sectionExists(const std::string &section);
475449SN/A
476449SN/A    // The following static functions have to do with checkpoint
477449SN/A    // creation rather than restoration.  This class makes a handy
4787491Ssteve.reinhardt@amd.com    // namespace for them though.  Currently no Checkpoint object is
4797491Ssteve.reinhardt@amd.com    // created on serialization (only unserialization) so we track the
4807491Ssteve.reinhardt@amd.com    // directory name as a global.  It would be nice to change this
4817491Ssteve.reinhardt@amd.com    // someday
4827491Ssteve.reinhardt@amd.com
4837491Ssteve.reinhardt@amd.com  private:
4847491Ssteve.reinhardt@amd.com    // current directory we're serializing into.
4857491Ssteve.reinhardt@amd.com    static std::string currentDirectory;
4867491Ssteve.reinhardt@amd.com
4877491Ssteve.reinhardt@amd.com  public:
4887491Ssteve.reinhardt@amd.com    // Set the current directory.  This function takes care of
4897823Ssteve.reinhardt@amd.com    // inserting curTick() if there's a '%d' in the argument, and
4907491Ssteve.reinhardt@amd.com    // appends a '/' if necessary.  The final name is returned.
4917491Ssteve.reinhardt@amd.com    static std::string setDir(const std::string &base_name);
492449SN/A
493449SN/A    // Export current checkpoint directory name so other objects can
494449SN/A    // derive filenames from it (e.g., memory).  The return value is
495449SN/A    // guaranteed to end in '/' so filenames can be directly appended.
496449SN/A    // This function is only valid while a checkpoint is being created.
497449SN/A    static std::string dir();
498449SN/A
499449SN/A    // Filename for base checkpoint file within directory.
500449SN/A    static const char *baseFilename;
501237SN/A};
5022SN/A
5032SN/A#endif // __SERIALIZE_HH__
504