serialize.hh revision 11067:5379f099e488
1/*
2 * Copyright (c) 2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 *          Erik Hallnor
42 *          Steve Reinhardt
43 *          Andreas Sandberg
44 */
45
46/* @file
47 * Serialization Interface Declarations
48 */
49
50#ifndef __SERIALIZE_HH__
51#define __SERIALIZE_HH__
52
53
54#include <iostream>
55#include <list>
56#include <map>
57#include <stack>
58#include <vector>
59
60#include "base/bitunion.hh"
61#include "base/types.hh"
62
63class IniFile;
64class Serializable;
65class CheckpointIn;
66class SimObject;
67class SimObjectResolver;
68class EventQueue;
69
70typedef std::ostream CheckpointOut;
71
72
73/** The current version of the checkpoint format.
74 * This should be incremented by 1 and only 1 for every new version, where a new
75 * version is defined as a checkpoint created before this version won't work on
76 * the current version until the checkpoint format is updated. Adding a new
77 * SimObject shouldn't cause the version number to increase, only changes to
78 * existing objects such as serializing/unserializing more state, changing sizes
79 * of serialized arrays, etc. */
80static const uint64_t gem5CheckpointVersion = 0x000000000000000f;
81
82template <class T>
83void paramOut(CheckpointOut &cp, const std::string &name, const T &param);
84
85template <typename DataType, typename BitUnion>
86void paramOut(CheckpointOut &cp, const std::string &name,
87              const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
88{
89    paramOut(cp, name, p.__data);
90}
91
92template <class T>
93void paramIn(CheckpointIn &cp, const std::string &name, T &param);
94
95template <typename DataType, typename BitUnion>
96void paramIn(CheckpointIn &cp, const std::string &name,
97             BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
98{
99    paramIn(cp, name, p.__data);
100}
101
102template <class T>
103bool optParamIn(CheckpointIn &cp, const std::string &name, T &param);
104
105template <typename DataType, typename BitUnion>
106bool optParamIn(CheckpointIn &cp, const std::string &name,
107                BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
108{
109    return optParamIn(cp, name, p.__data);
110}
111
112template <class T>
113void arrayParamOut(CheckpointOut &cp, const std::string &name,
114                   const T *param, unsigned size);
115
116template <class T>
117void arrayParamOut(CheckpointOut &cp, const std::string &name,
118                   const std::vector<T> &param);
119
120template <class T>
121void arrayParamOut(CheckpointOut &cp, const std::string &name,
122                   const std::list<T> &param);
123
124template <class T>
125void arrayParamIn(CheckpointIn &cp, const std::string &name,
126                  T *param, unsigned size);
127
128template <class T>
129void arrayParamIn(CheckpointIn &cp, const std::string &name,
130                  std::vector<T> &param);
131
132template <class T>
133void arrayParamIn(CheckpointIn &cp, const std::string &name,
134                  std::list<T> &param);
135
136void
137objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
138
139template <typename T>
140void fromInt(T &t, int i)
141{
142    t = (T)i;
143}
144
145template <typename T>
146void fromSimObject(T &t, SimObject *s)
147{
148    t = dynamic_cast<T>(s);
149}
150
151//
152// These macros are streamlined to use in serialize/unserialize
153// functions.  It's assumed that serialize() has a parameter 'os' for
154// the ostream, and unserialize() has parameters 'cp' and 'section'.
155#define SERIALIZE_SCALAR(scalar)        paramOut(cp, #scalar, scalar)
156
157#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, #scalar, scalar)
158#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, #scalar, scalar)
159
160// ENUMs are like SCALARs, but we cast them to ints on the way out
161#define SERIALIZE_ENUM(scalar)          paramOut(cp, #scalar, (int)scalar)
162
163#define UNSERIALIZE_ENUM(scalar)                \
164 do {                                           \
165    int tmp;                                    \
166    paramIn(cp, #scalar, tmp);                  \
167    fromInt(scalar, tmp);                       \
168  } while (0)
169
170#define SERIALIZE_ARRAY(member, size)           \
171        arrayParamOut(cp, #member, member, size)
172
173#define UNSERIALIZE_ARRAY(member, size)         \
174        arrayParamIn(cp, #member, member, size)
175
176#define SERIALIZE_CONTAINER(member)             \
177        arrayParamOut(cp, #member, member)
178
179#define UNSERIALIZE_CONTAINER(member)           \
180        arrayParamIn(cp, #member, member)
181
182#define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
183
184#define UNSERIALIZE_EVENT(event)                        \
185    do {                                                \
186        event.unserializeSection(cp, #event);           \
187        eventQueue()->checkpointReschedule(&event);     \
188    } while(0)
189
190#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
191#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
192
193#define SERIALIZE_OBJPTR(objptr)        paramOut(cp, #objptr, (objptr)->name())
194
195#define UNSERIALIZE_OBJPTR(objptr)                      \
196  do {                                                  \
197    SimObject *sptr;                                    \
198    objParamIn(cp, #objptr, sptr);                      \
199    fromSimObject(objptr, sptr);                        \
200  } while (0)
201
202/**
203 * Basic support for object serialization.
204 *
205 * Objects that support serialization should derive from this
206 * class. Such objects can largely be divided into two categories: 1)
207 * True SimObjects (deriving from SimObject), and 2) child objects
208 * (non-SimObjects).
209 *
210 * SimObjects are serialized automatically into their own sections
211 * automatically by the SimObject base class (see
212 * SimObject::serializeAll().
213 *
214 * SimObjects can contain other serializable objects that are not
215 * SimObjects. Much like normal serialized members are not serialized
216 * automatically, these objects will not be serialized automatically
217 * and it is expected that the objects owning such serializable
218 * objects call the required serialization/unserialization methods on
219 * child objects. The preferred method to serialize a child object is
220 * to call serializeSection() on the child, which serializes the
221 * object into a new subsection in the current section. Another option
222 * is to call serialize() directly, which serializes the object into
223 * the current section. The latter is not recommended as it can lead
224 * to naming clashes between objects.
225 *
226 * @note Many objects that support serialization need to be put in a
227 * consistent state when serialization takes place. We refer to the
228 * action of forcing an object into a consistent state as
229 * 'draining'. Objects that need draining inherit from Drainable. See
230 * Drainable for more information.
231 */
232class Serializable
233{
234  protected:
235    /**
236     * Scoped checkpoint section helper class
237     *
238     * This helper class creates a section within a checkpoint without
239     * the need for a separate serializeable object. It is mainly used
240     * within the Serializable class when serializing or unserializing
241     * section (see serializeSection() and unserializeSection()). It
242     * can also be used to maintain backwards compatibility in
243     * existing code that serializes structs that are not inheriting
244     * from Serializable into subsections.
245     *
246     * When the class is instantiated, it appends a name to the active
247     * path in a checkpoint. The old path is later restored when the
248     * instance is destroyed. For example, serializeSection() could be
249     * implemented by instantiating a ScopedCheckpointSection and then
250     * calling serialize() on an object.
251     */
252    class ScopedCheckpointSection {
253      public:
254        template<class CP>
255        ScopedCheckpointSection(CP &cp, const char *name) {
256            pushName(name);
257            nameOut(cp);
258        }
259
260        template<class CP>
261        ScopedCheckpointSection(CP &cp, const std::string &name) {
262            pushName(name.c_str());
263            nameOut(cp);
264        }
265
266        ~ScopedCheckpointSection();
267
268        ScopedCheckpointSection() = delete;
269        ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
270        ScopedCheckpointSection &operator=(
271            const ScopedCheckpointSection &) = delete;
272        ScopedCheckpointSection &operator=(
273            ScopedCheckpointSection &&) = delete;
274
275      private:
276        void pushName(const char *name);
277        void nameOut(CheckpointOut &cp);
278        void nameOut(CheckpointIn &cp) {};
279    };
280
281  public:
282    Serializable();
283    virtual ~Serializable();
284
285    /**
286     * Serialize an object
287     *
288     * Output an object's state into the current checkpoint section.
289     *
290     * @param cp Checkpoint state
291     */
292    virtual void serialize(CheckpointOut &cp) const = 0;
293
294    /**
295     * Unserialize an object
296     *
297     * Read an object's state from the current checkpoint section.
298     *
299     * @param cp Checkpoint state
300     */
301    virtual void unserialize(CheckpointIn &cp) = 0;
302
303    /**
304     * Serialize an object into a new section
305     *
306     * This method creates a new section in a checkpoint and calls
307     * serialize() to serialize the current object into that
308     * section. The name of the section is appended to the current
309     * checkpoint path.
310     *
311     * @param cp Checkpoint state
312     * @param name Name to append to the active path
313     */
314    void serializeSection(CheckpointOut &cp, const char *name) const;
315
316    void serializeSection(CheckpointOut &cp, const std::string &name) const {
317        serializeSection(cp, name.c_str());
318    }
319
320    /**
321     * Unserialize an a child object
322     *
323     * This method loads a child object from a checkpoint. The object
324     * name is appended to the active path to form a fully qualified
325     * section name and unserialize() is called.
326     *
327     * @param cp Checkpoint state
328     * @param name Name to append to the active path
329     */
330    void unserializeSection(CheckpointIn &cp, const char *name);
331
332    void unserializeSection(CheckpointIn &cp, const std::string &name) {
333        unserializeSection(cp, name.c_str());
334    }
335
336    /**
337     * @{
338     * @name Legacy interface
339     *
340     * Interface for objects that insist on changing their state when
341     * serializing. Such state change should be done in drain(),
342     * memWriteback(), or memInvalidate() and not in the serialization
343     * method. In general, if state changes occur in serialize, it
344     * complicates testing since it breaks assumptions about draining
345     * and serialization. It potentially also makes components more
346     * fragile since they there are no ordering guarantees when
347     * serializing SimObjects.
348     *
349     * @warn This interface is considered deprecated and should never
350     * be used.
351     */
352
353    virtual void serializeOld(CheckpointOut &cp) {
354        serialize(cp);
355    }
356    void serializeSectionOld(CheckpointOut &cp, const char *name);
357    void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
358        serializeSectionOld(cp, name.c_str());
359    }
360    /** @} */
361
362    /** Get the fully-qualified name of the active section */
363    static const std::string &currentSection();
364
365    static Serializable *create(CheckpointIn &cp, const std::string &section);
366
367    static int ckptCount;
368    static int ckptMaxCount;
369    static int ckptPrevCount;
370    static void serializeAll(const std::string &cpt_dir);
371    static void unserializeGlobals(CheckpointIn &cp);
372
373  private:
374    static std::stack<std::string> path;
375};
376
377void debug_serialize(const std::string &cpt_dir);
378
379//
380// A SerializableBuilder serves as an evaluation context for a set of
381// parameters that describe a specific instance of a Serializable.  This
382// evaluation context corresponds to a section in the .ini file (as
383// with the base ParamContext) plus an optional node in the
384// configuration hierarchy (the configNode member) for resolving
385// Serializable references.  SerializableBuilder is an abstract superclass;
386// derived classes specialize the class for particular subclasses of
387// Serializable (e.g., BaseCache).
388//
389// For typical usage, see the definition of
390// SerializableClass::createObject().
391//
392class SerializableBuilder
393{
394  public:
395
396    SerializableBuilder() {}
397
398    virtual ~SerializableBuilder() {}
399
400    // Create the actual Serializable corresponding to the parameter
401    // values in this context.  This function is overridden in derived
402    // classes to call a specific constructor for a particular
403    // subclass of Serializable.
404    virtual Serializable *create() = 0;
405};
406
407//
408// An instance of SerializableClass corresponds to a class derived from
409// Serializable.  The SerializableClass instance serves to bind the string
410// name (found in the config file) to a function that creates an
411// instance of the appropriate derived class.
412//
413// This would be much cleaner in Smalltalk or Objective-C, where types
414// are first-class objects themselves.
415//
416class SerializableClass
417{
418  public:
419
420    // Type CreateFunc is a pointer to a function that creates a new
421    // simulation object builder based on a .ini-file parameter
422    // section (specified by the first string argument), a unique name
423    // for the object (specified by the second string argument), and
424    // an optional config hierarchy node (specified by the third
425    // argument).  A pointer to the new SerializableBuilder is returned.
426    typedef Serializable *(*CreateFunc)(CheckpointIn &cp,
427                                        const std::string &section);
428
429    static std::map<std::string,CreateFunc> *classMap;
430
431    // Constructor.  For example:
432    //
433    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
434    //                         newBaseCacheSerializableBuilder);
435    //
436    SerializableClass(const std::string &className, CreateFunc createFunc);
437
438    // create Serializable given name of class and pointer to
439    // configuration hierarchy node
440    static Serializable *createObject(CheckpointIn &cp,
441                                      const std::string &section);
442};
443
444//
445// Macros to encapsulate the magic of declaring & defining
446// SerializableBuilder and SerializableClass objects
447//
448
449#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
450SerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
451                                         OBJ_CLASS::createForUnserialize);
452
453
454class CheckpointIn
455{
456  private:
457
458    IniFile *db;
459
460    SimObjectResolver &objNameResolver;
461
462  public:
463    CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
464    ~CheckpointIn();
465
466    const std::string cptDir;
467
468    bool find(const std::string &section, const std::string &entry,
469              std::string &value);
470
471    bool findObj(const std::string &section, const std::string &entry,
472                 SimObject *&value);
473
474    bool sectionExists(const std::string &section);
475
476    // The following static functions have to do with checkpoint
477    // creation rather than restoration.  This class makes a handy
478    // namespace for them though.  Currently no Checkpoint object is
479    // created on serialization (only unserialization) so we track the
480    // directory name as a global.  It would be nice to change this
481    // someday
482
483  private:
484    // current directory we're serializing into.
485    static std::string currentDirectory;
486
487  public:
488    // Set the current directory.  This function takes care of
489    // inserting curTick() if there's a '%d' in the argument, and
490    // appends a '/' if necessary.  The final name is returned.
491    static std::string setDir(const std::string &base_name);
492
493    // Export current checkpoint directory name so other objects can
494    // derive filenames from it (e.g., memory).  The return value is
495    // guaranteed to end in '/' so filenames can be directly appended.
496    // This function is only valid while a checkpoint is being created.
497    static std::string dir();
498
499    // Filename for base checkpoint file within directory.
500    static const char *baseFilename;
501};
502
503#endif // __SERIALIZE_HH__
504