From bc8274433a0a38fe83565f5311aef2eed171ac5d Mon Sep 17 00:00:00 2001 From: marcomoerz Date: Thu, 4 Jul 2024 10:54:04 +0200 Subject: [PATCH] Deserialize std::variant without throwing exception --- configs/ddr3-example.json | 1 + configs/ddr4-example.json | 1 + configs/ddr4-full-example.json | 1 + configs/ddr5-example.json | 1 + configs/hbm2-example.json | 1 + configs/lpddr4-example.json | 1 + configs/lpddr5-example.json | 1 + .../DRAMSys/config/AddressMapping.h | 3 ++- src/configuration/DRAMSys/config/TraceSetup.h | 21 ++++++++++++++-- .../DRAMSys/simulation/AddressDecoder.cpp | 24 +++++++------------ src/simulator/simulator/Simulator.cpp | 2 +- tests/tests_configuration/reference.json | 4 ++++ .../resources/tracesetup/ddr5-trace.json | 3 +++ .../test_configuration.cpp | 17 ++++++++++++- tests/tests_regression/DDR3/ddr3-example.json | 1 + tests/tests_regression/DDR4/ddr4-example.json | 1 + tests/tests_regression/DDR5/ddr5-example.json | 1 + tests/tests_regression/HBM2/hbm2-example.json | 4 +++- tests/tests_regression/HBM3/hbm3-example.json | 4 +++- .../LPDDR4/lpddr4-example.json | 1 + .../LPDDR5/lpddr5-example.json | 1 + 21 files changed, 71 insertions(+), 23 deletions(-) diff --git a/configs/ddr3-example.json b/configs/ddr3-example.json index 982ecf79..8be3a4f4 100644 --- a/configs/ddr3-example.json +++ b/configs/ddr3-example.json @@ -7,6 +7,7 @@ "simulationid": "ddr3-example", "tracesetup": [ { + "type": "player", "clkMhz": 800, "name": "traces/example.stl" } diff --git a/configs/ddr4-example.json b/configs/ddr4-example.json index 2f90e9ec..989a1f3f 100644 --- a/configs/ddr4-example.json +++ b/configs/ddr4-example.json @@ -7,6 +7,7 @@ "simulationid": "ddr4-example", "tracesetup": [ { + "type": "player", "clkMhz": 200, "name": "traces/example.stl" } diff --git a/configs/ddr4-full-example.json b/configs/ddr4-full-example.json index 82ce03f4..45722879 100644 --- a/configs/ddr4-full-example.json +++ b/configs/ddr4-full-example.json @@ -210,6 +210,7 @@ "simulationid": "ddr4-example", "tracesetup": [ { + "type": "player", "clkMhz": 200, "name": "traces/example.stl" } diff --git a/configs/ddr5-example.json b/configs/ddr5-example.json index 58dc1b78..e7eb7078 100644 --- a/configs/ddr5-example.json +++ b/configs/ddr5-example.json @@ -7,6 +7,7 @@ "simulationid": "ddr5-example", "tracesetup": [ { + "type": "player", "clkMhz": 2000, "name": "traces/example.stl" } diff --git a/configs/hbm2-example.json b/configs/hbm2-example.json index 2aa7fc3f..b59ffc15 100644 --- a/configs/hbm2-example.json +++ b/configs/hbm2-example.json @@ -7,6 +7,7 @@ "simulationid": "hbm2-example", "tracesetup": [ { + "type": "player", "clkMhz": 1000, "name": "traces/example.stl" } diff --git a/configs/lpddr4-example.json b/configs/lpddr4-example.json index 59964d6f..7b1f8996 100644 --- a/configs/lpddr4-example.json +++ b/configs/lpddr4-example.json @@ -7,6 +7,7 @@ "simulationid": "lpddr4-example", "tracesetup": [ { + "type": "player", "clkMhz": 200, "name": "traces/example.stl" } diff --git a/configs/lpddr5-example.json b/configs/lpddr5-example.json index 0fdf6c7d..53facf91 100644 --- a/configs/lpddr5-example.json +++ b/configs/lpddr5-example.json @@ -7,6 +7,7 @@ "simulationid": "lpddr5-example", "tracesetup": [ { + "type": "player", "clkMhz": 200, "name": "traces/example.stl" } diff --git a/src/configuration/DRAMSys/config/AddressMapping.h b/src/configuration/DRAMSys/config/AddressMapping.h index 95d27334..6cef8ee9 100644 --- a/src/configuration/DRAMSys/config/AddressMapping.h +++ b/src/configuration/DRAMSys/config/AddressMapping.h @@ -37,6 +37,7 @@ #define DRAMSYSCONFIGURATION_ADDRESSMAPPING_H #include +#include #include @@ -47,7 +48,7 @@ struct AddressMapping { static constexpr std::string_view KEY = "addressmapping"; - using BitEntry = std::variant>; + using BitEntry = DRAMUtils::util::CollapsingVector; std::optional> BYTE_BIT; std::optional> COLUMN_BIT; diff --git a/src/configuration/DRAMSys/config/TraceSetup.h b/src/configuration/DRAMSys/config/TraceSetup.h index 1b7581c8..0de42183 100644 --- a/src/configuration/DRAMSys/config/TraceSetup.h +++ b/src/configuration/DRAMSys/config/TraceSetup.h @@ -37,7 +37,10 @@ #define DRAMSYSCONFIGURATION_TRACESETUP_H #include +#include +#include +#include #include #include @@ -72,6 +75,8 @@ NLOHMANN_JSON_SERIALIZE_ENUM(AddressDistribution, struct TracePlayer { + static constexpr inline const std::string_view id = "player"; + uint64_t clkMhz{}; std::string name; std::optional maxPendingReadRequests; @@ -122,6 +127,8 @@ NLOHMANN_JSONIFY_ALL_THINGS(TrafficGeneratorStateTransition, from, to, probabili struct TrafficGenerator { + static constexpr inline const std::string_view id = "generator"; + uint64_t clkMhz{}; std::string name; std::optional maxPendingReadRequests; @@ -158,6 +165,8 @@ NLOHMANN_JSONIFY_ALL_THINGS(TrafficGenerator, struct TrafficGeneratorStateMachine { + static constexpr inline const std::string_view id = "statemachine"; + uint64_t clkMhz{}; std::string name; std::optional maxPendingReadRequests; @@ -185,6 +194,8 @@ NLOHMANN_JSONIFY_ALL_THINGS(TrafficGeneratorStateMachine, struct RowHammer { + static constexpr inline const std::string_view id = "rowhammer"; + uint64_t clkMhz{}; std::string name; std::optional maxPendingReadRequests; @@ -207,8 +218,14 @@ struct TraceSetupConstants static constexpr std::string_view KEY = "tracesetup"; }; -using Initiator = - std::variant; +using InitiatorTypes = DRAMUtils::util::type_sequence< + TracePlayer, + TrafficGenerator, + TrafficGeneratorStateMachine, + RowHammer +>; + +DRAMUTILS_DECLARE_IDVARIANT(Initiator, "type", InitiatorTypes) } // namespace DRAMSys::Config diff --git a/src/libdramsys/DRAMSys/simulation/AddressDecoder.cpp b/src/libdramsys/DRAMSys/simulation/AddressDecoder.cpp index 881fb060..e49f2baf 100644 --- a/src/libdramsys/DRAMSys/simulation/AddressDecoder.cpp +++ b/src/libdramsys/DRAMSys/simulation/AddressDecoder.cpp @@ -37,6 +37,7 @@ */ #include "AddressDecoder.h" +#include "DRAMSys/config/AddressMapping.h" #include #include @@ -50,23 +51,14 @@ static void addMapping(std::vector const& mapp std::vector& bitVector, std::vector>& xorVector) { - for (const auto& bitEntry : mappingVector) + for (const Config::AddressMapping::BitEntry& bitEntry : mappingVector) { - std::visit( - [&bitVector, &xorVector](auto&& arg) - { - using T = std::decay_t; - if constexpr (std::is_same_v) - { - bitVector.push_back(arg); - } - else if constexpr (std::is_same_v>) - { - bitVector.push_back(arg.at(0)); - xorVector.push_back(arg); - } - }, - bitEntry); + if (bitEntry.get_type() == Config::AddressMapping::BitEntry::Type::SINGLE) { + bitVector.push_back(bitEntry.at(0)); + } else { + bitVector.push_back(bitEntry.at(0)); + xorVector.push_back(bitEntry); + } } } diff --git a/src/simulator/simulator/Simulator.cpp b/src/simulator/simulator/Simulator.cpp index bd6e0ccc..e2c363c7 100644 --- a/src/simulator/simulator/Simulator.cpp +++ b/src/simulator/simulator/Simulator.cpp @@ -148,7 +148,7 @@ Simulator::instantiateInitiator(const DRAMSys::Config::Initiator& initiator) std::move(hammer)); } }, - initiator); + initiator.getVariant()); } void Simulator::run() diff --git a/tests/tests_configuration/reference.json b/tests/tests_configuration/reference.json index eebfa58c..b61019a6 100644 --- a/tests/tests_configuration/reference.json +++ b/tests/tests_configuration/reference.json @@ -213,10 +213,12 @@ "simulationid": "std::string_simulationId", "tracesetup": [ { + "type": "player", "clkMhz": 100, "name": "mytrace.stl" }, { + "type": "generator", "addressDistribution": "random", "clkMhz": 100, "name": "MyTestGen", @@ -224,6 +226,7 @@ "rwRatio": 0.5 }, { + "type": "statemachine", "clkMhz": 100, "maxPendingReadRequests": 8, "name": "MyTestGen", @@ -255,6 +258,7 @@ ] }, { + "type": "rowhammer", "clkMhz": 100, "name": "MyTestHammer", "numRequests": 4000, diff --git a/tests/tests_configuration/resources/tracesetup/ddr5-trace.json b/tests/tests_configuration/resources/tracesetup/ddr5-trace.json index 1ca86cfd..42a69fa8 100644 --- a/tests/tests_configuration/resources/tracesetup/ddr5-trace.json +++ b/tests/tests_configuration/resources/tracesetup/ddr5-trace.json @@ -1,10 +1,12 @@ { "tracesetup": [ { + "type": "player", "clkMhz": 2000, "name": "ddr3_example.stl" }, { + "type": "generator", "clkMhz": 2000, "name": "gen0", "numRequests": 2000, @@ -15,6 +17,7 @@ "maxPendingWriteRequests": 8 }, { + "type": "generator", "clkMhz": 2000, "name": "gen1", "numRequests": 2000, diff --git a/tests/tests_configuration/test_configuration.cpp b/tests/tests_configuration/test_configuration.cpp index 59a2aa21..2d8d138e 100644 --- a/tests/tests_configuration/test_configuration.cpp +++ b/tests/tests_configuration/test_configuration.cpp @@ -68,7 +68,18 @@ protected: static DRAMSys::Config::RowHammer createTraceHammer(); static std::vector - addressMapBitVector(std::vector bits) + addressMapBitVector(std::initializer_list> bits) + { + auto result = std::vector(); + for (const auto& bit : bits) + { + result.push_back(DRAMSys::Config::AddressMapping::BitEntry({bit.begin(), bit.end()})); + } + return result; + }; + + static std::vector + addressMapBitVector(std::vector bits) { return {bits.begin(), bits.end()}; }; @@ -707,10 +718,12 @@ TEST_F(ConfigurationTest, TraceSetup) { "tracesetup": [ { + "type": "player", "clkMhz": 100, "name": "mytrace.stl" }, { + "type": "generator", "addressDistribution": "random", "clkMhz": 100, "name": "MyTestGen", @@ -718,6 +731,7 @@ TEST_F(ConfigurationTest, TraceSetup) "rwRatio": 0.5 }, { + "type": "statemachine", "clkMhz": 100, "maxPendingReadRequests": 8, "name": "MyTestGen", @@ -749,6 +763,7 @@ TEST_F(ConfigurationTest, TraceSetup) ] }, { + "type": "rowhammer", "clkMhz": 100, "name": "MyTestHammer", "numRequests": 4000, diff --git a/tests/tests_regression/DDR3/ddr3-example.json b/tests/tests_regression/DDR3/ddr3-example.json index 34af9561..0d201661 100644 --- a/tests/tests_regression/DDR3/ddr3-example.json +++ b/tests/tests_regression/DDR3/ddr3-example.json @@ -135,6 +135,7 @@ "simulationid": "ddr3-dual-rank", "tracesetup": [ { + "type": "player", "clkMhz": 533, "name": "traces/trace_test2.stl" } diff --git a/tests/tests_regression/DDR4/ddr4-example.json b/tests/tests_regression/DDR4/ddr4-example.json index aaad9378..fd26fc30 100644 --- a/tests/tests_regression/DDR4/ddr4-example.json +++ b/tests/tests_regression/DDR4/ddr4-example.json @@ -211,6 +211,7 @@ "simulationid": "ddr4-bankgrp", "tracesetup": [ { + "type": "player", "clkMhz": 933, "name": "traces/trace_test3.stl" } diff --git a/tests/tests_regression/DDR5/ddr5-example.json b/tests/tests_regression/DDR5/ddr5-example.json index 8b3d3725..49e91043 100644 --- a/tests/tests_regression/DDR5/ddr5-example.json +++ b/tests/tests_regression/DDR5/ddr5-example.json @@ -218,6 +218,7 @@ "simulationid": "ddr5-example", "tracesetup": [ { + "type": "player", "clkMhz": 1600, "name": "traces/trace_test3.stl" } diff --git a/tests/tests_regression/HBM2/hbm2-example.json b/tests/tests_regression/HBM2/hbm2-example.json index 8d4f3433..f0494361 100644 --- a/tests/tests_regression/HBM2/hbm2-example.json +++ b/tests/tests_regression/HBM2/hbm2-example.json @@ -126,10 +126,12 @@ "simulationid": "hbm2-example", "tracesetup": [ { + "type": "player", "clkMhz": 1000, "name": "traces/trace1_test4.stl" }, - { + { + "type": "player", "clkMhz": 1000, "name": "traces/trace2_test4.stl" } diff --git a/tests/tests_regression/HBM3/hbm3-example.json b/tests/tests_regression/HBM3/hbm3-example.json index 11f5f5e0..238bb66e 100644 --- a/tests/tests_regression/HBM3/hbm3-example.json +++ b/tests/tests_regression/HBM3/hbm3-example.json @@ -129,10 +129,12 @@ "simulationid": "hbm3-example", "tracesetup": [ { + "type": "player", "clkMhz": 1600, "name": "traces/trace1_test4.stl" }, - { + { + "type": "player", "clkMhz": 1600, "name": "traces/trace2_test4.stl" } diff --git a/tests/tests_regression/LPDDR4/lpddr4-example.json b/tests/tests_regression/LPDDR4/lpddr4-example.json index f856a3f6..f44abaee 100644 --- a/tests/tests_regression/LPDDR4/lpddr4-example.json +++ b/tests/tests_regression/LPDDR4/lpddr4-example.json @@ -181,6 +181,7 @@ "simulationid": "lpddr4-example", "tracesetup": [ { + "type": "player", "clkMhz": 1600, "name": "traces/trace_lpddr4.stl" } diff --git a/tests/tests_regression/LPDDR5/lpddr5-example.json b/tests/tests_regression/LPDDR5/lpddr5-example.json index 69e84359..53da1716 100644 --- a/tests/tests_regression/LPDDR5/lpddr5-example.json +++ b/tests/tests_regression/LPDDR5/lpddr5-example.json @@ -203,6 +203,7 @@ "simulationid": "lpddr5-example", "tracesetup": [ { + "type": "player", "clkMhz": 1600, "name": "traces/trace_lpddr5.stl" }