From af7bf1f4cd987bc9112e9ee3fec85e1d8add52fd Mon Sep 17 00:00:00 2001 From: David Heejong Park Date: Tue, 7 May 2024 21:41:54 +0200 Subject: [PATCH] Using duration_cast --- include/cosim/timer.hpp | 4 ++-- src/cosim/timer.cpp | 35 +++++++++++++---------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/include/cosim/timer.hpp b/include/cosim/timer.hpp index 5ae67c25..cb2ba0f3 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 = -1; + std::atomic sampling_period_to_monitor = std::chrono::milliseconds(-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()); + boost::hash_combine(seed, v.sampling_period_to_monitor.load().count()); return seed; } }; diff --git a/src/cosim/timer.cpp b/src/cosim/timer.cpp index c273775e..1a191bab 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 > 0) { - sampling_period_to_monitor_ = std::chrono::milliseconds(sampling_period); + if (sampling_period.count() > 0) { + sampling_period_to_monitor_ = sampling_period; } else { sampling_period_to_monitor_ = std::nullopt; } @@ -78,20 +78,17 @@ 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; + template void update_rolling_average_real_time_factor( - Time::time_point& currentTime, - time_point& currentSimulationTime, - const cosim::duration& elapsedRealTime) + const Time::time_point& currentTime, + const time_point& currentSimulationTime, + const std::chrono::duration& elapsedRealTime) { const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_; - if (tick_period_match_) { - metrics_->rolling_average_real_time_factor = elapsedSimTime.count() / (1.0 * elapsedRealTime.count()); - } else { - metrics_->rolling_average_real_time_factor = - std::chrono::duration_cast(elapsedSimTime).count() / (1.0 * std::chrono::duration_cast(elapsedRealTime).count()); - } + + metrics_->rolling_average_real_time_factor = + std::chrono::duration_cast(elapsedSimTime).count() / (1.0 * std::chrono::duration_cast(elapsedRealTime).count()); rtStartTime_ = currentTime; rtSimulationStartTime_ = currentSimulationTime; rtCounter_ = 0L; @@ -99,25 +96,19 @@ class real_time_timer::impl void update_real_time_factor(Time::time_point currentTime, time_point currentSimulationTime) { - const auto relativeSimTime = currentSimulationTime - simulationStartTime_; - const auto relativeRealTime = currentTime - startTime_; - - if (tick_period_match_) { - metrics_->total_average_real_time_factor = relativeSimTime.count() / (1.0 * relativeRealTime.count()); - } else { - metrics_->total_average_real_time_factor = - std::chrono::duration_cast(relativeSimTime).count() / (1.0 * std::chrono::duration_cast(relativeRealTime).count()); - } + const auto relativeSimTime = std::chrono::duration_cast(currentSimulationTime - simulationStartTime_); + const auto relativeRealTime = 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_; if (elapsedRealTime > sampling_period_to_monitor_.value()) { + const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_; update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime); } } else if (rtCounter_ >= config_->steps_to_monitor.load()) { const auto elapsedRealTime = currentTime - rtStartTime_; - update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime); } rtCounter_++;