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 74template <class T> 75void paramOut(CheckpointOut &cp, const std::string &name, const T ¶m); 76 77template <typename DataType, typename BitUnion> 78void paramOut(CheckpointOut &cp, const std::string &name, 79 const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p) 80{ 81 paramOut(cp, name, p.__data); 82} 83 84template <class T> 85void paramIn(CheckpointIn &cp, const std::string &name, T ¶m); 86 87template <typename DataType, typename BitUnion> 88void paramIn(CheckpointIn &cp, const std::string &name, 89 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p) 90{ 91 paramIn(cp, name, p.__data); 92} 93 94template <class T> 95bool optParamIn(CheckpointIn &cp, const std::string &name, T ¶m, 96 bool warn = true); 97 98template <typename DataType, typename BitUnion> 99bool optParamIn(CheckpointIn &cp, const std::string &name, 100 BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p, 101 bool warn = true) 102{ 103 return optParamIn(cp, name, p.__data, warn); 104} 105 106template <class T> 107void arrayParamOut(CheckpointOut &cp, const std::string &name, 108 const T *param, unsigned size); 109 110template <class T> 111void arrayParamOut(CheckpointOut &cp, const std::string &name, 112 const std::vector<T> ¶m); 113 114template <class T> 115void arrayParamOut(CheckpointOut &cp, const std::string &name, 116 const std::list<T> ¶m); 117 118template <class T> 119void arrayParamOut(CheckpointOut &cp, const std::string &name, 120 const std::set<T> ¶m); 121 122template <class T> 123void arrayParamIn(CheckpointIn &cp, const std::string &name, 124 T *param, unsigned size); 125 126template <class T> 127void arrayParamIn(CheckpointIn &cp, const std::string &name, 128 std::vector<T> ¶m); 129 130template <class T> 131void arrayParamIn(CheckpointIn &cp, const std::string &name, 132 std::list<T> ¶m); 133 134template <class T> 135void arrayParamIn(CheckpointIn &cp, const std::string &name, 136 std::set<T> ¶m); 137 138void 139objParamIn(CheckpointIn &cp, const std::string &name, SimObject * ¶m); 140 141// 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) 146 147#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar) 148#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar) 149 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) 152 153#define UNSERIALIZE_ENUM(scalar) \ 154 do { \ 155 int tmp; \ 156 paramIn(cp, #scalar, tmp); \ 157 scalar = static_cast<decltype(scalar)>(tmp); \ 158 } while (0) 159 160#define SERIALIZE_ARRAY(member, size) \ 161 arrayParamOut(cp, #member, member, size) 162 163#define UNSERIALIZE_ARRAY(member, size) \ 164 arrayParamIn(cp, #member, member, size) 165 166#define SERIALIZE_CONTAINER(member) \ 167 arrayParamOut(cp, #member, member) 168 169#define UNSERIALIZE_CONTAINER(member) \ 170 arrayParamIn(cp, #member, member) 171 172#define SERIALIZE_EVENT(event) event.serializeSection(cp, #event); 173 174#define UNSERIALIZE_EVENT(event) \ 175 do { \ 176 event.unserializeSection(cp, #event); \ 177 eventQueue()->checkpointReschedule(&event); \ 178 } while (0) 179 180#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj) 181#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj) 182 183#define SERIALIZE_OBJPTR(objptr) paramOut(cp, #objptr, (objptr)->name()) 184 185#define UNSERIALIZE_OBJPTR(objptr) \ 186 do { \ 187 SimObject *sptr; \ 188 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 /** Get the fully-qualified name of the active section */ 327 static const std::string ¤tSection(); 328 329 static int ckptCount; 330 static int ckptMaxCount; 331 static int ckptPrevCount; 332 static void serializeAll(const std::string &cpt_dir); 333 static void unserializeGlobals(CheckpointIn &cp); 334 335 private: 336 static std::stack<std::string> path; 337}; 338 339void debug_serialize(const std::string &cpt_dir); 340 341 342class CheckpointIn 343{ 344 private: 345 346 IniFile *db; 347 348 SimObjectResolver &objNameResolver; 349 350 public: 351 CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver); 352 ~CheckpointIn(); 353 354 const std::string cptDir; 355 356 bool find(const std::string §ion, const std::string &entry, 357 std::string &value); 358 359 bool findObj(const std::string §ion, const std::string &entry, 360 SimObject *&value); 361
|