2 * Copyright (c) 2015 ARM Limited
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.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
40 * Authors: Nathan Binkert
47 * Serialization Interface Declarations
50 #ifndef __SERIALIZE_HH__
51 #define __SERIALIZE_HH__
60 #include "base/bitunion.hh"
61 #include "base/types.hh"
67 class SimObjectResolver;
70 typedef std::ostream CheckpointOut;
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. */
80 static const uint64_t gem5CheckpointVersion = 0x000000000000000f;
83 void paramOut(CheckpointOut &cp, const std::string &name, const T ¶m);
85 template <typename DataType, typename BitUnion>
86 void paramOut(CheckpointOut &cp, const std::string &name,
87 const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
89 paramOut(cp, name, p.__data);
93 void paramIn(CheckpointIn &cp, const std::string &name, T ¶m);
95 template <typename DataType, typename BitUnion>
96 void paramIn(CheckpointIn &cp, const std::string &name,
97 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
99 paramIn(cp, name, p.__data);
103 bool optParamIn(CheckpointIn &cp, const std::string &name, T ¶m,
106 template <typename DataType, typename BitUnion>
107 bool optParamIn(CheckpointIn &cp, const std::string &name,
108 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p,
111 return optParamIn(cp, name, p.__data, warn);
115 void arrayParamOut(CheckpointOut &cp, const std::string &name,
116 const T *param, unsigned size);
119 void arrayParamOut(CheckpointOut &cp, const std::string &name,
120 const std::vector<T> ¶m);
123 void arrayParamOut(CheckpointOut &cp, const std::string &name,
124 const std::list<T> ¶m);
127 void arrayParamIn(CheckpointIn &cp, const std::string &name,
128 T *param, unsigned size);
131 void arrayParamIn(CheckpointIn &cp, const std::string &name,
132 std::vector<T> ¶m);
135 void arrayParamIn(CheckpointIn &cp, const std::string &name,
136 std::list<T> ¶m);
139 objParamIn(CheckpointIn &cp, const std::string &name, SimObject * ¶m);
142 // These macros are streamlined to use in serialize/unserialize
143 // functions. It's assumed that serialize() has a parameter 'os' for
144 // the ostream, and unserialize() has parameters 'cp' and 'section'.
145 #define SERIALIZE_SCALAR(scalar) paramOut(cp, #scalar, scalar)
147 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar)
148 #define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar)
150 // ENUMs are like SCALARs, but we cast them to ints on the way out
151 #define SERIALIZE_ENUM(scalar) paramOut(cp, #scalar, (int)scalar)
153 #define UNSERIALIZE_ENUM(scalar) \
156 paramIn(cp, #scalar, tmp); \
157 scalar = static_cast<decltype(scalar)>(tmp); \
160 #define SERIALIZE_ARRAY(member, size) \
161 arrayParamOut(cp, #member, member, size)
163 #define UNSERIALIZE_ARRAY(member, size) \
164 arrayParamIn(cp, #member, member, size)
166 #define SERIALIZE_CONTAINER(member) \
167 arrayParamOut(cp, #member, member)
169 #define UNSERIALIZE_CONTAINER(member) \
170 arrayParamIn(cp, #member, member)
172 #define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
174 #define UNSERIALIZE_EVENT(event) \
176 event.unserializeSection(cp, #event); \
177 eventQueue()->checkpointReschedule(&event); \
180 #define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
181 #define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
183 #define SERIALIZE_OBJPTR(objptr) paramOut(cp, #objptr, (objptr)->name())
185 #define UNSERIALIZE_OBJPTR(objptr) \
188 objParamIn(cp, #objptr, sptr); \
189 objptr = dynamic_cast<decltype(objptr)>(sptr); \
193 * Basic support for object serialization.
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
200 * SimObjects are serialized automatically into their own sections
201 * automatically by the SimObject base class (see
202 * SimObject::serializeAll().
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.
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.
226 * Scoped checkpoint section helper class
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.
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.
242 class ScopedCheckpointSection {
245 ScopedCheckpointSection(CP &cp, const char *name) {
251 ScopedCheckpointSection(CP &cp, const std::string &name) {
252 pushName(name.c_str());
256 ~ScopedCheckpointSection();
258 ScopedCheckpointSection() = delete;
259 ScopedCheckpointSection(const ScopedCheckpointSection &) = delete;
260 ScopedCheckpointSection &operator=(
261 const ScopedCheckpointSection &) = delete;
262 ScopedCheckpointSection &operator=(
263 ScopedCheckpointSection &&) = delete;
266 void pushName(const char *name);
267 void nameOut(CheckpointOut &cp);
268 void nameOut(CheckpointIn &cp) {};
273 virtual ~Serializable();
276 * Serialize an object
278 * Output an object's state into the current checkpoint section.
280 * @param cp Checkpoint state
282 virtual void serialize(CheckpointOut &cp) const = 0;
285 * Unserialize an object
287 * Read an object's state from the current checkpoint section.
289 * @param cp Checkpoint state
291 virtual void unserialize(CheckpointIn &cp) = 0;
294 * Serialize an object into a new section
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
301 * @param cp Checkpoint state
302 * @param name Name to append to the active path
304 void serializeSection(CheckpointOut &cp, const char *name) const;
306 void serializeSection(CheckpointOut &cp, const std::string &name) const {
307 serializeSection(cp, name.c_str());
311 * Unserialize an a child object
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.
317 * @param cp Checkpoint state
318 * @param name Name to append to the active path
320 void unserializeSection(CheckpointIn &cp, const char *name);
322 void unserializeSection(CheckpointIn &cp, const std::string &name) {
323 unserializeSection(cp, name.c_str());
328 * @name Legacy interface
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.
339 * @warn This interface is considered deprecated and should never
343 virtual void serializeOld(CheckpointOut &cp) {
346 void serializeSectionOld(CheckpointOut &cp, const char *name);
347 void serializeSectionOld(CheckpointOut &cp, const std::string &name) {
348 serializeSectionOld(cp, name.c_str());
352 /** Get the fully-qualified name of the active section */
353 static const std::string ¤tSection();
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);
362 static std::stack<std::string> path;
365 void debug_serialize(const std::string &cpt_dir);
374 SimObjectResolver &objNameResolver;
377 CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
380 const std::string cptDir;
382 bool find(const std::string §ion, const std::string &entry,
385 bool findObj(const std::string §ion, const std::string &entry,
388 bool sectionExists(const std::string §ion);
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
398 // current directory we're serializing into.
399 static std::string currentDirectory;
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);
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();
413 // Filename for base checkpoint file within directory.
414 static const char *baseFilename;
417 #endif // __SERIALIZE_HH__