serialize.hh revision 11076:463a4b0f0dda
112841Sgabeblack@google.com/* 212841Sgabeblack@google.com * Copyright (c) 2015 ARM Limited 312841Sgabeblack@google.com * All rights reserved 412841Sgabeblack@google.com * 512841Sgabeblack@google.com * The license below extends only to copyright in the software and shall 612841Sgabeblack@google.com * not be construed as granting a license to any other intellectual 712841Sgabeblack@google.com * property including but not limited to intellectual property relating 812841Sgabeblack@google.com * to a hardware implementation of the functionality of the software 912841Sgabeblack@google.com * licensed hereunder. You may use the software subject to the license 1012841Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated 1112841Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software, 1212841Sgabeblack@google.com * modified or unmodified, in source code or in binary form. 1312841Sgabeblack@google.com * 1412841Sgabeblack@google.com * Copyright (c) 2002-2005 The Regents of The University of Michigan 1512841Sgabeblack@google.com * All rights reserved. 1612841Sgabeblack@google.com * 1712841Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 1812841Sgabeblack@google.com * modification, are permitted provided that the following conditions are 1912841Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 2012841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 2112841Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 2212841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 2312841Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 2412841Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 2512841Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 2612841Sgabeblack@google.com * this software without specific prior written permission. 2712841Sgabeblack@google.com * 2812841Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2912841Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3012841Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3112841Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3212841Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3313214Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3413214Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3512841Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3612841Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3712841Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3813214Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3912841Sgabeblack@google.com * 4012841Sgabeblack@google.com * Authors: Nathan Binkert 4112841Sgabeblack@google.com * Erik Hallnor 4212841Sgabeblack@google.com * Steve Reinhardt 4312841Sgabeblack@google.com * Andreas Sandberg 4412841Sgabeblack@google.com */ 4512841Sgabeblack@google.com 4612841Sgabeblack@google.com/* @file 4712841Sgabeblack@google.com * Serialization Interface Declarations 4812841Sgabeblack@google.com */ 4912841Sgabeblack@google.com 5012841Sgabeblack@google.com#ifndef __SERIALIZE_HH__ 5112841Sgabeblack@google.com#define __SERIALIZE_HH__ 5212841Sgabeblack@google.com 5312841Sgabeblack@google.com 5412841Sgabeblack@google.com#include <iostream> 5512841Sgabeblack@google.com#include <list> 5612841Sgabeblack@google.com#include <map> 5712841Sgabeblack@google.com#include <stack> 5812841Sgabeblack@google.com#include <set> 5912841Sgabeblack@google.com#include <vector> 6012841Sgabeblack@google.com 6113214Sgabeblack@google.com#include "base/bitunion.hh" 6212841Sgabeblack@google.com#include "base/types.hh" 6313214Sgabeblack@google.com 6412841Sgabeblack@google.comclass IniFile; 6512841Sgabeblack@google.comclass Serializable; 6612841Sgabeblack@google.comclass CheckpointIn; 6713214Sgabeblack@google.comclass SimObject; 6812841Sgabeblack@google.comclass SimObjectResolver; 6913214Sgabeblack@google.comclass EventQueue; 7012841Sgabeblack@google.com 7112841Sgabeblack@google.comtypedef std::ostream CheckpointOut; 7212841Sgabeblack@google.com 7313214Sgabeblack@google.com 7412841Sgabeblack@google.com/** The current version of the checkpoint format. 7513214Sgabeblack@google.com * This should be incremented by 1 and only 1 for every new version, where a new 7612841Sgabeblack@google.com * version is defined as a checkpoint created before this version won't work on 7712841Sgabeblack@google.com * the current version until the checkpoint format is updated. Adding a new 7812841Sgabeblack@google.com * SimObject shouldn't cause the version number to increase, only changes to 7913214Sgabeblack@google.com * existing objects such as serializing/unserializing more state, changing sizes 8012841Sgabeblack@google.com * of serialized arrays, etc. */ 8113214Sgabeblack@google.comstatic const uint64_t gem5CheckpointVersion = 0x000000000000000f; 8212841Sgabeblack@google.com 8312841Sgabeblack@google.comtemplate <class T> 8412841Sgabeblack@google.comvoid paramOut(CheckpointOut &cp, const std::string &name, const T ¶m); 8513214Sgabeblack@google.com 8612841Sgabeblack@google.comtemplate <typename DataType, typename BitUnion> 8713214Sgabeblack@google.comvoid paramOut(CheckpointOut &cp, const std::string &name, 8812841Sgabeblack@google.com const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p) 8912841Sgabeblack@google.com{ 9012841Sgabeblack@google.com paramOut(cp, name, p.__data); 9113214Sgabeblack@google.com} 9213214Sgabeblack@google.com 9313214Sgabeblack@google.comtemplate <class T> 9413214Sgabeblack@google.comvoid paramIn(CheckpointIn &cp, const std::string &name, T ¶m); 9513214Sgabeblack@google.com 9613214Sgabeblack@google.comtemplate <typename DataType, typename BitUnion> 9713214Sgabeblack@google.comvoid paramIn(CheckpointIn &cp, const std::string &name, 9813214Sgabeblack@google.com BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p) 9913214Sgabeblack@google.com{ 10013214Sgabeblack@google.com paramIn(cp, name, p.__data); 10113214Sgabeblack@google.com} 10213214Sgabeblack@google.com 10312841Sgabeblack@google.comtemplate <class T> 10412841Sgabeblack@google.combool optParamIn(CheckpointIn &cp, const std::string &name, T ¶m, 10512841Sgabeblack@google.com bool warn = true); 10612841Sgabeblack@google.com 10712841Sgabeblack@google.comtemplate <typename DataType, typename BitUnion> 10812841Sgabeblack@google.combool optParamIn(CheckpointIn &cp, const std::string &name, 10912841Sgabeblack@google.com 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> ¶m); 122 123template <class T> 124void arrayParamOut(CheckpointOut &cp, const std::string &name, 125 const std::list<T> ¶m); 126 127template <class T> 128void arrayParamOut(CheckpointOut &cp, const std::string &name, 129 const std::set<T> ¶m); 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> ¶m); 138 139template <class T> 140void arrayParamIn(CheckpointIn &cp, const std::string &name, 141 std::list<T> ¶m); 142 143template <class T> 144void arrayParamIn(CheckpointIn &cp, const std::string &name, 145 std::set<T> ¶m); 146 147void 148objParamIn(CheckpointIn &cp, const std::string &name, SimObject * ¶m); 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 ¤tSection(); 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 §ion, const std::string &entry, 392 std::string &value); 393 394 bool findObj(const std::string §ion, const std::string &entry, 395 SimObject *&value); 396 397 bool sectionExists(const std::string §ion); 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