serialize.hh revision 11076:463a4b0f0dda
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 <set>
59#include <vector>
60
61#include "base/bitunion.hh"
62#include "base/types.hh"
63
64class IniFile;
65class Serializable;
66class CheckpointIn;
67class SimObject;
68class SimObjectResolver;
69class EventQueue;
70
71typedef std::ostream CheckpointOut;
72
73
74/** The current version of the checkpoint format.
75 * This should be incremented by 1 and only 1 for every new version, where a new
76 * version is defined as a checkpoint created before this version won't work on
77 * the current version until the checkpoint format is updated. Adding a new
78 * SimObject shouldn't cause the version number to increase, only changes to
79 * existing objects such as serializing/unserializing more state, changing sizes
80 * of serialized arrays, etc. */
81static const uint64_t gem5CheckpointVersion = 0x000000000000000f;
82
83template <class T>
84void paramOut(CheckpointOut &cp, const std::string &name, const T &param);
85
86template <typename DataType, typename BitUnion>
87void paramOut(CheckpointOut &cp, const std::string &name,
88              const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
89{
90    paramOut(cp, name, p.__data);
91}
92
93template <class T>
94void paramIn(CheckpointIn &cp, const std::string &name, T &param);
95
96template <typename DataType, typename BitUnion>
97void paramIn(CheckpointIn &cp, const std::string &name,
98             BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
99{
100    paramIn(cp, name, p.__data);
101}
102
103template <class T>
104bool optParamIn(CheckpointIn &cp, const std::string &name, T &param,
105                bool warn = true);
106
107template <typename DataType, typename BitUnion>
108bool optParamIn(CheckpointIn &cp, const std::string &name,
109                BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p,
110                bool warn = true)
111{
112    return optParamIn(cp, name, p.__data, warn);
113}
114
115template <class T>
116void arrayParamOut(CheckpointOut &cp, const std::string &name,
117                   const T *param, unsigned size);
118
119template <class T>
120void arrayParamOut(CheckpointOut &cp, const std::string &name,
121                   const std::vector<T> &param);
122
123template <class T>
124void arrayParamOut(CheckpointOut &cp, const std::string &name,
125                   const std::list<T> &param);
126
127template <class T>
128void arrayParamOut(CheckpointOut &cp, const std::string &name,
129                   const std::set<T> &param);
130
131template <class T>
132void arrayParamIn(CheckpointIn &cp, const std::string &name,
133                  T *param, unsigned size);
134
135template <class T>
136void arrayParamIn(CheckpointIn &cp, const std::string &name,
137                  std::vector<T> &param);
138
139template <class T>
140void arrayParamIn(CheckpointIn &cp, const std::string &name,
141                  std::list<T> &param);
142
143template <class T>
144void arrayParamIn(CheckpointIn &cp, const std::string &name,
145                  std::set<T> &param);
146
147void
148objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
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        scalar = static_cast<decltype(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        objptr = dynamic_cast<decltype(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 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
377class CheckpointIn
378{
379  private:
380
381    IniFile *db;
382
383    SimObjectResolver &objNameResolver;
384
385  public:
386    CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
387    ~CheckpointIn();
388
389    const std::string cptDir;
390
391    bool find(const std::string &section, const std::string &entry,
392              std::string &value);
393
394    bool findObj(const std::string &section, const std::string &entry,
395                 SimObject *&value);
396
397    bool sectionExists(const std::string &section);
398
399    // The following static functions have to do with checkpoint
400    // creation rather than restoration.  This class makes a handy
401    // namespace for them though.  Currently no Checkpoint object is
402    // created on serialization (only unserialization) so we track the
403    // directory name as a global.  It would be nice to change this
404    // someday
405
406  private:
407    // current directory we're serializing into.
408    static std::string currentDirectory;
409
410  public:
411    // Set the current directory.  This function takes care of
412    // inserting curTick() if there's a '%d' in the argument, and
413    // appends a '/' if necessary.  The final name is returned.
414    static std::string setDir(const std::string &base_name);
415
416    // Export current checkpoint directory name so other objects can
417    // derive filenames from it (e.g., memory).  The return value is
418    // guaranteed to end in '/' so filenames can be directly appended.
419    // This function is only valid while a checkpoint is being created.
420    static std::string dir();
421
422    // Filename for base checkpoint file within directory.
423    static const char *baseFilename;
424};
425
426#endif // __SERIALIZE_HH__
427