gem5  v21.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
serialize.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, 2018, 2020 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 
41 /* @file
42  * Serialization Interface Declarations
43  */
44 
45 #ifndef __SERIALIZE_HH__
46 #define __SERIALIZE_HH__
47 
48 
49 #include <algorithm>
50 #include <iostream>
51 #include <iterator>
52 #include <stack>
53 #include <set>
54 #include <type_traits>
55 #include <unordered_map>
56 #include <vector>
57 
58 #include "base/inifile.hh"
59 #include "base/logging.hh"
61 
62 class IniFile;
63 class SimObject;
65 
66 typedef std::ostream CheckpointOut;
67 
69 {
70  private:
71 
73 
75 
76  const std::string _cptDir;
77 
78  public:
79  CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
80  ~CheckpointIn();
81 
88  const std::string getCptDir() { return _cptDir; }
89 
90  bool find(const std::string &section, const std::string &entry,
91  std::string &value);
92 
93  bool findObj(const std::string &section, const std::string &entry,
94  SimObject *&value);
95 
96  bool entryExists(const std::string &section, const std::string &entry);
97  bool sectionExists(const std::string &section);
98  void visitSection(const std::string &section,
99  IniFile::VisitSectionCallback cb); //end of api_checkout group
101 
102  // The following static functions have to do with checkpoint
103  // creation rather than restoration. This class makes a handy
104  // namespace for them though. Currently no Checkpoint object is
105  // created on serialization (only unserialization) so we track the
106  // directory name as a global. It would be nice to change this
107  // someday
108 
109  private:
110  // current directory we're serializing into.
111  static std::string currentDirectory;
112 
113 
114  public:
123  static std::string setDir(const std::string &base_name);
124 
135  static std::string dir();
136 
137  // Filename for base checkpoint file within directory.
138  static const char *baseFilename;
139 };
140 
176 {
177  public:
179  public:
202  template<class CP>
203  ScopedCheckpointSection(CP &cp, const char *name) {
204  pushName(name);
205  nameOut(cp);
206  }
207 
208  template<class CP>
209  ScopedCheckpointSection(CP &cp, const std::string &name) {
210  pushName(name.c_str());
211  nameOut(cp);
212  } //end of api_serialize group
214 
216 
217  ScopedCheckpointSection() = delete;
220  const ScopedCheckpointSection &) = delete;
222  ScopedCheckpointSection &&) = delete;
223 
224  private:
225  void pushName(const char *name);
226  void nameOut(CheckpointOut &cp);
228  };
229 
233  Serializable();
234  virtual ~Serializable();
235 
245  virtual void serialize(CheckpointOut &cp) const = 0;
246 
256  virtual void unserialize(CheckpointIn &cp) = 0;
257 
271  void serializeSection(CheckpointOut &cp, const char *name) const;
272 
276  void serializeSection(CheckpointOut &cp, const std::string &name) const {
277  serializeSection(cp, name.c_str());
278  }
279 
292  void unserializeSection(CheckpointIn &cp, const char *name);
293 
297  void unserializeSection(CheckpointIn &cp, const std::string &name) {
298  unserializeSection(cp, name.c_str());
299  }
300 
306  static const std::string &currentSection();
307 
313  static void serializeAll(const std::string &cpt_dir);
314 
318  static void unserializeGlobals(CheckpointIn &cp);
319 
320  private:
321  static std::stack<std::string> path;
322 };
323 
331 template <class T>
332 void
333 paramOut(CheckpointOut &os, const std::string &name, const T &param)
334 {
335  os << name << "=";
336  ShowParam<T>::show(os, param);
337  os << "\n";
338 }
339 
340 template <class T>
341 bool
342 paramInImpl(CheckpointIn &cp, const std::string &name, T &param)
343 {
344  const std::string &section(Serializable::currentSection());
345  std::string str;
346  return cp.find(section, name, str) && ParseParam<T>::parse(str, param);
347 }
348 
361 template <class T>
362 bool
363 optParamIn(CheckpointIn &cp, const std::string &name, T &param,
364  bool do_warn=true)
365 {
366  if (paramInImpl(cp, name, param))
367  return true;
368 
369  warn_if(do_warn, "optional parameter %s:%s not present",
371  return false;
372 }
373 
381 template <class T>
382 void
383 paramIn(CheckpointIn &cp, const std::string &name, T &param)
384 {
385  fatal_if(!paramInImpl(cp, name, param),
386  "Can't unserialize '%s:%s'", Serializable::currentSection(), name);
387 }
388 
392 template <class InputIterator>
393 void
394 arrayParamOut(CheckpointOut &os, const std::string &name,
395  InputIterator start, InputIterator end)
396 {
397  os << name << "=";
398  auto it = start;
399  using Elem = std::remove_cv_t<std::remove_reference_t<decltype(*it)>>;
400  if (it != end)
401  ShowParam<Elem>::show(os, *it++);
402  while (it != end) {
403  os << " ";
404  ShowParam<Elem>::show(os, *it++);
405  }
406  os << "\n";
407 }
408 
412 template <class T>
413 decltype(std::begin(std::declval<const T&>()),
414  std::end(std::declval<const T&>()), void())
416  const T &param)
417 {
418  arrayParamOut(os, name, std::begin(param), std::end(param));
419 }
420 
421 
425 template <class T>
426 void
427 arrayParamOut(CheckpointOut &os, const std::string &name,
428  const T *param, unsigned size)
429 {
430  arrayParamOut(os, name, param, param + size);
431 }
432 
445 template <class T, class InsertIterator>
446 void
447 arrayParamIn(CheckpointIn &cp, const std::string &name,
448  InsertIterator inserter, ssize_t fixed_size=-1)
449 {
450  const std::string &section = Serializable::currentSection();
451  std::string str;
452  fatal_if(!cp.find(section, name, str),
453  "Can't unserialize '%s:%s'.", section, name);
454 
456  tokenize(tokens, str, ' ');
457 
458  fatal_if(fixed_size >= 0 && tokens.size() != fixed_size,
459  "Array size mismatch on %s:%s (Got %u, expected %u)'\n",
460  section, name, tokens.size(), fixed_size);
461 
462  for (const auto &token: tokens) {
463  T value;
465  "Could not parse \"%s\".", str);
466  *inserter = value;
467  }
468 }
469 
473 template <class T>
474 decltype(std::declval<T>().insert(std::declval<typename T::value_type>()),
475  void())
476 arrayParamIn(CheckpointIn &cp, const std::string &name, T &param)
477 {
478  param.clear();
479  arrayParamIn<typename T::value_type>(
480  cp, name, std::inserter(param, param.begin()));
481 }
482 
486 template <class T>
487 decltype(std::declval<T>().push_back(std::declval<typename T::value_type>()),
488  void())
489 arrayParamIn(CheckpointIn &cp, const std::string &name, T &param)
490 {
491  param.clear();
492  arrayParamIn<typename T::value_type>(cp, name, std::back_inserter(param));
493 }
494 
498 template <class T>
499 void
500 arrayParamIn(CheckpointIn &cp, const std::string &name,
501  T *param, unsigned size)
502 {
503  struct ArrayInserter
504  {
505  T *data;
506  T &operator *() { return *data++; }
507  } insert_it{param};
508 
509  arrayParamIn<T>(cp, name, insert_it, size);
510 }
511 
512 void
513 debug_serialize(const std::string &cpt_dir);
514 
515 
519 void
520 objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
521 
530 template <class T>
531 void
532 mappingParamOut(CheckpointOut &os, const char* sectionName,
533  const char* const names[], const T *param, unsigned size)
534 {
535  Serializable::ScopedCheckpointSection sec(os, sectionName);
536  for (unsigned i = 0; i < size; ++i) {
537  paramOut(os, names[i], param[i]);
538  }
539 }
540 
544 template <class T>
545 void
546 mappingParamIn(CheckpointIn &cp, const char* sectionName,
547  const char* const names[], T *param, unsigned size)
548 {
549  Serializable::ScopedCheckpointSection sec(cp, sectionName);
550  std::unordered_map<std::string, size_t> name_to_index;
551  for (size_t i = 0; i < size; i++) {
552  name_to_index[names[i]] = i;
553  }
554  for (size_t i = 0; i < size; i++) {
555  auto& key = names[i];
556  T value;
557  if (optParamIn(cp, key, value)) {
558  param[name_to_index[key]] = value;
559  }
560  }
561  cp.visitSection(
563  [name_to_index](const std::string& key, const std::string& val)
564  {
565  if (!name_to_index.count(key)) {
566  warn("unknown entry found in checkpoint: %s %s %s\n",
567  Serializable::currentSection(), key, val);
568  }
569  }
570  );
571 }
572 
573 //
574 // These macros are streamlined to use in serialize/unserialize
575 // functions. It's assumed that serialize() has a parameter 'os' for
576 // the ostream, and unserialize() has parameters 'cp' and 'section'.
577 
578 
584 #define SERIALIZE_SCALAR(scalar) paramOut(cp, #scalar, scalar)
585 
591 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar)
592 
598 #define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar)
599 
600 // ENUMs are like SCALARs, but we cast them to ints on the way out
601 
607 #define SERIALIZE_ENUM(scalar) paramOut(cp, #scalar, (int)scalar)
608 
614 #define UNSERIALIZE_ENUM(scalar) \
615  do { \
616  int tmp; \
617  paramIn(cp, #scalar, tmp); \
618  scalar = static_cast<decltype(scalar)>(tmp); \
619  } while (0)
620 
626 #define SERIALIZE_ARRAY(member, size) \
627  arrayParamOut(cp, #member, member, size)
628 
634 #define UNSERIALIZE_ARRAY(member, size) \
635  arrayParamIn(cp, #member, member, size)
636 
642 #define SERIALIZE_CONTAINER(member) \
643  arrayParamOut(cp, #member, member)
644 
650 #define UNSERIALIZE_CONTAINER(member) \
651  arrayParamIn(cp, #member, member)
652 
658 #define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
659 
665 #define UNSERIALIZE_EVENT(event) \
666  do { \
667  event.unserializeSection(cp, #event); \
668  eventQueue()->checkpointReschedule(&event); \
669  } while (0)
670 
676 #define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
677 
683 #define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
684 
690 #define SERIALIZE_OBJPTR(objptr) paramOut(cp, #objptr, (objptr)->name())
691 
697 #define UNSERIALIZE_OBJPTR(objptr) \
698  do { \
699  SimObject *sptr; \
700  objParamIn(cp, #objptr, sptr); \
701  objptr = dynamic_cast<decltype(objptr)>(sptr); \
702  } while (0)
703 
707 #define SERIALIZE_MAPPING(member, names, size) \
708  mappingParamOut(cp, #member, names, member, size)
709 
713 #define UNSERIALIZE_MAPPING(member, names, size) \
714  mappingParamIn(cp, #member, names, member, size)
715 
716 #endif // __SERIALIZE_HH__
Serializable::ScopedCheckpointSection::nameOut
void nameOut(CheckpointOut &cp)
Definition: serialize.cc:230
SimObjectResolver
Base class to wrap object resolving functionality.
Definition: sim_object.hh:358
Serializable::unserializeSection
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition: serialize.cc:177
X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:803
CheckpointIn::objNameResolver
SimObjectResolver & objNameResolver
Definition: serialize.hh:74
data
const char data[]
Definition: circlebuf.test.cc:47
CheckpointIn::CheckpointIn
CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver)
Definition: serialize.cc:267
CheckpointIn::visitSection
void visitSection(const std::string &section, IniFile::VisitSectionCallback cb)
Definition: serialize.cc:341
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Serializable::ScopedCheckpointSection::~ScopedCheckpointSection
~ScopedCheckpointSection()
Definition: serialize.cc:211
Serializable
Basic support for object serialization.
Definition: serialize.hh:175
CheckpointIn::sectionExists
bool sectionExists(const std::string &section)
Definition: serialize.cc:335
tokenize
void tokenize(std::vector< std::string > &v, const std::string &s, char token, bool ignore)
Definition: str.cc:65
IniFile
This class represents the contents of a ".ini" file.
Definition: inifile.hh:52
Serializable::unserializeSection
void unserializeSection(CheckpointIn &cp, const std::string &name)
Definition: serialize.hh:297
CheckpointIn::findObj
bool findObj(const std::string &section, const std::string &entry, SimObject *&value)
Definition: serialize.cc:322
arrayParamIn
void arrayParamIn(CheckpointIn &cp, const std::string &name, InsertIterator inserter, ssize_t fixed_size=-1)
Extract values stored in the checkpoint, and assign them to the provided array container.
Definition: serialize.hh:447
std::vector< std::string >
paramIn
void paramIn(CheckpointIn &cp, const std::string &name, T &param)
This function is used for restoring parameters from a checkpoint.
Definition: serialize.hh:383
CheckpointIn::find
bool find(const std::string &section, const std::string &entry, std::string &value)
Definition: serialize.cc:306
Serializable::ScopedCheckpointSection::ScopedCheckpointSection
ScopedCheckpointSection(CP &cp, const std::string &name)
Definition: serialize.hh:209
Serializable::serializeSection
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
Definition: serialize.cc:170
IniFile::VisitSectionCallback
std::function< void(const std::string &, const std::string &)> VisitSectionCallback
Visitor callback that receives key/value pairs.
Definition: inifile.hh:213
Serializable::ScopedCheckpointSection::nameOut
void nameOut(CheckpointIn &cp)
Definition: serialize.hh:227
optParamIn
bool optParamIn(CheckpointIn &cp, const std::string &name, T &param, bool do_warn=true)
This function is used for restoring optional parameters from the checkpoint.
Definition: serialize.hh:363
cp
Definition: cprintf.cc:37
Serializable::Serializable
Serializable()
Definition: serialize.cc:161
arrayParamOut
decltype(std::begin(std::declval< const T & >()), std::end(std::declval< const T & >()), void()) arrayParamOut(CheckpointOut &os, const std::string &name, const T &param)
Definition: serialize.hh:415
Serializable::serialize
virtual void serialize(CheckpointOut &cp) const =0
Serialize an object.
debug_serialize
void debug_serialize(const std::string &cpt_dir)
Definition: serialize.cc:357
Serializable::ScopedCheckpointSection::pushName
void pushName(const char *name)
Definition: serialize.cc:219
CheckpointIn::currentDirectory
static std::string currentDirectory
Definition: serialize.hh:111
Serializable::~Serializable
virtual ~Serializable()
Definition: serialize.cc:165
CheckpointIn::baseFilename
static const char * baseFilename
Definition: serialize.hh:138
ParseParam
Definition: serialize_handlers.hh:75
Serializable::unserializeGlobals
static void unserializeGlobals(CheckpointIn &cp)
Definition: serialize.cc:203
CheckpointIn::setDir
static std::string setDir(const std::string &base_name)
Set the current directory.
Definition: serialize.cc:250
CheckpointIn::db
IniFile * db
Definition: serialize.hh:72
CheckpointIn::_cptDir
const std::string _cptDir
Definition: serialize.hh:76
mappingParamIn
void mappingParamIn(CheckpointIn &cp, const char *sectionName, const char *const names[], T *param, unsigned size)
Restore mappingParamOut.
Definition: serialize.hh:546
inifile.hh
SCMI::token
token
Definition: scmi_platform.hh:77
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
Serializable::ScopedCheckpointSection::ScopedCheckpointSection
ScopedCheckpointSection(CP &cp, const char *name)
This is the constructor for Scoped checkpoint section helper class.
Definition: serialize.hh:203
Serializable::ScopedCheckpointSection
Definition: serialize.hh:178
name
const std::string & name()
Definition: trace.cc:48
CheckpointIn::~CheckpointIn
~CheckpointIn()
Definition: serialize.cc:277
Serializable::unserialize
virtual void unserialize(CheckpointIn &cp)=0
Unserialize an object.
warn_if
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition: logging.hh:263
CheckpointIn::getCptDir
const std::string getCptDir()
Definition: serialize.hh:88
Stats::operator*
Temp operator*(Temp l, Temp r)
Definition: statistics.hh:2845
CheckpointIn::entryExists
bool entryExists(const std::string &section, const std::string &entry)
Definition: serialize.cc:291
serialize_handlers.hh
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
logging.hh
Serializable::serializeAll
static void serializeAll(const std::string &cpt_dir)
Serializes all the SimObjects.
Definition: serialize.cc:184
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:64
Serializable::ScopedCheckpointSection::operator=
ScopedCheckpointSection & operator=(const ScopedCheckpointSection &)=delete
Serializable::currentSection
static const std::string & currentSection()
Gets the fully-qualified name of the active section.
Definition: serialize.cc:238
mappingParamOut
void mappingParamOut(CheckpointOut &os, const char *sectionName, const char *const names[], const T *param, unsigned size)
Serialize a mapping represented as two arrays: one containing names and the other containing values.
Definition: serialize.hh:532
CheckpointIn::dir
static std::string dir()
Get the current checkout directory name.
Definition: serialize.cc:262
objParamIn
void objParamIn(CheckpointIn &cp, const std::string &name, SimObject *&param)
Definition: serialize.cc:348
Serializable::serializeSection
void serializeSection(CheckpointOut &cp, const std::string &name) const
Definition: serialize.hh:276
paramOut
void paramOut(CheckpointOut &os, const std::string &name, const T &param)
This function is used for writing parameters to a checkpoint.
Definition: serialize.hh:333
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:219
CheckpointIn
Definition: serialize.hh:68
ShowParam::show
static void show(std::ostream &os, const T &value)
Definition: serialize_handlers.hh:124
paramInImpl
bool paramInImpl(CheckpointIn &cp, const std::string &name, T &param)
Definition: serialize.hh:342
Serializable::path
static std::stack< std::string > path
Definition: serialize.hh:321
Serializable::ScopedCheckpointSection::ScopedCheckpointSection
ScopedCheckpointSection()=delete
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:141

Generated on Tue Mar 23 2021 19:41:28 for gem5 by doxygen 1.8.17