gem5  v20.1.0.0
serialize.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, 2018 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 <list>
52 #include <map>
53 #include <stack>
54 #include <set>
55 #include <vector>
56 
57 #include "base/bitunion.hh"
58 #include "base/logging.hh"
59 #include "base/str.hh"
60 
61 class IniFile;
62 class SimObject;
64 
65 typedef std::ostream CheckpointOut;
66 
68 {
69  private:
70 
72 
74 
75  const std::string _cptDir;
76 
77  public:
78  CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver);
79  ~CheckpointIn();
80 
87  const std::string getCptDir() { return _cptDir; }
88 
89  bool find(const std::string &section, const std::string &entry,
90  std::string &value);
91 
92  bool findObj(const std::string &section, const std::string &entry,
93  SimObject *&value);
94 
95  bool entryExists(const std::string &section, const std::string &entry);
96  bool sectionExists(const std::string &section); //end of api_checkout group
98 
99  // The following static functions have to do with checkpoint
100  // creation rather than restoration. This class makes a handy
101  // namespace for them though. Currently no Checkpoint object is
102  // created on serialization (only unserialization) so we track the
103  // directory name as a global. It would be nice to change this
104  // someday
105 
106  private:
107  // current directory we're serializing into.
108  static std::string currentDirectory;
109 
110 
111  public:
120  static std::string setDir(const std::string &base_name);
121 
132  static std::string dir();
133 
134  // Filename for base checkpoint file within directory.
135  static const char *baseFilename;
136 };
137 
173 {
174  protected:
176  public:
199  template<class CP>
200  ScopedCheckpointSection(CP &cp, const char *name) {
201  pushName(name);
202  nameOut(cp);
203  }
204 
205  template<class CP>
206  ScopedCheckpointSection(CP &cp, const std::string &name) {
207  pushName(name.c_str());
208  nameOut(cp);
209  } //end of api_serialize group
211 
213 
214  ScopedCheckpointSection() = delete;
217  const ScopedCheckpointSection &) = delete;
219  ScopedCheckpointSection &&) = delete;
220 
221  private:
222  void pushName(const char *name);
223  void nameOut(CheckpointOut &cp);
225  };
226 
227  public:
231  Serializable();
232  virtual ~Serializable();
233 
243  virtual void serialize(CheckpointOut &cp) const = 0;
244 
254  virtual void unserialize(CheckpointIn &cp) = 0;
255 
269  void serializeSection(CheckpointOut &cp, const char *name) const;
270 
274  void serializeSection(CheckpointOut &cp, const std::string &name) const {
275  serializeSection(cp, name.c_str());
276  }
277 
290  void unserializeSection(CheckpointIn &cp, const char *name);
291 
295  void unserializeSection(CheckpointIn &cp, const std::string &name) {
296  unserializeSection(cp, name.c_str());
297  }
298 
304  static const std::string &currentSection();
305 
311  static void serializeAll(const std::string &cpt_dir);
312 
316  static void unserializeGlobals(CheckpointIn &cp);
317 
318  private:
319  static std::stack<std::string> path;
320 };
321 
325 template <class T>
326 bool
327 parseParam(const std::string &s, T &value)
328 {
329  // The base implementations use to_number for parsing and '<<' for
330  // displaying, suitable for integer types.
331  return to_number(s, value);
332 }
333 
337 template <class T>
338 void
339 showParam(CheckpointOut &os, const T &value)
340 {
341  os << value;
342 }
343 
347 template <class T>
348 bool
349 parseParam(const std::string &s, BitUnionType<T> &value)
350 {
351  // Zero initialize storage to avoid leaking an uninitialized value
353  auto res = to_number(s, storage);
354  value = storage;
355  return res;
356 }
357 
361 template <class T>
362 void
364 {
365  auto storage = static_cast<BitUnionBaseType<T>>(value);
366 
367  // For a BitUnion8, the storage type is an unsigned char.
368  // Since we want to serialize a number we need to cast to
369  // unsigned int
370  os << ((sizeof(storage) == 1) ?
371  static_cast<unsigned int>(storage) : storage);
372 }
373 
377 template <>
378 inline void
379 showParam(CheckpointOut &os, const char &value)
380 {
381  // Treat 8-bit ints (chars) as ints on output, not as chars
382  os << (int)value;
383 }
384 
388 template <>
389 inline void
390 showParam(CheckpointOut &os, const signed char &value)
391 {
392  os << (int)value;
393 }
394 
398 template <>
399 inline void
400 showParam(CheckpointOut &os, const unsigned char &value)
401 {
402  os << (unsigned int)value;
403 }
404 
408 template <>
409 inline bool
410 parseParam(const std::string &s, float &value)
411 {
412  return to_number(s, value);
413 }
414 
418 template <>
419 inline bool
420 parseParam(const std::string &s, double &value)
421 {
422  return to_number(s, value);
423 }
424 
428 template <>
429 inline bool
430 parseParam(const std::string &s, bool &value)
431 {
432  return to_bool(s, value);
433 }
434 
438 template <>
439 inline void
440 showParam(CheckpointOut &os, const bool &value)
441 {
442  // Display bools as strings
443  os << (value ? "true" : "false");
444 }
445 
449 template <>
450 inline bool
451 parseParam(const std::string &s, std::string &value)
452 {
453  // String requires no processing to speak of
454  value = s;
455  return true;
456 }
457 
465 template <class T>
466 void
467 paramOut(CheckpointOut &os, const std::string &name, const T &param)
468 {
469  os << name << "=";
470  showParam(os, param);
471  os << "\n";
472 }
473 
481 template <class T>
482 void
483 paramIn(CheckpointIn &cp, const std::string &name, T &param)
484 {
485  const std::string &section(Serializable::currentSection());
486  std::string str;
487  if (!cp.find(section, name, str) || !parseParam(str, param)) {
488  fatal("Can't unserialize '%s:%s'\n", section, name);
489  }
490 }
491 
505 template <class T>
506 bool
507 optParamIn(CheckpointIn &cp, const std::string &name,
508  T &param, bool warn = true)
509 {
510  const std::string &section(Serializable::currentSection());
511  std::string str;
512  if (!cp.find(section, name, str) || !parseParam(str, param)) {
513  if (warn)
514  warn("optional parameter %s:%s not present\n", section, name);
515  return false;
516  } else {
517  return true;
518  }
519 }
520 
524 template <class T>
525 void
526 arrayParamOut(CheckpointOut &os, const std::string &name,
527  const std::vector<T> &param)
528 {
529  typename std::vector<T>::size_type size = param.size();
530  os << name << "=";
531  if (size > 0)
532  showParam(os, param[0]);
533  for (typename std::vector<T>::size_type i = 1; i < size; ++i) {
534  os << " ";
535  showParam(os, param[i]);
536  }
537  os << "\n";
538 }
539 
543 template <class T>
544 void
545 arrayParamOut(CheckpointOut &os, const std::string &name,
546  const std::list<T> &param)
547 {
548  typename std::list<T>::const_iterator it = param.begin();
549 
550  os << name << "=";
551  if (param.size() > 0)
552  showParam(os, *it);
553  it++;
554  while (it != param.end()) {
555  os << " ";
556  showParam(os, *it);
557  it++;
558  }
559  os << "\n";
560 }
561 
565 template <class T>
566 void
567 arrayParamOut(CheckpointOut &os, const std::string &name,
568  const std::set<T> &param)
569 {
570  typename std::set<T>::const_iterator it = param.begin();
571 
572  os << name << "=";
573  if (param.size() > 0)
574  showParam(os, *it);
575  it++;
576  while (it != param.end()) {
577  os << " ";
578  showParam(os, *it);
579  it++;
580  }
581  os << "\n";
582 }
583 
587 template <class T>
588 void
589 arrayParamOut(CheckpointOut &os, const std::string &name,
590  const T *param, unsigned size)
591 {
592  os << name << "=";
593  if (size > 0)
594  showParam(os, param[0]);
595  for (unsigned i = 1; i < size; ++i) {
596  os << " ";
597  showParam(os, param[i]);
598  }
599  os << "\n";
600 }
601 
613 template <class T>
614 void
615 arrayParamIn(CheckpointIn &cp, const std::string &name,
616  T *param, unsigned size)
617 {
618  const std::string &section(Serializable::currentSection());
619  std::string str;
620  if (!cp.find(section, name, str)) {
621  fatal("Can't unserialize '%s:%s'\n", section, name);
622  }
623 
624  // code below stolen from VectorParam<T>::parse().
625  // it would be nice to unify these somehow...
626 
628 
629  tokenize(tokens, str, ' ');
630 
631  // Need this if we were doing a vector
632  // value.resize(tokens.size());
633 
634  fatal_if(tokens.size() != size,
635  "Array size mismatch on %s:%s (Got %u, expected %u)'\n",
636  section, name, tokens.size(), size);
637 
638  for (std::vector<std::string>::size_type i = 0; i < tokens.size(); i++) {
639  // need to parse into local variable to handle vector<bool>,
640  // for which operator[] returns a special reference class
641  // that's not the same as 'bool&', (since it's a packed
642  // vector)
643  T scalar_value;
644  if (!parseParam(tokens[i], scalar_value)) {
645  std::string err("could not parse \"");
646 
647  err += str;
648  err += "\"";
649 
650  fatal(err);
651  }
652 
653  // assign parsed value to vector
654  param[i] = scalar_value;
655  }
656 }
657 
661 template <class T>
662 void
663 arrayParamIn(CheckpointIn &cp, const std::string &name, std::vector<T> &param)
664 {
665  const std::string &section(Serializable::currentSection());
666  std::string str;
667  if (!cp.find(section, name, str)) {
668  fatal("Can't unserialize '%s:%s'\n", section, name);
669  }
670 
671  // code below stolen from VectorParam<T>::parse().
672  // it would be nice to unify these somehow...
673 
675 
676  tokenize(tokens, str, ' ');
677 
678  // Need this if we were doing a vector
679  // value.resize(tokens.size());
680 
681  param.resize(tokens.size());
682 
683  for (std::vector<std::string>::size_type i = 0; i < tokens.size(); i++) {
684  // need to parse into local variable to handle vector<bool>,
685  // for which operator[] returns a special reference class
686  // that's not the same as 'bool&', (since it's a packed
687  // vector)
688  T scalar_value;
689  if (!parseParam(tokens[i], scalar_value)) {
690  std::string err("could not parse \"");
691 
692  err += str;
693  err += "\"";
694 
695  fatal(err);
696  }
697 
698  // assign parsed value to vector
699  param[i] = scalar_value;
700  }
701 }
702 
706 template <class T>
707 void
708 arrayParamIn(CheckpointIn &cp, const std::string &name, std::list<T> &param)
709 {
710  const std::string &section(Serializable::currentSection());
711  std::string str;
712  if (!cp.find(section, name, str)) {
713  fatal("Can't unserialize '%s:%s'\n", section, name);
714  }
715  param.clear();
716 
718  tokenize(tokens, str, ' ');
719 
720  for (std::vector<std::string>::size_type i = 0; i < tokens.size(); i++) {
721  T scalar_value;
722  if (!parseParam(tokens[i], scalar_value)) {
723  std::string err("could not parse \"");
724 
725  err += str;
726  err += "\"";
727 
728  fatal(err);
729  }
730 
731  // assign parsed value to vector
732  param.push_back(scalar_value);
733  }
734 }
735 
739 template <class T>
740 void
741 arrayParamIn(CheckpointIn &cp, const std::string &name, std::set<T> &param)
742 {
743  const std::string &section(Serializable::currentSection());
744  std::string str;
745  if (!cp.find(section, name, str)) {
746  fatal("Can't unserialize '%s:%s'\n", section, name);
747  }
748  param.clear();
749 
751  tokenize(tokens, str, ' ');
752 
753  for (std::vector<std::string>::size_type i = 0; i < tokens.size(); i++) {
754  T scalar_value;
755  if (!parseParam(tokens[i], scalar_value)) {
756  std::string err("could not parse \"");
757 
758  err += str;
759  err += "\"";
760 
761  fatal(err);
762  }
763 
764  // assign parsed value to vector
765  param.insert(scalar_value);
766  }
767 }
768 
769 void
770 debug_serialize(const std::string &cpt_dir);
771 
772 
776 void
777 objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
778 
779 //
780 // These macros are streamlined to use in serialize/unserialize
781 // functions. It's assumed that serialize() has a parameter 'os' for
782 // the ostream, and unserialize() has parameters 'cp' and 'section'.
783 
784 
790 #define SERIALIZE_SCALAR(scalar) paramOut(cp, #scalar, scalar)
791 
797 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar)
798 
804 #define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar)
805 
806 // ENUMs are like SCALARs, but we cast them to ints on the way out
807 
813 #define SERIALIZE_ENUM(scalar) paramOut(cp, #scalar, (int)scalar)
814 
820 #define UNSERIALIZE_ENUM(scalar) \
821  do { \
822  int tmp; \
823  paramIn(cp, #scalar, tmp); \
824  scalar = static_cast<decltype(scalar)>(tmp); \
825  } while (0)
826 
832 #define SERIALIZE_ARRAY(member, size) \
833  arrayParamOut(cp, #member, member, size)
834 
840 #define UNSERIALIZE_ARRAY(member, size) \
841  arrayParamIn(cp, #member, member, size)
842 
848 #define SERIALIZE_CONTAINER(member) \
849  arrayParamOut(cp, #member, member)
850 
856 #define UNSERIALIZE_CONTAINER(member) \
857  arrayParamIn(cp, #member, member)
858 
864 #define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
865 
871 #define UNSERIALIZE_EVENT(event) \
872  do { \
873  event.unserializeSection(cp, #event); \
874  eventQueue()->checkpointReschedule(&event); \
875  } while (0)
876 
882 #define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
883 
889 #define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
890 
896 #define SERIALIZE_OBJPTR(objptr) paramOut(cp, #objptr, (objptr)->name())
897 
903 #define UNSERIALIZE_OBJPTR(objptr) \
904  do { \
905  SimObject *sptr; \
906  objParamIn(cp, #objptr, sptr); \
907  objptr = dynamic_cast<decltype(objptr)>(sptr); \
908  } while (0)
909 
910 #endif // __SERIALIZE_HH__
Serializable::ScopedCheckpointSection::nameOut
void nameOut(CheckpointOut &cp)
Definition: serialize.cc:231
tokenize
void tokenize(vector< string > &v, const string &s, char token, bool ignore)
Definition: str.cc:67
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:183
SimObjectResolver
Base class to wrap object resolving functionality.
Definition: sim_object.hh:294
to_bool
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:186
Serializable::unserializeSection
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition: serialize.cc:178
X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:803
warn
#define warn(...)
Definition: logging.hh:239
CheckpointIn::objNameResolver
SimObjectResolver & objNameResolver
Definition: serialize.hh:73
CheckpointIn::CheckpointIn
CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver)
Definition: serialize.cc:268
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Serializable::ScopedCheckpointSection::~ScopedCheckpointSection
~ScopedCheckpointSection()
Definition: serialize.cc:212
Serializable
Basic support for object serialization.
Definition: serialize.hh:172
arrayParamOut
void arrayParamOut(CheckpointOut &os, const std::string &name, const std::vector< T > &param)
Definition: serialize.hh:526
CheckpointIn::sectionExists
bool sectionExists(const std::string &section)
Definition: serialize.cc:334
IniFile
This class represents the contents of a ".ini" file.
Definition: inifile.hh:51
Serializable::unserializeSection
void unserializeSection(CheckpointIn &cp, const std::string &name)
Definition: serialize.hh:295
CheckpointIn::findObj
bool findObj(const std::string &section, const std::string &entry, SimObject *&value)
Definition: serialize.cc:321
std::vector
STL vector class.
Definition: stl.hh:37
paramIn
void paramIn(CheckpointIn &cp, const std::string &name, T &param)
This function is used for restoring parameters from a checkpoint.
Definition: serialize.hh:483
CheckpointIn::setDir
static std::string setDir(const std::string &base_name)
Set the current directory.
Definition: serialize.cc:251
arrayParamIn
void arrayParamIn(CheckpointIn &cp, const std::string &name, T *param, unsigned size)
Extract values stored in the checkpoint, and assign them to the provided array container.
Definition: serialize.hh:615
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:206
Serializable::serializeSection
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
Definition: serialize.cc:171
str.hh
Serializable::ScopedCheckpointSection::nameOut
void nameOut(CheckpointIn &cp)
Definition: serialize.hh:224
to_number
bool to_number(const std::string &value, VecPredRegContainer< NumBits, Packed > &p)
Helper functions used for serialization/de-serialization.
Definition: vec_pred_reg.hh:379
cp
Definition: cprintf.cc:40
parseParam
bool parseParam(const std::string &s, T &value)
Definition: serialize.hh:327
Serializable::Serializable
Serializable()
Definition: serialize.cc:162
showParam
void showParam(CheckpointOut &os, const T &value)
Definition: serialize.hh:339
Serializable::serialize
virtual void serialize(CheckpointOut &cp) const =0
Serialize an object.
debug_serialize
void debug_serialize(const std::string &cpt_dir)
Serializable::ScopedCheckpointSection::pushName
void pushName(const char *name)
Definition: serialize.cc:220
Serializable::~Serializable
virtual ~Serializable()
Definition: serialize.cc:166
CheckpointIn::baseFilename
static const char * baseFilename
Definition: serialize.hh:135
Serializable::unserializeGlobals
static void unserializeGlobals(CheckpointIn &cp)
Definition: serialize.cc:204
optParamIn
bool optParamIn(CheckpointIn &cp, const std::string &name, T &param, bool warn=true)
This function is used for restoring optional parameters from the checkpoint.
Definition: serialize.hh:507
bitunion.hh
CheckpointIn::db
IniFile * db
Definition: serialize.hh:71
CheckpointIn::_cptDir
const std::string _cptDir
Definition: serialize.hh:75
BitfieldBackend::BitUnionOperators
Definition: bitunion.hh:247
Serializable::ScopedCheckpointSection::ScopedCheckpointSection
ScopedCheckpointSection(CP &cp, const char *name)
This is the constructor for Scoped checkpoint section helper class.
Definition: serialize.hh:200
Serializable::ScopedCheckpointSection
Definition: serialize.hh:175
name
const std::string & name()
Definition: trace.cc:50
CheckpointIn::~CheckpointIn
~CheckpointIn()
Definition: serialize.cc:277
Serializable::unserialize
virtual void unserialize(CheckpointIn &cp)=0
Unserialize an object.
CheckpointIn::dir
static std::string dir()
Get the current checkout directory name.
Definition: serialize.cc:263
CheckpointIn::getCptDir
const std::string getCptDir()
Definition: serialize.hh:87
CheckpointIn::entryExists
bool entryExists(const std::string &section, const std::string &entry)
Definition: serialize.cc:291
logging.hh
Serializable::serializeAll
static void serializeAll(const std::string &cpt_dir)
Serializes all the SimObjects.
Definition: serialize.cc:185
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
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:239
ArmISA::err
Bitfield< 6 > err
Definition: miscregs_types.hh:744
objParamIn
void objParamIn(CheckpointIn &cp, const std::string &name, SimObject *&param)
std::list
STL list class.
Definition: stl.hh:51
Serializable::serializeSection
void serializeSection(CheckpointOut &cp, const std::string &name) const
Definition: serialize.hh:274
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
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:467
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:67
CheckpointIn::currentDirectory
static std::string currentDirectory
Definition: serialize.hh:108
Serializable::path
static std::stack< std::string > path
Definition: serialize.hh:319
Serializable::ScopedCheckpointSection::ScopedCheckpointSection
ScopedCheckpointSection()=delete
BitUnionBaseType
typename BitfieldBackend::BitUnionBaseType< T >::Type BitUnionBaseType
Definition: bitunion.hh:445
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17