serialize.hh revision 10906:3ab1d7ed6545
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 EventQueue;
68
69typedef std::ostream CheckpointOut;
70
71
72/** The current version of the checkpoint format.
73 * This should be incremented by 1 and only 1 for every new version, where a new
74 * version is defined as a checkpoint created before this version won't work on
75 * the current version until the checkpoint format is updated. Adding a new
76 * SimObject shouldn't cause the version number to increase, only changes to
77 * existing objects such as serializing/unserializing more state, changing sizes
78 * of serialized arrays, etc. */
79static const uint64_t gem5CheckpointVersion = 0x000000000000000e;
80
81template <class T>
82void paramOut(CheckpointOut &cp, const std::string &name, const T &param);
83
84template <typename DataType, typename BitUnion>
85void paramOut(CheckpointOut &cp, const std::string &name,
86              const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
87{
88    paramOut(cp, name, p.__data);
89}
90
91template <class T>
92void paramIn(CheckpointIn &cp, const std::string &name, T &param);
93
94template <typename DataType, typename BitUnion>
95void paramIn(CheckpointIn &cp, const std::string &name,
96             BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
97{
98    paramIn(cp, name, p.__data);
99}
100
101template <class T>
102bool optParamIn(CheckpointIn &cp, const std::string &name, T &param);
103
104template <typename DataType, typename BitUnion>
105bool optParamIn(CheckpointIn &cp, const std::string &name,
106                BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
107{
108    return optParamIn(cp, name, p.__data);
109}
110
111template <class T>
112void arrayParamOut(CheckpointOut &cp, const std::string &name,
113                   const T *param, unsigned size);
114
115template <class T>
116void arrayParamOut(CheckpointOut &cp, const std::string &name,
117                   const std::vector<T> &param);
118
119template <class T>
120void arrayParamOut(CheckpointOut &cp, const std::string &name,
121                   const std::list<T> &param);
122
123template <class T>
124void arrayParamIn(CheckpointIn &cp, const std::string &name,
125                  T *param, unsigned size);
126
127template <class T>
128void arrayParamIn(CheckpointIn &cp, const std::string &name,
129                  std::vector<T> &param);
130
131template <class T>
132void arrayParamIn(CheckpointIn &cp, const std::string &name,
133                  std::list<T> &param);
134
135void
136objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
137
138template <typename T>
139void fromInt(T &t, int i)
140{
141    t = (T)i;
142}
143
144template <typename T>
145void fromSimObject(T &t, SimObject *s)
146{
147    t = dynamic_cast<T>(s);
148}
149
150//
151// These macros are streamlined to use in serialize/unserialize
152// functions.  It's assumed that serialize() has a parameter 'os' for
153// the ostream, and unserialize() has parameters 'cp' and 'section'.
154#define SERIALIZE_SCALAR(scalar)        paramOut(cp, #scalar, scalar)
155
156#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, #scalar, scalar)
157#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, #scalar, scalar)
158
159// ENUMs are like SCALARs, but we cast them to ints on the way out
160#define SERIALIZE_ENUM(scalar)          paramOut(cp, #scalar, (int)scalar)
161
162#define UNSERIALIZE_ENUM(scalar)                \
163 do {                                           \
164    int tmp;                                    \
165    paramIn(cp, #scalar, tmp);                  \
166    fromInt(scalar, tmp);                       \
167  } while (0)
168
169#define SERIALIZE_ARRAY(member, size)           \
170        arrayParamOut(cp, #member, member, size)
171
172#define UNSERIALIZE_ARRAY(member, size)         \
173        arrayParamIn(cp, #member, member, size)
174
175#define SERIALIZE_CONTAINER(member)             \
176        arrayParamOut(cp, #member, member)
177
178#define UNSERIALIZE_CONTAINER(member)           \
179        arrayParamIn(cp, #member, member)
180
181#define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
182
183#define UNSERIALIZE_EVENT(event)                        \
184    do {                                                \
185        event.unserializeSection(cp, #event);           \
186        eventQueue()->checkpointReschedule(&event);     \
187    } while(0)
188
189
190#define SERIALIZE_OBJPTR(objptr)        paramOut(cp, #objptr, (objptr)->name())
191
192#define UNSERIALIZE_OBJPTR(objptr)                      \
193  do {                                                  \
194    SimObject *sptr;                                    \
195    objParamIn(cp, #objptr, sptr);                      \
196    fromSimObject(objptr, sptr);                        \
197  } while (0)
198
199/**
200 * Basic support for object serialization.
201 *
202 * Objects that support serialization should derive from this
203 * class. Such objects can largely be divided into two categories: 1)
204 * True SimObjects (deriving from SimObject), and 2) child objects
205 * (non-SimObjects).
206 *
207 * SimObjects are serialized automatically into their own sections
208 * automatically by the SimObject base class (see
209 * SimObject::serializeAll().
210 *
211 * SimObjects can contain other serializable objects that are not
212 * SimObjects. Much like normal serialized members are not serialized
213 * automatically, these objects will not be serialized automatically
214 * and it is expected that the objects owning such serializable
215 * objects call the required serialization/unserialization methods on
216 * child objects. The preferred method to serialize a child object is
217 * to call serializeSection() on the child, which serializes the
218 * object into a new subsection in the current section. Another option
219 * is to call serialize() directly, which serializes the object into
220 * the current section. The latter is not recommended as it can lead
221 * to naming clashes between objects.
222 *
223 * @note Many objects that support serialization need to be put in a
224 * consistent state when serialization takes place. We refer to the
225 * action of forcing an object into a consistent state as
226 * 'draining'. Objects that need draining inherit from Drainable. See
227 * Drainable for more information.
228 */
229class Serializable
230{
231  protected:
232    /**
233     * Scoped checkpoint section helper class
234     *
235     * This helper class creates a section within a checkpoint without
236     * the need for a separate serializeable object. It is mainly used
237     * within the Serializable class when serializing or unserializing
238     * section (see serializeSection() and unserializeSection()). It
239     * can also be used to maintain backwards compatibility in
240     * existing code that serializes structs that are not inheriting
241     * from Serializable into subsections.
242     *
243     * When the class is instantiated, it appends a name to the active
244     * path in a checkpoint. The old path is later restored when the
245     * instance is destroyed. For example, serializeSection() could be
246     * implemented by instantiating a ScopedCheckpointSection and then
247     * calling serialize() on an object.
248     */
249    class ScopedCheckpointSection {
250      public:
251        template<class CP>
252        ScopedCheckpointSection(CP &cp, const char *name) {
253            pushName(name);
254            nameOut(cp);
255        }
256
257        template<class CP>
258        ScopedCheckpointSection(CP &cp, const std::string &name) {
259            pushName(name.c_str());
260            nameOut(cp);
261        }
262
263        ~ScopedCheckpointSection();
264
265        ScopedCheckpointSection() = delete;
266        ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
267        ScopedCheckpointSection &operator=(
268            const ScopedCheckpointSection &) = delete;
269        ScopedCheckpointSection &operator=(
270            ScopedCheckpointSection &&) = delete;
271
272      private:
273        void pushName(const char *name);
274        void nameOut(CheckpointOut &cp);
275        void nameOut(CheckpointIn &cp) {};
276    };
277
278  public:
279    Serializable();
280    virtual ~Serializable();
281
282    /**
283     * Serialize an object
284     *
285     * Output an object's state into the current checkpoint section.
286     *
287     * @param cp Checkpoint state
288     */
289    virtual void serialize(CheckpointOut &cp) const = 0;
290
291    /**
292     * Unserialize an object
293     *
294     * Read an object's state from the current checkpoint section.
295     *
296     * @param cp Checkpoint state
297     */
298    virtual void unserialize(CheckpointIn &cp) = 0;
299
300    /**
301     * Serialize an object into a new section
302     *
303     * This method creates a new section in a checkpoint and calls
304     * serialize() to serialize the current object into that
305     * section. The name of the section is appended to the current
306     * checkpoint path.
307     *
308     * @param cp Checkpoint state
309     * @param name Name to append to the active path
310     */
311    void serializeSection(CheckpointOut &cp, const char *name) const;
312
313    void serializeSection(CheckpointOut &cp, const std::string &name) const {
314        serializeSection(cp, name.c_str());
315    }
316
317    /**
318     * Unserialize an a child object
319     *
320     * This method loads a child object from a checkpoint. The object
321     * name is appended to the active path to form a fully qualified
322     * section name and unserialize() is called.
323     *
324     * @param cp Checkpoint state
325     * @param name Name to append to the active path
326     */
327    void unserializeSection(CheckpointIn &cp, const char *name);
328
329    void unserializeSection(CheckpointIn &cp, const std::string &name) {
330        unserializeSection(cp, name.c_str());
331    }
332
333    /**
334     * @{
335     * @name Legacy interface
336     *
337     * Interface for objects that insist on changing their state when
338     * serializing. Such state change should be done in drain(),
339     * memWriteback(), or memInvalidate() and not in the serialization
340     * method. In general, if state changes occur in serialize, it
341     * complicates testing since it breaks assumptions about draining
342     * and serialization. It potentially also makes components more
343     * fragile since they there are no ordering guarantees when
344     * serializing SimObjects.
345     *
346     * @warn This interface is considered deprecated and should never
347     * be used.
348     */
349
350    virtual void serializeOld(CheckpointOut &cp) {
351        serialize(cp);
352    }
353    void serializeSectionOld(CheckpointOut &cp, const char *name);
354    void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
355        serializeSectionOld(cp, name.c_str());
356    }
357    /** @} */
358
359    /** Get the fully-qualified name of the active section */
360    static const std::string &currentSection();
361
362    static Serializable *create(CheckpointIn &cp, const std::string &section);
363
364    static int ckptCount;
365    static int ckptMaxCount;
366    static int ckptPrevCount;
367    static void serializeAll(const std::string &cpt_dir);
368    static void unserializeGlobals(CheckpointIn &cp);
369
370  private:
371    static std::stack<std::string> path;
372};
373
374void debug_serialize(const std::string &cpt_dir);
375
376//
377// A SerializableBuilder serves as an evaluation context for a set of
378// parameters that describe a specific instance of a Serializable.  This
379// evaluation context corresponds to a section in the .ini file (as
380// with the base ParamContext) plus an optional node in the
381// configuration hierarchy (the configNode member) for resolving
382// Serializable references.  SerializableBuilder is an abstract superclass;
383// derived classes specialize the class for particular subclasses of
384// Serializable (e.g., BaseCache).
385//
386// For typical usage, see the definition of
387// SerializableClass::createObject().
388//
389class SerializableBuilder
390{
391  public:
392
393    SerializableBuilder() {}
394
395    virtual ~SerializableBuilder() {}
396
397    // Create the actual Serializable corresponding to the parameter
398    // values in this context.  This function is overridden in derived
399    // classes to call a specific constructor for a particular
400    // subclass of Serializable.
401    virtual Serializable *create() = 0;
402};
403
404//
405// An instance of SerializableClass corresponds to a class derived from
406// Serializable.  The SerializableClass instance serves to bind the string
407// name (found in the config file) to a function that creates an
408// instance of the appropriate derived class.
409//
410// This would be much cleaner in Smalltalk or Objective-C, where types
411// are first-class objects themselves.
412//
413class SerializableClass
414{
415  public:
416
417    // Type CreateFunc is a pointer to a function that creates a new
418    // simulation object builder based on a .ini-file parameter
419    // section (specified by the first string argument), a unique name
420    // for the object (specified by the second string argument), and
421    // an optional config hierarchy node (specified by the third
422    // argument).  A pointer to the new SerializableBuilder is returned.
423    typedef Serializable *(*CreateFunc)(CheckpointIn &cp,
424                                        const std::string &section);
425
426    static std::map<std::string,CreateFunc> *classMap;
427
428    // Constructor.  For example:
429    //
430    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
431    //                         newBaseCacheSerializableBuilder);
432    //
433    SerializableClass(const std::string &className, CreateFunc createFunc);
434
435    // create Serializable given name of class and pointer to
436    // configuration hierarchy node
437    static Serializable *createObject(CheckpointIn &cp,
438                                      const std::string &section);
439};
440
441//
442// Macros to encapsulate the magic of declaring & defining
443// SerializableBuilder and SerializableClass objects
444//
445
446#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
447SerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
448                                         OBJ_CLASS::createForUnserialize);
449
450// Base class to wrap object resolving functionality.  This can be
451// provided to Checkpoint to allow it to map object names onto
452// object C++ objects in which to unserialize
453class SimObjectResolver
454{
455  public:
456    virtual ~SimObjectResolver() { }
457
458    // Find a SimObject given a full path name
459    virtual SimObject *resolveSimObject(const std::string &name) = 0;
460};
461
462class CheckpointIn
463{
464  private:
465
466    IniFile *db;
467
468    SimObjectResolver &objNameResolver;
469
470  public:
471    CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
472    ~CheckpointIn();
473
474    const std::string cptDir;
475
476    bool find(const std::string &section, const std::string &entry,
477              std::string &value);
478
479    bool findObj(const std::string &section, const std::string &entry,
480                 SimObject *&value);
481
482    bool sectionExists(const std::string &section);
483
484    // The following static functions have to do with checkpoint
485    // creation rather than restoration.  This class makes a handy
486    // namespace for them though.  Currently no Checkpoint object is
487    // created on serialization (only unserialization) so we track the
488    // directory name as a global.  It would be nice to change this
489    // someday
490
491  private:
492    // current directory we're serializing into.
493    static std::string currentDirectory;
494
495  public:
496    // Set the current directory.  This function takes care of
497    // inserting curTick() if there's a '%d' in the argument, and
498    // appends a '/' if necessary.  The final name is returned.
499    static std::string setDir(const std::string &base_name);
500
501    // Export current checkpoint directory name so other objects can
502    // derive filenames from it (e.g., memory).  The return value is
503    // guaranteed to end in '/' so filenames can be directly appended.
504    // This function is only valid while a checkpoint is being created.
505    static std::string dir();
506
507    // Filename for base checkpoint file within directory.
508    static const char *baseFilename;
509};
510
511#endif // __SERIALIZE_HH__
512