serialize.hh revision 11075
12914Ssaidi@eecs.umich.edu/*
22914Ssaidi@eecs.umich.edu * Copyright (c) 2015 ARM Limited
32914Ssaidi@eecs.umich.edu * All rights reserved
42914Ssaidi@eecs.umich.edu *
52914Ssaidi@eecs.umich.edu * The license below extends only to copyright in the software and shall
62914Ssaidi@eecs.umich.edu * not be construed as granting a license to any other intellectual
72914Ssaidi@eecs.umich.edu * property including but not limited to intellectual property relating
82914Ssaidi@eecs.umich.edu * to a hardware implementation of the functionality of the software
92914Ssaidi@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
102914Ssaidi@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
112914Ssaidi@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
122914Ssaidi@eecs.umich.edu * modified or unmodified, in source code or in binary form.
132914Ssaidi@eecs.umich.edu *
142914Ssaidi@eecs.umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
152914Ssaidi@eecs.umich.edu * All rights reserved.
162914Ssaidi@eecs.umich.edu *
172914Ssaidi@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
182914Ssaidi@eecs.umich.edu * modification, are permitted provided that the following conditions are
192914Ssaidi@eecs.umich.edu * met: redistributions of source code must retain the above copyright
202914Ssaidi@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
212914Ssaidi@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
222914Ssaidi@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
232914Ssaidi@eecs.umich.edu * documentation and/or other materials provided with the distribution;
242914Ssaidi@eecs.umich.edu * neither the name of the copyright holders nor the names of its
252914Ssaidi@eecs.umich.edu * contributors may be used to endorse or promote products derived from
262914Ssaidi@eecs.umich.edu * this software without specific prior written permission.
272914Ssaidi@eecs.umich.edu *
282914Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292914Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302914Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312914Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322914Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
334929Sstever@gmail.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
344490Sstever@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
353091Sstever@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
364490Sstever@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
374490Sstever@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
383296Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
394492Sstever@eecs.umich.edu *
404490Sstever@eecs.umich.edu * Authors: Nathan Binkert
413284Srdreslin@umich.edu *          Erik Hallnor
423284Srdreslin@umich.edu *          Steve Reinhardt
434874Sstever@eecs.umich.edu *          Andreas Sandberg
444929Sstever@gmail.com */
454490Sstever@eecs.umich.edu
463284Srdreslin@umich.edu/* @file
474929Sstever@gmail.com * Serialization Interface Declarations
484929Sstever@gmail.com */
494490Sstever@eecs.umich.edu
503342Srdreslin@umich.edu#ifndef __SERIALIZE_HH__
514490Sstever@eecs.umich.edu#define __SERIALIZE_HH__
524490Sstever@eecs.umich.edu
534490Sstever@eecs.umich.edu
544929Sstever@gmail.com#include <iostream>
554929Sstever@gmail.com#include <list>
563296Ssaidi@eecs.umich.edu#include <map>
574929Sstever@gmail.com#include <stack>
583091Sstever@eecs.umich.edu#include <vector>
593091Sstever@eecs.umich.edu
603091Sstever@eecs.umich.edu#include "base/bitunion.hh"
613349Sbinkertn@umich.edu#include "base/types.hh"
623091Sstever@eecs.umich.edu
633091Sstever@eecs.umich.educlass IniFile;
643091Sstever@eecs.umich.educlass Serializable;
653091Sstever@eecs.umich.educlass CheckpointIn;
663091Sstever@eecs.umich.educlass SimObject;
673091Sstever@eecs.umich.educlass SimObjectResolver;
684626Sstever@eecs.umich.educlass EventQueue;
694670Sstever@eecs.umich.edu
704670Sstever@eecs.umich.edutypedef std::ostream CheckpointOut;
714670Sstever@eecs.umich.edu
724670Sstever@eecs.umich.edu
734670Sstever@eecs.umich.edu/** The current version of the checkpoint format.
744670Sstever@eecs.umich.edu * This should be incremented by 1 and only 1 for every new version, where a new
754670Sstever@eecs.umich.edu * version is defined as a checkpoint created before this version won't work on
764670Sstever@eecs.umich.edu * the current version until the checkpoint format is updated. Adding a new
774626Sstever@eecs.umich.edu * SimObject shouldn't cause the version number to increase, only changes to
783091Sstever@eecs.umich.edu * existing objects such as serializing/unserializing more state, changing sizes
793175Srdreslin@umich.edu * of serialized arrays, etc. */
804626Sstever@eecs.umich.edustatic const uint64_t gem5CheckpointVersion = 0x000000000000000f;
814670Sstever@eecs.umich.edu
824670Sstever@eecs.umich.edutemplate <class T>
834626Sstever@eecs.umich.eduvoid paramOut(CheckpointOut &cp, const std::string &name, const T &param);
844493Sstever@eecs.umich.edu
854626Sstever@eecs.umich.edutemplate <typename DataType, typename BitUnion>
864490Sstever@eecs.umich.eduvoid paramOut(CheckpointOut &cp, const std::string &name,
873309Srdreslin@umich.edu              const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
884670Sstever@eecs.umich.edu{
893091Sstever@eecs.umich.edu    paramOut(cp, name, p.__data);
903091Sstever@eecs.umich.edu}
913091Sstever@eecs.umich.edu
922914Ssaidi@eecs.umich.edutemplate <class T>
932914Ssaidi@eecs.umich.eduvoid paramIn(CheckpointIn &cp, const std::string &name, T &param);
944492Sstever@eecs.umich.edu
953403Ssaidi@eecs.umich.edutemplate <typename DataType, typename BitUnion>
964492Sstever@eecs.umich.eduvoid paramIn(CheckpointIn &cp, const std::string &name,
974970Ssaidi@eecs.umich.edu             BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
984492Sstever@eecs.umich.edu{
993450Ssaidi@eecs.umich.edu    paramIn(cp, name, p.__data);
1004666Sstever@eecs.umich.edu}
1014666Sstever@eecs.umich.edu
1024666Sstever@eecs.umich.edutemplate <class T>
1034666Sstever@eecs.umich.edubool optParamIn(CheckpointIn &cp, const std::string &name, T &param,
1044666Sstever@eecs.umich.edu                bool warn = true);
1054666Sstever@eecs.umich.edu
1064666Sstever@eecs.umich.edutemplate <typename DataType, typename BitUnion>
1074666Sstever@eecs.umich.edubool optParamIn(CheckpointIn &cp, const std::string &name,
1084492Sstever@eecs.umich.edu                BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p,
1093450Ssaidi@eecs.umich.edu                bool warn = true)
1103403Ssaidi@eecs.umich.edu{
1113450Ssaidi@eecs.umich.edu    return optParamIn(cp, name, p.__data, warn);
1124666Sstever@eecs.umich.edu}
1134490Sstever@eecs.umich.edu
1144666Sstever@eecs.umich.edutemplate <class T>
1154490Sstever@eecs.umich.eduvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1163450Ssaidi@eecs.umich.edu                   const T *param, unsigned size);
1174492Sstever@eecs.umich.edu
1184492Sstever@eecs.umich.edutemplate <class T>
1194492Sstever@eecs.umich.eduvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1204492Sstever@eecs.umich.edu                   const std::vector<T> &param);
1213610Srdreslin@umich.edu
1223450Ssaidi@eecs.umich.edutemplate <class T>
1234492Sstever@eecs.umich.eduvoid arrayParamOut(CheckpointOut &cp, const std::string &name,
1243403Ssaidi@eecs.umich.edu                   const std::list<T> &param);
1253403Ssaidi@eecs.umich.edu
1264492Sstever@eecs.umich.edutemplate <class T>
1273403Ssaidi@eecs.umich.eduvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
1284492Sstever@eecs.umich.edu                  T *param, unsigned size);
1292914Ssaidi@eecs.umich.edu
1304492Sstever@eecs.umich.edutemplate <class T>
1314870Sstever@eecs.umich.eduvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
1324870Sstever@eecs.umich.edu                  std::vector<T> &param);
1334870Sstever@eecs.umich.edu
1344870Sstever@eecs.umich.edutemplate <class T>
1354870Sstever@eecs.umich.eduvoid arrayParamIn(CheckpointIn &cp, const std::string &name,
1364870Sstever@eecs.umich.edu                  std::list<T> &param);
1374492Sstever@eecs.umich.edu
1384492Sstever@eecs.umich.eduvoid
1394870Sstever@eecs.umich.eduobjParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
1404490Sstever@eecs.umich.edu
1415606Snate@binkert.org//
1423263Srdreslin@umich.edu// These macros are streamlined to use in serialize/unserialize
1434492Sstever@eecs.umich.edu// functions.  It's assumed that serialize() has a parameter 'os' for
1444490Sstever@eecs.umich.edu// the ostream, and unserialize() has parameters 'cp' and 'section'.
1454490Sstever@eecs.umich.edu#define SERIALIZE_SCALAR(scalar)        paramOut(cp, #scalar, scalar)
1464490Sstever@eecs.umich.edu
1473091Sstever@eecs.umich.edu#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, #scalar, scalar)
1484870Sstever@eecs.umich.edu#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, #scalar, scalar)
1494870Sstever@eecs.umich.edu
1504870Sstever@eecs.umich.edu// ENUMs are like SCALARs, but we cast them to ints on the way out
1514870Sstever@eecs.umich.edu#define SERIALIZE_ENUM(scalar)          paramOut(cp, #scalar, (int)scalar)
1524870Sstever@eecs.umich.edu
1534870Sstever@eecs.umich.edu#define UNSERIALIZE_ENUM(scalar)                        \
1543091Sstever@eecs.umich.edu    do {                                                \
1554492Sstever@eecs.umich.edu        int tmp;                                        \
1564492Sstever@eecs.umich.edu        paramIn(cp, #scalar, tmp);                      \
1574492Sstever@eecs.umich.edu        scalar = static_cast<decltype(scalar)>(tmp);    \
1584492Sstever@eecs.umich.edu    } while (0)
1594492Sstever@eecs.umich.edu
1604492Sstever@eecs.umich.edu#define SERIALIZE_ARRAY(member, size)           \
1614492Sstever@eecs.umich.edu        arrayParamOut(cp, #member, member, size)
1624492Sstever@eecs.umich.edu
1634492Sstever@eecs.umich.edu#define UNSERIALIZE_ARRAY(member, size)         \
1644492Sstever@eecs.umich.edu        arrayParamIn(cp, #member, member, size)
1654492Sstever@eecs.umich.edu
1664492Sstever@eecs.umich.edu#define SERIALIZE_CONTAINER(member)             \
1674492Sstever@eecs.umich.edu        arrayParamOut(cp, #member, member)
1684492Sstever@eecs.umich.edu
1694492Sstever@eecs.umich.edu#define UNSERIALIZE_CONTAINER(member)           \
1704492Sstever@eecs.umich.edu        arrayParamIn(cp, #member, member)
1714492Sstever@eecs.umich.edu
1724492Sstever@eecs.umich.edu#define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
1734492Sstever@eecs.umich.edu
1744492Sstever@eecs.umich.edu#define UNSERIALIZE_EVENT(event)                        \
1754492Sstever@eecs.umich.edu    do {                                                \
1764492Sstever@eecs.umich.edu        event.unserializeSection(cp, #event);           \
1774492Sstever@eecs.umich.edu        eventQueue()->checkpointReschedule(&event);     \
1782914Ssaidi@eecs.umich.edu    } while(0)
1792914Ssaidi@eecs.umich.edu
1802914Ssaidi@eecs.umich.edu#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
1812914Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
1822914Ssaidi@eecs.umich.edu
1832914Ssaidi@eecs.umich.edu#define SERIALIZE_OBJPTR(objptr)        paramOut(cp, #objptr, (objptr)->name())
1843403Ssaidi@eecs.umich.edu
1852914Ssaidi@eecs.umich.edu#define UNSERIALIZE_OBJPTR(objptr)                      \
1862914Ssaidi@eecs.umich.edu    do {                                                \
1872914Ssaidi@eecs.umich.edu        SimObject *sptr;                                \
1882914Ssaidi@eecs.umich.edu        objParamIn(cp, #objptr, sptr);                  \
189        objptr = dynamic_cast<decltype(objptr)>(sptr);  \
190    } while (0)
191
192/**
193 * Basic support for object serialization.
194 *
195 * Objects that support serialization should derive from this
196 * class. Such objects can largely be divided into two categories: 1)
197 * True SimObjects (deriving from SimObject), and 2) child objects
198 * (non-SimObjects).
199 *
200 * SimObjects are serialized automatically into their own sections
201 * automatically by the SimObject base class (see
202 * SimObject::serializeAll().
203 *
204 * SimObjects can contain other serializable objects that are not
205 * SimObjects. Much like normal serialized members are not serialized
206 * automatically, these objects will not be serialized automatically
207 * and it is expected that the objects owning such serializable
208 * objects call the required serialization/unserialization methods on
209 * child objects. The preferred method to serialize a child object is
210 * to call serializeSection() on the child, which serializes the
211 * object into a new subsection in the current section. Another option
212 * is to call serialize() directly, which serializes the object into
213 * the current section. The latter is not recommended as it can lead
214 * to naming clashes between objects.
215 *
216 * @note Many objects that support serialization need to be put in a
217 * consistent state when serialization takes place. We refer to the
218 * action of forcing an object into a consistent state as
219 * 'draining'. Objects that need draining inherit from Drainable. See
220 * Drainable for more information.
221 */
222class Serializable
223{
224  protected:
225    /**
226     * Scoped checkpoint section helper class
227     *
228     * This helper class creates a section within a checkpoint without
229     * the need for a separate serializeable object. It is mainly used
230     * within the Serializable class when serializing or unserializing
231     * section (see serializeSection() and unserializeSection()). It
232     * can also be used to maintain backwards compatibility in
233     * existing code that serializes structs that are not inheriting
234     * from Serializable into subsections.
235     *
236     * When the class is instantiated, it appends a name to the active
237     * path in a checkpoint. The old path is later restored when the
238     * instance is destroyed. For example, serializeSection() could be
239     * implemented by instantiating a ScopedCheckpointSection and then
240     * calling serialize() on an object.
241     */
242    class ScopedCheckpointSection {
243      public:
244        template<class CP>
245        ScopedCheckpointSection(CP &cp, const char *name) {
246            pushName(name);
247            nameOut(cp);
248        }
249
250        template<class CP>
251        ScopedCheckpointSection(CP &cp, const std::string &name) {
252            pushName(name.c_str());
253            nameOut(cp);
254        }
255
256        ~ScopedCheckpointSection();
257
258        ScopedCheckpointSection() = delete;
259        ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
260        ScopedCheckpointSection &operator=(
261            const ScopedCheckpointSection &) = delete;
262        ScopedCheckpointSection &operator=(
263            ScopedCheckpointSection &&) = delete;
264
265      private:
266        void pushName(const char *name);
267        void nameOut(CheckpointOut &cp);
268        void nameOut(CheckpointIn &cp) {};
269    };
270
271  public:
272    Serializable();
273    virtual ~Serializable();
274
275    /**
276     * Serialize an object
277     *
278     * Output an object's state into the current checkpoint section.
279     *
280     * @param cp Checkpoint state
281     */
282    virtual void serialize(CheckpointOut &cp) const = 0;
283
284    /**
285     * Unserialize an object
286     *
287     * Read an object's state from the current checkpoint section.
288     *
289     * @param cp Checkpoint state
290     */
291    virtual void unserialize(CheckpointIn &cp) = 0;
292
293    /**
294     * Serialize an object into a new section
295     *
296     * This method creates a new section in a checkpoint and calls
297     * serialize() to serialize the current object into that
298     * section. The name of the section is appended to the current
299     * checkpoint path.
300     *
301     * @param cp Checkpoint state
302     * @param name Name to append to the active path
303     */
304    void serializeSection(CheckpointOut &cp, const char *name) const;
305
306    void serializeSection(CheckpointOut &cp, const std::string &name) const {
307        serializeSection(cp, name.c_str());
308    }
309
310    /**
311     * Unserialize an a child object
312     *
313     * This method loads a child object from a checkpoint. The object
314     * name is appended to the active path to form a fully qualified
315     * section name and unserialize() is called.
316     *
317     * @param cp Checkpoint state
318     * @param name Name to append to the active path
319     */
320    void unserializeSection(CheckpointIn &cp, const char *name);
321
322    void unserializeSection(CheckpointIn &cp, const std::string &name) {
323        unserializeSection(cp, name.c_str());
324    }
325
326    /**
327     * @{
328     * @name Legacy interface
329     *
330     * Interface for objects that insist on changing their state when
331     * serializing. Such state change should be done in drain(),
332     * memWriteback(), or memInvalidate() and not in the serialization
333     * method. In general, if state changes occur in serialize, it
334     * complicates testing since it breaks assumptions about draining
335     * and serialization. It potentially also makes components more
336     * fragile since they there are no ordering guarantees when
337     * serializing SimObjects.
338     *
339     * @warn This interface is considered deprecated and should never
340     * be used.
341     */
342
343    virtual void serializeOld(CheckpointOut &cp) {
344        serialize(cp);
345    }
346    void serializeSectionOld(CheckpointOut &cp, const char *name);
347    void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
348        serializeSectionOld(cp, name.c_str());
349    }
350    /** @} */
351
352    /** Get the fully-qualified name of the active section */
353    static const std::string &currentSection();
354
355    static int ckptCount;
356    static int ckptMaxCount;
357    static int ckptPrevCount;
358    static void serializeAll(const std::string &cpt_dir);
359    static void unserializeGlobals(CheckpointIn &cp);
360
361  private:
362    static std::stack<std::string> path;
363};
364
365void debug_serialize(const std::string &cpt_dir);
366
367
368class CheckpointIn
369{
370  private:
371
372    IniFile *db;
373
374    SimObjectResolver &objNameResolver;
375
376  public:
377    CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
378    ~CheckpointIn();
379
380    const std::string cptDir;
381
382    bool find(const std::string &section, const std::string &entry,
383              std::string &value);
384
385    bool findObj(const std::string &section, const std::string &entry,
386                 SimObject *&value);
387
388    bool sectionExists(const std::string &section);
389
390    // The following static functions have to do with checkpoint
391    // creation rather than restoration.  This class makes a handy
392    // namespace for them though.  Currently no Checkpoint object is
393    // created on serialization (only unserialization) so we track the
394    // directory name as a global.  It would be nice to change this
395    // someday
396
397  private:
398    // current directory we're serializing into.
399    static std::string currentDirectory;
400
401  public:
402    // Set the current directory.  This function takes care of
403    // inserting curTick() if there's a '%d' in the argument, and
404    // appends a '/' if necessary.  The final name is returned.
405    static std::string setDir(const std::string &base_name);
406
407    // Export current checkpoint directory name so other objects can
408    // derive filenames from it (e.g., memory).  The return value is
409    // guaranteed to end in '/' so filenames can be directly appended.
410    // This function is only valid while a checkpoint is being created.
411    static std::string dir();
412
413    // Filename for base checkpoint file within directory.
414    static const char *baseFilename;
415};
416
417#endif // __SERIALIZE_HH__
418