sim: create SERIALIZE_MAPPING and UNSERIALIZE_MAPPING
authorCiro Santilli <ciro.santilli@arm.com>
Thu, 15 Oct 2020 11:12:54 +0000 (12:12 +0100)
committerCiro Santilli <ciro.santilli@arm.com>
Mon, 23 Nov 2020 16:51:08 +0000 (16:51 +0000)
The motivation for those new methods is to prevent checkpoints from
breaking when new map entries are added.

Change-Id: I0ff8681498bcf669492e6b876ad385fda4673d77
JIRA: https://gem5.atlassian.net/browse/GEM5-661
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/36135
Reviewed-by: Richard Cooper <richard.cooper@arm.com>
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu>
Tested-by: kokoro <noreply+kokoro@google.com>
src/sim/serialize.hh

index 51313a4b30d8708a4913664215a904c1a84e90a3..987bee2506648cb2a8359d6c85fb80805df62731 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2018 ARM Limited
+ * Copyright (c) 2015, 2018, 2020 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -50,7 +50,9 @@
 #include <iostream>
 #include <iterator>
 #include <stack>
+#include <set>
 #include <type_traits>
+#include <unordered_map>
 #include <vector>
 
 #include "base/logging.hh"
@@ -515,6 +517,47 @@ debug_serialize(const std::string &cpt_dir);
 void
 objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
 
+/**
+ * Serialize a mapping represented as two arrays: one containing names
+ * and the other containing values.
+ *
+ * @param names array of keys
+ * @param param array of values
+ * @param size size of the names and param arrays
+ */
+template <class T>
+void
+mappingParamOut(CheckpointOut &os, const char* sectionName,
+    const char* const names[], const T *param, unsigned size)
+{
+    Serializable::ScopedCheckpointSection sec(os, sectionName);
+    for (unsigned i = 0; i < size; ++i) {
+        paramOut(os, names[i], param[i]);
+    }
+}
+
+/**
+ * Restore mappingParamOut. Keys missing from the checkpoint are ignored.
+ */
+template <class T>
+void
+mappingParamIn(CheckpointIn &cp, const char* sectionName,
+    const char* const names[], T *param, unsigned size)
+{
+    Serializable::ScopedCheckpointSection sec(cp, sectionName);
+    std::unordered_map<std::string, size_t> name_to_index;
+    for (size_t i = 0; i < size; i++) {
+        name_to_index[names[i]] = i;
+    }
+    for (size_t i = 0; i < size; i++) {
+        auto& key = names[i];
+        T value;
+        if (optParamIn(cp, key, value)) {
+            param[name_to_index[key]] = value;
+        }
+    }
+}
+
 //
 // These macros are streamlined to use in serialize/unserialize
 // functions.  It's assumed that serialize() has a parameter 'os' for
@@ -646,4 +689,16 @@ objParamIn(CheckpointIn &cp, const std::string &name, SimObject * &param);
         objptr = dynamic_cast<decltype(objptr)>(sptr);  \
     } while (0)
 
+/**
+ * \def SERIALIZE_MAPPING(member, names, size)
+ */
+#define SERIALIZE_MAPPING(member, names, size) \
+        mappingParamOut(cp, #member, names, member, size)
+
+/**
+ * \def UNSERIALIZE_MAPPING(member, names, size)
+ */
+#define UNSERIALIZE_MAPPING(member, names, size) \
+        mappingParamIn(cp, #member, names, member, size)
+
 #endif // __SERIALIZE_HH__