serialize.hh revision 10930
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;
679983Sstever@gmail.comclass EventQueue;
682SN/A
6910905Sandreas.sandberg@arm.comtypedef std::ostream CheckpointOut;
7010905Sandreas.sandberg@arm.com
7110905Sandreas.sandberg@arm.com
729048SAli.Saidi@ARM.com/** The current version of the checkpoint format.
739048SAli.Saidi@ARM.com * This should be incremented by 1 and only 1 for every new version, where a new
749056SAli.Saidi@ARM.com * version is defined as a checkpoint created before this version won't work on
759048SAli.Saidi@ARM.com * the current version until the checkpoint format is updated. Adding a new
769048SAli.Saidi@ARM.com * SimObject shouldn't cause the version number to increase, only changes to
779056SAli.Saidi@ARM.com * existing objects such as serializing/unserializing more state, changing sizes
789048SAli.Saidi@ARM.com * of serialized arrays, etc. */
7910930Sbrandon.potter@amd.comstatic const uint64_t gem5CheckpointVersion = 0x000000000000000f;
809048SAli.Saidi@ARM.com
81217SN/Atemplate <class T>
8210905Sandreas.sandberg@arm.comvoid paramOut(CheckpointOut &cp, const std::string &name, const T &param);
83217SN/A
8410459SAndreas.Sandberg@ARM.comtemplate <typename DataType, typename BitUnion>
8510905Sandreas.sandberg@arm.comvoid paramOut(CheckpointOut &cp, const std::string &name,
8610459SAndreas.Sandberg@ARM.com              const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
8710459SAndreas.Sandberg@ARM.com{
8810905Sandreas.sandberg@arm.com    paramOut(cp, name, p.__data);
8910459SAndreas.Sandberg@ARM.com}
9010459SAndreas.Sandberg@ARM.com
91217SN/Atemplate <class T>
9210905Sandreas.sandberg@arm.comvoid paramIn(CheckpointIn &cp, const std::string &name, T &param);
93217SN/A
9410459SAndreas.Sandberg@ARM.comtemplate <typename DataType, typename BitUnion>
9510905Sandreas.sandberg@arm.comvoid paramIn(CheckpointIn &cp, const std::string &name,
9610459SAndreas.Sandberg@ARM.com             BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
9710459SAndreas.Sandberg@ARM.com{
9810905Sandreas.sandberg@arm.com    paramIn(cp, name, p.__data);
9910459SAndreas.Sandberg@ARM.com}
10010459SAndreas.Sandberg@ARM.com
101217SN/Atemplate <class T>
10210905Sandreas.sandberg@arm.combool optParamIn(CheckpointIn &cp, const std::string &name, T &param);
1036820SLisa.Hsu@amd.com
10410459SAndreas.Sandberg@ARM.comtemplate <typename DataType, typename BitUnion>
10510905Sandreas.sandberg@arm.combool optParamIn(CheckpointIn &cp, const std::string &name,
10610459SAndreas.Sandberg@ARM.com                BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
10710459SAndreas.Sandberg@ARM.com{
10810905Sandreas.sandberg@arm.com    return optParamIn(cp, name, p.__data);
10910459SAndreas.Sandberg@ARM.com}
11010459SAndreas.Sandberg@ARM.com
1116820SLisa.Hsu@amd.comtemplate <class T>
11210905Sandreas.sandberg@arm.comvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1136227Snate@binkert.org                   const T *param, unsigned size);
114217SN/A
115217SN/Atemplate <class T>
11610905Sandreas.sandberg@arm.comvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1174841Ssaidi@eecs.umich.edu                   const std::vector<T> &param);
1184841Ssaidi@eecs.umich.edu
1194841Ssaidi@eecs.umich.edutemplate <class T>
12010905Sandreas.sandberg@arm.comvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1217948SAli.Saidi@ARM.com                   const std::list<T> &param);
1227948SAli.Saidi@ARM.com
1237948SAli.Saidi@ARM.comtemplate <class T>
12410905Sandreas.sandberg@arm.comvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
12510905Sandreas.sandberg@arm.com                  T *param, unsigned size);
126217SN/A
1274841Ssaidi@eecs.umich.edutemplate <class T>
12810905Sandreas.sandberg@arm.comvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
12910905Sandreas.sandberg@arm.com                  std::vector<T> &param);
1304841Ssaidi@eecs.umich.edu
1317948SAli.Saidi@ARM.comtemplate <class T>
13210905Sandreas.sandberg@arm.comvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
13310905Sandreas.sandberg@arm.com                  std::list<T> &param);
1347948SAli.Saidi@ARM.com
135237SN/Avoid
13610905Sandreas.sandberg@arm.comobjParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
137237SN/A
1388902Sandreas.hansson@arm.comtemplate <typename T>
1398902Sandreas.hansson@arm.comvoid fromInt(T &t, int i)
1408902Sandreas.hansson@arm.com{
1418902Sandreas.hansson@arm.com    t = (T)i;
1428902Sandreas.hansson@arm.com}
1438902Sandreas.hansson@arm.com
1448902Sandreas.hansson@arm.comtemplate <typename T>
1458902Sandreas.hansson@arm.comvoid fromSimObject(T &t, SimObject *s)
1468902Sandreas.hansson@arm.com{
1478902Sandreas.hansson@arm.com    t = dynamic_cast<T>(s);
1488902Sandreas.hansson@arm.com}
149237SN/A
150217SN/A//
151217SN/A// These macros are streamlined to use in serialize/unserialize
152217SN/A// functions.  It's assumed that serialize() has a parameter 'os' for
153237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'.
15410905Sandreas.sandberg@arm.com#define SERIALIZE_SCALAR(scalar)        paramOut(cp, #scalar, scalar)
155217SN/A
15610905Sandreas.sandberg@arm.com#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, #scalar, scalar)
15710905Sandreas.sandberg@arm.com#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, #scalar, scalar)
158217SN/A
159223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out
16010905Sandreas.sandberg@arm.com#define SERIALIZE_ENUM(scalar)          paramOut(cp, #scalar, (int)scalar)
161223SN/A
1625543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar)                \
1635543Ssaidi@eecs.umich.edu do {                                           \
1645543Ssaidi@eecs.umich.edu    int tmp;                                    \
16510905Sandreas.sandberg@arm.com    paramIn(cp, #scalar, tmp);                  \
16610905Sandreas.sandberg@arm.com    fromInt(scalar, tmp);                       \
167223SN/A  } while (0)
168223SN/A
1695543Ssaidi@eecs.umich.edu#define SERIALIZE_ARRAY(member, size)           \
17010905Sandreas.sandberg@arm.com        arrayParamOut(cp, #member, member, size)
171217SN/A
1725543Ssaidi@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size)         \
17310905Sandreas.sandberg@arm.com        arrayParamIn(cp, #member, member, size)
174237SN/A
17510903Sandreas.sandberg@arm.com#define SERIALIZE_CONTAINER(member)             \
17610905Sandreas.sandberg@arm.com        arrayParamOut(cp, #member, member)
17710903Sandreas.sandberg@arm.com
17810903Sandreas.sandberg@arm.com#define UNSERIALIZE_CONTAINER(member)           \
17910905Sandreas.sandberg@arm.com        arrayParamIn(cp, #member, member)
18010903Sandreas.sandberg@arm.com
18110906Sandreas.sandberg@arm.com#define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
18210906Sandreas.sandberg@arm.com
18310906Sandreas.sandberg@arm.com#define UNSERIALIZE_EVENT(event)                        \
18410906Sandreas.sandberg@arm.com    do {                                                \
18510906Sandreas.sandberg@arm.com        event.unserializeSection(cp, #event);           \
18610906Sandreas.sandberg@arm.com        eventQueue()->checkpointReschedule(&event);     \
18710906Sandreas.sandberg@arm.com    } while(0)
18810906Sandreas.sandberg@arm.com
18910908Sandreas.sandberg@arm.com#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
19010908Sandreas.sandberg@arm.com#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
19110906Sandreas.sandberg@arm.com
19210905Sandreas.sandberg@arm.com#define SERIALIZE_OBJPTR(objptr)        paramOut(cp, #objptr, (objptr)->name())
193237SN/A
1945543Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr)                      \
1955543Ssaidi@eecs.umich.edu  do {                                                  \
1965543Ssaidi@eecs.umich.edu    SimObject *sptr;                                    \
19710905Sandreas.sandberg@arm.com    objParamIn(cp, #objptr, sptr);                      \
1988902Sandreas.hansson@arm.com    fromSimObject(objptr, sptr);                        \
199237SN/A  } while (0)
200217SN/A
2019342SAndreas.Sandberg@arm.com/**
2022SN/A * Basic support for object serialization.
2039342SAndreas.Sandberg@arm.com *
20410905Sandreas.sandberg@arm.com * Objects that support serialization should derive from this
20510905Sandreas.sandberg@arm.com * class. Such objects can largely be divided into two categories: 1)
20610905Sandreas.sandberg@arm.com * True SimObjects (deriving from SimObject), and 2) child objects
20710905Sandreas.sandberg@arm.com * (non-SimObjects).
20810905Sandreas.sandberg@arm.com *
20910905Sandreas.sandberg@arm.com * SimObjects are serialized automatically into their own sections
21010905Sandreas.sandberg@arm.com * automatically by the SimObject base class (see
21110905Sandreas.sandberg@arm.com * SimObject::serializeAll().
21210905Sandreas.sandberg@arm.com *
21310905Sandreas.sandberg@arm.com * SimObjects can contain other serializable objects that are not
21410905Sandreas.sandberg@arm.com * SimObjects. Much like normal serialized members are not serialized
21510905Sandreas.sandberg@arm.com * automatically, these objects will not be serialized automatically
21610905Sandreas.sandberg@arm.com * and it is expected that the objects owning such serializable
21710905Sandreas.sandberg@arm.com * objects call the required serialization/unserialization methods on
21810905Sandreas.sandberg@arm.com * child objects. The preferred method to serialize a child object is
21910905Sandreas.sandberg@arm.com * to call serializeSection() on the child, which serializes the
22010905Sandreas.sandberg@arm.com * object into a new subsection in the current section. Another option
22110905Sandreas.sandberg@arm.com * is to call serialize() directly, which serializes the object into
22210905Sandreas.sandberg@arm.com * the current section. The latter is not recommended as it can lead
22310905Sandreas.sandberg@arm.com * to naming clashes between objects.
22410905Sandreas.sandberg@arm.com *
2259342SAndreas.Sandberg@arm.com * @note Many objects that support serialization need to be put in a
2269342SAndreas.Sandberg@arm.com * consistent state when serialization takes place. We refer to the
2279342SAndreas.Sandberg@arm.com * action of forcing an object into a consistent state as
2289342SAndreas.Sandberg@arm.com * 'draining'. Objects that need draining inherit from Drainable. See
2299342SAndreas.Sandberg@arm.com * Drainable for more information.
2302SN/A */
231395SN/Aclass Serializable
2322SN/A{
2332SN/A  protected:
23410905Sandreas.sandberg@arm.com    /**
23510905Sandreas.sandberg@arm.com     * Scoped checkpoint section helper class
23610905Sandreas.sandberg@arm.com     *
23710905Sandreas.sandberg@arm.com     * This helper class creates a section within a checkpoint without
23810905Sandreas.sandberg@arm.com     * the need for a separate serializeable object. It is mainly used
23910905Sandreas.sandberg@arm.com     * within the Serializable class when serializing or unserializing
24010905Sandreas.sandberg@arm.com     * section (see serializeSection() and unserializeSection()). It
24110905Sandreas.sandberg@arm.com     * can also be used to maintain backwards compatibility in
24210905Sandreas.sandberg@arm.com     * existing code that serializes structs that are not inheriting
24310905Sandreas.sandberg@arm.com     * from Serializable into subsections.
24410905Sandreas.sandberg@arm.com     *
24510905Sandreas.sandberg@arm.com     * When the class is instantiated, it appends a name to the active
24610905Sandreas.sandberg@arm.com     * path in a checkpoint. The old path is later restored when the
24710905Sandreas.sandberg@arm.com     * instance is destroyed. For example, serializeSection() could be
24810905Sandreas.sandberg@arm.com     * implemented by instantiating a ScopedCheckpointSection and then
24910905Sandreas.sandberg@arm.com     * calling serialize() on an object.
25010905Sandreas.sandberg@arm.com     */
25110905Sandreas.sandberg@arm.com    class ScopedCheckpointSection {
25210905Sandreas.sandberg@arm.com      public:
25310905Sandreas.sandberg@arm.com        template<class CP>
25410905Sandreas.sandberg@arm.com        ScopedCheckpointSection(CP &cp, const char *name) {
25510905Sandreas.sandberg@arm.com            pushName(name);
25610905Sandreas.sandberg@arm.com            nameOut(cp);
25710905Sandreas.sandberg@arm.com        }
25810905Sandreas.sandberg@arm.com
25910905Sandreas.sandberg@arm.com        template<class CP>
26010905Sandreas.sandberg@arm.com        ScopedCheckpointSection(CP &cp, const std::string &name) {
26110905Sandreas.sandberg@arm.com            pushName(name.c_str());
26210905Sandreas.sandberg@arm.com            nameOut(cp);
26310905Sandreas.sandberg@arm.com        }
26410905Sandreas.sandberg@arm.com
26510905Sandreas.sandberg@arm.com        ~ScopedCheckpointSection();
26610905Sandreas.sandberg@arm.com
26710905Sandreas.sandberg@arm.com        ScopedCheckpointSection() = delete;
26810905Sandreas.sandberg@arm.com        ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
26910905Sandreas.sandberg@arm.com        ScopedCheckpointSection &operator=(
27010905Sandreas.sandberg@arm.com            const ScopedCheckpointSection &) = delete;
27110905Sandreas.sandberg@arm.com        ScopedCheckpointSection &operator=(
27210905Sandreas.sandberg@arm.com            ScopedCheckpointSection &&) = delete;
27310905Sandreas.sandberg@arm.com
27410905Sandreas.sandberg@arm.com      private:
27510905Sandreas.sandberg@arm.com        void pushName(const char *name);
27610905Sandreas.sandberg@arm.com        void nameOut(CheckpointOut &cp);
27710905Sandreas.sandberg@arm.com        void nameOut(CheckpointIn &cp) {};
27810905Sandreas.sandberg@arm.com    };
2792SN/A
2802SN/A  public:
2815739Snate@binkert.org    Serializable();
2825739Snate@binkert.org    virtual ~Serializable();
2832SN/A
28410905Sandreas.sandberg@arm.com    /**
28510905Sandreas.sandberg@arm.com     * Serialize an object
28610905Sandreas.sandberg@arm.com     *
28710905Sandreas.sandberg@arm.com     * Output an object's state into the current checkpoint section.
28810905Sandreas.sandberg@arm.com     *
28910905Sandreas.sandberg@arm.com     * @param cp Checkpoint state
29010905Sandreas.sandberg@arm.com     */
29110905Sandreas.sandberg@arm.com    virtual void serialize(CheckpointOut &cp) const = 0;
2922SN/A
29310905Sandreas.sandberg@arm.com    /**
29410905Sandreas.sandberg@arm.com     * Unserialize an object
29510905Sandreas.sandberg@arm.com     *
29610905Sandreas.sandberg@arm.com     * Read an object's state from the current checkpoint section.
29710905Sandreas.sandberg@arm.com     *
29810905Sandreas.sandberg@arm.com     * @param cp Checkpoint state
29910905Sandreas.sandberg@arm.com     */
30010905Sandreas.sandberg@arm.com    virtual void unserialize(CheckpointIn &cp) = 0;
301237SN/A
30210905Sandreas.sandberg@arm.com    /**
30310905Sandreas.sandberg@arm.com     * Serialize an object into a new section
30410905Sandreas.sandberg@arm.com     *
30510905Sandreas.sandberg@arm.com     * This method creates a new section in a checkpoint and calls
30610905Sandreas.sandberg@arm.com     * serialize() to serialize the current object into that
30710905Sandreas.sandberg@arm.com     * section. The name of the section is appended to the current
30810905Sandreas.sandberg@arm.com     * checkpoint path.
30910905Sandreas.sandberg@arm.com     *
31010905Sandreas.sandberg@arm.com     * @param cp Checkpoint state
31110905Sandreas.sandberg@arm.com     * @param name Name to append to the active path
31210905Sandreas.sandberg@arm.com     */
31310905Sandreas.sandberg@arm.com    void serializeSection(CheckpointOut &cp, const char *name) const;
31410905Sandreas.sandberg@arm.com
31510905Sandreas.sandberg@arm.com    void serializeSection(CheckpointOut &cp, const std::string &name) const {
31610905Sandreas.sandberg@arm.com        serializeSection(cp, name.c_str());
31710905Sandreas.sandberg@arm.com    }
31810905Sandreas.sandberg@arm.com
31910905Sandreas.sandberg@arm.com    /**
32010905Sandreas.sandberg@arm.com     * Unserialize an a child object
32110905Sandreas.sandberg@arm.com     *
32210905Sandreas.sandberg@arm.com     * This method loads a child object from a checkpoint. The object
32310905Sandreas.sandberg@arm.com     * name is appended to the active path to form a fully qualified
32410905Sandreas.sandberg@arm.com     * section name and unserialize() is called.
32510905Sandreas.sandberg@arm.com     *
32610905Sandreas.sandberg@arm.com     * @param cp Checkpoint state
32710905Sandreas.sandberg@arm.com     * @param name Name to append to the active path
32810905Sandreas.sandberg@arm.com     */
32910905Sandreas.sandberg@arm.com    void unserializeSection(CheckpointIn &cp, const char *name);
33010905Sandreas.sandberg@arm.com
33110905Sandreas.sandberg@arm.com    void unserializeSection(CheckpointIn &cp, const std::string &name) {
33210905Sandreas.sandberg@arm.com        unserializeSection(cp, name.c_str());
33310905Sandreas.sandberg@arm.com    }
33410905Sandreas.sandberg@arm.com
33510905Sandreas.sandberg@arm.com    /**
33610905Sandreas.sandberg@arm.com     * @{
33710905Sandreas.sandberg@arm.com     * @name Legacy interface
33810905Sandreas.sandberg@arm.com     *
33910905Sandreas.sandberg@arm.com     * Interface for objects that insist on changing their state when
34010905Sandreas.sandberg@arm.com     * serializing. Such state change should be done in drain(),
34110905Sandreas.sandberg@arm.com     * memWriteback(), or memInvalidate() and not in the serialization
34210905Sandreas.sandberg@arm.com     * method. In general, if state changes occur in serialize, it
34310905Sandreas.sandberg@arm.com     * complicates testing since it breaks assumptions about draining
34410905Sandreas.sandberg@arm.com     * and serialization. It potentially also makes components more
34510905Sandreas.sandberg@arm.com     * fragile since they there are no ordering guarantees when
34610905Sandreas.sandberg@arm.com     * serializing SimObjects.
34710905Sandreas.sandberg@arm.com     *
34810905Sandreas.sandberg@arm.com     * @warn This interface is considered deprecated and should never
34910905Sandreas.sandberg@arm.com     * be used.
35010905Sandreas.sandberg@arm.com     */
35110905Sandreas.sandberg@arm.com
35210905Sandreas.sandberg@arm.com    virtual void serializeOld(CheckpointOut &cp) {
35310905Sandreas.sandberg@arm.com        serialize(cp);
35410905Sandreas.sandberg@arm.com    }
35510905Sandreas.sandberg@arm.com    void serializeSectionOld(CheckpointOut &cp, const char *name);
35610905Sandreas.sandberg@arm.com    void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
35710905Sandreas.sandberg@arm.com        serializeSectionOld(cp, name.c_str());
35810905Sandreas.sandberg@arm.com    }
35910905Sandreas.sandberg@arm.com    /** @} */
36010905Sandreas.sandberg@arm.com
36110905Sandreas.sandberg@arm.com    /** Get the fully-qualified name of the active section */
36210905Sandreas.sandberg@arm.com    static const std::string &currentSection();
36310905Sandreas.sandberg@arm.com
36410905Sandreas.sandberg@arm.com    static Serializable *create(CheckpointIn &cp, const std::string &section);
3652SN/A
3662287SN/A    static int ckptCount;
3672287SN/A    static int ckptMaxCount;
3682287SN/A    static int ckptPrevCount;
3692868Sktlim@umich.edu    static void serializeAll(const std::string &cpt_dir);
37010905Sandreas.sandberg@arm.com    static void unserializeGlobals(CheckpointIn &cp);
37110905Sandreas.sandberg@arm.com
37210905Sandreas.sandberg@arm.com  private:
37310905Sandreas.sandberg@arm.com    static std::stack<std::string> path;
3742SN/A};
3752SN/A
3769554Sandreas.hansson@arm.comvoid debug_serialize(const std::string &cpt_dir);
3779554Sandreas.hansson@arm.com
3782SN/A//
379395SN/A// A SerializableBuilder serves as an evaluation context for a set of
380395SN/A// parameters that describe a specific instance of a Serializable.  This
3812SN/A// evaluation context corresponds to a section in the .ini file (as
3822SN/A// with the base ParamContext) plus an optional node in the
3832SN/A// configuration hierarchy (the configNode member) for resolving
384395SN/A// Serializable references.  SerializableBuilder is an abstract superclass;
3852SN/A// derived classes specialize the class for particular subclasses of
386395SN/A// Serializable (e.g., BaseCache).
3872SN/A//
3882SN/A// For typical usage, see the definition of
389395SN/A// SerializableClass::createObject().
3902SN/A//
391395SN/Aclass SerializableBuilder
3922SN/A{
3932SN/A  public:
3942SN/A
395395SN/A    SerializableBuilder() {}
3962SN/A
397395SN/A    virtual ~SerializableBuilder() {}
3982SN/A
399395SN/A    // Create the actual Serializable corresponding to the parameter
4002SN/A    // values in this context.  This function is overridden in derived
4012SN/A    // classes to call a specific constructor for a particular
402395SN/A    // subclass of Serializable.
403395SN/A    virtual Serializable *create() = 0;
4042SN/A};
4052SN/A
4062SN/A//
407395SN/A// An instance of SerializableClass corresponds to a class derived from
408395SN/A// Serializable.  The SerializableClass instance serves to bind the string
4092SN/A// name (found in the config file) to a function that creates an
4102SN/A// instance of the appropriate derived class.
4112SN/A//
4122SN/A// This would be much cleaner in Smalltalk or Objective-C, where types
4132SN/A// are first-class objects themselves.
4142SN/A//
415395SN/Aclass SerializableClass
4162SN/A{
4172SN/A  public:
4182SN/A
4192SN/A    // Type CreateFunc is a pointer to a function that creates a new
4202SN/A    // simulation object builder based on a .ini-file parameter
4212SN/A    // section (specified by the first string argument), a unique name
4222SN/A    // for the object (specified by the second string argument), and
4232SN/A    // an optional config hierarchy node (specified by the third
424395SN/A    // argument).  A pointer to the new SerializableBuilder is returned.
42510905Sandreas.sandberg@arm.com    typedef Serializable *(*CreateFunc)(CheckpointIn &cp,
4262738Sstever@eecs.umich.edu                                        const std::string &section);
4272SN/A
4282SN/A    static std::map<std::string,CreateFunc> *classMap;
4292SN/A
4302SN/A    // Constructor.  For example:
4312SN/A    //
432395SN/A    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
433395SN/A    //                         newBaseCacheSerializableBuilder);
4342SN/A    //
435395SN/A    SerializableClass(const std::string &className, CreateFunc createFunc);
4362SN/A
437395SN/A    // create Serializable given name of class and pointer to
4382SN/A    // configuration hierarchy node
43910905Sandreas.sandberg@arm.com    static Serializable *createObject(CheckpointIn &cp,
4402738Sstever@eecs.umich.edu                                      const std::string &section);
4412SN/A};
4422SN/A
4432SN/A//
4442SN/A// Macros to encapsulate the magic of declaring & defining
445395SN/A// SerializableBuilder and SerializableClass objects
4462SN/A//
4472SN/A
4485543Ssaidi@eecs.umich.edu#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
4495543Ssaidi@eecs.umich.eduSerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
450237SN/A                                         OBJ_CLASS::createForUnserialize);
4512SN/A
45210453SAndrew.Bardsley@arm.com// Base class to wrap object resolving functionality.  This can be
45310453SAndrew.Bardsley@arm.com// provided to Checkpoint to allow it to map object names onto
45410453SAndrew.Bardsley@arm.com// object C++ objects in which to unserialize
45510453SAndrew.Bardsley@arm.comclass SimObjectResolver
45610453SAndrew.Bardsley@arm.com{
45710453SAndrew.Bardsley@arm.com  public:
45810453SAndrew.Bardsley@arm.com    virtual ~SimObjectResolver() { }
45910453SAndrew.Bardsley@arm.com
46010453SAndrew.Bardsley@arm.com    // Find a SimObject given a full path name
46110453SAndrew.Bardsley@arm.com    virtual SimObject *resolveSimObject(const std::string &name) = 0;
46210453SAndrew.Bardsley@arm.com};
46310453SAndrew.Bardsley@arm.com
46410905Sandreas.sandberg@arm.comclass CheckpointIn
465237SN/A{
466237SN/A  private:
467237SN/A
468237SN/A    IniFile *db;
469237SN/A
47010453SAndrew.Bardsley@arm.com    SimObjectResolver &objNameResolver;
47110453SAndrew.Bardsley@arm.com
472237SN/A  public:
47310905Sandreas.sandberg@arm.com    CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
47410905Sandreas.sandberg@arm.com    ~CheckpointIn();
475237SN/A
476937SN/A    const std::string cptDir;
477937SN/A
478237SN/A    bool find(const std::string &section, const std::string &entry,
479237SN/A              std::string &value);
480237SN/A
481237SN/A    bool findObj(const std::string &section, const std::string &entry,
4824000Ssaidi@eecs.umich.edu                 SimObject *&value);
483304SN/A
484304SN/A    bool sectionExists(const std::string &section);
485449SN/A
486449SN/A    // The following static functions have to do with checkpoint
487449SN/A    // creation rather than restoration.  This class makes a handy
4887491Ssteve.reinhardt@amd.com    // namespace for them though.  Currently no Checkpoint object is
4897491Ssteve.reinhardt@amd.com    // created on serialization (only unserialization) so we track the
4907491Ssteve.reinhardt@amd.com    // directory name as a global.  It would be nice to change this
4917491Ssteve.reinhardt@amd.com    // someday
4927491Ssteve.reinhardt@amd.com
4937491Ssteve.reinhardt@amd.com  private:
4947491Ssteve.reinhardt@amd.com    // current directory we're serializing into.
4957491Ssteve.reinhardt@amd.com    static std::string currentDirectory;
4967491Ssteve.reinhardt@amd.com
4977491Ssteve.reinhardt@amd.com  public:
4987491Ssteve.reinhardt@amd.com    // Set the current directory.  This function takes care of
4997823Ssteve.reinhardt@amd.com    // inserting curTick() if there's a '%d' in the argument, and
5007491Ssteve.reinhardt@amd.com    // appends a '/' if necessary.  The final name is returned.
5017491Ssteve.reinhardt@amd.com    static std::string setDir(const std::string &base_name);
502449SN/A
503449SN/A    // Export current checkpoint directory name so other objects can
504449SN/A    // derive filenames from it (e.g., memory).  The return value is
505449SN/A    // guaranteed to end in '/' so filenames can be directly appended.
506449SN/A    // This function is only valid while a checkpoint is being created.
507449SN/A    static std::string dir();
508449SN/A
509449SN/A    // Filename for base checkpoint file within directory.
510449SN/A    static const char *baseFilename;
511237SN/A};
5122SN/A
5132SN/A#endif // __SERIALIZE_HH__
514