Skip to content

Commit 2168a4c

Browse files
authored
Sampling period for computing the rolling average real time factor (#758)
* Introduced cosim::real_time_config.step_duration_to_monitor for computing the rolling average real time factor using the specified period in second. * Local variable naming change. * Addressed comments * Additional fix * Using duration_cast
1 parent 6993d6e commit 2168a4c

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

include/cosim/timer.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ struct real_time_config
3636
* This value is used for monitoring purposes only.
3737
*/
3838
std::atomic<int> steps_to_monitor = 5;
39+
40+
/**
41+
* A sampling period in real time (wall clock) in milliseconds used in the rolling average real time factor calculation.
42+
* This can be useful when simulation step size is small and a fixed value for `steps_to_monitor`
43+
* would not compute an accurate rolling average real time factor.
44+
* When the value is greater than zero, the real time factor is computed periodically using the
45+
* specified time instead of the `steps_to_monitor` value.
46+
*/
47+
std::atomic<std::chrono::milliseconds> sampling_period_to_monitor = std::chrono::milliseconds(-1);
3948
};
4049

4150
} // namespace cosim
@@ -54,6 +63,7 @@ class hash<cosim::real_time_config>
5463
boost::hash_combine(seed, v.real_time_simulation.load());
5564
boost::hash_combine(seed, v.real_time_factor_target.load());
5665
boost::hash_combine(seed, v.steps_to_monitor.load());
66+
boost::hash_combine(seed, v.sampling_period_to_monitor.load().count());
5767
return seed;
5868
}
5969
};

src/cosim/timer.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <chrono>
99
#include <thread>
10-
1110
typedef std::chrono::steady_clock Time;
1211
constexpr std::chrono::microseconds MIN_SLEEP(100);
1312

@@ -37,6 +36,12 @@ class real_time_timer::impl
3736
const auto newHash = std::hash<real_time_config>()(*config_);
3837
if (newHash != configHashValue_) {
3938
start(currentTime);
39+
const auto sampling_period = config_->sampling_period_to_monitor.load();
40+
if (sampling_period.count() > 0) {
41+
sampling_period_to_monitor_ = sampling_period;
42+
} else {
43+
sampling_period_to_monitor_ = std::nullopt;
44+
}
4045
configHashValue_ = newHash;
4146
}
4247
double rtfTarget = config_->real_time_factor_target.load();
@@ -72,21 +77,39 @@ class real_time_timer::impl
7277
std::shared_ptr<real_time_config> config_;
7378
size_t configHashValue_;
7479
std::shared_ptr<real_time_metrics> metrics_;
80+
std::optional<std::chrono::milliseconds> sampling_period_to_monitor_ = std::nullopt;
81+
82+
template<typename Rep, typename Period>
83+
void update_rolling_average_real_time_factor(
84+
const Time::time_point& currentTime,
85+
const time_point& currentSimulationTime,
86+
const std::chrono::duration<Rep, Period>& elapsedRealTime)
87+
{
88+
const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_;
89+
90+
metrics_->rolling_average_real_time_factor =
91+
std::chrono::duration_cast<cosim::duration>(elapsedSimTime).count() / (1.0 * std::chrono::duration_cast<cosim::duration>(elapsedRealTime).count());
92+
rtStartTime_ = currentTime;
93+
rtSimulationStartTime_ = currentSimulationTime;
94+
rtCounter_ = 0L;
95+
}
7596

7697
void update_real_time_factor(Time::time_point currentTime, time_point currentSimulationTime)
7798
{
78-
const auto relativeSimTime = currentSimulationTime - simulationStartTime_;
79-
const auto relativeRealTime = currentTime - startTime_;
99+
const auto relativeSimTime = std::chrono::duration_cast<cosim::duration>(currentSimulationTime - simulationStartTime_);
100+
const auto relativeRealTime = std::chrono::duration_cast<cosim::duration>(currentTime - startTime_);
80101
metrics_->total_average_real_time_factor = relativeSimTime.count() / (1.0 * relativeRealTime.count());
81102

82-
if (rtCounter_ >= config_->steps_to_monitor.load()) {
83-
const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_;
103+
if (sampling_period_to_monitor_.has_value()) {
84104
const auto elapsedRealTime = currentTime - rtStartTime_;
85105

86-
metrics_->rolling_average_real_time_factor = elapsedSimTime.count() / (1.0 * elapsedRealTime.count());
87-
rtStartTime_ = currentTime;
88-
rtSimulationStartTime_ = currentSimulationTime;
89-
rtCounter_ = 0L;
106+
if (elapsedRealTime > sampling_period_to_monitor_.value()) {
107+
const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_;
108+
update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime);
109+
}
110+
} else if (rtCounter_ >= config_->steps_to_monitor.load()) {
111+
const auto elapsedRealTime = currentTime - rtStartTime_;
112+
update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime);
90113
}
91114
rtCounter_++;
92115
}

0 commit comments

Comments
 (0)