gem5  v20.0.0.3
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 
85  const std::string getCptDir() { return _cptDir; }
86 
87  bool find(const std::string &section, const std::string &entry,
88  std::string &value);
89 
90  bool findObj(const std::string &section, const std::string &entry,
91  SimObject *&value);
92 
93  bool entryExists(const std::string &section, const std::string &entry);
94  bool sectionExists(const std::string &section); //end of api_checkout group
96 
97  // The following static functions have to do with checkpoint
98  // creation rather than restoration. This class makes a handy
99  // namespace for them though. Currently no Checkpoint object is
100  // created on serialization (only unserialization) so we track the
101  // directory name as a global. It would be nice to change this
102  // someday
103 
104  private:
105  // current directory we're serializing into.
106  static std::string currentDirectory;
107 
108 
109  public:
118  static std::string setDir(const std::string &base_name);
119 
130  static std::string dir();
131 
132  // Filename for base checkpoint file within directory.
133  static const char *baseFilename;
134 };
135 
167 {
168  protected:
187  public:
192  template<class CP>
193  ScopedCheckpointSection(CP &cp, const char *name) {
194  pushName(name);
195  nameOut(cp);
196  }
197 
198  template<class CP>
199  ScopedCheckpointSection(CP &cp, const std::string &name) {
200  pushName(name.c_str());
201  nameOut(cp);
202  } //end of api_serialize group
204 
206 
207  ScopedCheckpointSection() = delete;
209  ScopedCheckpointSection &operator=(
210  const ScopedCheckpointSection &) = delete;
211  ScopedCheckpointSection &operator=(
212  ScopedCheckpointSection &&) = delete;
213 
214  private:
215  void pushName(const char *name);
216  void nameOut(CheckpointOut &cp);
217  void nameOut(CheckpointIn &cp) {};
218  };
219 
220  public:
224  Serializable();
225  virtual ~Serializable();
226 
236  virtual void serialize(CheckpointOut &cp) const = 0;
237 
247  virtual void unserialize(CheckpointIn &cp) = 0;
248 
262  void serializeSection(CheckpointOut &cp, const char *name) const;
263 
267  void serializeSection(CheckpointOut &cp, const std::string &name) const {
268  serializeSection(cp, name.c_str());
269  }
270 
283  void unserializeSection(CheckpointIn &cp, const char *name);
284 
288  void unserializeSection(CheckpointIn &cp, const std::string &name) {
289  unserializeSection(cp, name.c_str());
290  }
291 
297  static const std::string &currentSection();
298 
302  static void serializeAll(const std::string &cpt_dir);
303 
307  static void unserializeGlobals(CheckpointIn &cp);
308 
309  private:
310  static std::stack<std::string> path;
311 };
312 
316 template <class T>
317 bool
318 parseParam(const std::string &s, T &value)
319 {
320  // The base implementations use to_number for parsing and '<<' for
321  // displaying, suitable for integer types.
322  return to_number(s, value);
323 }
324 
328 template <class T>
329 void
330 showParam(CheckpointOut &os, const T &value)
331 {
332  os << value;
333 }
334 
338 template <class T>
339 bool
340 parseParam(const std::string &s, BitUnionType<T> &value)
341 {
342  // Zero initialize storage to avoid leaking an uninitialized value
344  auto res = to_number(s, storage);
345  value = storage;
346  return res;
347 }
348 
352 template <class T>
353 void
355 {
356  auto storage = static_cast<BitUnionBaseType<T>>(value);
357 
358  // For a BitUnion8, the storage type is an unsigned char.
359  // Since we want to serialize a number we need to cast to
360  // unsigned int
361  os << ((sizeof(storage) == 1) ?
362  static_cast<unsigned int>(storage) : storage);
363 }
364 
368 template <>
369 inline void
370 showParam(CheckpointOut &os, const char &value)
371 {
372  // Treat 8-bit ints (chars) as ints on output, not as chars
373  os << (int)value;
374 }
375 
379 template <>
380 inline void
381 showParam(CheckpointOut &os, const signed char &value)
382 {
383  os << (int)value;
384 }
385 
389 template <>
390 inline void
391 showParam(CheckpointOut &os, const unsigned char &value)
392 {
393  os << (unsigned int)value;
394 }
395 
399 template <>
400 inline bool
401 parseParam(const std::string &s, float &value)
402 {
403  return to_number(s, value);
404 }
405 
409 template <>
410 inline bool
411 parseParam(const std::string &s, double &value)
412 {
413  return to_number(s, value);
414 }
415 
419 template <>
420 inline bool
421 parseParam(const std::string &s, bool &value)
422 {
423  return to_bool(s, value);
424 }
425 
429 template <>
430 inline void
431 showParam(CheckpointOut &os, const bool &value)
432 {
433  // Display bools as strings
434  os << (value ? "true" : "false");
435 }
436 
440 template <>
441 inline bool
442 parseParam(const std::string &s, std::string &value)
443 {
444  // String requires no processing to speak of
445  value = s;
446  return true;
447 }
448 
452 template <class T>
453 void
454 paramOut(CheckpointOut &os, const std::string &name, const T &param)
455 {
456  os << name << "=";
457  showParam(os, param);
458  os << "\n";
459 }
460 
464 template <class T>
465 void
466 paramIn(CheckpointIn &cp, const std::string &name, T &param)
467 {
468  const std::string &section(Serializable::currentSection());
469  std::string str;
470  if (!cp.find(section, name, str) || !parseParam(str, param)) {
471  fatal("Can't unserialize '%s:%s'\n", section, name);
472  }
473 }
474 
478 template <class T>
479 bool
480 optParamIn(CheckpointIn &cp, const std::string &name,
481  T &param, bool warn = true)
482 {
483  const std::string &section(Serializable::currentSection());
484  std::string str;
485  if (!cp.find(section, name, str) || !parseParam(str, param)) {
486  if (warn)
487  warn("optional parameter %s:%s not present\n", section, name);
488  return false;
489  } else {
490  return true;
491  }
492 }
493 
497 template <class T>
498 void
499 arrayParamOut(CheckpointOut &os, const std::string &name,
500  const std::vector<T> &param)
501 {
502  typename std::vector<T>::size_type size = param.size();
503  os << name << "=";
504  if (size > 0)
505  showParam(os, param[0]);
506  for (typename std::vector<T>::size_type i = 1; i < size; ++i) {
507  os << " ";
508  showParam(os, param[i]);
509  }
510  os << "\n";
511 }
512 
516 template <class T>
517 void
518 arrayParamOut(CheckpointOut &os, const std::string &name,
519  const std::list<T> &param)
520 {
521  typename std::list<T>::const_iterator it = param.begin();
522 
523  os << name << "=";
524  if (param.size() > 0)
525  showParam(os, *it);
526  it++;
527  while (it != param.end()) {
528  os << " ";
529  showParam(os, *it);
530  it++;
531  }
532  os << "\n";
533 }
534 
538 template <class T>
539 void
540 arrayParamOut(CheckpointOut &os, const std::string &name,
541  const std::set<T> &param)
542 {
543  typename std::set<T>::const_iterator it = param.begin();
544 
545  os << name << "=";
546  if (param.size() > 0)
547  showParam(os, *it);
548  it++;
549  while (it != param.end()) {
550  os << " ";
551  showParam(os, *it);
552  it++;
553  }
554  os << "\n";
555 }
556 
560 template <class T>
561 void
562 arrayParamOut(CheckpointOut &os, const std::string &name,
563  const T *param, unsigned size)
564 {
565  os << name << "=";
566  if (size > 0)
567  showParam(os, param[0]);
568  for (unsigned i = 1; i < size; ++i) {
569  os << " ";
570  showParam(os, param[i]);
571  }
572  os << "\n";
573 }
574 
586 template <class T>
587 void
588 arrayParamIn(CheckpointIn &cp, const std::string &name,
589  T *param, unsigned size)
590 {
591  const std::string &section(Serializable::currentSection());
592  std::string str;
593  if (!cp.find(section, name, str)) {
594  fatal("Can't unserialize '%s:%s'\n", section, name);
595  }
596 
597  // code below stolen from VectorParam<T>::parse().
598  // it would be nice to unify these somehow...
599 
601 
602  tokenize(tokens, str, ' ');
603 
604  // Need this if we were doing a vector
605  // value.resize(tokens.size());
606 
607  fatal_if(tokens.size() != size,
608  "Array size mismatch on %s:%s (Got %u, expected %u)'\n",
609  section, name, tokens.size(), size);
610 
611  for (std::vector<std::string>::size_type i = 0; i < tokens.size(); i++) {
612  // need to parse into local variable to handle vector<bool>,
613  // for which operator[] returns a special reference class
614  // that's not the same as 'bool&', (since it's a packed
615  // vector)
616  T scalar_value;
617  if (!parseParam(tokens[i], scalar_value)) {
618  std::string err("could not parse \"");
619 
620  err += str;
621  err += "\"";
622 
623  fatal(err);
624  }
625 
626  // assign parsed value to vector
627  param[i] = scalar_value;
628  }
629 }
630 
634 template <class T>
635 void
636 arrayParamIn(CheckpointIn &cp, const std::string &name, std::vector<T> &param)
637 {
638  const std::string &section(Serializable::currentSection());
639  std::string str;
640  if (!cp.find(section, name, str)) {
641  fatal("Can't unserialize '%s:%s'\n", section, name);
642  }
643 
644  // code below stolen from VectorParam<T>::parse().
645  // it would be nice to unify these somehow...
646 
648 
649  tokenize(tokens, str, ' ');
650 
651  // Need this if we were doing a vector
652  // value.resize(tokens.size());
653 
654  param.resize(tokens.size());
655 
656  for (std::vector<std::string>::size_type i = 0; i < tokens.size(); i++) {
657  // need to parse into local variable to handle vector<bool>,
658  // for which operator[] returns a special reference class
659  // that's not the same as 'bool&', (since it's a packed
660  // vector)
661  T scalar_value;
662  if (!parseParam(tokens[i], scalar_value)) {
663  std::string err("could not parse \"");
664 
665  err += str;
666  err += "\"";
667 
668  fatal(err);
669  }
670 
671  // assign parsed value to vector
672  param[i] = scalar_value;
673  }
674 }
675 
679 template <class T>
680 void
681 arrayParamIn(CheckpointIn &cp, const std::string &name, std::list<T> &param)
682 {
683  const std::string &section(Serializable::currentSection());
684  std::string str;
685  if (!cp.find(section, name, str)) {
686  fatal("Can't unserialize '%s:%s'\n", section, name);
687  }
688  param.clear();
689 
691  tokenize(tokens, str, ' ');
692 
693  for (std::vector<std::string>::size_type i = 0; i < tokens.size(); i++) {
694  T scalar_value;
695  if (!parseParam(tokens[i], scalar_value)) {
696  std::string err("could not parse \"");
697 
698  err += str;
699  err += "\"";
700 
701  fatal(err);
702  }
703 
704  // assign parsed value to vector
705  param.push_back(scalar_value);
706  }
707 }
708 
712 template <class T>
713 void
714 arrayParamIn(CheckpointIn &cp, const std::string &name, std::set<T> &param)
715 {
716  const std::string &section(Serializable::currentSection());
717  std::string str;
718  if (!cp.find(section, name, str)) {
719  fatal("Can't unserialize '%s:%s'\n", section, name);
720  }
721  param.clear();
722 
724  tokenize(tokens, str, ' ');
725 
726  for (std::vector<std::string>::size_type i = 0; i < tokens.size(); i++) {
727  T scalar_value;
728  if (!parseParam(tokens[i], scalar_value)) {
729  std::string err("could not parse \"");
730 
731  err += str;
732  err += "\"";
733 
734  fatal(err);
735  }
736 
737  // assign parsed value to vector
738  param.insert(scalar_value);
739  }
740 }
741 
742 void
743 debug_serialize(const std::string &cpt_dir);
744 
745 
749 void
750 objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
751 
752 //
753 // These macros are streamlined to use in serialize/unserialize
754 // functions. It's assumed that serialize() has a parameter 'os' for
755 // the ostream, and unserialize() has parameters 'cp' and 'section'.
756 
757 
763 #define SERIALIZE_SCALAR(scalar) paramOut(cp, #scalar, scalar)
764 
770 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar)
771 
777 #define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar)
778 
779 // ENUMs are like SCALARs, but we cast them to ints on the way out
780 
786 #define SERIALIZE_ENUM(scalar) paramOut(cp, #scalar, (int)scalar)
787 
793 #define UNSERIALIZE_ENUM(scalar) \
794  do { \
795  int tmp; \
796  paramIn(cp, #scalar, tmp); \
797  scalar = static_cast<decltype(scalar)>(tmp); \
798  } while (0)
799 
805 #define SERIALIZE_ARRAY(member, size) \
806  arrayParamOut(cp, #member, member, size)
807 
813 #define UNSERIALIZE_ARRAY(member, size) \
814  arrayParamIn(cp, #member, member, size)
815 
821 #define SERIALIZE_CONTAINER(member) \
822  arrayParamOut(cp, #member, member)
823 
829 #define UNSERIALIZE_CONTAINER(member) \
830  arrayParamIn(cp, #member, member)
831 
837 #define SERIALIZE_EVENT(event) event.serializeSection(cp, #event);
838 
844 #define UNSERIALIZE_EVENT(event) \
845  do { \
846  event.unserializeSection(cp, #event); \
847  eventQueue()->checkpointReschedule(&event); \
848  } while (0)
849 
855 #define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
856 
862 #define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
863 
869 #define SERIALIZE_OBJPTR(objptr) paramOut(cp, #objptr, (objptr)->name())
870 
876 #define UNSERIALIZE_OBJPTR(objptr) \
877  do { \
878  SimObject *sptr; \
879  objParamIn(cp, #objptr, sptr); \
880  objptr = dynamic_cast<decltype(objptr)>(sptr); \
881  } while (0)
882 
883 #endif // __SERIALIZE_HH__
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:186
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:171
const std::string & name()
Definition: trace.cc:50
bool sectionExists(const std::string &section)
Definition: serialize.cc:308
Bitfield< 7 > i
static std::stack< std::string > path
Definition: serialize.hh:310
void arrayParamOut(CheckpointOut &os, const std::string &name, const std::vector< T > &param)
Definition: serialize.hh:499
SimObjectResolver & objNameResolver
Definition: serialize.hh:73
static std::string currentDirectory
Definition: serialize.hh:106
bool findObj(const std::string &section, const std::string &entry, SimObject *&value)
Definition: serialize.cc:295
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:588
static std::string setDir(const std::string &base_name)
Set the current directory.
Definition: serialize.cc:251
Definition: cprintf.cc:40
void paramIn(CheckpointIn &cp, const std::string &name, T &param)
Definition: serialize.hh:466
void unserializeSection(CheckpointIn &cp, const std::string &name)
Definition: serialize.hh:288
Base class to wrap object resolving functionality.
Definition: sim_object.hh:273
Bitfield< 17 > os
Definition: misc.hh:803
STL vector class.
Definition: stl.hh:37
ScopedCheckpointSection(CP &cp, const std::string &name)
Definition: serialize.hh:199
bool find(const std::string &section, const std::string &entry, std::string &value)
Definition: serialize.cc:289
const std::string _cptDir
Definition: serialize.hh:75
void showParam(CheckpointOut &os, const T &value)
Definition: serialize.hh:330
Bitfield< 4 > s
bool optParamIn(CheckpointIn &cp, const std::string &name, T &param, bool warn=true)
Definition: serialize.hh:480
void debug_serialize(const std::string &cpt_dir)
bool parseParam(const std::string &s, T &value)
Definition: serialize.hh:318
void serialize(const ThreadContext &tc, CheckpointOut &cp)
Thread context serialization helpers.
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:199
STL list class.
Definition: stl.hh:51
bool to_number(const std::string &value, VecPredRegContainer< NumBits, Packed > &p)
Helper functions used for serialization/de-serialization.
Basic support for object serialization.
Definition: serialize.hh:166
ScopedCheckpointSection(CP &cp, const char *name)
Definition: serialize.hh:193
static std::string dir()
Get the current checkout directory name.
Definition: serialize.cc:263
std::ostream CheckpointOut
Definition: serialize.hh:63
const std::string getCptDir()
Definition: serialize.hh:85
typename BitfieldBackend::BitUnionBaseType< T >::Type BitUnionBaseType
Definition: bitunion.hh:414
static const std::string & currentSection()
Gets the fully-qualified name of the active section.
Definition: serialize.cc:239
bool entryExists(const std::string &section, const std::string &entry)
Definition: serialize.cc:283
void nameOut(CheckpointIn &cp)
Definition: serialize.hh:217
void tokenize(vector< string > &v, const string &s, char token, bool ignore)
Definition: str.cc:67
IniFile * db
Definition: serialize.hh:71
void unserialize(ThreadContext &tc, CheckpointIn &cp)
void serializeSection(CheckpointOut &cp, const std::string &name) const
Definition: serialize.hh:267
#define warn(...)
Definition: logging.hh:208
Scoped checkpoint section helper class.
Definition: serialize.hh:186
void objParamIn(CheckpointIn &cp, const std::string &name, SimObject *&param)
Abstract superclass for simulation objects.
Definition: sim_object.hh:93
This class represents the contents of a ".ini" file.
Definition: inifile.hh:51
static const char * baseFilename
Definition: serialize.hh:133
void paramOut(CheckpointOut &os, const std::string &name, const T &param)
Definition: serialize.hh:454
CheckpointIn(const std::string &cpt_dir, SimObjectResolver &resolver)
Definition: serialize.cc:268

Generated on Fri Jul 3 2020 15:53:04 for gem5 by doxygen 1.8.13