serialize.hh revision 10908:235d75ea01d8
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#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
190#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
191
192#define SERIALIZE_OBJPTR(objptr)        paramOut(cp, #objptr, (objptr)->name())
193
194#define UNSERIALIZE_OBJPTR(objptr)                      \
195  do {                                                  \
196    SimObject *sptr;                                    \
197    objParamIn(cp, #objptr, sptr);                      \
198    fromSimObject(objptr, sptr);                        \
199  } while (0)
200
201/**
202 * Basic support for object serialization.
203 *
204 * Objects that support serialization should derive from this
205 * class. Such objects can largely be divided into two categories: 1)
206 * True SimObjects (deriving from SimObject), and 2) child objects
207 * (non-SimObjects).
208 *
209 * SimObjects are serialized automatically into their own sections
210 * automatically by the SimObject base class (see
211 * SimObject::serializeAll().
212 *
213 * SimObjects can contain other serializable objects that are not
214 * SimObjects. Much like normal serialized members are not serialized
215 * automatically, these objects will not be serialized automatically
216 * and it is expected that the objects owning such serializable
217 * objects call the required serialization/unserialization methods on
218 * child objects. The preferred method to serialize a child object is
219 * to call serializeSection() on the child, which serializes the
220 * object into a new subsection in the current section. Another option
221 * is to call serialize() directly, which serializes the object into
222 * the current section. The latter is not recommended as it can lead
223 * to naming clashes between objects.
224 *
225 * @note Many objects that support serialization need to be put in a
226 * consistent state when serialization takes place. We refer to the
227 * action of forcing an object into a consistent state as
228 * 'draining'. Objects that need draining inherit from Drainable. See
229 * Drainable for more information.
230 */
231class Serializable
232{
233  protected:
234    /**
235     * Scoped checkpoint section helper class
236     *
237     * This helper class creates a section within a checkpoint without
238     * the need for a separate serializeable object. It is mainly used
239     * within the Serializable class when serializing or unserializing
240     * section (see serializeSection() and unserializeSection()). It
241     * can also be used to maintain backwards compatibility in
242     * existing code that serializes structs that are not inheriting
243     * from Serializable into subsections.
244     *
245     * When the class is instantiated, it appends a name to the active
246     * path in a checkpoint. The old path is later restored when the
247     * instance is destroyed. For example, serializeSection() could be
248     * implemented by instantiating a ScopedCheckpointSection and then
249     * calling serialize() on an object.
250     */
251    class ScopedCheckpointSection {
252      public:
253        template<class CP>
254        ScopedCheckpointSection(CP &cp, const char *name) {
255            pushName(name);
256            nameOut(cp);
257        }
258
259        template<class CP>
260        ScopedCheckpointSection(CP &cp, const std::string &name) {
261            pushName(name.c_str());
262            nameOut(cp);
263        }
264
265        ~ScopedCheckpointSection();
266
267        ScopedCheckpointSection() = delete;
268        ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
269        ScopedCheckpointSection &operator=(
270            const ScopedCheckpointSection &) = delete;
271        ScopedCheckpointSection &operator=(
272            ScopedCheckpointSection &&) = delete;
273
274      private:
275        void pushName(const char *name);
276        void nameOut(CheckpointOut &cp);
277        void nameOut(CheckpointIn &cp) {};
278    };
279
280  public:
281    Serializable();
282    virtual ~Serializable();
283
284    /**
285     * Serialize an object
286     *
287     * Output an object's state into the current checkpoint section.
288     *
289     * @param cp Checkpoint state
290     */
291    virtual void serialize(CheckpointOut &cp) const = 0;
292
293    /**
294     * Unserialize an object
295     *
296     * Read an object's state from the current checkpoint section.
297     *
298     * @param cp Checkpoint state
299     */
300    virtual void unserialize(CheckpointIn &cp) = 0;
301
302    /**
303     * Serialize an object into a new section
304     *
305     * This method creates a new section in a checkpoint and calls
306     * serialize() to serialize the current object into that
307     * section. The name of the section is appended to the current
308     * checkpoint path.
309     *
310     * @param cp Checkpoint state
311     * @param name Name to append to the active path
312     */
313    void serializeSection(CheckpointOut &cp, const char *name) const;
314
315    void serializeSection(CheckpointOut &cp, const std::string &name) const {
316        serializeSection(cp, name.c_str());
317    }
318
319    /**
320     * Unserialize an a child object
321     *
322     * This method loads a child object from a checkpoint. The object
323     * name is appended to the active path to form a fully qualified
324     * section name and unserialize() is called.
325     *
326     * @param cp Checkpoint state
327     * @param name Name to append to the active path
328     */
329    void unserializeSection(CheckpointIn &cp, const char *name);
330
331    void unserializeSection(CheckpointIn &cp, const std::string &name) {
332        unserializeSection(cp, name.c_str());
333    }
334
335    /**
336     * @{
337     * @name Legacy interface
338     *
339     * Interface for objects that insist on changing their state when
340     * serializing. Such state change should be done in drain(),
341     * memWriteback(), or memInvalidate() and not in the serialization
342     * method. In general, if state changes occur in serialize, it
343     * complicates testing since it breaks assumptions about draining
344     * and serialization. It potentially also makes components more
345     * fragile since they there are no ordering guarantees when
346     * serializing SimObjects.
347     *
348     * @warn This interface is considered deprecated and should never
349     * be used.
350     */
351
352    virtual void serializeOld(CheckpointOut &cp) {
353        serialize(cp);
354    }
355    void serializeSectionOld(CheckpointOut &cp, const char *name);
356    void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
357        serializeSectionOld(cp, name.c_str());
358    }
359    /** @} */
360
361    /** Get the fully-qualified name of the active section */
362    static const std::string &currentSection();
363
364    static Serializable *create(CheckpointIn &cp, const std::string &section);
365
366    static int ckptCount;
367    static int ckptMaxCount;
368    static int ckptPrevCount;
369    static void serializeAll(const std::string &cpt_dir);
370    static void unserializeGlobals(CheckpointIn &cp);
371
372  private:
373    static std::stack<std::string> path;
374};
375
376void debug_serialize(const std::string &cpt_dir);
377
378//
379// A SerializableBuilder serves as an evaluation context for a set of
380// parameters that describe a specific instance of a Serializable.  This
381// evaluation context corresponds to a section in the .ini file (as
382// with the base ParamContext) plus an optional node in the
383// configuration hierarchy (the configNode member) for resolving
384// Serializable references.  SerializableBuilder is an abstract superclass;
385// derived classes specialize the class for particular subclasses of
386// Serializable (e.g., BaseCache).
387//
388// For typical usage, see the definition of
389// SerializableClass::createObject().
390//
391class SerializableBuilder
392{
393  public:
394
395    SerializableBuilder() {}
396
397    virtual ~SerializableBuilder() {}
398
399    // Create the actual Serializable corresponding to the parameter
400    // values in this context.  This function is overridden in derived
401    // classes to call a specific constructor for a particular
402    // subclass of Serializable.
403    virtual Serializable *create() = 0;
404};
405
406//
407// An instance of SerializableClass corresponds to a class derived from
408// Serializable.  The SerializableClass instance serves to bind the string
409// name (found in the config file) to a function that creates an
410// instance of the appropriate derived class.
411//
412// This would be much cleaner in Smalltalk or Objective-C, where types
413// are first-class objects themselves.
414//
415class SerializableClass
416{
417  public:
418
419    // Type CreateFunc is a pointer to a function that creates a new
420    // simulation object builder based on a .ini-file parameter
421    // section (specified by the first string argument), a unique name
422    // for the object (specified by the second string argument), and
423    // an optional config hierarchy node (specified by the third
424    // argument).  A pointer to the new SerializableBuilder is returned.
425    typedef Serializable *(*CreateFunc)(CheckpointIn &cp,
426                                        const std::string &section);
427
428    static std::map<std::string,CreateFunc> *classMap;
429
430    // Constructor.  For example:
431    //
432    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
433    //                         newBaseCacheSerializableBuilder);
434    //
435    SerializableClass(const std::string &className, CreateFunc createFunc);
436
437    // create Serializable given name of class and pointer to
438    // configuration hierarchy node
439    static Serializable *createObject(CheckpointIn &cp,
440                                      const std::string &section);
441};
442
443//
444// Macros to encapsulate the magic of declaring & defining
445// SerializableBuilder and SerializableClass objects
446//
447
448#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
449SerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
450                                         OBJ_CLASS::createForUnserialize);
451
452// Base class to wrap object resolving functionality.  This can be
453// provided to Checkpoint to allow it to map object names onto
454// object C++ objects in which to unserialize
455class SimObjectResolver
456{
457  public:
458    virtual ~SimObjectResolver() { }
459
460    // Find a SimObject given a full path name
461    virtual SimObject *resolveSimObject(const std::string &name) = 0;
462};
463
464class CheckpointIn
465{
466  private:
467
468    IniFile *db;
469
470    SimObjectResolver &objNameResolver;
471
472  public:
473    CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
474    ~CheckpointIn();
475
476    const std::string cptDir;
477
478    bool find(const std::string &section, const std::string &entry,
479              std::string &value);
480
481    bool findObj(const std::string &section, const std::string &entry,
482                 SimObject *&value);
483
484    bool sectionExists(const std::string &section);
485
486    // The following static functions have to do with checkpoint
487    // creation rather than restoration.  This class makes a handy
488    // namespace for them though.  Currently no Checkpoint object is
489    // created on serialization (only unserialization) so we track the
490    // directory name as a global.  It would be nice to change this
491    // someday
492
493  private:
494    // current directory we're serializing into.
495    static std::string currentDirectory;
496
497  public:
498    // Set the current directory.  This function takes care of
499    // inserting curTick() if there's a '%d' in the argument, and
500    // appends a '/' if necessary.  The final name is returned.
501    static std::string setDir(const std::string &base_name);
502
503    // Export current checkpoint directory name so other objects can
504    // derive filenames from it (e.g., memory).  The return value is
505    // guaranteed to end in '/' so filenames can be directly appended.
506    // This function is only valid while a checkpoint is being created.
507    static std::string dir();
508
509    // Filename for base checkpoint file within directory.
510    static const char *baseFilename;
511};
512
513#endif // __SERIALIZE_HH__
514