Skip to content

Commit

Permalink
Addressed comments - 02
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhjp01 committed May 7, 2024
1 parent 8519ba2 commit 2ff7de3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions include/cosim/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::milliseconds> sampling_period_to_monitor = std::chrono::milliseconds(-1);
std::atomic<int> sampling_period_to_monitor = -1;
};

} // namespace cosim
Expand All @@ -63,7 +63,7 @@ class hash<cosim::real_time_config>
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;
}
};
Expand Down
21 changes: 14 additions & 7 deletions src/cosim/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -78,13 +78,16 @@ class real_time_timer::impl
size_t configHashValue_;
std::shared_ptr<real_time_metrics> metrics_;
std::optional<std::chrono::milliseconds> sampling_period_to_monitor_ = std::nullopt;
const volatile bool tick_period_match_ = std::is_same<cosim::time_point::period, Time::time_point::period>::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<duration>(currentSimulationTime - rtSimulationStartTime_);
const auto elapsedSimTime =
tick_period_match_ ? currentSimulationTime - rtSimulationStartTime_ : std::chrono::duration_cast<cosim::duration>(currentSimulationTime - rtSimulationStartTime_);

metrics_->rolling_average_real_time_factor = elapsedSimTime.count() / (1.0 * elapsedRealTime.count());
rtStartTime_ = currentTime;
rtSimulationStartTime_ = currentSimulationTime;
Expand All @@ -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<duration>(currentSimulationTime - simulationStartTime_);
const auto relativeRealTime = std::chrono::duration_cast<duration>(currentTime - startTime_);
const auto relativeSimTime =
tick_period_match_ ? currentSimulationTime - simulationStartTime_ : std::chrono::duration_cast<cosim::duration>(currentSimulationTime - simulationStartTime_);
const auto relativeRealTime =
tick_period_match_ ? currentTime - startTime_ : std::chrono::duration_cast<cosim::duration>(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<cosim::duration>(currentTime - rtStartTime_);

if (elapsedRealTime > sampling_period_to_monitor_.value()) {
update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime);
Expand Down

0 comments on commit 2ff7de3

Please sign in to comment.