From 91aa2f82b4f2a9b954d2f8f394ecc183bf1e2445 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Thu, 15 Oct 2020 12:12:54 +0100 Subject: [PATCH] sim: create SERIALIZE_MAPPING and UNSERIALIZE_MAPPING 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 Reviewed-by: Daniel Carvalho Reviewed-by: Andreas Sandberg Maintainer: Bobby R. Bruce Tested-by: kokoro --- src/sim/serialize.hh | 57 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/sim/serialize.hh b/src/sim/serialize.hh index 51313a4b3..987bee250 100644 --- a/src/sim/serialize.hh +++ b/src/sim/serialize.hh @@ -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 #include #include +#include #include +#include #include #include "base/logging.hh" @@ -515,6 +517,47 @@ debug_serialize(const std::string &cpt_dir); void objParamIn(CheckpointIn &cp, const std::string &name, SimObject * ¶m); +/** + * 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 +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 +void +mappingParamIn(CheckpointIn &cp, const char* sectionName, + const char* const names[], T *param, unsigned size) +{ + Serializable::ScopedCheckpointSection sec(cp, sectionName); + std::unordered_map 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 * ¶m); objptr = dynamic_cast(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__ -- 2.30.2