From 2ff7de3fa2f1abb7b26c21b9645d5bf7c378911a Mon Sep 17 00:00:00 2001 From: David Heejong Park Date: Tue, 7 May 2024 18:36:27 +0200 Subject: [PATCH] Addressed comments - 02 --- include/cosim/timer.hpp | 4 ++-- src/cosim/timer.cpp | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/include/cosim/timer.hpp b/include/cosim/timer.hpp index cb2ba0f3..5ae67c25 100644 --- a/include/cosim/timer.hpp +++ b/include/cosim/timer.hpp @@ -44,7 +44,7 @@ struct real_time_config * When the value is greater than zero, the real time factor is computed periodically using the * specified time instead of the `steps_to_monitor` value. */ - std::atomic sampling_period_to_monitor = std::chrono::milliseconds(-1); + std::atomic sampling_period_to_monitor = -1; }; } // namespace cosim @@ -63,7 +63,7 @@ class hash boost::hash_combine(seed, v.real_time_simulation.load()); boost::hash_combine(seed, v.real_time_factor_target.load()); boost::hash_combine(seed, v.steps_to_monitor.load()); - boost::hash_combine(seed, v.sampling_period_to_monitor.load().count()); + boost::hash_combine(seed, v.sampling_period_to_monitor.load()); return seed; } }; diff --git a/src/cosim/timer.cpp b/src/cosim/timer.cpp index 8a7b5ed7..99026229 100644 --- a/src/cosim/timer.cpp +++ b/src/cosim/timer.cpp @@ -37,8 +37,8 @@ class real_time_timer::impl if (newHash != configHashValue_) { start(currentTime); const auto sampling_period = config_->sampling_period_to_monitor.load(); - if (sampling_period.count() > 0) { - sampling_period_to_monitor_ = sampling_period; + if (sampling_period > 0) { + sampling_period_to_monitor_ = std::chrono::milliseconds(sampling_period); } else { sampling_period_to_monitor_ = std::nullopt; } @@ -78,13 +78,16 @@ class real_time_timer::impl size_t configHashValue_; std::shared_ptr metrics_; std::optional sampling_period_to_monitor_ = std::nullopt; + const volatile bool tick_period_match_ = std::is_same::value; void update_rolling_average_real_time_factor( Time::time_point& currentTime, time_point& currentSimulationTime, - const duration& elapsedRealTime) + const cosim::duration& elapsedRealTime) { - const auto elapsedSimTime = std::chrono::duration_cast(currentSimulationTime - rtSimulationStartTime_); + const auto elapsedSimTime = + tick_period_match_ ? currentSimulationTime - rtSimulationStartTime_ : std::chrono::duration_cast(currentSimulationTime - rtSimulationStartTime_); + metrics_->rolling_average_real_time_factor = elapsedSimTime.count() / (1.0 * elapsedRealTime.count()); rtStartTime_ = currentTime; rtSimulationStartTime_ = currentSimulationTime; @@ -93,12 +96,16 @@ class real_time_timer::impl void update_real_time_factor(Time::time_point currentTime, time_point currentSimulationTime) { - const auto relativeSimTime = std::chrono::duration_cast(currentSimulationTime - simulationStartTime_); - const auto relativeRealTime = std::chrono::duration_cast(currentTime - startTime_); + const auto relativeSimTime = + tick_period_match_ ? currentSimulationTime - simulationStartTime_ : std::chrono::duration_cast(currentSimulationTime - simulationStartTime_); + const auto relativeRealTime = + tick_period_match_ ? currentTime - startTime_ : std::chrono::duration_cast(currentTime - startTime_); + metrics_->total_average_real_time_factor = relativeSimTime.count() / (1.0 * relativeRealTime.count()); if (sampling_period_to_monitor_.has_value()) { - const auto elapsedRealTime = currentTime - rtStartTime_; + const auto elapsedRealTime = + tick_period_match_ ? currentTime - rtStartTime_ : std::chrono::duration_cast(currentTime - rtStartTime_); if (elapsedRealTime > sampling_period_to_monitor_.value()) { update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime);