Skip to content

Commit af7bf1f

Browse files
committed
Using duration_cast
1 parent 55ce42e commit af7bf1f

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

include/cosim/timer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct real_time_config
4444
* When the value is greater than zero, the real time factor is computed periodically using the
4545
* specified time instead of the `steps_to_monitor` value.
4646
*/
47-
std::atomic<int> sampling_period_to_monitor = -1;
47+
std::atomic<std::chrono::milliseconds> sampling_period_to_monitor = std::chrono::milliseconds(-1);
4848
};
4949

5050
} // namespace cosim
@@ -63,7 +63,7 @@ class hash<cosim::real_time_config>
6363
boost::hash_combine(seed, v.real_time_simulation.load());
6464
boost::hash_combine(seed, v.real_time_factor_target.load());
6565
boost::hash_combine(seed, v.steps_to_monitor.load());
66-
boost::hash_combine(seed, v.sampling_period_to_monitor.load());
66+
boost::hash_combine(seed, v.sampling_period_to_monitor.load().count());
6767
return seed;
6868
}
6969
};

src/cosim/timer.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class real_time_timer::impl
3737
if (newHash != configHashValue_) {
3838
start(currentTime);
3939
const auto sampling_period = config_->sampling_period_to_monitor.load();
40-
if (sampling_period > 0) {
41-
sampling_period_to_monitor_ = std::chrono::milliseconds(sampling_period);
40+
if (sampling_period.count() > 0) {
41+
sampling_period_to_monitor_ = sampling_period;
4242
} else {
4343
sampling_period_to_monitor_ = std::nullopt;
4444
}
@@ -78,46 +78,37 @@ class real_time_timer::impl
7878
size_t configHashValue_;
7979
std::shared_ptr<real_time_metrics> metrics_;
8080
std::optional<std::chrono::milliseconds> sampling_period_to_monitor_ = std::nullopt;
81-
const volatile bool tick_period_match_ = std::is_same<cosim::time_point::period, Time::time_point::period>::value;
8281

82+
template<typename Rep, typename Period>
8383
void update_rolling_average_real_time_factor(
84-
Time::time_point& currentTime,
85-
time_point& currentSimulationTime,
86-
const cosim::duration& elapsedRealTime)
84+
const Time::time_point& currentTime,
85+
const time_point& currentSimulationTime,
86+
const std::chrono::duration<Rep, Period>& elapsedRealTime)
8787
{
8888
const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_;
89-
if (tick_period_match_) {
90-
metrics_->rolling_average_real_time_factor = elapsedSimTime.count() / (1.0 * elapsedRealTime.count());
91-
} else {
92-
metrics_->rolling_average_real_time_factor =
93-
std::chrono::duration_cast<cosim::duration>(elapsedSimTime).count() / (1.0 * std::chrono::duration_cast<cosim::duration>(elapsedRealTime).count());
94-
}
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());
9592
rtStartTime_ = currentTime;
9693
rtSimulationStartTime_ = currentSimulationTime;
9794
rtCounter_ = 0L;
9895
}
9996

10097
void update_real_time_factor(Time::time_point currentTime, time_point currentSimulationTime)
10198
{
102-
const auto relativeSimTime = currentSimulationTime - simulationStartTime_;
103-
const auto relativeRealTime = currentTime - startTime_;
104-
105-
if (tick_period_match_) {
106-
metrics_->total_average_real_time_factor = relativeSimTime.count() / (1.0 * relativeRealTime.count());
107-
} else {
108-
metrics_->total_average_real_time_factor =
109-
std::chrono::duration_cast<cosim::duration>(relativeSimTime).count() / (1.0 * std::chrono::duration_cast<cosim::duration>(relativeRealTime).count());
110-
}
99+
const auto relativeSimTime = std::chrono::duration_cast<cosim::duration>(currentSimulationTime - simulationStartTime_);
100+
const auto relativeRealTime = std::chrono::duration_cast<cosim::duration>(currentTime - startTime_);
101+
metrics_->total_average_real_time_factor = relativeSimTime.count() / (1.0 * relativeRealTime.count());
111102

112103
if (sampling_period_to_monitor_.has_value()) {
113104
const auto elapsedRealTime = currentTime - rtStartTime_;
114105

115106
if (elapsedRealTime > sampling_period_to_monitor_.value()) {
107+
const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_;
116108
update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime);
117109
}
118110
} else if (rtCounter_ >= config_->steps_to_monitor.load()) {
119111
const auto elapsedRealTime = currentTime - rtStartTime_;
120-
121112
update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime);
122113
}
123114
rtCounter_++;

0 commit comments

Comments
 (0)